Files
clean-architecture-frontend…/scripts/lib/dependency-advisories.ts
DongHyeonka 629c713ce4 fix(ci): immutable_build가 gate 로그와 advisory 기록을 항상 올린다
문제: ci:gate는 명령 출력을 artifacts/quality/gates/FE-GATE-<NNN>.txt에만
쓰고 콘솔에는 PASS/FAIL 한 줄만 남긴다(scripts/run-ci-gate.ts). release
단계 job immutable_build는 후보 tar만, 그것도 성공했을 때만 올린다. 그래서
실제 run에서 FE-GATE-015·027이 왜 실패했는지와 FE-GATE-027이 쓰는
artifacts/security/dependency-advisories.json(1103/13-03의 신호)이 runner
작업 디렉터리와 함께 사라진다. 다른 gate job은 artifacts/를 if: always()로
올린다.

고른 방법:
- config/ci/gates.json의 immutable_build 마지막에 upload 단계를 더한다.
  transfer ID는 immutable-gate-evidence, 이름은
  immutable-gate-evidence-<run>-<attempt>, always: true. 대상은
  FE-GATE-015·027의 log와 evidence 산출물(gate 순서)과 advisory 기록 7개다.
  FE-GATE-015의 verification.json 등은 후보가 만들어지지 않으면 후보 안에도
  없으므로 같이 올린다.
- 계약(scripts/contracts/ci-gates.ts)이 이 단계를 고정한다. immutable_build의
  단계 목록에 upload 하나가 늘고, 이 upload가 always이고 마지막 단계이며
  경로가 "job이 도는 gate의 log·evidence 경로 + advisory 기록"과 같아야
  한다. 경로는 계약의 artifact 등록에서 계산하므로 log 경로가 바뀌면 함께
  따라온다. advisory 기록은 gate evidence가 아니라(막지 않는 신호) 경로를
  scripts/lib/dependency-advisories.ts의 상수로 옮기고 scan 스크립트도 그
  상수를 쓴다.
- 후보 archive의 member set, 후보 upload(always 없음), 하위 job의 download는
  그대로다. 생성 workflow를 generator 출력과 같게 손으로 고쳤다.

버린 대안:
- immutable_build도 artifacts/ 전체를 올림: 다른 job과 같지만 후보 증거가
  두 이름으로 나가 어느 것이 승격 대상인지 흐려진다. 필요한 파일만 고정했다.
- ci:gate가 실패한 명령의 마지막 N줄을 콘솔에 씀(리뷰 대안 c, d의 콘솔
  한 줄 포함): 모든 job에 걸리고 provider 출력의 비밀 노출과 로그 상한을
  다시 따져야 한다. 담당 지시 범위(업로드) 밖이라 이번에 하지 않았다.

위험·한계:
- checkout·install 단계에서 실패하면 올릴 파일이 없어 이 단계도
  if-no-files-found: error로 실패한다. job은 이미 실패한 뒤라 결과는 같다.
  merge·release job의 upload도 같은 동작이다.
- 실제 Gitea runner에서 stage=release를 돌려 산출물이 남는지는 확인하지
  못했다(runner 없음). 확인한 것은 계약·생성 workflow까지다.
- 콘솔에는 여전히 한 줄만 나온다. 사람이 산출물을 열어야 이유를 본다.

검증: desktop batch에서 확인
- node scripts/run-vitest.ts run tests/unit/ci-workflow-generation.test.ts
- corepack pnpm check:ci, check:types, lint, verify:documentation

Refs: 1105/13-01, 1103/13-03
2026-11-05 22:12:30 +09:00

107 lines
4.0 KiB
TypeScript

import { validateVulnerabilityReport } from "./supply-chain.ts";
/**
* Where scan:dependency-advisories writes its record. 1105/13-01: the
* immutable_build job uploads it on every outcome (scripts/contracts/ci-gates.ts).
*/
export const DEPENDENCY_ADVISORIES_RECORD_PATH = "artifacts/security/dependency-advisories.json";
/**
* 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<Severity, number>;
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<string, unknown> {
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<Record<string, unknown>>,
exceptions: Readonly<Record<string, unknown>>,
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,
};
}