forked from mtusi2026/mtusi2026lab7
54 lines
1.5 KiB
TypeScript
54 lines
1.5 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
import { readFile, writeFile } from "node:fs/promises";
|
|
import { resolve } from "node:path";
|
|
import { root, run, requireThat, safeFailure } from "./runtime";
|
|
try {
|
|
const exceptions = new Set([
|
|
".gitignore",
|
|
".dockerignore",
|
|
"REPORT.md",
|
|
"security/THREAT_MODEL.md",
|
|
"contracts/trust-matrix.md",
|
|
]),
|
|
files: Record<string, string> = {};
|
|
const head = (await run(["git", "rev-parse", "HEAD"])).out.trim();
|
|
for (const path of (await run(["git", "ls-files", "-z"])).out
|
|
.split("\0")
|
|
.filter(Boolean)) {
|
|
if (exceptions.has(path)) continue;
|
|
const child = Bun.spawn(["git", "show", "HEAD:" + path], {
|
|
cwd: root,
|
|
stdout: "pipe",
|
|
stderr: "pipe",
|
|
});
|
|
const [bytes, stderr, exit] = await Promise.all([
|
|
new Response(child.stdout).arrayBuffer(),
|
|
new Response(child.stderr).text(),
|
|
child.exited,
|
|
]);
|
|
requireThat(exit === 0, "PUBLIC_BASELINE_UNREADABLE");
|
|
requireThat(
|
|
Buffer.from(bytes).equals(await readFile(resolve(root, path))),
|
|
"PUBLIC_BASELINE_DRIFT",
|
|
);
|
|
files[path] = createHash("sha256")
|
|
.update(new Uint8Array(bytes))
|
|
.digest("hex");
|
|
}
|
|
await writeFile(
|
|
resolve(root, "PUBLIC-BASELINE.json"),
|
|
JSON.stringify(
|
|
{
|
|
original_head: head,
|
|
permitted_student_templates: [...exceptions],
|
|
files,
|
|
},
|
|
null,
|
|
2,
|
|
) + "\n",
|
|
);
|
|
console.log("PASS_ORIGINAL_PUBLIC_BYTES_MATCH_HEAD");
|
|
} catch (error) {
|
|
safeFailure(error);
|
|
}
|