From 26e726e21ec8ef926dda2cdf12ecc2fe177d2e62 Mon Sep 17 00:00:00 2001 From: nullStack65 <112618179+nullStack65@users.noreply.github.com> Date: Wed, 18 Nov 2026 01:08:08 +0000 Subject: [PATCH 1/2] fix(contracts): show unclassified history by default on upgraded devices The founder inbox picker defaulted to the Kameron view. A device upgrading from a build without the picker (the Air's 0.1.9-assemblyai.2, official 0.1.10) has no saved view, so it opened on Kameron, where every thread without an inbox record, which is all prior history, is hidden; the notification filter used the same view. Default to All instead. A view the user already picked is saved and keeps winning. Also pin the AssemblyAI behaviour the Air relies on with zero-network mock regressions: universal-3-5-pro header and raw key, config then audio parts, mono 16 kHz 16-bit WAV header and samples, language_code only when not auto, word_boost with preferred terms only, 401/402/403 rejected-key, 429 rate-limit, 5xx unavailable without leaking upstream text, and alias plus filler post-processing. These already pass on release/native-0.1.10. Task: CLO-866 Model: claude-opus-5-5 Co-Authored-By: Claude Opus 5.5 --- apps/server/src/speech/CloudSpeech.test.ts | 70 +++++++++++++++++++--- packages/contracts/src/settings.ts | 3 +- packages/contracts/src/threadInbox.test.ts | 19 ++++++ 3 files changed, 82 insertions(+), 10 deletions(-) diff --git a/apps/server/src/speech/CloudSpeech.test.ts b/apps/server/src/speech/CloudSpeech.test.ts index f85244780c4be..1a1054f8d251d 100644 --- a/apps/server/src/speech/CloudSpeech.test.ts +++ b/apps/server/src/speech/CloudSpeech.test.ts @@ -1,5 +1,9 @@ import { it } from "@effect/vitest"; -import { DEFAULT_SPEECH_TRANSCRIPTION_OPTIONS, SecretProviderId } from "@t3tools/contracts"; +import { + DEFAULT_SPEECH_TRANSCRIPTION_OPTIONS, + SecretProviderId, + type SpeechTranscriptionOptions, +} from "@t3tools/contracts"; import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretResolver"; import * as Effect from "effect/Effect"; import * as Fiber from "effect/Fiber"; @@ -219,12 +223,21 @@ it.effect( ), ); yield* service.setKey("assemblyai", Redacted.make("assembly-fixture")); - const transcribe = (samples: number) => + const samples = (count: number) => { + const audio = new Float32Array(count); + audio.set([0.5, -0.5]); + return new Uint8Array(audio.buffer); + }; + const transcribe = (count: number, options: Partial = {}) => service - .transcribe("assemblyai", new Uint8Array(new Float32Array(samples).buffer), { + .transcribe("assemblyai", samples(count), { ...DEFAULT_SPEECH_TRANSCRIPTION_OPTIONS, speechLanguage: "en", - speechCustomWords: [{ term: "Closura", aliases: [] }], + speechCustomWords: [ + { term: "Closura", aliases: ["closer a"] }, + { term: "T3 Code", aliases: [] }, + ], + ...options, }) .pipe(Effect.provideService(FetchHttpClient.Fetch, upstream)); expect(yield* transcribe(1600)).toBe("hello T3"); @@ -235,16 +248,55 @@ it.effect( expect(headers.get("authorization")).toBe("assembly-fixture"); expect(headers.get("X-AAI-Model")).toBe("universal-3-5-pro"); const form = init?.body as FormData; + expect([...form.keys()]).toEqual(["config", "audio"]); expect(JSON.parse(String(form.get("config")))).toEqual({ language_code: "en", - word_boost: ["Closura"], + word_boost: ["Closura", "T3 Code"], }); - expect((form.get("audio") as Blob).type).toBe("audio/wav"); + const audio = form.get("audio") as File; + expect([audio.type, audio.name]).toEqual(["audio/wav", "dictation.wav"]); + const wav = new DataView(yield* Effect.promise(() => audio.arrayBuffer())); + // RIFF/WAVE, PCM, mono, 16 kHz, 32000 B/s, 2-byte frames, 16-bit, 3200 data bytes. + expect([ + new TextDecoder().decode(wav.buffer.slice(0, 4)), + new TextDecoder().decode(wav.buffer.slice(8, 12)), + wav.getUint16(20, true), + wav.getUint16(22, true), + wav.getUint32(24, true), + wav.getUint32(28, true), + wav.getUint16(32, true), + wav.getUint16(34, true), + wav.getUint32(40, true), + wav.getInt16(44, true), + wav.getInt16(46, true), + ]).toEqual(["RIFF", "WAVE", 1, 1, 16_000, 32_000, 2, 16, 3200, 16384, -16384]); + upstream.mockResolvedValueOnce(Response.json({ text: "uh ship it" })); + expect(yield* transcribe(1600, { speechLanguage: "auto" })).toBe("ship it"); + const autoForm = upstream.mock.calls[1]![1]!.body as FormData; + expect(JSON.parse(String(autoForm.get("config")))).toEqual({ + word_boost: ["Closura", "T3 Code"], + }); + upstream.mockResolvedValueOnce(Response.json({ text: "Um, ship closer a today" })); + expect(yield* transcribe(1600)).toBe("ship Closura today"); + upstream.mockResolvedValueOnce(Response.json({ text: "Um, ship closer a today" })); + expect(yield* transcribe(1600, { speechRemoveFillerWords: false })).toBe( + "Um, ship Closura today", + ); expect(yield* Effect.flip(transcribe(1_920_001))).toMatchObject({ reason: "audio-duration" }); expect(yield* Effect.flip(transcribe(100))).toMatchObject({ reason: "audio-duration" }); - expect(upstream).toHaveBeenCalledTimes(1); - upstream.mockResolvedValueOnce(new Response("private diagnostic", { status: 401 })); - expect(yield* Effect.flip(transcribe(1600))).toMatchObject({ reason: "rejected-key" }); + expect(upstream).toHaveBeenCalledTimes(4); + for (const [status, reason] of [ + [401, "rejected-key"], + [402, "rejected-key"], + [403, "rejected-key"], + [429, "rate-limit"], + [500, "unavailable"], + ] as const) { + upstream.mockResolvedValueOnce(new Response("private diagnostic", { status })); + const error = yield* Effect.flip(transcribe(1600)); + expect(error).toMatchObject({ provider: "assemblyai", reason }); + expect(`${error.message} ${JSON.stringify(error)}`).not.toContain("private diagnostic"); + } yield* service.setKey("assemblyai", null); expect((yield* service.configured).assemblyai).toBe(false); expect(yield* Effect.flip(transcribe(1600))).toMatchObject({ reason: "missing-key" }); diff --git a/packages/contracts/src/settings.ts b/packages/contracts/src/settings.ts index aedceab598a0a..460c30a6d759e 100644 --- a/packages/contracts/src/settings.ts +++ b/packages/contracts/src/settings.ts @@ -405,7 +405,8 @@ export type VoiceTranscriptionDestination = typeof VoiceTranscriptionDestination export const ClientSettingsSchema = Schema.Struct({ founderInboxOwner: Founder.pipe(Schema.withDecodingDefault(Effect.succeed("kameron" as const))), - founderInboxView: InboxView.pipe(Schema.withDecodingDefault(Effect.succeed("kameron" as const))), + // Upgraded devices have no saved view; a founder view would hide all unclassified history. + founderInboxView: InboxView.pipe(Schema.withDecodingDefault(Effect.succeed("all" as const))), notificationMode: NotificationMode.pipe( Schema.withDecodingDefault(Effect.succeed("off" as const)), ), diff --git a/packages/contracts/src/threadInbox.test.ts b/packages/contracts/src/threadInbox.test.ts index fba1beb843e84..2dd747fa7405f 100644 --- a/packages/contracts/src/threadInbox.test.ts +++ b/packages/contracts/src/threadInbox.test.ts @@ -1,5 +1,7 @@ +import * as Schema from "effect/Schema"; import { describe, expect, it } from "vite-plus/test"; import { EnvironmentId } from "./baseSchemas.ts"; +import { ClientSettingsSchema } from "./settings.ts"; import { makeThreadInbox, routeThreadInbox, @@ -25,6 +27,23 @@ describe("shared fleet organizational inbox", () => { expect(matchesInboxView(j, "all")).toBe(true); expect(matchesInboxView(k, "all")).toBe(true); }); + it("shows unclassified history on upgraded devices and persists the picked view", () => { + const decode = Schema.decodeUnknownSync(ClientSettingsSchema); + const encode = Schema.encodeSync(ClientSettingsSchema); + const upgraded = decode({ notificationMode: "off" }); + expect(upgraded.founderInboxView).toBe("all"); + expect(upgraded.founderInboxOwner).toBe("kameron"); + for (const unclassified of [undefined, null]) { + expect(matchesInboxView(unclassified, upgraded.founderInboxView)).toBe(true); + expect(matchesInboxView(unclassified, "jeff")).toBe(false); + } + for (const view of ["jeff", "kameron", "shared", "agents", "all"] as const) { + const saved = decode( + encode({ ...upgraded, founderInboxView: view, founderInboxOwner: "jeff" }), + ); + expect([saved.founderInboxView, saved.founderInboxOwner]).toEqual([view, "jeff"]); + } + }); it("hides bot-only work by default while All and Agents retain it", () => { const inbox = makeThreadInbox({ actor: bot }); expect(matchesInboxView(inbox, "kameron")).toBe(false); From ac699e86364100f24803e04f59f8242fb24cfa88 Mon Sep 17 00:00:00 2001 From: nullStack65 <112618179+nullStack65@users.noreply.github.com> Date: Wed, 18 Nov 2026 01:12:36 +0000 Subject: [PATCH 2/2] build(closura): Air-only macOS arm64 desktop recipe and qualifier macOS desktop packaging needs clang, sips and iconutil, so the Apple Silicon Air candidate cannot be built on the Linux builders. Add a pinned recipe for that host plus a platform-neutral qualifier. build-mac-arm64-desktop.sh (bash 3.2 compatible) refuses anything but an Apple Silicon macOS 13+ host with Command Line Tools, the engines Node, a clean tracked tree and native/ equal to upstream v0.1.10. It derives SDKROOT and CXXFLAGS (or validates host-provided values) with a real clang++ probe, reuses the official v0.1.10 arm64 resource monitor (zip and binary pinned by SHA-256), passes the four public Connect keys from .env.example, sets no update feed, and builds unsigned through the supported build script. qualify-desktop-app.py then gates the app: version and bundle id, arm64 main executable and node-pty, codesign verification, no app-update.yml, AssemblyAI and founder-inbox markers, public relay config, no credential patterns, and records asar/server/web hashes. Upstream's official 0.1.10 Linux and macOS apps carry byte-identical server and web bundles, so the recipe also requires the Air build to reproduce the Linux reference build of 26e726e2 (server 966504d0, bin.mjs b68a6c92, web 264d5422). Task: CLO-866 Model: claude-opus-5-5 Co-Authored-By: Claude Opus 5.5 --- scripts/closura/build-mac-arm64-desktop.sh | 155 +++++++++++++++++++++ scripts/closura/qualify-desktop-app.py | 130 +++++++++++++++++ 2 files changed, 285 insertions(+) create mode 100755 scripts/closura/build-mac-arm64-desktop.sh create mode 100755 scripts/closura/qualify-desktop-app.py diff --git a/scripts/closura/build-mac-arm64-desktop.sh b/scripts/closura/build-mac-arm64-desktop.sh new file mode 100755 index 0000000000000..489edbc2e370c --- /dev/null +++ b/scripts/closura/build-mac-arm64-desktop.sh @@ -0,0 +1,155 @@ +#!/usr/bin/env bash +# Unsigned macOS arm64 desktop build for an Apple Silicon host (bash 3.2 compatible), with pinned +# inputs, no update feed, and gates on version, bundle id, arch, signature, features and the +# server/web bundles of the Linux reference build of the same app source. +# Usage: --work DIR --output-dir DIR --build-version X.Y.Z [--target zip|dmg] [--preflight-only] +set -euo pipefail + +UPSTREAM_BASE="7d32075e3757ded707914e2450c3e392a9665765" # pingdotgg/t3code v0.1.10 +OFFICIAL_ZIP_URL="https://github.com/pingdotgg/t3code/releases/download/v0.1.10/T3-Code-0.1.10-arm64.zip" +OFFICIAL_ZIP_SHA256="3d34c9ef7956495a2c830d3280c217c299bae47cb2b9144152607f68174cf116" +MONITOR_MEMBER="T3 Code (Beta).app/Contents/Resources/resource-monitor/t3-resource-monitor" +MONITOR_SHA256="f021339a35a894cae92842aa463c6cc2ed4c1b99ad36007e28f81eb15c34186f" +RUST_TARGET="aarch64-apple-darwin" +BUNDLE_ID="com.t3tools.t3code" +APP_NAME="T3 Code (Beta)" +# Linux reference build of the same app source; upstream's official 0.1.10 Linux and macOS +# apps carry byte-identical bundled server and web trees. +REFERENCE_COMMIT="26e726e21ec8ef926dda2cdf12ecc2fe177d2e62" +REFERENCE_SERVER_TREE="966504d0251100b1fe5b54b0d05499e86455cac6c7b79228ea60b860d405f94c" +REFERENCE_SERVER_BIN="b68a6c924866292f51ffabfa21ac4f53fc6e711a7d81fc14dca25bc476b80b9a" +REFERENCE_WEB_TREE="264d54225a9eb4dd5772bb23b231d4a32e2ac3d92ad500ae023b1b44b93dd6ac" + +work="" out="" version="" target="zip" preflight_only=0 +while [ $# -gt 0 ]; do + case "$1" in + --work) work="$2"; shift 2 ;; + --output-dir) out="$2"; shift 2 ;; + --build-version) version="$2"; shift 2 ;; + --target) target="$2"; shift 2 ;; + --preflight-only) preflight_only=1; shift ;; + *) echo "unknown argument: $1" >&2; exit 2 ;; + esac +done +[ -n "$work" ] && [ -n "$out" ] && [ -n "$version" ] || { + echo "usage: $0 --work DIR --output-dir DIR --build-version X.Y.Z [--target zip|dmg] [--preflight-only]" >&2 + exit 2 +} +case "$target" in zip|dmg) ;; *) echo "--target must be zip or dmg" >&2; exit 2 ;; esac + +repo="$(cd "$(dirname "$0")/../.." && pwd)" +fail() { echo "build-mac-arm64-desktop: $*" >&2; exit 1; } +sha() { shasum -a 256 "$1" | cut -c1-64; } + +# ---- preflight (Air-only gate) ------------------------------------------------ +[ "$(uname -s)" = "Darwin" ] && [ "$(uname -m)" = "arm64" ] || fail "needs an Apple Silicon macOS host" +macos="$(sw_vers -productVersion)" +[ "${macos%%.*}" -ge 13 ] || fail "macOS $macos is older than the app's 13.0 minimum" +missing="" +for tool in xcrun clang clang++ make sips python3 curl shasum ditto git node codesign lipo otool; do + command -v "$tool" >/dev/null || missing="$missing $tool" +done +[ -z "$missing" ] || fail "missing host tools:$missing (install Xcode Command Line Tools)" +xcode-select -p >/dev/null || fail "no active developer directory (xcode-select --install)" +xcrun --find iconutil >/dev/null || fail "iconutil not found" +want_node="$(python3 -c 'import json,sys;print(json.load(open(sys.argv[1]))["engines"]["node"])' "$repo/package.json")" +have_node="$(node --version | sed 's/^v//')" +[ "$have_node" = "$want_node" ] || fail "node $have_node on PATH; package.json engines require $want_node" +[ -d "$repo/node_modules" ] || fail "dependencies are not installed (run vp install)" +git -C "$repo" diff --quiet HEAD -- || fail "tracked files have local changes" +git -C "$repo" cat-file -e "$UPSTREAM_BASE^{commit}" 2>/dev/null || fail "upstream v0.1.10 commit is not in this clone" +git -C "$repo" diff --quiet "$UPSTREAM_BASE" HEAD -- native \ + || fail "native/ differs from upstream v0.1.10; the official resource monitor would not match" +git -C "$repo" check-ignore -q "native/resource-monitor/target/$RUST_TARGET/release/x" \ + || fail "native/resource-monitor/target is not gitignored" + +export SDKROOT="${SDKROOT:-$(xcrun --sdk macosx --show-sdk-path)}" +probe_dir="$(mktemp -d)" +printf '#include \n#include \n#include \nint main(){std::vector v{"t3"};return v.size()==1?EXIT_SUCCESS:EXIT_FAILURE;}\n' > "$probe_dir/p.cc" +probe() { clang++ $1 -std=c++20 -o "$probe_dir/p" "$probe_dir/p.cc" 2>"$probe_dir/err" && "$probe_dir/p"; } +if [ -z "${CXXFLAGS:-}" ]; then + if probe "-isysroot $SDKROOT"; then CXXFLAGS="-isysroot $SDKROOT" + elif probe "-isysroot $SDKROOT -I$SDKROOT/usr/include/c++/v1"; then CXXFLAGS="-isysroot $SDKROOT -I$SDKROOT/usr/include/c++/v1" + else fail "clang++ cannot build C++ against $SDKROOT; set SDKROOT/CXXFLAGS as recorded for this host"; fi +else + probe "$CXXFLAGS" || fail "clang++ cannot build C++ with the given CXXFLAGS" +fi +export CXXFLAGS +rm -rf "$probe_dir" +echo "preflight ok: macOS $macos arm64, node $have_node, native/ == v0.1.10, SDKROOT=$SDKROOT, CXXFLAGS=$CXXFLAGS" +[ "$preflight_only" -eq 1 ] && exit 0 + +mkdir -p "$work" "$out" +work="$(cd "$work" && pwd)"; out="$(cd "$out" && pwd)" +[ -z "$(ls -A "$out")" ] || fail "output directory $out is not empty" + +# ---- official v0.1.10 resource monitor ----------------------------------------- +zip="$work/official-arm64.zip" +if [ ! -f "$zip" ] || [ "$(sha "$zip")" != "$OFFICIAL_ZIP_SHA256" ]; then + curl -fsSL --retry 3 --connect-timeout 20 --max-time 1800 -o "$zip.part" "$OFFICIAL_ZIP_URL" + [ "$(sha "$zip.part")" = "$OFFICIAL_ZIP_SHA256" ] || fail "official zip SHA-256 mismatch" + mv "$zip.part" "$zip" +fi +monitor="$repo/native/resource-monitor/target/$RUST_TARGET/release/t3-resource-monitor" +mkdir -p "$(dirname "$monitor")" +python3 -I -c 'import sys,zipfile; open(sys.argv[3],"wb").write(zipfile.ZipFile(sys.argv[1]).read(sys.argv[2]))' \ + "$zip" "$MONITOR_MEMBER" "$monitor" +chmod 755 "$monitor" +[ "$(sha "$monitor")" = "$MONITOR_SHA256" ] || fail "resource monitor SHA-256 mismatch" + +# ---- build ----------------------------------------------------------------------- +public_config=() +while IFS= read -r line; do public_config+=("$line"); done < <( + grep -E '^T3CODE_(CLERK_PUBLISHABLE_KEY|CLERK_JWT_TEMPLATE|CLERK_CLI_OAUTH_CLIENT_ID|RELAY_URL)=' "$repo/.env.example" +) +[ ${#public_config[@]} -eq 4 ] || fail ".env.example does not define the four public cloud keys" +cd "$repo" +env -u T3CODE_DESKTOP_UPDATE_REPOSITORY -u GITHUB_REPOSITORY \ + "${public_config[@]}" SDKROOT="$SDKROOT" CXXFLAGS="$CXXFLAGS" \ + T3CODE_TELEMETRY_CHANNEL=dev T3CODE_DESKTOP_REUSE_RESOURCE_MONITOR=true \ + node scripts/build-desktop-artifact.ts --platform mac --target "$target" --arch arm64 \ + --build-version "$version" --output-dir "$out" + +# ---- qualify ----------------------------------------------------------------------- +artifact="$(ls "$out"/*."$target" | head -1)" +[ -f "$artifact" ] || fail "no .$target artifact in $out" +verify="$work/verify"; rm -rf "$verify"; mkdir -p "$verify" +if [ "$target" = zip ]; then + ditto -x -k "$artifact" "$verify" +else + hdiutil attach -nobrowse -readonly -mountpoint "$work/mnt" "$artifact" >/dev/null + ditto "$work/mnt/$APP_NAME.app" "$verify/$APP_NAME.app" + hdiutil detach "$work/mnt" >/dev/null +fi +app="$verify/$APP_NAME.app" +[ "$(lipo -archs "$app/Contents/MacOS/$APP_NAME")" = arm64 ] || fail "main executable is not arm64-only" +pty="$app/Contents/Resources/app.asar.unpacked/node_modules/node-pty/build/Release/pty.node" +[ "$(lipo -archs "$pty")" = arm64 ] || fail "compiled node-pty is not arm64" +codesign --verify --deep --strict "$app" || fail "codesign verification failed" +pty_minos="$(otool -l "$pty" | awk '/LC_BUILD_VERSION/{f=1} f&&/minos/{print $2; exit}')" +asar="$(ls "$repo"/node_modules/.pnpm/@electron+asar@*/node_modules/@electron/asar/bin/asar.mjs | head -1)" +python3 -I "$repo/scripts/closura/qualify-desktop-app.py" \ + --resources "$app/Contents/Resources" --info-plist "$app/Contents/Info.plist" --bundle-id "$BUNDLE_ID" \ + --node "$(command -v node)" --asar "$asar" --version "$version" --out "$out/manifest.json" \ + --meta "source_commit=$(git rev-parse HEAD)" --meta "source_tree=$(git rev-parse 'HEAD^{tree}')" \ + --meta "artifact=$(basename "$artifact")" --meta "artifact_sha256=$(sha "$artifact")" \ + --meta "host_macos=$macos" --meta "clang=$(clang --version | head -1)" --meta "node=$have_node" \ + --meta "sdkroot=$SDKROOT" --meta "cxxflags=$CXXFLAGS" --meta "pty_node_minos=$pty_minos" \ + --meta "signature=$(codesign -dv "$app" 2>&1 | grep -E '^(Signature|TeamIdentifier)=' | tr '\n' ' ')" \ + --meta "flags=--platform mac --target $target --arch arm64 --build-version $version; T3CODE_TELEMETRY_CHANNEL=dev; T3CODE_DESKTOP_REUSE_RESOURCE_MONITOR=true; public config from .env.example; no update repository" + +if git diff --quiet "$REFERENCE_COMMIT" HEAD -- . ':!scripts/closura' ':!.github' 2>/dev/null; then + python3 -I - "$out/manifest.json" "$REFERENCE_SERVER_TREE" "$REFERENCE_SERVER_BIN" "$REFERENCE_WEB_TREE" <<'EOF' +import json, sys +h = json.load(open(sys.argv[1]))["hashes"] +want = dict(zip(("server_dist_tree_sha256", "server_bin_mjs_sha256", "web_client_tree_sha256"), sys.argv[2:])) +bad = [k for k, v in want.items() if h[k] != v] +sys.exit(f"bundles differ from the Linux reference: {bad}" if bad else 0) +EOF + echo "bundled server and web match the Linux reference build of $REFERENCE_COMMIT" +else + echo "warning: app source differs from $REFERENCE_COMMIT; reference bundle comparison skipped" >&2 +fi +echo "artifact: $artifact" +echo "sha256: $(sha "$artifact")" +echo "manifest: $out/manifest.json" diff --git a/scripts/closura/qualify-desktop-app.py b/scripts/closura/qualify-desktop-app.py new file mode 100755 index 0000000000000..9131192c38856 --- /dev/null +++ b/scripts/closura/qualify-desktop-app.py @@ -0,0 +1,130 @@ +#!/usr/bin/env python3 +"""Qualify a packaged desktop app's resources and write a provenance manifest. + +Usage: qualify-desktop-app.py --resources DIR --node NODE --asar ASAR_MJS --version X.Y.Z + --out MANIFEST.json [--info-plist PATH --bundle-id ID] [--meta KEY=VALUE ...] +Exits 1 and lists the failed gates when the app is not the expected build. +""" +import argparse, hashlib, json, os, plistlib, re, subprocess, sys, tempfile + +MARKERS = { + "assemblyai_sync_endpoint": ("server", "sync.assemblyai.com/v1/transcribe"), + "assemblyai_model_header": ("server", "universal-3-5-pro"), + "assemblyai_word_boost": ("server", "word_boost"), + "assemblyai_settings": ("web", "assemblyai"), + "founder_inbox_settings": ("web", "founderInboxView"), + "founder_inbox_all_view": ("web", "All threads"), + "public_relay_url": ("server", "relay.t3.codes"), + "public_relay_url_web": ("web", "relay.t3.codes"), +} +CREDENTIAL_PATTERNS = [ + rb"gh[opusr]_[A-Za-z0-9]{36}", + rb"github_pat_[A-Za-z0-9_]{40,}", + rb"sk-ant-[A-Za-z0-9_-]{20,}", + rb"xox[baprs]-[A-Za-z0-9-]{10,}", + rb"-----BEGIN [A-Z ]*PRIVATE KEY-----", +] + + +def sha256(path): + digest = hashlib.sha256() + with open(path, "rb") as handle: + for chunk in iter(lambda: handle.read(1 << 20), b""): + digest.update(chunk) + return digest.hexdigest() + + +def tree(root, skip=None): + """sha256 over ' ' lines sorted by UTF-8 path bytes.""" + lines = [] + for directory, dirs, files in os.walk(root): + if skip: + dirs[:] = [d for d in dirs if os.path.join(directory, d) != skip] + for name in files: + path = os.path.join(directory, name) + lines.append((os.path.relpath(path, root).encode(), sha256(path))) + body = b"".join(h.encode() + b" " + p + b"\n" for p, h in sorted(lines)) + return hashlib.sha256(body).hexdigest(), len(lines) + + +def contains(root, needle, skip=None): + data = needle.encode() + for directory, dirs, files in os.walk(root): + if skip: + dirs[:] = [d for d in dirs if os.path.join(directory, d) != skip] + for name in files: + with open(os.path.join(directory, name), "rb") as handle: + if data in handle.read(): + return True + return False + + +def main(): + parser = argparse.ArgumentParser() + for flag in ("--resources", "--node", "--asar", "--version", "--out"): + parser.add_argument(flag, required=True) + parser.add_argument("--info-plist") + parser.add_argument("--bundle-id") + parser.add_argument("--meta", action="append", default=[]) + args = parser.parse_args() + failures = [] + resources = os.path.abspath(args.resources) + asar = os.path.join(resources, "app.asar") + result = {"meta": dict(item.split("=", 1) for item in args.meta), "gates": {}} + + def gate(name, ok, detail=None): + result["gates"][name] = {"ok": bool(ok), **({"detail": detail} if detail else {})} + if not ok: + failures.append(name) + + gate("no_update_feed", not os.path.exists(os.path.join(resources, "app-update.yml"))) + if args.info_plist: + with open(args.info_plist, "rb") as handle: + plist = plistlib.load(handle) + result["info_plist"] = { + key: plist.get(key) + for key in ("CFBundleIdentifier", "CFBundleName", "CFBundleShortVersionString", + "LSMinimumSystemVersion", "CFBundleExecutable") + } + gate("plist_version", plist.get("CFBundleShortVersionString") == args.version) + if args.bundle_id: + gate("bundle_id", plist.get("CFBundleIdentifier") == args.bundle_id) + + with tempfile.TemporaryDirectory() as work: + app = os.path.join(work, "app") + subprocess.run([args.node, args.asar, "extract", asar, app], check=True) + with open(os.path.join(app, "package.json")) as handle: + version = json.load(handle).get("version") + gate("app_version", version == args.version, version) + dist = os.path.join(app, "apps/server/dist") + client = os.path.join(dist, "client") + server_tree, server_files = tree(dist, skip=client) + web_tree, web_files = tree(client) + result["hashes"] = { + "app_asar_sha256": sha256(asar), + "server_dist_tree_sha256": server_tree, + "server_dist_files": server_files, + "server_bin_mjs_sha256": sha256(os.path.join(dist, "bin.mjs")), + "web_client_tree_sha256": web_tree, + "web_client_files": web_files, + } + for name, (where, needle) in MARKERS.items(): + root, skip = (dist, client) if where == "server" else (client, None) + gate(f"marker_{name}", contains(root, needle, skip)) + found = [] + for directory, _, files in os.walk(resources): + for name in files: + with open(os.path.join(directory, name), "rb") as handle: + blob = handle.read() + found += [p.decode() for p in CREDENTIAL_PATTERNS if re.search(p, blob)] + gate("no_credential_patterns", not found, sorted(set(found)) or None) + + result["failed_gates"] = failures + with open(args.out, "w") as handle: + json.dump(result, handle, indent=1) + print(json.dumps({"failed_gates": failures, **result["hashes"]}, indent=1)) + sys.exit(1 if failures else 0) + + +if __name__ == "__main__": + main()