import { createHash } from "node:crypto"; export type SourceHashes = Record; export const privateTrackingPatterns = [".env", ".local/**", "*.key", "*.pem"]; export type GitTrackingSource = | { kind: "native-git" } | { kind: "canonical-proof"; path: string; sha256: string }; export const sourceGeneration = (files: SourceHashes) => createHash("sha256") .update( JSON.stringify( Object.keys(files) .sort() .map((path) => [path, files[path]]), ), ) .digest("hex"); export const proofHash = (bytes: Uint8Array) => createHash("sha256").update(bytes).digest("hex"); function validFiles(files: unknown): files is SourceHashes { return ( !!files && typeof files === "object" && !Array.isArray(files) && Object.keys(files).length > 0 && Object.keys(files).length <= 10000 && Object.entries(files).every( ([path, sha]) => path.length <= 1024 && !path.startsWith("/") && !path.split("/").includes("..") && !/[\r\n\0]/.test(path) && typeof sha === "string" && /^[a-f0-9]{64}$/.test(sha), ) ); } export function createGitProofProducer(ports: { query: (args: string[]) => Promise<{ code: number; stdout: string }>; head: () => Promise; snapshot: () => Promise; }) { if ( !ports || [ports.query, ports.head, ports.snapshot].some( (p) => typeof p !== "function", ) ) throw Error("GIT_PRODUCER_PORTS_REQUIRED"); return async (repository: string) => { const query = [ "git", "-C", repository, "ls-files", "-z", "--", ...privateTrackingPatterns, ], head = await ports.head(), result = await ports.query(query), files = await ports.snapshot(); if ( !repository.startsWith("/") || !/^[a-f0-9]{40}$/.test(head) || !validFiles(files) ) throw Error("GIT_CAPTURE_INVALID"); if (result.code !== 0 || result.stdout !== "") throw Error("CANONICAL_GIT_PRIVATE_TRACKING"); const second = await ports.query(query), afterHead = await ports.head(), after = await ports.snapshot(); if ( second.code !== 0 || second.stdout !== "" || afterHead !== head || !validFiles(after) || sourceGeneration(after) !== sourceGeneration(files) ) throw Error("GIT_CAPTURE_CHANGED"); return { kind: "CANONICAL_GIT_TRACKING_V1", canonical_repository: repository, query, head, result: { exit_code: result.code, stdout_base64: Buffer.from(result.stdout).toString("base64"), }, source_generation_sha256: sourceGeneration(files), files, }; }; } export function createGitProofReader(ports: { read: () => Promise; snapshot: () => Promise; }) { if ( !ports || [ports.read, ports.snapshot].some((p) => typeof p !== "function") ) throw Error("GIT_READER_PORTS_REQUIRED"); return async (expectedSHA: string) => { const bytes = await ports.read(); if (!/^[a-f0-9]{64}$/.test(expectedSHA) || proofHash(bytes) !== expectedSHA) throw Error("GIT_PROOF_SHA_MISMATCH"); if (bytes.length > 2 * 1024 * 1024) throw Error("GIT_PROOF_INVALID"); let proof: any; try { proof = JSON.parse(Buffer.from(bytes).toString()); } catch { throw Error("GIT_PROOF_INVALID"); } if ( proof?.kind !== "CANONICAL_GIT_TRACKING_V1" || typeof proof.canonical_repository !== "string" || !proof.canonical_repository.startsWith("/") || !/^[a-f0-9]{40}$/.test(proof.head) || proof.result?.exit_code !== 0 || proof.result?.stdout_base64 !== "" || !validFiles(proof.files) || JSON.stringify(proof.query) !== JSON.stringify([ "git", "-C", proof.canonical_repository, "ls-files", "-z", "--", ...privateTrackingPatterns, ]) || proof.source_generation_sha256 !== sourceGeneration(proof.files) ) throw Error("GIT_PROOF_INVALID"); const current = await ports.snapshot(); if ( !validFiles(current) || sourceGeneration(current) !== proof.source_generation_sha256 ) throw Error("GIT_PROOF_SOURCE_MISMATCH"); return { proof_sha256: proofHash(bytes), source_generation_sha256: proof.source_generation_sha256, tracked_private_files: 0, }; }; }