import { validateVulnerabilityReport } from "./supply-chain.ts"; /** * 1103/13-03. The vulnerability verdict belongs to the external provider of * the production promotion (signed, independent of the build). Without a * provider nothing told the project that its lockfile already broke the * template's own policy (high and above, no exception). This summary is the * early signal: it reads `pnpm audit --json` and applies that policy, so it * names what the promotion would refuse. It does not replace the provider and * blocks nothing. */ export type AdvisoryStatus = "PASS" | "POLICY_VIOLATION" | "UNVERIFIED"; type Severity = "critical" | "high" | "moderate" | "low" | "info"; type SeverityCounts = Record; export type DependencyAdvisorySummary = Readonly<{ status: AdvisoryStatus; reason: string | null; counts: Readonly<{ production: SeverityCounts; development: SeverityCounts }>; /** Findings the vulnerability policy would block at promotion: `id:package@version:severity`. */ blocking: readonly string[]; advisories: readonly Readonly<{ id: string; packageName: string; versions: readonly string[]; severity: string; development: boolean; patchedVersions: string; }>[]; }>; const SEVERITIES: readonly Severity[] = ["critical", "high", "moderate", "low", "info"]; function isRecord(value: unknown): value is Record { return Boolean(value) && typeof value === "object" && !Array.isArray(value); } function emptyCounts(): SeverityCounts { return { critical: 0, high: 0, moderate: 0, low: 0, info: 0 }; } export function summarizeDependencyAdvisories( audit: unknown, policy: Readonly>, exceptions: Readonly>, lockfileSha256: string, now: Date = new Date(), ): DependencyAdvisorySummary { const counts = { production: emptyCounts(), development: emptyCounts() }; if (!isRecord(audit) || !isRecord(audit["advisories"]) || !isRecord(audit["metadata"])) { return { status: "UNVERIFIED", reason: "pnpm audit returned no advisory report (network or registry unavailable)", counts, blocking: [], advisories: [], }; } const advisories = Object.values(audit["advisories"]).filter(isRecord).map((advisory) => { const findings = Array.isArray(advisory["findings"]) ? advisory["findings"].filter(isRecord) : []; return { id: String(advisory["github_advisory_id"] ?? advisory["id"] ?? "unknown"), packageName: String(advisory["module_name"] ?? "unknown"), versions: findings.map((finding) => String(finding["version"] ?? "unknown")), severity: String(advisory["severity"] ?? "unknown").toLowerCase(), // A package reached only through development dependencies never ships. development: findings.length > 0 && findings.every((finding) => finding["dev"] === true), patchedVersions: String(advisory["patched_versions"] ?? ""), }; }); for (const advisory of advisories) { const scope = advisory.development ? counts.development : counts.production; if ((SEVERITIES as readonly string[]).includes(advisory.severity)) scope[advisory.severity as Severity] += 1; } const verdict = validateVulnerabilityReport( { provider: "pnpm-audit (local, not the promotion provider)", scannedLockfileSha256: lockfileSha256, findings: advisories.flatMap((advisory) => advisory.versions.map((version) => ({ id: advisory.id, packageName: advisory.packageName, version, severity: advisory.severity, })), ), }, policy, exceptions, lockfileSha256, now, ); return { status: verdict.blocking.length > 0 ? "POLICY_VIOLATION" : "PASS", reason: null, counts, blocking: verdict.blocking, advisories, }; }