import { spawnSync } from "node:child_process"; import { createHash } from "node:crypto"; import { mkdir, readFile, writeFile } from "node:fs/promises"; import { DEPENDENCY_ADVISORIES_RECORD_PATH, summarizeDependencyAdvisories, } from "./lib/dependency-advisories.ts"; /** * 1103/13-03. Release-stage signal for known dependency advisories * (FE-GATE-027, before the release admission of the candidate, so a refused * candidate still leaves the record: 1106/13-01). It runs * `pnpm audit --json` over the lockfile and applies * config/security/vulnerability-policy.json with its exceptions, the policy the * production promotion provider is held to. The result is a line in the gate * log and artifacts/security/dependency-advisories.json. * * It never fails the gate. An advisory database that changes under unchanged * code would otherwise break a release run, and without the registry the * result is UNVERIFIED, not PASS. The signed verdict of the vulnerability * provider stays the only one that decides a promotion. */ const RECORD_PATH = DEPENDENCY_ADVISORIES_RECORD_PATH; const [policy, exceptions, lockfile] = await Promise.all([ readFile("config/security/vulnerability-policy.json", "utf8").then((text) => JSON.parse(text) as Record), readFile("config/security/vulnerability-exceptions.json", "utf8").then((text) => JSON.parse(text) as Record), readFile("pnpm-lock.yaml"), ]); const lockfileSha256 = createHash("sha256").update(lockfile).digest("hex"); // pnpm audit exits 1 when it finds advisories; the JSON on stdout is the result. const run = spawnSync("corepack", ["pnpm", "audit", "--json"], { encoding: "utf8", maxBuffer: 64 * 1024 * 1024, timeout: 120_000, }); let audit: unknown; try { audit = JSON.parse(run.stdout ?? ""); } catch { audit = null; } const summary = summarizeDependencyAdvisories(audit, policy, exceptions, lockfileSha256); await mkdir("artifacts/security", { recursive: true }); await writeFile( RECORD_PATH, `${JSON.stringify( { schemaVersion: 1, generatedAt: new Date().toISOString(), source: "pnpm audit --json", authoritative: false, lockfileSha256, blockAtSeverity: policy["blockAtSeverity"] ?? null, ...summary, }, null, 2, )}\n`, ); const count = (scope: Readonly>) => `critical ${scope["critical"]}, high ${scope["high"]}, moderate ${scope["moderate"]}, low ${scope["low"]}`; process.stdout.write( `Dependency advisories: ${summary.status} (production: ${count(summary.counts.production)}; ` + `development: ${count(summary.counts.development)}); record at ${RECORD_PATH}. ` + "Not blocking: the production promotion provider decides.\n", ); if (summary.status === "POLICY_VIOLATION") { process.stderr.write( `WARNING: the vulnerability policy (block at ${String(policy["blockAtSeverity"])}) would refuse this lockfile at production promotion:\n` + summary.blocking.map((finding) => ` ${finding}\n`).join("") + "Update the dependency or record a reviewed exception in config/security/vulnerability-exceptions.json.\n", ); } if (summary.status === "UNVERIFIED") { process.stderr.write(`WARNING: dependency advisories UNVERIFIED: ${summary.reason}\n`); }