forked from mtusi2026/mtusi2026lab7
134 lines
3.6 KiB
TypeScript
134 lines
3.6 KiB
TypeScript
import { open, readFile, readdir, stat } from "node:fs/promises";
|
|
import { constants } from "node:fs";
|
|
import { resolve, relative } from "node:path";
|
|
import { createHash } from "node:crypto";
|
|
import { root, local, privateWrite, requireThat } from "./runtime";
|
|
import { createGitProofReader, type SourceHashes } from "./git-tracking-proof";
|
|
export const gitProofSourceDirectories = [
|
|
"backend",
|
|
"scripts",
|
|
"student-tests",
|
|
"config",
|
|
"infra",
|
|
"contracts",
|
|
"tests",
|
|
"examples",
|
|
".github",
|
|
];
|
|
export const gitProofRootFiles = [
|
|
"Dockerfile",
|
|
"Dockerfile.example",
|
|
"compose.yaml",
|
|
"compose.example.yaml",
|
|
"package.json",
|
|
"bun.lock",
|
|
"tsconfig.json",
|
|
"Makefile",
|
|
"PUBLIC-BASELINE.json",
|
|
"BACKEND-READY.json",
|
|
".dockerignore",
|
|
".gitignore",
|
|
".env.example",
|
|
"lab.json",
|
|
"requirements-dev.txt",
|
|
"OPENBAO-IMAGE.json",
|
|
];
|
|
export async function gitProofSourceSnapshot(
|
|
directory: string,
|
|
): Promise<SourceHashes> {
|
|
const result: SourceHashes = {},
|
|
hash = (bytes: Uint8Array) =>
|
|
createHash("sha256").update(bytes).digest("hex");
|
|
async function add(path: string) {
|
|
const info = await stat(path);
|
|
requireThat(
|
|
info.isFile() && info.size <= 20 * 1024 * 1024,
|
|
"GIT_SOURCE_FILE_INVALID",
|
|
);
|
|
result[relative(directory, path)] = hash(await readFile(path));
|
|
}
|
|
async function walk(path: string) {
|
|
for (const entry of await readdir(path, { withFileTypes: true })) {
|
|
if (
|
|
[
|
|
"node_modules",
|
|
"__pycache__",
|
|
".pytest_cache",
|
|
".mypy_cache",
|
|
".ruff_cache",
|
|
].includes(entry.name) ||
|
|
/\.(pyc|pyo)$/.test(entry.name)
|
|
)
|
|
continue;
|
|
const p = resolve(path, entry.name);
|
|
if (entry.isDirectory()) await walk(p);
|
|
else {
|
|
requireThat(entry.isFile(), "GIT_SOURCE_NONREGULAR");
|
|
await add(p);
|
|
}
|
|
}
|
|
}
|
|
for (const name of gitProofSourceDirectories)
|
|
await walk(resolve(directory, name));
|
|
for (const name of gitProofRootFiles) await add(resolve(directory, name));
|
|
return Object.fromEntries(
|
|
Object.keys(result)
|
|
.sort()
|
|
.map((p) => [p, result[p]]),
|
|
);
|
|
}
|
|
async function boundedProof(path: string) {
|
|
const handle = await open(
|
|
path,
|
|
constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK,
|
|
);
|
|
let primary = false;
|
|
try {
|
|
requireThat((await handle.stat()).isFile(), "GIT_PROOF_NOT_REGULAR");
|
|
const buffer = Buffer.alloc(2 * 1024 * 1024 + 1);
|
|
let position = 0;
|
|
while (position < buffer.length) {
|
|
const r = await handle.read(
|
|
buffer,
|
|
position,
|
|
buffer.length - position,
|
|
null,
|
|
);
|
|
if (r.bytesRead === 0) break;
|
|
position += r.bytesRead;
|
|
}
|
|
requireThat(position <= 2 * 1024 * 1024, "GIT_PROOF_TOO_LARGE");
|
|
return buffer.subarray(0, position);
|
|
} catch (error) {
|
|
primary = true;
|
|
throw error;
|
|
} finally {
|
|
try {
|
|
await handle.close();
|
|
} catch (error) {
|
|
await Promise.allSettled([
|
|
privateWrite(
|
|
resolve(local, "diagnostics/git-proof-close.json"),
|
|
JSON.stringify({ code: "GIT_PROOF_CLOSE_FAILED" }),
|
|
),
|
|
]);
|
|
if (!primary) throw error;
|
|
}
|
|
}
|
|
}
|
|
export async function readCanonicalGitProof(reference: {
|
|
path: string;
|
|
sha256: string;
|
|
}) {
|
|
requireThat(
|
|
/^evidence\/openbao\/canonical-git-proof-[a-f0-9-]+\.json$/.test(
|
|
reference.path,
|
|
),
|
|
"GIT_PROOF_PATH_INVALID",
|
|
);
|
|
return createGitProofReader({
|
|
read: () => boundedProof(resolve(root, reference.path)),
|
|
snapshot: () => gitProofSourceSnapshot(root),
|
|
})(reference.sha256);
|
|
}
|