diff --git a/config/electron-builder.config.cjs b/config/electron-builder.config.cjs index 9e482825a..ae1db494b 100644 --- a/config/electron-builder.config.cjs +++ b/config/electron-builder.config.cjs @@ -1,6 +1,7 @@ -const { chmodSync, existsSync, readdirSync } = require('node:fs') +const { chmodSync, existsSync, mkdtempSync, readdirSync, rmSync } = require('node:fs') const { execFileSync } = require('node:child_process') -const { join, resolve } = require('node:path') +const { basename, dirname, join, resolve } = require('node:path') +const { tmpdir } = require('node:os') const electronBuilderNativeRebuild = require('./scripts/electron-builder-native-rebuild.cjs') const { createPackagedRuntimeNodeModuleResources, @@ -144,6 +145,9 @@ module.exports = { await signMacComputerUseHelper(join(resourcesDir, 'Orca Computer Use.app'), context.packager) } }, + afterSign: async (context) => { + await notarizeMacServeSimCameraDylibs(context) + }, win: { executableName: 'Orca', // Why: Windows installers are signed after electron-builder packaging by @@ -371,6 +375,184 @@ function chmodMacServeSimHelpers(resourcesDir, electronPlatformName) { } } +async function notarizeMacServeSimCameraDylibs(context) { + await notarizeMacServeSimCameraDylibsWithHost(context, macNotarizationHost) +} + +async function notarizeMacServeSimCameraDylibsWithHost(context, host) { + if (!isMacRelease || context.electronPlatformName !== 'darwin') { + return + } + const resourcesDir = join( + context.appOutDir, + `${context.packager.appInfo.productFilename}.app`, + 'Contents', + 'Resources' + ) + const requiredDylibPaths = [ + join(resourcesDir, 'serve-sim', 'dist', 'simcam', 'libSimCameraInjector.dylib'), + join(resourcesDir, 'node_modules', 'serve-sim', 'dist', 'simcam', 'libSimCameraInjector.dylib') + ] + const optionalDylibPaths = [ + join( + resourcesDir, + 'app.asar.unpacked', + 'node_modules', + 'serve-sim', + 'dist', + 'simcam', + 'libSimCameraInjector.dylib' + ) + ] + for (const dylibPath of requiredDylibPaths) { + if (!host.existsSync(dylibPath)) { + throw new Error(`Missing serve-sim camera dylib for notarization: ${dylibPath}`) + } + } + const dylibPaths = [ + ...requiredDylibPaths, + ...optionalDylibPaths.filter((dylibPath) => host.existsSync(dylibPath)) + ] + // Why: stapler cannot attach tickets to bare dylibs; submitting each signed + // dylib zip registers its cdhash for direct Gatekeeper assessment. + for (const dylibPath of dylibPaths) { + notarizeMacStandaloneBinary(dylibPath, host) + } +} + +let macNotarizationHost = { + existsSync, + execFileSync, + mkdtempSync, + rmSync +} + +async function withMacNotarizationHost(host, callback) { + const previousHost = macNotarizationHost + macNotarizationHost = host + try { + return await callback() + } finally { + macNotarizationHost = previousHost + } +} + +function notarizeMacStandaloneBinary(binaryPath, host) { + const archiveDir = host.mkdtempSync(join(tmpdir(), 'orca-mac-standalone-notary-')) + const archivePath = join(archiveDir, `${basename(binaryPath)}.zip`) + try { + host.execFileSync( + 'ditto', + ['-c', '-k', '--sequesterRsrc', '--keepParent', basename(binaryPath), archivePath], + { + cwd: dirname(binaryPath), + stdio: 'inherit', + timeout: macDylibArchiveTimeoutMs + } + ) + const credentialArgs = macNotarytoolCredentialArgs() + const output = execFileSyncWithRedactedError( + host, + 'xcrun', + ['notarytool', 'submit', archivePath, ...credentialArgs, '--wait', '--output-format', 'json'], + { + encoding: 'utf8', + timeout: macDylibNotarytoolTimeoutMs + }, + credentialArgs, + binaryPath + ) + const result = JSON.parse(output) + if (result.status !== 'Accepted') { + throw new Error(`Standalone binary notarization failed for ${binaryPath}: ${output}`) + } + } finally { + host.rmSync(archiveDir, { recursive: true, force: true }) + } +} + +const macDylibArchiveTimeoutMs = 2 * 60 * 1000 +// Why: release jobs should fail loudly instead of hanging on a stuck submission. +const macDylibNotarytoolTimeoutMs = 45 * 60 * 1000 + +const macNotarytoolCredentialFlags = new Set([ + '--apple-id', + '--password', + '--team-id', + '--key', + '--key-id', + '--issuer', + '--keychain', + '--keychain-profile' +]) + +function execFileSyncWithRedactedError(host, command, args, options, credentialArgs, binaryPath) { + try { + return host.execFileSync(command, args, options) + } catch (error) { + throw new Error( + `Standalone binary notarization subprocess failed for ${binaryPath}: ${redactCredentialValues( + error instanceof Error ? error.message : String(error), + credentialArgs + )}` + ) + } +} + +function redactCredentialValues(message, credentialArgs) { + let redacted = message + for (let index = 0; index < credentialArgs.length - 1; index += 2) { + const value = credentialArgs[index + 1] + if (macNotarytoolCredentialFlags.has(credentialArgs[index]) && value) { + redacted = redacted.split(value).join('[REDACTED]') + } + } + return redacted +} + +function macNotarytoolCredentialArgs() { + if (process.env.APPLE_ID || process.env.APPLE_APP_SPECIFIC_PASSWORD) { + if (!process.env.APPLE_ID) { + throw new Error('APPLE_ID env var needs to be set') + } + if (!process.env.APPLE_APP_SPECIFIC_PASSWORD) { + throw new Error('APPLE_APP_SPECIFIC_PASSWORD env var needs to be set') + } + if (!process.env.APPLE_TEAM_ID) { + throw new Error('APPLE_TEAM_ID env var needs to be set') + } + return [ + '--apple-id', + process.env.APPLE_ID, + '--password', + process.env.APPLE_APP_SPECIFIC_PASSWORD, + '--team-id', + process.env.APPLE_TEAM_ID + ] + } + if (process.env.APPLE_API_KEY && process.env.APPLE_API_KEY_ID && process.env.APPLE_API_ISSUER) { + return [ + '--key', + process.env.APPLE_API_KEY, + '--key-id', + process.env.APPLE_API_KEY_ID, + '--issuer', + process.env.APPLE_API_ISSUER + ] + } + if (process.env.APPLE_KEYCHAIN_PROFILE) { + return [ + ...(process.env.APPLE_KEYCHAIN ? ['--keychain', process.env.APPLE_KEYCHAIN] : []), + '--keychain-profile', + process.env.APPLE_KEYCHAIN_PROFILE + ] + } + if (process.env.APPLE_API_KEY || process.env.APPLE_API_KEY_ID || process.env.APPLE_API_ISSUER) { + throw new Error('Env vars APPLE_API_KEY, APPLE_API_KEY_ID and APPLE_API_ISSUER need to be set') + } + throw new Error('Missing notarization credentials for serve-sim camera dylibs') +} + async function signMacComputerUseHelper(helperAppPath, packager) { if (!existsSync(helperAppPath)) { if (isMacRelease) { @@ -434,3 +616,14 @@ function findInstalledMacSigningIdentity(keychainFile) { } catch {} return null } + +if (process.env.VITEST || process.env.VITEST_WORKER_ID) { + Object.defineProperty(module.exports, '__test', { + enumerable: false, + value: { + macNotarytoolCredentialArgs, + notarizeMacServeSimCameraDylibsWithHost, + withMacNotarizationHost + } + }) +} diff --git a/config/scripts/electron-builder-config.test.mjs b/config/scripts/electron-builder-config.test.mjs index 3f318219e..c3a32e99c 100644 --- a/config/scripts/electron-builder-config.test.mjs +++ b/config/scripts/electron-builder-config.test.mjs @@ -1,8 +1,8 @@ import { mkdir, mkdtemp, readFile, readdir, rm, stat, writeFile } from 'node:fs/promises' import { createRequire } from 'node:module' import { tmpdir } from 'node:os' -import { join } from 'node:path' -import { describe, expect, it } from 'vitest' +import { dirname, join } from 'node:path' +import { describe, expect, it, vi } from 'vitest' const require = createRequire(import.meta.url) const electronBuilderConfig = require('../electron-builder.config.cjs') @@ -18,6 +18,128 @@ const { verifyPackagedMainRuntimeDeps } = require('../packaged-runtime-node-modules.cjs') +const electronBuilderConfigPath = require.resolve('../electron-builder.config.cjs') +const macNotaryEnvKeys = [ + 'APPLE_ID', + 'APPLE_APP_SPECIFIC_PASSWORD', + 'APPLE_TEAM_ID', + 'APPLE_API_KEY', + 'APPLE_API_KEY_ID', + 'APPLE_API_ISSUER', + 'APPLE_KEYCHAIN_PROFILE', + 'APPLE_KEYCHAIN', + 'ORCA_MAC_RELEASE' +] +const appleIdMacReleaseEnv = { + APPLE_APP_SPECIFIC_PASSWORD: 'app-password', + APPLE_ID: 'release@example.com', + APPLE_TEAM_ID: 'TEAMID', + ORCA_MAC_RELEASE: '1' +} + +async function withElectronBuilderConfigEnv(env, callback) { + const previous = new Map(macNotaryEnvKeys.map((key) => [key, process.env[key]])) + try { + for (const key of macNotaryEnvKeys) { + delete process.env[key] + } + Object.assign(process.env, env) + delete require.cache[electronBuilderConfigPath] + return await callback(require('../electron-builder.config.cjs')) + } finally { + for (const key of macNotaryEnvKeys) { + const value = previous.get(key) + if (value === undefined) { + delete process.env[key] + } else { + process.env[key] = value + } + } + delete require.cache[electronBuilderConfigPath] + } +} + +function createMacNotarizationContext(appOutDir) { + return { + appOutDir, + electronPlatformName: 'darwin', + packager: { + appInfo: { + productFilename: 'Orca' + } + } + } +} + +function requiredMacServeSimDylibPaths(appOutDir) { + const resourcesDir = join(appOutDir, 'Orca.app', 'Contents', 'Resources') + return [ + join(resourcesDir, 'serve-sim', 'dist', 'simcam', 'libSimCameraInjector.dylib'), + join(resourcesDir, 'node_modules', 'serve-sim', 'dist', 'simcam', 'libSimCameraInjector.dylib') + ] +} + +function optionalMacServeSimDylibPaths(appOutDir) { + const resourcesDir = join(appOutDir, 'Orca.app', 'Contents', 'Resources') + return [ + join( + resourcesDir, + 'app.asar.unpacked', + 'node_modules', + 'serve-sim', + 'dist', + 'simcam', + 'libSimCameraInjector.dylib' + ) + ] +} + +function macServeSimDylibPaths(appOutDir) { + return [...requiredMacServeSimDylibPaths(appOutDir), ...optionalMacServeSimDylibPaths(appOutDir)] +} + +function createMacNotarizationHost({ existingPaths, output = { status: 'Accepted' } }) { + const calls = [] + let temporaryDirectoryIndex = 0 + const host = { + calls, + execFileSync: vi.fn((command, args, options = {}) => { + calls.push({ args, command, options }) + if (command === 'xcrun') { + return typeof output === 'string' ? output : JSON.stringify(output) + } + return '' + }), + existsSync: vi.fn((targetPath) => existingPaths.has(targetPath)), + mkdtempSync: vi.fn((prefix) => `${prefix}${++temporaryDirectoryIndex}`), + rmSync: vi.fn() + } + return host +} + +async function runMacAfterSignWithHost(config, appOutDir, host) { + return await config.__test.withMacNotarizationHost(host, async () => { + await config.afterSign(createMacNotarizationContext(appOutDir)) + }) +} + +function expectNotaryCredentials(calls, credentials, expectedCallCount = 3) { + const notaryCalls = calls.filter(({ command }) => command === 'xcrun') + expect(notaryCalls).toHaveLength(expectedCallCount) + for (const notaryCall of notaryCalls) { + expect(notaryCall.args).toEqual( + expect.arrayContaining([ + 'notarytool', + 'submit', + ...credentials, + '--wait', + '--output-format', + 'json' + ]) + ) + } +} + describe('electron-builder config', () => { it('excludes repo-only source trees from app.asar', () => { expect(electronBuilderConfig.files).toEqual( @@ -63,6 +185,208 @@ describe('electron-builder config', () => { ) }) + it('keeps the macOS afterSign hook inert outside macOS release builds', async () => { + for (const { context, env } of [ + { context: { electronPlatformName: 'darwin' }, env: {} }, + { context: { electronPlatformName: 'linux' }, env: { ORCA_MAC_RELEASE: '1' } } + ]) { + await withElectronBuilderConfigEnv(env, async (config) => { + const host = createMacNotarizationHost({ existingPaths: new Set() }) + + await expect( + config.__test.withMacNotarizationHost(host, async () => { + await config.afterSign(context) + }) + ).resolves.toBeUndefined() + expect(host.execFileSync).not.toHaveBeenCalled() + }) + } + }) + + it('submits all packaged serve-sim camera dylib copies in macOS releases', async () => { + await withElectronBuilderConfigEnv(appleIdMacReleaseEnv, async (config) => { + const appOutDir = join(tmpdir(), 'orca-electron-builder-test') + const dylibPaths = macServeSimDylibPaths(appOutDir) + const host = createMacNotarizationHost({ existingPaths: new Set(dylibPaths) }) + + await runMacAfterSignWithHost(config, appOutDir, host) + + const dittoCalls = host.calls.filter(({ command }) => command === 'ditto') + const notaryCalls = host.calls.filter(({ command }) => command === 'xcrun') + expect(dittoCalls).toHaveLength(dylibPaths.length) + expect(notaryCalls).toHaveLength(dylibPaths.length) + expect(dittoCalls.map(({ options }) => options.cwd)).toEqual( + dylibPaths.map((dylibPath) => dirname(dylibPath)) + ) + expect(dittoCalls.every(({ options }) => options.timeout === 2 * 60 * 1000)).toBe(true) + expect(notaryCalls.every(({ options }) => options.timeout === 45 * 60 * 1000)).toBe(true) + expect(dittoCalls.map(({ args }) => args.at(-1))).toEqual( + notaryCalls.map(({ args }) => args[2]) + ) + expectNotaryCredentials(host.calls, [ + '--apple-id', + 'release@example.com', + '--password', + 'app-password', + '--team-id', + 'TEAMID' + ]) + expect(host.rmSync).toHaveBeenCalledTimes(dylibPaths.length) + }) + }) + + it('does not require optional app.asar.unpacked serve-sim dylibs in macOS releases', async () => { + await withElectronBuilderConfigEnv(appleIdMacReleaseEnv, async (config) => { + const appOutDir = join(tmpdir(), 'orca-electron-builder-test') + const dylibPaths = requiredMacServeSimDylibPaths(appOutDir) + const host = createMacNotarizationHost({ existingPaths: new Set(dylibPaths) }) + + await runMacAfterSignWithHost(config, appOutDir, host) + + const dittoCalls = host.calls.filter(({ command }) => command === 'ditto') + expect(dittoCalls).toHaveLength(dylibPaths.length) + expect(dittoCalls.map(({ options }) => options.cwd)).toEqual( + dylibPaths.map((dylibPath) => dirname(dylibPath)) + ) + expectNotaryCredentials( + host.calls, + ['--apple-id', 'release@example.com', '--password', 'app-password', '--team-id', 'TEAMID'], + dylibPaths.length + ) + expect(host.rmSync).toHaveBeenCalledTimes(dylibPaths.length) + }) + }) + + it('fails before notarization when a packaged serve-sim camera dylib is missing', async () => { + await withElectronBuilderConfigEnv({ ORCA_MAC_RELEASE: '1' }, async (config) => { + const appOutDir = join(tmpdir(), 'orca-electron-builder-test') + const [firstDylibPath] = macServeSimDylibPaths(appOutDir) + const host = createMacNotarizationHost({ existingPaths: new Set([firstDylibPath]) }) + + await expect( + config.__test.notarizeMacServeSimCameraDylibsWithHost( + createMacNotarizationContext(appOutDir), + host + ) + ).rejects.toThrow('Missing serve-sim camera dylib for notarization') + + expect(host.execFileSync).not.toHaveBeenCalled() + }) + }) + + it('maps supported macOS notarization credential families to notarytool flags', async () => { + const cases = [ + { + credentials: ['--key', '/tmp/AuthKey.p8', '--key-id', 'KEYID', '--issuer', 'issuer'], + env: { + APPLE_API_ISSUER: 'issuer', + APPLE_API_KEY: '/tmp/AuthKey.p8', + APPLE_API_KEY_ID: 'KEYID', + ORCA_MAC_RELEASE: '1' + } + }, + { + credentials: [ + '--apple-id', + 'release@example.com', + '--password', + 'app-password', + '--team-id', + 'TEAMID' + ], + env: { + APPLE_API_ISSUER: 'issuer', + APPLE_API_KEY: '/tmp/AuthKey.p8', + APPLE_API_KEY_ID: 'KEYID', + ...appleIdMacReleaseEnv + } + }, + { + credentials: ['--keychain', '/tmp/login.keychain-db', '--keychain-profile', 'orca-release'], + env: { + APPLE_KEYCHAIN: '/tmp/login.keychain-db', + APPLE_KEYCHAIN_PROFILE: 'orca-release', + ORCA_MAC_RELEASE: '1' + } + } + ] + + for (const { credentials, env } of cases) { + await withElectronBuilderConfigEnv(env, async (config) => { + const appOutDir = join(tmpdir(), 'orca-electron-builder-test') + const dylibPaths = macServeSimDylibPaths(appOutDir) + const host = createMacNotarizationHost({ existingPaths: new Set(dylibPaths) }) + + await runMacAfterSignWithHost(config, appOutDir, host) + expectNotaryCredentials(host.calls, credentials) + }) + } + }) + + it('fails macOS standalone notarization on partial credentials and bad notary output', async () => { + for (const { env, message } of [ + { + env: { APPLE_ID: 'release@example.com', ORCA_MAC_RELEASE: '1' }, + message: 'APPLE_APP_SPECIFIC_PASSWORD env var needs to be set' + }, + { + env: { APPLE_API_KEY: '/tmp/AuthKey.p8', APPLE_API_KEY_ID: 'KEYID', ORCA_MAC_RELEASE: '1' }, + message: 'Env vars APPLE_API_KEY, APPLE_API_KEY_ID and APPLE_API_ISSUER need to be set' + } + ]) { + await withElectronBuilderConfigEnv(env, async (config) => { + expect(() => config.__test.macNotarytoolCredentialArgs()).toThrow(message) + }) + } + + for (const { output, message } of [ + { message: 'Standalone binary notarization failed', output: { status: 'Invalid' } }, + { message: '', output: '{not-json' } + ]) { + await withElectronBuilderConfigEnv(appleIdMacReleaseEnv, async (config) => { + const appOutDir = join(tmpdir(), 'orca-electron-builder-test') + const dylibPaths = macServeSimDylibPaths(appOutDir) + const host = createMacNotarizationHost({ existingPaths: new Set(dylibPaths), output }) + + const expectation = expect( + config.__test.notarizeMacServeSimCameraDylibsWithHost( + createMacNotarizationContext(appOutDir), + host + ) + ).rejects + await (message ? expectation.toThrow(message) : expectation.toThrow()) + }) + } + }) + + it('surfaces macOS standalone notarization subprocess failures without secrets', async () => { + await withElectronBuilderConfigEnv(appleIdMacReleaseEnv, async (config) => { + const appOutDir = join(tmpdir(), 'orca-electron-builder-test') + const dylibPaths = macServeSimDylibPaths(appOutDir) + const host = createMacNotarizationHost({ existingPaths: new Set(dylibPaths) }) + host.execFileSync.mockImplementation((command, args, options = {}) => { + host.calls.push({ args, command, options }) + if (command === 'xcrun') { + throw new Error(`Command failed: xcrun ${args.join(' ')}`) + } + return '' + }) + + try { + await config.__test.notarizeMacServeSimCameraDylibsWithHost( + createMacNotarizationContext(appOutDir), + host + ) + throw new Error('Expected notarization to fail') + } catch (error) { + expect(error.message).toContain('Standalone binary notarization subprocess failed') + expect(error.message).not.toMatch(/release@example\.com|app-password|TEAMID/) + expect(error.message).toContain('[REDACTED]') + } + expect(host.rmSync).toHaveBeenCalled() + }) + }) + it('unpacks the compiled CommonJS boundary with CLI runtime files', () => { expect(electronBuilderConfig.asarUnpack).toEqual( expect.arrayContaining(['out/package.json', 'out/cli/**', 'out/shared/**'])