import { createHash } from "node:crypto"; const hash = (bytes: Uint8Array) => createHash("sha256").update(bytes).digest("hex"); function requireGuard(value: unknown, code: string): asserts value { if (!value) throw new Error(code); } export function assertSameSnapshot( first: Record, second: Record, ) { const paths = Object.keys(first).sort(); requireGuard( JSON.stringify(paths) === JSON.stringify(Object.keys(second).sort()) && paths.every((path) => first[path] === second[path]), "SOURCE_SNAPSHOT_CHANGED", ); } export function createEvidencePreserver(ports: { writeExclusive: (path: string, bytes: Uint8Array) => Promise; read: (path: string) => Promise; }) { requireGuard( ports && typeof ports.writeExclusive === "function" && typeof ports.read === "function", "INVALID_EVIDENCE_PORTS", ); return async (path: string, bytes: Uint8Array) => { try { await ports.writeExclusive(path, bytes); } catch (error) { if ((error as { code?: string }).code !== "EEXIST") throw error; requireGuard( Buffer.from(await ports.read(path)).equals(Buffer.from(bytes)), "HISTORICAL_EVIDENCE_CONFLICT", ); } }; } export async function pinHandover(ports: { readReceipt: () => Promise; readSource: (path: string) => Promise; }) { requireGuard( ports && typeof ports.readReceipt === "function" && typeof ports.readSource === "function", "INVALID_HANDOVER_PORTS", ); const initial = Buffer.from(await ports.readReceipt()), receipt = JSON.parse(initial.toString()); requireGuard( receipt.runtimeTelemetry === false && receipt.runtimeSecurity === false, "UNEXPECTED_HANDOVER_SCOPE", ); const files = { ...receipt.files, ...receipt.preservedFreshness }; const paths = Object.keys(files); requireGuard( paths.length > 0 && paths.length === Object.keys(receipt.files).length + Object.keys(receipt.preservedFreshness).length, "INVALID_HANDOVER_MAP", ); for (const path of paths) requireGuard( !path.startsWith("/") && !path.split("/").includes("..") && !/[\x00\r\n]/.test(path) && /^[a-f0-9]{64}$/.test(files[path]), "INVALID_HANDOVER_PATH_OR_HASH", ); const verify = async () => { requireGuard( initial.equals(Buffer.from(await ports.readReceipt())), "HANDOVER_GENERATION_CHANGED", ); for (const path of paths) requireGuard( hash(await ports.readSource(path)) === files[path], "HANDOVER_SOURCE_CHANGED", ); requireGuard( initial.equals(Buffer.from(await ports.readReceipt())), "HANDOVER_GENERATION_CHANGED", ); }; await verify(); return { receipt, count: paths.length, bytes: Buffer.from(initial), sha256: hash(initial), verify, }; } export function assertAcceptedGeneration( accepted: any, source: Uint8Array, build: any, ) { try { const receipt = JSON.parse(Buffer.from(source).toString()), count = Object.keys({ ...receipt.files, ...receipt.preservedFreshness, }).length; requireGuard( accepted && build && accepted.source_handover_sha256 === hash(source) && build.source_handover_sha256 === hash(source) && accepted.backend_checked_at === receipt.checkedAt && build.backend_checked_at === receipt.checkedAt && accepted.source_files_verified === count && build.source_files_verified === count && accepted.binaries.app === build.binaries.app && accepted.binaries.worker === build.binaries.worker, "ACCEPTED_GENERATION_MISMATCH", ); } catch { throw new Error("ACCEPTED_GENERATION_MISMATCH"); } }