Files
ethernet 92e0717a3a perf(desktop): reuse Developer ID signatures for loose macOS payload binaries
osx-sign spends 2.1-3.3 min per mac leg running one codesign call for each of
610-836 payload binaries (run 39214394169), and only 321 of 837 arm64 names
are distinct: most Python extensions sit three times in the payload (venv,
uv wheels, uv archive). None of it is cached, unlike Windows' Authenticode
signatures (payload-sign-cache.mjs).

createMacSigner now signs loose payload Mach-O itself before osx-sign runs,
through a cache modelled on the Windows one:
- key: the unsigned bytes and file name (codesign derives the identifier
  from the name), under a policy hash of the identity, team, flags and
  inherited entitlements; the per-job keychain path is not part of it;
- codesign's embedded signature carries a secure timestamp, so the signed
  bytes are stored, and every restored file must pass codesign --verify
  --strict with this team's Developer ID, a timestamp and the hardened
  runtime before it replaces the unsigned one;
- each distinct input is signed once (bounded concurrency) and copied to
  its duplicates; files inside nested bundles and the app executable are
  left to osx-sign, which still seals every bundle and the app.

The darwin legs restore the cache before building and save it only after
the notarized-app verification passes, on release runs. The mac runners are
GitHub-hosted, so it lands in the repo's GitHub cache store; commit builds
hold a read-only cache token.

Inject the typed app-signing function into the cache integration tests instead of
mocking its module. Exercise cold and warm reuse and refuse outer sealing after
cache verification fails; preserve the production lazy import and native skips.
2026-10-20 15:50:30 -04:00

54 lines
2.8 KiB
JavaScript

import path from 'node:path'
import { createMacSignCache, identityTeam, looseMachOFiles } from './mac-sign-cache.mjs'
import { rehashPayloadDigests } from './payload-digests.mjs'
/**
* @param {Pick<import('app-builder-lib').ElectronSignOptions, 'entitlements' | 'entitlementsInherit' | 'hardenedRuntime'> & { ignore: (file: string) => boolean }} policy
* @param {{ env?: NodeJS.ProcessEnv, makeCache?: typeof createMacSignCache, team?: typeof identityTeam, sign?: typeof import('@electron/osx-sign').sign }} [deps]
* @returns {(opts: import('@electron/osx-sign').SignOptions, packager: { helper: Pick<import('app-builder-lib').MacPackager['helper'], 'getOptionsForFile'> }) => Promise<void>}
*/
export function createMacSigner(policy, { env = process.env, makeCache = createMacSignCache, team = identityTeam, sign: signApp } = {}) {
return async (opts, packager) => {
const target = opts.platform === 'mas' ? (opts.type === 'development' ? 'mas-dev' : 'mas') : 'mac'
const optionsForFile = await packager.helper.getOptionsForFile(opts.app, target, policy)
const inheritedIgnore = opts.ignore
/** @param {string} file */
const ignored = file => (typeof inheritedIgnore === 'function' && inheritedIgnore(file)) || policy.ignore(file)
// Release builds (HERMES_MAC_SIGNATURE_CACHE) sign loose payload binaries through
// the signature cache with the arguments osx-sign would use (inherited
// entitlements, hardened runtime, secure timestamp); osx-sign then skips them
// and still seals every bundle and the app.
const identity = opts.identity
const enabled = target === 'mac' && env.HERMES_MAC_SIGNATURE_CACHE && identity &&
policy.entitlementsInherit && policy.hardenedRuntime
const cache = enabled && policy.entitlementsInherit ? makeCache({
root: env.HERMES_MAC_SIGNATURE_CACHE, teamId: await team(identity, opts.keychain), identity,
keychain: opts.keychain, entitlements: policy.entitlementsInherit
}) : null
/** @type {Set<string>} */
const presigned = new Set()
if (cache) {
const files = looseMachOFiles(opts.app, ignored)
const plan = await cache.prepare(files)
await cache.sign(plan)
for (const file of files) presigned.add(file)
console.log(`mac signature cache: ${files.length} loose binaries, ${plan.restored} restored, ` +
`${plan.pending.length} signed`)
}
const sign = signApp ?? (await import('@electron/osx-sign')).sign
await sign({
...opts,
ignore: file => presigned.has(file) || ignored(file),
// Batching defers child signatures until after all option callbacks.
batchCodesignCalls: false,
optionsForFile: file => {
if (file === opts.app) {
rehashPayloadDigests(path.join(opts.app, 'Contents', 'Resources', 'agent-payload'))
}
return optionsForFile(file)
}
})
}
}