fix(desktop): explain why Apple notarization produced no result (#2723)

notarize.mjs kept only stdout from notarytool submit and reduced every
outcome to one 'no notarization result' error, so the v1.7.3 macOS jobs
failed with an empty {} artifact and nothing in the log (#2714).

notarytool reports most submission failures on stderr, which the script
discarded. Classify the four cases instead - the process never started,
it exited with no output, it printed no result, or it printed something
that is not JSON - and carry the reason into both the thrown message and
a notarization-submission.error.json artifact that records the exit code,
the spawn error and bounded stdout/stderr tails.

The account and password are redacted from everything written out, and
notarization-submission.json keeps its previous shape (raw stdout or {}),
so the upload step and any existing consumer are unaffected.
This commit is contained in:
Leveling
2026-12-01 17:41:37 +08:00
committed by GitHub
parent c20e42fd99
commit efea7b5c8d
2 changed files with 181 additions and 26 deletions
+37 -3
View File
@@ -10,12 +10,46 @@ const dmg = path.join(out, images[0]);
const auth = ['--apple-id', process.env.APPLE_ID, '--team-id', process.env.APPLE_TEAM_ID,
'--password', process.env.APPLE_APP_PASSWORD];
if (auth.some(value => !value)) throw new Error('Missing Apple notarization credentials');
// notarytool reports most submission failures on stderr, and can also exit zero
// without printing anything. Reading only stdout turned all of those into the
// same "no notarization result" error with an empty artifact (#2714).
const secrets = [process.env.APPLE_ID, process.env.APPLE_APP_PASSWORD].filter(Boolean);
const redact = text => secrets.reduce((acc, secret) => acc.split(secret).join('[redacted]'), String(text ?? ''));
// Capture output instead of throwing execFile errors containing secret arguments.
const submit = spawnSync('xcrun', ['notarytool', 'submit', dmg, ...auth, '--wait', '--output-format', 'json'], { encoding: 'utf8' });
await writeFile(path.join(out, 'notarization-submission.json'), submit.stdout || '{}');
const submitOut = submit.stdout ?? '';
const submitErr = submit.stderr ?? '';
// The submission artifact keeps its original shape: the raw stdout, or ``{}``.
await writeFile(path.join(out, 'notarization-submission.json'), submitOut || '{}');
// Classify why the submission produced no usable result before parsing, so each
// failure names its own cause instead of collapsing into one message.
let result;
try { result = JSON.parse(submit.stdout); }
catch { throw new Error('Apple returned no notarization result; check the submission artifact'); }
let problem;
if (submit.error) {
problem = `could not start xcrun (${submit.error.code ?? submit.error.message})`;
} else if (!submitOut.trim()) {
const stderrNote = submitErr.trim()
? ` and reported on stderr: ${redact(submitErr.trim()).split('\n').slice(-3).join(' | ')}`
: '';
problem = submit.status === 0
? `notarytool printed no result${stderrNote}`
: `notarytool exited with status ${submit.status} and printed no result${stderrNote}`;
} else {
try { result = JSON.parse(submitOut); }
catch { problem = 'notarytool printed a result that is not JSON'; }
}
if (problem) {
// Bounded tails keep the artifact small while still carrying the reason.
await writeFile(path.join(out, 'notarization-submission.error.json'), JSON.stringify({
stage: 'submit', problem, exitCode: submit.status, signal: submit.signal,
spawnError: submit.error?.code ?? null,
stdout: redact(submitOut).slice(-4000), stderr: redact(submitErr).slice(-8000),
}, null, 2));
throw new Error(`Apple notarization submission failed: ${problem}; see notarization-submission.error.json`);
}
if (result.id) {
console.log(`Apple notarization ${result.id}: ${result.status}`);
const log = spawnSync('xcrun', ['notarytool', 'log', result.id, ...auth,
+144 -23
View File
@@ -5,35 +5,156 @@ import os from 'node:os';
import path from 'node:path';
import test from 'node:test';
const FAKE_XCRUN = `#!${process.execPath}
const fs = require('node:fs');
const args = process.argv.slice(2);
fs.appendFileSync(process.env.CALLS, args.slice(0, 2).join(' ') + '\\n');
if (args[1] === 'submit') {
// RESULT selects the fake notarytool behaviour; MODE narrows it further.
const mode = process.env.MODE || 'normal';
if (mode === 'stderr-failure') {
console.error(process.env.STDERR_TEXT || 'fake notarytool diagnostic');
process.exit(Number(process.env.EXIT_CODE || '1'));
}
if (mode === 'empty-zero') process.exit(0);
if (mode === 'not-json') { console.log('not json at all'); process.exit(0); }
console.log(JSON.stringify({id: 'fixture-id', status: process.env.RESULT}));
}
if (args[1] === 'log') fs.writeFileSync(args.at(-1), JSON.stringify({status: process.env.RESULT}));
`;
/**
* Run the real notarize.mjs against a fake xcrun.
*
* `extraEnv` overrides the defaults, and `omitXcrun` removes the tool from PATH
* entirely so the spawn itself fails the way a missing Command Line Tools
* install would.
*/
async function runNotarize({ result = 'Accepted', mode = 'normal', extraEnv = {}, omitXcrun = false } = {}) {
const root = await mkdtemp(path.join(os.tmpdir(), 'notarization-'));
await mkdir(path.join(root, 'scripts/lib'), { recursive: true });
await mkdir(path.join(root, 'out'));
await cp(new URL('../scripts/notarize.mjs', import.meta.url), path.join(root, 'scripts/notarize.mjs'));
await writeFile(path.join(root, 'scripts/lib/target.mjs'), `export const appRoot = ${JSON.stringify(root)};\n`);
await writeFile(path.join(root, 'out/test.dmg'), 'fixture');
if (!omitXcrun) await writeFile(path.join(root, 'xcrun'), FAKE_XCRUN, { mode: 0o755 });
const calls = path.join(root, 'calls');
const env = {
...process.env,
// An empty bin dir stands in for "xcrun is not installed".
PATH: omitXcrun ? path.join(root, 'empty-bin') : root + path.delimiter + process.env.PATH,
CALLS: calls,
RESULT: result,
MODE: mode,
APPLE_ID: 'fixture@example.test',
APPLE_TEAM_ID: 'fixture-team',
APPLE_APP_PASSWORD: 'secret-fixture',
...extraEnv,
};
if (omitXcrun) await mkdir(env.PATH, { recursive: true });
const run = spawnSync(process.execPath, [path.join(root, 'scripts', 'notarize.mjs')], { encoding: 'utf8', env });
const read = async name => {
try { return await readFile(path.join(root, 'out', name), 'utf8'); } catch { return null; }
};
const callList = async () => {
try { return await readFile(calls, 'utf8'); } catch { return ''; }
};
return {
root, run, read, callList,
cleanup: () => rm(root, { recursive: true, force: true }),
};
}
// Exercise the actual CLI with a fake Apple tool: Invalid still exits zero.
// Rejected submissions must keep a report and must never reach stapler.
for (const status of ['Invalid', 'Accepted']) {
test(`notarization handles Apple's ${status} result`, { skip: process.platform === 'win32' }, async () => {
const root = await mkdtemp(path.join(os.tmpdir(), 'notarization-'));
const h = await runNotarize({ result: status });
try {
await mkdir(path.join(root, 'scripts/lib'), { recursive: true });
await mkdir(path.join(root, 'out'));
await cp(new URL('../scripts/notarize.mjs', import.meta.url), path.join(root, 'scripts/notarize.mjs'));
await writeFile(path.join(root, 'scripts/lib/target.mjs'), `export const appRoot = ${JSON.stringify(root)};\n`);
await writeFile(path.join(root, 'out/test.dmg'), 'fixture');
await writeFile(path.join(root, 'xcrun'), `#!${process.execPath}
const fs = require('node:fs');
const args = process.argv.slice(2);
fs.appendFileSync(process.env.CALLS, args.slice(0, 2).join(' ') + '\\n');
if (args[1] === 'submit') console.log(JSON.stringify({id: 'fixture-id', status: process.env.RESULT}));
if (args[1] === 'log') fs.writeFileSync(args.at(-1), JSON.stringify({status: process.env.RESULT}));
`, { mode: 0o755 });
const result = spawnSync(process.execPath, [path.join(root, 'scripts/notarize.mjs')], {
encoding: 'utf8', env: { ...process.env, PATH: root + path.delimiter + process.env.PATH,
CALLS: path.join(root, 'calls'), RESULT: status, APPLE_ID: 'fixture',
APPLE_TEAM_ID: 'fixture', APPLE_APP_PASSWORD: 'secret-fixture' },
});
assert.equal(result.status, status === 'Accepted' ? 0 : 1);
const calls = await readFile(path.join(root, 'calls'), 'utf8');
assert.equal(h.run.status, status === 'Accepted' ? 0 : 1);
const calls = await h.callList();
assert.equal(calls.includes('stapler staple'), status === 'Accepted');
assert.equal(calls.includes('stapler validate'), status === 'Accepted');
assert.equal(JSON.parse(await readFile(path.join(root, 'out/notarization-log.json'))).status, status);
assert.equal((result.stdout + result.stderr).includes('secret-fixture'), false);
} finally { await rm(root, { recursive: true, force: true }); }
assert.equal(JSON.parse(await h.read('notarization-log.json')).status, status);
assert.equal((h.run.stdout + h.run.stderr).includes('secret-fixture'), false);
// A handshake that worked must not leave an error artifact behind.
assert.equal(await h.read('notarization-submission.error.json'), null);
} finally { await h.cleanup(); }
});
}
// #2714: notarytool writes most submission failures to stderr, which the script
// used to discard, leaving an empty {} artifact and an unexplained error.
test('a stderr-only submission failure is recorded and explained', { skip: process.platform === 'win32' }, async () => {
const h = await runNotarize({
mode: 'stderr-failure',
extraEnv: { STDERR_TEXT: 'Error: HTTP status code: 401. Invalid credentials.', EXIT_CODE: '1' },
});
try {
assert.equal(h.run.status, 1);
assert.match(h.run.stderr, /Apple notarization submission failed/);
// The reason must reach the log, not only the artifact.
assert.match(h.run.stderr, /notarytool exited with status 1/);
assert.match(h.run.stderr, /401\. Invalid credentials\./);
const error = JSON.parse(await h.read('notarization-submission.error.json'));
assert.equal(error.stage, 'submit');
assert.equal(error.exitCode, 1);
assert.match(error.stderr, /401/);
// The pre-existing artifact keeps its shape: stdout was empty, so {}.
assert.equal(await h.read('notarization-submission.json'), '{}');
assert.equal((await h.callList()).includes('stapler'), false);
} finally { await h.cleanup(); }
});
test('a zero exit with no output is distinguished from a non-zero exit', { skip: process.platform === 'win32' }, async () => {
const h = await runNotarize({ mode: 'empty-zero' });
try {
assert.equal(h.run.status, 1);
assert.match(h.run.stderr, /printed no result/);
assert.doesNotMatch(h.run.stderr, /exited with status/);
assert.equal(JSON.parse(await h.read('notarization-submission.error.json')).exitCode, 0);
} finally { await h.cleanup(); }
});
test('unparsable output is reported as invalid JSON', { skip: process.platform === 'win32' }, async () => {
const h = await runNotarize({ mode: 'not-json' });
try {
assert.equal(h.run.status, 1);
assert.match(h.run.stderr, /not JSON/);
const error = JSON.parse(await h.read('notarization-submission.error.json'));
assert.match(error.stdout, /not json at all/);
} finally { await h.cleanup(); }
});
test('a missing xcrun is reported as a spawn failure, not a submission failure', { skip: process.platform === 'win32' }, async () => {
const h = await runNotarize({ omitXcrun: true });
try {
assert.equal(h.run.status, 1);
assert.match(h.run.stderr, /could not start xcrun/);
const error = JSON.parse(await h.read('notarization-submission.error.json'));
assert.match(String(error.spawnError), /ENOENT/);
} finally { await h.cleanup(); }
});
test('the diagnostic artifacts never contain the Apple credentials', { skip: process.platform === 'win32' }, async () => {
// Force the account and password into notarytool's own output to prove the
// redaction, not just the absence of an echo.
const h = await runNotarize({
mode: 'stderr-failure',
extraEnv: {
STDERR_TEXT: 'Error: 401 for fixture@example.test with secret-fixture',
EXIT_CODE: '1',
},
});
try {
const error = await h.read('notarization-submission.error.json');
assert.match(error, /\[redacted\]/);
assert.equal(error.includes('secret-fixture'), false);
assert.equal(error.includes('fixture@example.test'), false);
assert.equal((h.run.stdout + h.run.stderr).includes('secret-fixture'), false);
} finally { await h.cleanup(); }
});