Skip to content

7.4 billion tokens per month. 34 free LLM providers. 635 free model endpoints. All behind one /v1 endpoint, plus any custom OpenAI-compatible endpoint. Smart routing, automatic failover, encrypted keys. Personal experimentation only.

TypeScript

ci(desktop): notarize the app in our own afterSign hook with stapling retries (#1498)

Tashfeen96b6367c2e95
diff --git a/desktop/electron-builder.yml b/desktop/electron-builder.yml
index 21c1caee..44caba76 100644
--- a/desktop/electron-builder.yml
+++ b/desktop/electron-builder.yml
@@ -22,6 +22,7 @@ asar: true
# Linux chrome-sandbox must be mode 4755 (setuid) in the .deb; otherwise
# Electron aborts at launch (#1229). Path is relative to desktop/.
afterPack: ./scripts/after-pack-linux-sandbox.mjs
+afterSign: ./scripts/after-sign-notarize.mjs
asarUnpack:
- "**/node_modules/better-sqlite3/**/*.node"
- "**/node_modules/better-sqlite3/build/Release/**"
@@ -39,7 +40,10 @@ mac:
gatekeeperAssess: false
entitlements: assets/entitlements.mac.plist
entitlementsInherit: assets/entitlements.mac.inherit.plist
- notarize: true
+ # Notarization runs in afterSign (scripts/after-sign-notarize.mjs) rather
+ # than electron-builder's built-in step, which staples once and fails the
+ # whole build when Apple's ticket lookup drops the connection (v0.14.0/1).
+ notarize: false
# Select the architecture with --arm64 or --x64 in the build command.
# Per-target arch lists override that selection and make each CI job build
# both architectures, including the native SQLite module for the other CPU.
diff --git a/desktop/scripts/after-sign-notarize.mjs b/desktop/scripts/after-sign-notarize.mjs
new file mode 100644
index 00000000..dd83842f
--- /dev/null
+++ b/desktop/scripts/after-sign-notarize.mjs
@@ -0,0 +1,108 @@
+// Notarize and staple the signed .app (replaces electron-builder's built-in
+// `mac.notarize`). The built-in path staples once and gives up, and on the
+// macos-15-intel runners the staple's ticket lookup at api.apple-cloudkit.com
+// regularly loses its connection ("Error 68", NSURLErrorDomain -1005) right
+// after Apple accepted the submission — that held back the v0.14.0 and v0.14.1
+// desktop installers. The ticket already exists by then, so retrying the
+// staple with a short backoff is all it takes.
+//
+// Same credentials as electron-builder: an App Store Connect API key
+// (APPLE_API_KEY = path to the .p8, APPLE_API_KEY_ID, APPLE_API_ISSUER) or a
+// stored notarytool keychain profile (APPLE_KEYCHAIN_PROFILE, optionally
+// APPLE_KEYCHAIN). Without credentials, or for an app that is not signed with
+// a Developer ID (local and pull-request builds), it skips, as electron-builder
+// did.
+import { execFile } from 'node:child_process';
+import { mkdtempSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { promisify } from 'node:util';
+
+const execFileAsync = promisify(execFile);
+
+export const STAPLE_ATTEMPTS = 5;
+export const STAPLE_BACKOFF_MS = 20_000;
+
+/** Runs a command and resolves { code, stdout, stderr } without throwing. */
+async function run(cmd, args) {
+ try {
+ const { stdout, stderr } = await execFileAsync(cmd, args, { maxBuffer: 16 * 1024 * 1024 });
+ return { code: 0, stdout, stderr };
+ } catch (err) {
+ return { code: typeof err.code === 'number' ? err.code : 1, stdout: err.stdout ?? '', stderr: err.stderr ?? String(err.message ?? err) };
+ }
+}
+
+/** notarytool authentication arguments from the environment, or null. */
+export function notaryAuthArgs(env = process.env) {
+ if (env.APPLE_API_KEY && env.APPLE_API_KEY_ID && env.APPLE_API_ISSUER) {
+ return ['--key', env.APPLE_API_KEY, '--key-id', env.APPLE_API_KEY_ID, '--issuer', env.APPLE_API_ISSUER];
+ }
+ if (env.APPLE_KEYCHAIN_PROFILE) {
+ return ['--keychain-profile', env.APPLE_KEYCHAIN_PROFILE, ...(env.APPLE_KEYCHAIN ? ['--keychain', env.APPLE_KEYCHAIN] : [])];
+ }
+ return null;
+}
+
+/** Staple, then validate, retrying both together on failure. */
+export async function stapleWithRetry(target, { exec = run, sleep = ms => new Promise(r => setTimeout(r, ms)), log = console.log, attempts = STAPLE_ATTEMPTS, backoffMs = STAPLE_BACKOFF_MS } = {}) {
+ let last = '';
+ for (let attempt = 1; attempt <= attempts; attempt++) {
+ const staple = await exec('xcrun', ['stapler', 'staple', target]);
+ const validate = staple.code === 0 ? await exec('xcrun', ['stapler', 'validate', target]) : null;
+ if (validate && validate.code === 0) {
+ log(` stapled ${target}${attempt > 1 ? ` (attempt ${attempt})` : ''}`);
+ return attempt;
+ }
+ const failed = validate ?? staple;
+ last = `${failed.stdout}${failed.stderr}`.trim().split('\n').slice(-2).join(' ');
+ if (attempt < attempts) {
+ log(` staple attempt ${attempt} failed, retrying in ${(backoffMs * attempt) / 1000}s: ${last}`);
+ await sleep(backoffMs * attempt);
+ }
+ }
+ throw new Error(`stapling ${target} failed after ${attempts} attempts: ${last}`);
+}
+
+/** Submit a zip of the app and wait; throws unless Apple accepts it. */
+export async function submitForNotarization(appPath, auth, { exec = run, log = console.log } = {}) {
+ const dir = mkdtempSync(join(tmpdir(), 'notarize-'));
+ const zip = join(dir, 'app.zip');
+ try {
+ const zipped = await exec('ditto', ['-c', '-k', '--sequesterRsrc', '--keepParent', appPath, zip]);
+ if (zipped.code !== 0) throw new Error(`zipping ${appPath} failed: ${zipped.stderr}`);
+ log(` submitting ${appPath} to Apple's notary service`);
+ const submitted = await exec('xcrun', ['notarytool', 'submit', zip, ...auth, '--wait', '--timeout', '30m', '--output-format', 'json']);
+ let result = {};
+ try { result = JSON.parse(submitted.stdout); } catch { /* reported below */ }
+ if (result.status !== 'Accepted') {
+ let detail = '';
+ if (result.id) {
+ const notes = await exec('xcrun', ['notarytool', 'log', result.id, ...auth]);
+ detail = `\n${notes.stdout}`;
+ }
+ throw new Error(`notarization was not accepted (status: ${result.status ?? 'unknown'}, exit ${submitted.code}): ${submitted.stderr}${detail}`);
+ }
+ log(` notarization accepted (${result.id})`);
+ } finally {
+ rmSync(dir, { recursive: true, force: true });
+ }
+}
+
+export default async function afterSign(context, deps = {}) {
+ if (context.electronPlatformName !== 'darwin') return;
+ const { exec = run, log = console.log, env = process.env } = deps;
+ const appPath = join(context.appOutDir, `${context.packager.appInfo.productFilename}.app`);
+ const auth = notaryAuthArgs(env);
+ if (!auth) {
+ log(' skipped notarization: no Apple API key or keychain profile in the environment');
+ return;
+ }
+ const signature = await exec('codesign', ['-dv', '--verbose=2', appPath]);
+ if (!`${signature.stdout}${signature.stderr}`.includes('Authority=Developer ID Application')) {
+ log(' skipped notarization: the app is not signed with a Developer ID certificate');
+ return;
+ }
+ await submitForNotarization(appPath, auth, { exec, log });
+ await stapleWithRetry(appPath, { ...deps, exec, log });
+}
diff --git a/desktop/src/__tests__/after-sign-notarize.test.ts b/desktop/src/__tests__/after-sign-notarize.test.ts
new file mode 100644
index 00000000..bcd04fcf
--- /dev/null
+++ b/desktop/src/__tests__/after-sign-notarize.test.ts
@@ -0,0 +1,101 @@
+import { readFileSync, existsSync } from 'node:fs';
+import { createRequire } from 'node:module';
+import { dirname, join, resolve } from 'node:path';
+import { fileURLToPath, pathToFileURL } from 'node:url';
+import { describe, expect, it } from 'vitest';
+
+// v0.14.0/v0.14.1: the Intel macOS release failed when stapling lost its
+// connection to Apple right after notarization was accepted. Notarization now
+// runs in our own afterSign hook, which retries the staple.
+
+const yaml = createRequire(import.meta.url)('js-yaml');
+const here = dirname(fileURLToPath(import.meta.url));
+const desktopRoot = resolve(here, '../..');
+const hookRel = './scripts/after-sign-notarize.mjs';
+const hook = await import(pathToFileURL(resolve(desktopRoot, hookRel)).href);
+
+type Result = { code: number; stdout: string; stderr: string };
+const ok = (stdout = ''): Result => ({ code: 0, stdout, stderr: '' });
+const fail = (stderr: string): Result => ({ code: 68, stdout: '', stderr });
+
+function fakeExec(script: Array<[string, Result]>) {
+ const calls: string[] = [];
+ const exec = async (cmd: string, args: string[]) => {
+ const line = `${cmd} ${args.join(' ')}`;
+ calls.push(line);
+ const i = script.findIndex(([prefix]) => line.startsWith(prefix));
+ if (i === -1) return ok();
+ const [, result] = script.splice(i, 1)[0];
+ return result;
+ };
+ return { exec, calls };
+}
+
+const quiet = { log: () => {}, sleep: async () => {} };
+const apiEnv = { APPLE_API_KEY: '/tmp/AuthKey.p8', APPLE_API_KEY_ID: 'KEYID', APPLE_API_ISSUER: 'ISSUER' };
+const context = { electronPlatformName: 'darwin', appOutDir: '/out/mac', packager: { appInfo: { productFilename: 'FreeLLMAPI' } } };
+
+describe('afterSign notarization', () => {
+ it('replaces electron-builder notarization with the hook', () => {
+ const config = yaml.load(readFileSync(join(desktopRoot, 'electron-builder.yml'), 'utf8')) as { afterSign?: string; mac?: { notarize?: boolean } };
+ expect(config.afterSign).toBe(hookRel);
+ expect(config.mac?.notarize).toBe(false);
+ expect(existsSync(resolve(desktopRoot, hookRel))).toBe(true);
+ });
+
+ it('builds notarytool auth from an API key or a keychain profile', () => {
+ expect(hook.notaryAuthArgs(apiEnv)).toEqual(['--key', '/tmp/AuthKey.p8', '--key-id', 'KEYID', '--issuer', 'ISSUER']);
+ expect(hook.notaryAuthArgs({ APPLE_KEYCHAIN_PROFILE: 'p', APPLE_KEYCHAIN: 'k' })).toEqual(['--keychain-profile', 'p', '--keychain', 'k']);
+ expect(hook.notaryAuthArgs({})).toBeNull();
+ });
+
+ it('retries the staple when Apple drops the connection, then succeeds', async () => {
+ const { exec, calls } = fakeExec([
+ ['xcrun stapler staple', fail('The staple and validate action failed! Error 68.')],
+ ['xcrun stapler staple', ok()],
+ ['xcrun stapler validate', fail('The validate action failed! Error 68.')],
+ ]);
+ const attempt = await hook.stapleWithRetry('/out/mac/FreeLLMAPI.app', { exec, ...quiet });
+ expect(attempt).toBe(3);
+ expect(calls.filter(c => c.startsWith('xcrun stapler staple'))).toHaveLength(3);
+ });
+
+ it('gives up with a clear error after the last attempt', async () => {
+ const { exec } = fakeExec(Array.from({ length: 5 }, () => ['xcrun stapler staple', fail('Error 68')] as [string, Result]));
+ await expect(hook.stapleWithRetry('/x.app', { exec, ...quiet })).rejects.toThrow(/failed after 5 attempts/);
+ });
+
+ it('notarizes and staples a Developer ID signed app', async () => {
+ const { exec, calls } = fakeExec([
+ ['codesign', ok('Authority=Developer ID Application: Example (TEAM)')],
+ ['xcrun notarytool submit', ok(JSON.stringify({ id: 'abc', status: 'Accepted' }))],
+ ]);
+ await hook.default(context, { exec, env: apiEnv, ...quiet });
+ expect(calls.some(c => c.startsWith('ditto -c -k'))).toBe(true);
+ expect(calls.find(c => c.startsWith('xcrun notarytool submit'))).toContain('--key-id KEYID');
+ expect(calls).toContain(`xcrun stapler staple ${join('/out/mac', 'FreeLLMAPI.app')}`);
+ });
+
+ it('fails the build when Apple rejects the submission', async () => {
+ const { exec } = fakeExec([
+ ['codesign', ok('Authority=Developer ID Application: Example (TEAM)')],
+ ['xcrun notarytool submit', ok(JSON.stringify({ id: 'abc', status: 'Invalid' }))],
+ ['xcrun notarytool log', ok('{"issues":[]}')],
+ ]);
+ await expect(hook.default(context, { exec, env: apiEnv, ...quiet })).rejects.toThrow(/not accepted \(status: Invalid/);
+ });
+
+ it('skips unsigned builds, builds without credentials, and other platforms', async () => {
+ const unsigned = fakeExec([['codesign', ok('Signature=adhoc')]]);
+ await hook.default(context, { exec: unsigned.exec, env: apiEnv, ...quiet });
+ expect(unsigned.calls.some(c => c.includes('notarytool'))).toBe(false);
+
+ const noCreds = fakeExec([]);
+ await hook.default(context, { exec: noCreds.exec, env: {}, ...quiet });
+ expect(noCreds.calls).toEqual([]);
+
+ const linux = fakeExec([]);
+ await hook.default({ ...context, electronPlatformName: 'linux' }, { exec: linux.exec, env: apiEnv, ...quiet });
+ expect(linux.calls).toEqual([]);
+ });
+});