| 1 | #!/usr/bin/env node |
| 2 | // Verify, notarize and staple an already-signed app or disk image. |
| 3 | import { spawnSync } from "node:child_process"; |
| 4 | import { mkdirSync, rmSync, writeFileSync } from "node:fs"; |
| 5 | import { join, resolve } from "node:path"; |
| 6 | import { pathToFileURL } from "node:url"; |
| 7 | |
| 8 | export function notarizeDesktop({ archive, target, kind, diagnosticsDir, env = process.env, |
| 9 | run = (command, args) => spawnSync(command, args, { |
| 10 | encoding: "utf8", stdio: ["ignore", "pipe", "inherit"], maxBuffer: 16 * 1024 * 1024, |
| 11 | }), warn = console.warn }) { |
| 12 | if (!["app", "dmg"].includes(kind)) throw new Error("Expected notarization kind app or dmg"); |
| 13 | for (const name of ["APPLE_API_KEY_PATH", "APPLE_API_KEY_ID", "APPLE_API_ISSUER_ID"]) { |
| 14 | if (!env[name]) throw new Error(`Missing ${name}`); |
| 15 | } |
| 16 | mkdirSync(diagnosticsDir, { recursive: true }); |
| 17 | for (const suffix of ["submission", "notary-log"]) { |
| 18 | rmSync(join(diagnosticsDir, `${kind}-${suffix}.json`), { force: true }); |
| 19 | } |
| 20 | const auth = ["--key", env.APPLE_API_KEY_PATH, "--key-id", env.APPLE_API_KEY_ID, |
| 21 | "--issuer", env.APPLE_API_ISSUER_ID]; |
| 22 | const checked = (command, args) => { |
| 23 | const result = run(command, args); |
| 24 | if (result.stdout) process.stdout.write(result.stdout); |
| 25 | if (result.status !== 0 || result.error) throw new Error(`${command} ${args[0]} failed`); |
| 26 | return result; |
| 27 | }; |
| 28 | checked("codesign", ["--verify", ...(kind === "app" ? ["--deep"] : []), "--strict", "--verbose=4", target]); |
| 29 | console.log(`==> notarytool submit (${kind})`); |
| 30 | const submission = run("xcrun", ["notarytool", "submit", archive, ...auth, "--wait", "--output-format", "json"]); |
| 31 | let response; |
| 32 | try { response = JSON.parse(submission.stdout); } catch { /* Fail closed below. */ } |
| 33 | const id = typeof response?.id === "string" && /^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i.test(response.id) |
| 34 | ? response.id : null; |
| 35 | const status = typeof response?.status === "string" ? response.status : null; |
| 36 | // Do not archive command arguments, credentials or machine-local upload paths. |
| 37 | writeFileSync(join(diagnosticsDir, `${kind}-submission.json`), JSON.stringify({ |
| 38 | id, status, exitCode: submission.status, signal: submission.signal ?? null, |
| 39 | }, null, 2) + "\n"); |
| 40 | console.log(`==> notarization (${kind}): ${status ?? "unknown"}; submission: ${id ?? "unavailable"}`); |
| 41 | if (id) { |
| 42 | const log = run("xcrun", ["notarytool", "log", id, ...auth]); |
| 43 | if (log.status === 0 && !log.error) { |
| 44 | try { |
| 45 | const report = JSON.parse(log.stdout); |
| 46 | writeFileSync(join(diagnosticsDir, `${kind}-notary-log.json`), JSON.stringify(report, null, 2) + "\n"); |
| 47 | } catch { |
| 48 | warn(`Could not decode notarization log for ${id}; retrieve it with notarytool log.`); |
| 49 | } |
| 50 | } else { |
| 51 | warn(`Could not fetch notarization log for ${id}; retrieve it with notarytool log.`); |
| 52 | } |
| 53 | } |
| 54 | // notarytool can exit successfully after Apple rejects the submission. |
| 55 | if (submission.status !== 0 || submission.error || !id || status !== "Accepted") { |
| 56 | throw new Error(`Notarization ${status ?? "unknown"} (${id ?? "no submission ID"}); see ${kind}-submission.json and the notary log`); |
| 57 | } |
| 58 | checked("xcrun", ["stapler", "staple", target]); |
| 59 | checked("xcrun", ["stapler", "validate", target]); |
| 60 | // Gatekeeper requires notarization; it is not a pre-submission signature test. |
| 61 | checked("spctl", ["--assess", "--verbose=4", "--type", kind === "app" ? "exec" : "open", |
| 62 | ...(kind === "dmg" ? ["--context", "context:primary-signature"] : []), target]); |
| 63 | } |
| 64 | |
| 65 | if (process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href) { |
| 66 | const [archive, target, kind, diagnosticsDir] = process.argv.slice(2); |
| 67 | if (!archive || !target || !kind || !diagnosticsDir) { |
| 68 | console.error("usage: notarize-desktop.mjs <archive> <target> <app|dmg> <diagnostics-directory>"); |
| 69 | process.exitCode = 2; |
| 70 | } else { |
| 71 | try { notarizeDesktop({ archive, target, kind, diagnosticsDir }); } |
| 72 | catch (error) { console.error(error.message); process.exitCode = 1; } |
| 73 | } |
| 74 | } |
| 75 |