Branchwell

Jotley / website / scripts / verify-mac-release.mjs

1import { createHash, createPublicKey, verify } from 'node:crypto';
2import { readFile } from 'node:fs/promises';
3import { parseStringPromise } from 'xml2js';
4
5const base = new URL('../public/', import.meta.url);
6const sparkle = 'http://www.andymatuschak.org/xml-namespaces/sparkle';
7const origin = 'https://jotley.ai';
8const publicKey = createPublicKey({
9 key: Buffer.concat([
10 Buffer.from('302a300506032b6570032100', 'hex'),
11 Buffer.from('oVkqL3P3OGrwFhzgcWJQcF2dQ2Dn2rBvlB3bNOXGZSc=', 'base64'),
12 ]),
13 type: 'spki',
14 format: 'der',
15});
16
17function requireValid(condition, message) {
18 if (!condition) throw new Error(`Invalid Mac release: ${message}`);
19}
20
21const [manifestText, appcast] = await Promise.all([
22 readFile(new URL('mac-release.json', base), 'utf8'),
23 readFile(new URL('appcast.xml', base), 'utf8'),
24]);
25const manifest = JSON.parse(manifestText);
26requireValid(/^\d+(\.\d+){1,2}$/.test(manifest.version), 'invalid version');
27requireValid(Number.isSafeInteger(manifest.build) && manifest.build > 0, 'invalid build');
28requireValid(manifest.file === `Jotley-${manifest.version}-${manifest.build}.dmg`, 'unexpected filename');
29requireValid(Number.isSafeInteger(manifest.bytes) && manifest.bytes > 0, 'invalid installer size');
30requireValid(typeof manifest.sha256 === 'string' && /^[a-f0-9]{64}$/.test(manifest.sha256), 'invalid SHA-256');
31
32const parsed = await parseStringPromise(appcast, { explicitArray: false, xmlns: true });
33const entries = parsed.rss?.channel?.item;
34const items = entries ? [entries].flat() : [];
35function sparkleValue(item, name) {
36 return Object.values(item).find(value => value?.$ns?.uri === sparkle && value.$ns.local === name)?._;
37}
38
39const builds = items.map(item => Number(sparkleValue(item, 'version')));
40requireValid(
41 builds[0] === manifest.build && builds.every((build, index) =>
42 Number.isSafeInteger(build) && build > 0 && (index === 0 || build < builds[index - 1])),
43 'Sparkle builds must descend from the website download build',
44);
45const latest = items[0];
46requireValid(sparkleValue(latest, 'shortVersionString') === manifest.version, 'version differs from download');
47const enclosure = latest.enclosure?.$;
48requireValid(enclosure?.url?.value === `${origin}/${manifest.file}`, 'installer URL differs from download');
49requireValid(enclosure?.length?.value === String(manifest.bytes), 'installer size differs from download');
50const signature = Object.values(enclosure).find(attribute =>
51 attribute.uri === sparkle && attribute.local === 'edSignature',
52)?.value;
53requireValid(typeof signature === 'string' && /^[A-Za-z0-9+/]{86}==$/.test(signature), 'missing or malformed Sparkle signature');
54
55const [option, path, ...extra] = process.argv.slice(2);
56requireValid(extra.length === 0 && (!option || option === '--verify-installer'), 'unknown options');
57async function readInstaller() {
58 if (path) return readFile(path);
59 const response = await fetch(enclosure.url.value, { signal: AbortSignal.timeout(60_000) });
60 requireValid(response.ok, `installer download failed (HTTP ${response.status})`);
61 return Buffer.from(await response.arrayBuffer());
62}
63
64if (option === '--verify-installer') {
65 const bytes = await readInstaller();
66 requireValid(bytes.length === manifest.bytes, 'installer size differs from manifest');
67 requireValid(createHash('sha256').update(bytes).digest('hex') === manifest.sha256, 'installer SHA-256 differs from manifest');
68 requireValid(verify(null, bytes, publicKey, Buffer.from(signature, 'base64')), 'installer signature does not match the shipped Sparkle key');
69}
70console.log(`Verified Mac release ${manifest.version} (${manifest.build})${option ? ' and signed installer' : ' metadata'}`);