Jotley / website / scripts / verify-mac-release.mjs
| 1 | import { createHash, createPublicKey, verify } from 'node:crypto'; |
| 2 | import { readFile } from 'node:fs/promises'; |
| 3 | import { parseStringPromise } from 'xml2js'; |
| 4 | |
| 5 | const base = new URL('../public/', import.meta.url); |
| 6 | const sparkle = 'http://www.andymatuschak.org/xml-namespaces/sparkle'; |
| 7 | const origin = 'https://jotley.ai'; |
| 8 | const 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 | |
| 17 | function requireValid(condition, message) { |
| 18 | if (!condition) throw new Error(`Invalid Mac release: ${message}`); |
| 19 | } |
| 20 | |
| 21 | const [manifestText, appcast] = await Promise.all([ |
| 22 | readFile(new URL('mac-release.json', base), 'utf8'), |
| 23 | readFile(new URL('appcast.xml', base), 'utf8'), |
| 24 | ]); |
| 25 | const manifest = JSON.parse(manifestText); |
| 26 | requireValid(/^\d+(\.\d+){1,2}$/.test(manifest.version), 'invalid version'); |
| 27 | requireValid(Number.isSafeInteger(manifest.build) && manifest.build > 0, 'invalid build'); |
| 28 | requireValid(manifest.file === `Jotley-${manifest.version}-${manifest.build}.dmg`, 'unexpected filename'); |
| 29 | requireValid(Number.isSafeInteger(manifest.bytes) && manifest.bytes > 0, 'invalid installer size'); |
| 30 | requireValid(typeof manifest.sha256 === 'string' && /^[a-f0-9]{64}$/.test(manifest.sha256), 'invalid SHA-256'); |
| 31 | |
| 32 | const parsed = await parseStringPromise(appcast, { explicitArray: false, xmlns: true }); |
| 33 | const entries = parsed.rss?.channel?.item; |
| 34 | const items = entries ? [entries].flat() : []; |
| 35 | function sparkleValue(item, name) { |
| 36 | return Object.values(item).find(value => value?.$ns?.uri === sparkle && value.$ns.local === name)?._; |
| 37 | } |
| 38 | |
| 39 | const builds = items.map(item => Number(sparkleValue(item, 'version'))); |
| 40 | requireValid( |
| 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 | ); |
| 45 | const latest = items[0]; |
| 46 | requireValid(sparkleValue(latest, 'shortVersionString') === manifest.version, 'version differs from download'); |
| 47 | const enclosure = latest.enclosure?.$; |
| 48 | requireValid(enclosure?.url?.value === `${origin}/${manifest.file}`, 'installer URL differs from download'); |
| 49 | requireValid(enclosure?.length?.value === String(manifest.bytes), 'installer size differs from download'); |
| 50 | const signature = Object.values(enclosure).find(attribute => |
| 51 | attribute.uri === sparkle && attribute.local === 'edSignature', |
| 52 | )?.value; |
| 53 | requireValid(typeof signature === 'string' && /^[A-Za-z0-9+/]{86}==$/.test(signature), 'missing or malformed Sparkle signature'); |
| 54 | |
| 55 | const [option, path, ...extra] = process.argv.slice(2); |
| 56 | requireValid(extra.length === 0 && (!option || option === '--verify-installer'), 'unknown options'); |
| 57 | async 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 | |
| 64 | if (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 | } |
| 70 | console.log(`Verified Mac release ${manifest.version} (${manifest.build})${option ? ' and signed installer' : ' metadata'}`); |