forked from mtusi2026/mtusi2026lab7
109 lines
3.6 KiB
TypeScript
109 lines
3.6 KiB
TypeScript
import { readFile, readdir, stat, writeFile } from "node:fs/promises";
|
|
import { resolve, relative } from "node:path";
|
|
import { root, local, run, compose, requireThat } from "../scripts/runtime";
|
|
import { decrypt } from "../scripts/operator-crypto";
|
|
import { containerID } from "../scripts/runtime-inspect";
|
|
import { readCanonicalGitProof } from "../scripts/git-tracking-source";
|
|
import { type GitTrackingSource } from "../scripts/git-tracking-proof";
|
|
export async function auditArtifacts(
|
|
password: string,
|
|
gitSource: GitTrackingSource,
|
|
) {
|
|
const secrets = decrypt<Record<string, string>>(
|
|
await readFile(resolve(local, "operator/services.enc"), "utf8"),
|
|
password,
|
|
);
|
|
const values = Object.values(secrets).filter(
|
|
(v) => typeof v === "string" && v.length >= 20,
|
|
);
|
|
const files: string[] = [];
|
|
async function walk(path: string) {
|
|
for (const entry of await readdir(path, { withFileTypes: true })) {
|
|
if (
|
|
[".git", "node_modules", ".venv"].includes(entry.name) ||
|
|
entry.name.startsWith(".local") ||
|
|
entry.name === ".env" ||
|
|
entry.name.endsWith(".zip")
|
|
)
|
|
continue;
|
|
const target = resolve(path, entry.name);
|
|
if (entry.isDirectory()) await walk(target);
|
|
else if (entry.isFile()) files.push(target);
|
|
}
|
|
}
|
|
await walk(root);
|
|
let violations = 0;
|
|
for (const file of files) {
|
|
const info = await stat(file);
|
|
requireThat(
|
|
info.size <= 20 * 1024 * 1024,
|
|
"UNEXPECTED_LARGE_SOURCE_ARTIFACT",
|
|
);
|
|
const content = await readFile(file);
|
|
for (const value of values)
|
|
if (content.includes(Buffer.from(value))) violations++;
|
|
}
|
|
let gitProvenance: Record<string, unknown>;
|
|
if (gitSource.kind === "canonical-proof")
|
|
gitProvenance = {
|
|
kind: gitSource.kind,
|
|
...(await readCanonicalGitProof(gitSource)),
|
|
};
|
|
else {
|
|
const forbidden = await run([
|
|
"git",
|
|
"ls-files",
|
|
".env",
|
|
".local/**",
|
|
"*.key",
|
|
"*.pem",
|
|
]);
|
|
requireThat(!forbidden.out.trim(), "PRIVATE_MATERIAL_TRACKED");
|
|
gitProvenance = { kind: "native-git", tracked_private_files: 0 };
|
|
}
|
|
for (const service of ["api1", "api2", "worker"]) {
|
|
const id = await containerID(service),
|
|
info = JSON.parse((await run(["docker", "inspect", id])).out)[0];
|
|
requireThat(
|
|
info.Config.User !== "0" &&
|
|
info.Config.User !== "" &&
|
|
info.HostConfig.ReadonlyRootfs === true,
|
|
"APP_PRIVILEGE_OR_FILESYSTEM",
|
|
);
|
|
requireThat(
|
|
info.HostConfig.CapDrop.includes("ALL") &&
|
|
info.HostConfig.SecurityOpt.includes("no-new-privileges:true") &&
|
|
info.HostConfig.Memory > 0 &&
|
|
info.HostConfig.PidsLimit > 0,
|
|
"APP_LIMITS_OR_CAPABILITIES",
|
|
);
|
|
requireThat(
|
|
!info.Mounts.some((m: any) =>
|
|
/docker.sock|operator|\/ca$/.test(m.Source),
|
|
),
|
|
"OPERATOR_MATERIAL_MOUNTED_IN_APP",
|
|
);
|
|
const logs = await compose(["logs", "--no-color", service]);
|
|
for (const value of values)
|
|
if ((logs.out + logs.err).includes(value)) violations++;
|
|
}
|
|
requireThat(violations === 0, "SECRET_LEAK_DETECTED");
|
|
await writeFile(
|
|
resolve(root, "evidence/security/artifacts-and-hardening.json"),
|
|
JSON.stringify(
|
|
{
|
|
source_files_checked: files.length,
|
|
generated_secret_values_checked: values.length,
|
|
secret_matches: violations,
|
|
tracked_private_files: 0,
|
|
git_tracking_provenance: gitProvenance,
|
|
app_nonroot_readonly_capdrop_limits: true,
|
|
operator_mounts_in_app: 0,
|
|
},
|
|
null,
|
|
2,
|
|
) + "\n",
|
|
);
|
|
console.log("PASS_SECRET_ARTIFACTS_LOGS_AND_EFFECTIVE_APP_HARDENING");
|
|
}
|