import { createHash, createPublicKey, verify } from 'node:crypto'; import { readFile } from 'node:fs/promises'; import { parseStringPromise } from 'xml2js'; const base = new URL('../public/', import.meta.url); const sparkle = 'http://www.andymatuschak.org/xml-namespaces/sparkle'; const origin = 'https://jotley.ai'; const publicKey = createPublicKey({ key: Buffer.concat([ Buffer.from('302a300506032b6570032100', 'hex'), Buffer.from('oVkqL3P3OGrwFhzgcWJQcF2dQ2Dn2rBvlB3bNOXGZSc=', 'base64'), ]), type: 'spki', format: 'der', }); function requireValid(condition, message) { if (!condition) throw new Error(`Invalid Mac release: ${message}`); } const [manifestText, appcast] = await Promise.all([ readFile(new URL('mac-release.json', base), 'utf8'), readFile(new URL('appcast.xml', base), 'utf8'), ]); const manifest = JSON.parse(manifestText); requireValid(/^\d+(\.\d+){1,2}$/.test(manifest.version), 'invalid version'); requireValid(Number.isSafeInteger(manifest.build) && manifest.build > 0, 'invalid build'); requireValid(manifest.file === `Jotley-${manifest.version}-${manifest.build}.dmg`, 'unexpected filename'); requireValid(Number.isSafeInteger(manifest.bytes) && manifest.bytes > 0, 'invalid installer size'); requireValid(typeof manifest.sha256 === 'string' && /^[a-f0-9]{64}$/.test(manifest.sha256), 'invalid SHA-256'); const parsed = await parseStringPromise(appcast, { explicitArray: false, xmlns: true }); const entries = parsed.rss?.channel?.item; const items = entries ? [entries].flat() : []; function sparkleValue(item, name) { return Object.values(item).find(value => value?.$ns?.uri === sparkle && value.$ns.local === name)?._; } const builds = items.map(item => Number(sparkleValue(item, 'version'))); requireValid( builds[0] === manifest.build && builds.every((build, index) => Number.isSafeInteger(build) && build > 0 && (index === 0 || build < builds[index - 1])), 'Sparkle builds must descend from the website download build', ); const latest = items[0]; requireValid(sparkleValue(latest, 'shortVersionString') === manifest.version, 'version differs from download'); const enclosure = latest.enclosure?.$; requireValid(enclosure?.url?.value === `${origin}/${manifest.file}`, 'installer URL differs from download'); requireValid(enclosure?.length?.value === String(manifest.bytes), 'installer size differs from download'); const signature = Object.values(enclosure).find(attribute => attribute.uri === sparkle && attribute.local === 'edSignature', )?.value; requireValid(typeof signature === 'string' && /^[A-Za-z0-9+/]{86}==$/.test(signature), 'missing or malformed Sparkle signature'); const [option, path, ...extra] = process.argv.slice(2); requireValid(extra.length === 0 && (!option || option === '--verify-installer'), 'unknown options'); async function readInstaller() { if (path) return readFile(path); const response = await fetch(enclosure.url.value, { signal: AbortSignal.timeout(60_000) }); requireValid(response.ok, `installer download failed (HTTP ${response.status})`); return Buffer.from(await response.arrayBuffer()); } if (option === '--verify-installer') { const bytes = await readInstaller(); requireValid(bytes.length === manifest.bytes, 'installer size differs from manifest'); requireValid(createHash('sha256').update(bytes).digest('hex') === manifest.sha256, 'installer SHA-256 differs from manifest'); requireValid(verify(null, bytes, publicKey, Buffer.from(signature, 'base64')), 'installer signature does not match the shipped Sparkle key'); } console.log(`Verified Mac release ${manifest.version} (${manifest.build})${option ? ' and signed installer' : ' metadata'}`);