From 78f4c5c02846a1c92df8c44def5443e35e388395 Mon Sep 17 00:00:00 2001 From: Alex Holmberg <113964069+Alex793x@users.noreply.github.com> Date: Mon, 23 Nov 2026 15:08:18 +0100 Subject: [PATCH 1/2] fix: sign the bundled onnxruntime libraries for notarized Mac builds [skip ci] Apple rejected the beta.21 Mac build: libonnxruntime.1.23.2.dylib and onnxruntime_binding.node in retriever-runtime had no Developer ID signature or secure timestamp. Public packaging signed only the bundled Node. It now signs every native binary in the bundled dist-* folders the same way, then writes their new fingerprints into the folder's provenance.json (keeping the unsigned ones), because MemCode checks those files against it before loading local search. beta.20 got through only because its notarized build was re-signed by hand. Co-Authored-By: Claude Opus 5.5 --- scripts/macos-release-trust.d.mts | 4 ++ scripts/macos-release-trust.mjs | 66 ++++++++++++++++++++++++++++++- tests/macos-release-trust.test.ts | 65 ++++++++++++++++++++++++++++++ 3 files changed, 134 insertions(+), 1 deletion(-) diff --git a/scripts/macos-release-trust.d.mts b/scripts/macos-release-trust.d.mts index 05a8c46..1f7bff4 100644 --- a/scripts/macos-release-trust.d.mts +++ b/scripts/macos-release-trust.d.mts @@ -11,3 +11,7 @@ export function macReleasePolicy( env?: Record, ): MacReleasePolicy; export function assertPublicMacTrust(proof: T): T; +/** Native binaries in the bundled `dist-*` folders, other than Node, to sign for public release. */ +export function bundledLibrariesToSign(root: string): Promise; +/** Records signed files' fingerprints in their folder's provenance.json, keeping the unsigned ones. */ +export function recordSignedFiles(root: string, signed: string[]): Promise; diff --git a/scripts/macos-release-trust.mjs b/scripts/macos-release-trust.mjs index 9795680..5ffd0ac 100644 --- a/scripts/macos-release-trust.mjs +++ b/scripts/macos-release-trust.mjs @@ -12,7 +12,7 @@ import { chmod, } from "node:fs/promises"; import { tmpdir } from "node:os"; -import { join } from "node:path"; +import { join, relative, sep } from "node:path"; const appleKeys = [ "APPLE_CERTIFICATE", @@ -245,6 +245,24 @@ export async function prepareMacSigning(root, inherited = process.env) { node, ]); run("/usr/bin/codesign", ["--verify", "--strict", node]); + // Native libraries in the other bundled folders, such as onnxruntime for local + // search, are loaded by that Node. Apple rejects the app while any is unsigned. + const libraries = await bundledLibrariesToSign(root); + for (const library of libraries) { + run("/usr/bin/codesign", [ + "--force", + "--timestamp", + "--options", + "runtime", + "--sign", + policy.identity, + "--keychain", + keychain, + library, + ]); + run("/usr/bin/codesign", ["--verify", "--strict", library]); + } + await recordSignedFiles(root, libraries); const signedRuntime = { originalExecutableSha256: provenance.executableSha256, signedExecutableSha256: digest(await readFile(node)), @@ -258,6 +276,52 @@ export async function prepareMacSigning(root, inherited = process.env) { } } +/** + * Writes the fingerprint of each signed file into the provenance.json of the bundled + * folder it is in, keeping the unsigned one, since the app checks those files + * against it before it loads them. + */ +export async function recordSignedFiles(root, signed) { + const byFolder = new Map(); + for (const path of signed) { + const [folder, ...rest] = relative(root, path).split(sep); + byFolder.set(folder, [...(byFolder.get(folder) ?? []), rest.join("/")]); + } + for (const [folder, names] of byFolder) { + const manifest = join(root, folder, "provenance.json"); + let provenance; + try { + provenance = JSON.parse(await readFile(manifest, "utf8")); + } catch { + continue; + } + if (!Array.isArray(provenance.files)) continue; + for (const entry of provenance.files) { + if (!names.includes(entry.name)) continue; + const bytes = await readFile(join(root, folder, entry.name)); + entry.unsignedSha256 ??= entry.sha256; + entry.bytes = bytes.length; + entry.sha256 = digest(bytes); + } + await writeFile(manifest, JSON.stringify(provenance, null, 2) + "\n"); + } +} + +/** + * The native binaries in the folders packaging copies into the app as resources + * (`dist-*` at the root), other than Node, which is signed with its own entitlement. + */ +export async function bundledLibrariesToSign(root) { + const node = join(root, "dist-desktop-runtime/node"); + const folders = (await readdir(root, { withFileTypes: true })).filter( + (entry) => entry.isDirectory() && entry.name.startsWith("dist-"), + ); + const found = await Promise.all( + folders.map((folder) => machoFiles(join(root, folder.name))), + ); + return found.flat().filter((path) => path !== node); +} + export async function machoFiles(directory) { const files = []; for (const entry of await readdir(directory, { withFileTypes: true })) { diff --git a/tests/macos-release-trust.test.ts b/tests/macos-release-trust.test.ts index 77b6b6c..58541b3 100644 --- a/tests/macos-release-trust.test.ts +++ b/tests/macos-release-trust.test.ts @@ -164,3 +164,68 @@ test("the installer background is genuinely 2x at the configured logical size", height: 508, }); }); + +test("public packaging signs the native libraries bundled beside the app, which Apple otherwise rejects", async () => { + const { mkdtemp, mkdir, writeFile, rm } = await import("node:fs/promises"); + const { tmpdir } = await import("node:os"); + const { join } = await import("node:path"); + const { bundledLibrariesToSign } = await import("../scripts/macos-release-trust.mjs"); + const root = await mkdtemp(join(tmpdir(), "memcode-mac-resources-")); + const macho = Buffer.from("cffaedfe0000000000000000", "hex"); + const place = async (path: string, bytes: Buffer | string) => { + await mkdir(join(root, path, ".."), { recursive: true }); + await writeFile(join(root, path), bytes); + }; + try { + const onnx = "dist-retriever-runtime/node_modules/onnxruntime-node/bin/napi-v6/darwin/arm64"; + await place(`${onnx}/onnxruntime_binding.node`, macho); + await place(`${onnx}/libonnxruntime.1.23.2.dylib`, macho); + await place("dist-retriever-runtime/node_modules/onnxruntime-node/README.md", "# docs"); + // Node is signed on its own, with the JIT entitlement it needs. + await place("dist-desktop-runtime/node", macho); + await place("src-tauri/target/release/memcode", macho); + assert.deepEqual( + (await bundledLibrariesToSign(root)).map((path: string) => path.slice(root.length + 1)).sort(), + [`${onnx}/libonnxruntime.1.23.2.dylib`, `${onnx}/onnxruntime_binding.node`], + ); + } finally { + await rm(root, { recursive: true, force: true }); + } +}); + +test("a signed library's new fingerprint is recorded, so the app's own check still accepts it", async () => { + const { mkdtemp, mkdir, writeFile, readFile: read, rm } = await import("node:fs/promises"); + const { tmpdir } = await import("node:os"); + const { join } = await import("node:path"); + const { createHash } = await import("node:crypto"); + const { recordSignedFiles } = await import("../scripts/macos-release-trust.mjs"); + const sha = (bytes: Buffer | string) => createHash("sha256").update(bytes).digest("hex"); + const root = await mkdtemp(join(tmpdir(), "memcode-mac-provenance-")); + const folder = join(root, "dist-retriever-runtime"); + const library = "node_modules/onnxruntime-node/bin/napi-v6/darwin/arm64/onnxruntime_binding.node"; + try { + await mkdir(join(folder, library, ".."), { recursive: true }); + await writeFile(join(folder, library), "signed bytes, longer than before"); + await writeFile( + join(folder, "provenance.json"), + JSON.stringify({ + schema: 1, + files: [ + { name: library, bytes: 8, sha256: sha("unsigned") }, + { name: "package.json", bytes: 2, sha256: sha("{}") }, + ], + }), + ); + await recordSignedFiles(root, [join(folder, library)]); + const { files } = JSON.parse(await read(join(folder, "provenance.json"), "utf8")); + assert.deepEqual(files[0], { + name: library, + bytes: "signed bytes, longer than before".length, + sha256: sha("signed bytes, longer than before"), + unsignedSha256: sha("unsigned"), + }); + assert.deepEqual(files[1], { name: "package.json", bytes: 2, sha256: sha("{}") }); + } finally { + await rm(root, { recursive: true, force: true }); + } +}); From 07da753bd4143acaa3985eb887797ee6e46cf733 Mon Sep 17 00:00:00 2001 From: Alex Holmberg <113964069+Alex793x@users.noreply.github.com> Date: Mon, 23 Nov 2026 16:33:34 +0100 Subject: [PATCH 2/2] fix: check the engine's programs for notarization, not as apps [skip ci] The public Mac check ran Gatekeeper's app assessment on the four engine programs extracted from engine.zip. macOS always answers "valid but does not seem to be an app" for command-line tools, so a correctly signed and notarized engine failed the beta.21 build every time. It now asks codesign whether Apple notarized each one (=notarized requirement). Co-Authored-By: Claude Opus 5.5 --- scripts/macos-release-trust.d.mts | 2 ++ scripts/macos-release-trust.mjs | 22 ++++++++++++++++++---- tests/macos-release-trust.test.ts | 24 ++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/scripts/macos-release-trust.d.mts b/scripts/macos-release-trust.d.mts index 1f7bff4..df8d700 100644 --- a/scripts/macos-release-trust.d.mts +++ b/scripts/macos-release-trust.d.mts @@ -15,3 +15,5 @@ export function assertPublicMacTrust(proof: T): T; export function bundledLibrariesToSign(root: string): Promise; /** Records signed files' fingerprints in their folder's provenance.json, keeping the unsigned ones. */ export function recordSignedFiles(root: string, signed: string[]): Promise; +/** Whether Apple notarized a standalone program; Gatekeeper's app assessment cannot tell. */ +export function isNotarizedExecutable(binary: string): boolean; diff --git a/scripts/macos-release-trust.mjs b/scripts/macos-release-trust.mjs index 5ffd0ac..e4a1a39 100644 --- a/scripts/macos-release-trust.mjs +++ b/scripts/macos-release-trust.mjs @@ -1,4 +1,4 @@ -import { execFileSync } from "node:child_process"; +import { execFileSync, spawnSync } from "node:child_process"; import { randomBytes, createHash } from "node:crypto"; import { mkdir, @@ -276,6 +276,21 @@ export async function prepareMacSigning(root, inherited = process.env) { } } +/** + * Whether Apple notarized a standalone program, such as the engine's command-line + * tools. Gatekeeper's app assessment rejects anything that is not an app bundle + * ("does not seem to be an app"), so it cannot answer this; codesign can. + */ +export function isNotarizedExecutable(binary) { + return ( + spawnSync( + "/usr/bin/codesign", + ["--verify", "--strict", "--test-requirement==notarized", binary], + { stdio: "ignore" }, + ).status === 0 + ); +} + /** * Writes the fingerprint of each signed file into the provenance.json of the bundled * folder it is in, keeping the unsigned one, since the app checks those files @@ -417,7 +432,6 @@ export async function verifyMacRelease(root, signing) { try { run("/usr/bin/ditto", ["-x", "-k", engineArchive, engineTemp]); const names = ["memtrace", "memcore-server", "memcortex-daemon", "memcortex-mcp"]; - const { spawnSync } = await import("node:child_process"); for (const name of names) { const binary = join(engineTemp, name); const expected = engineManifest.files.find(file => file.name === name); @@ -430,7 +444,8 @@ export async function verifyMacRelease(root, signing) { const details = display.stdout + display.stderr; if (display.status !== 0 || !details.includes("Authority=Developer ID Application:") || !details.includes("TeamIdentifier=" + proof.teamId) || !/flags=.*runtime/.test(details)) throw Error("Public Mac distribution requires separately Developer ID signed, hardened and notarized engine executables. This bundled engine has not established that trust; use an explicitly labelled manual-approval beta until a trusted engine release is pinned."); - run("/usr/sbin/spctl", ["--assess", "--type", "execute", "--verbose=4", binary]); + if (!isNotarizedExecutable(binary)) + throw Error(`Apple has not notarized the bundled engine program ${name}.`); } proof.bundledEngineNativeTrust = { developerIdSigned: true, gatekeeper: "accepted", nativeExecutables: names.length, archiveSha256: engineManifest.sha256 }; } finally { @@ -459,7 +474,6 @@ export async function verifyMacRelease(root, signing) { run("/usr/bin/codesign", ["--verify", "--strict", binary]); // codesign writes metadata to stderr even on success. // Collect both streams without printing any content. - const { spawnSync } = await import("node:child_process"); const display = spawnSync( "/usr/bin/codesign", ["--display", "--verbose=4", binary], diff --git a/tests/macos-release-trust.test.ts b/tests/macos-release-trust.test.ts index 58541b3..7a35976 100644 --- a/tests/macos-release-trust.test.ts +++ b/tests/macos-release-trust.test.ts @@ -229,3 +229,27 @@ test("a signed library's new fingerprint is recorded, so the app's own check sti await rm(root, { recursive: true, force: true }); } }); + +test("a command-line program counts as notarized only when Apple notarized it", { skip: process.platform !== "darwin" }, async () => { + const { mkdtemp, copyFile, rm } = await import("node:fs/promises"); + const { existsSync } = await import("node:fs"); + const { tmpdir } = await import("node:os"); + const { join } = await import("node:path"); + const { execFileSync } = await import("node:child_process"); + const { isNotarizedExecutable } = await import("../scripts/macos-release-trust.mjs"); + const folder = await mkdtemp(join(tmpdir(), "memcode-notarized-")); + try { + const local = join(folder, "echo"); + await copyFile("/bin/echo", local); + execFileSync("/usr/bin/codesign", ["--force", "--sign", "-", local], { stdio: "ignore" }); + assert.equal(isNotarizedExecutable(local), false); + // The engine packaging staged, Developer ID signed and notarized, when this computer has one. + const engine = new URL("../dist-memtrace-engine/engine.zip", import.meta.url).pathname; + if (existsSync(engine)) { + execFileSync("/usr/bin/ditto", ["-x", "-k", engine, folder]); + assert.equal(isNotarizedExecutable(join(folder, "memtrace")), true); + } + } finally { + await rm(folder, { recursive: true, force: true }); + } +});