84 lines
3.3 KiB
Python
84 lines
3.3 KiB
Python
"""Summarize CI registry advisory JSON without changing dependencies.
|
|
|
|
npm: audit --omit=dev --json; pnpm: audit --prod --json.
|
|
High/critical gating is performed separately by the workflow.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
import sys
|
|
|
|
|
|
def read_report(path: str) -> dict:
|
|
report = json.loads(Path(path).read_text(encoding="utf-8"))
|
|
if not isinstance(report, dict) or (
|
|
"advisories" not in report and "vulnerabilities" not in report
|
|
):
|
|
raise ValueError("Unexpected or incomplete audit JSON; triage cannot pass")
|
|
if "error" in report:
|
|
raise ValueError("Registry audit reported an error; not a valid pass")
|
|
return report
|
|
|
|
|
|
def triage_pnpm(report: dict) -> None:
|
|
advisories = report.get("advisories")
|
|
if not isinstance(advisories, dict):
|
|
raise ValueError("Expected pnpm audit advisories map")
|
|
counts: dict[str, int] = {}
|
|
for identifier, issue in sorted(advisories.items()):
|
|
if not isinstance(issue, dict):
|
|
raise ValueError(f"Invalid advisory record: {identifier}")
|
|
severity = str(issue.get("severity", "unknown")).lower()
|
|
counts[severity] = counts.get(severity, 0) + 1
|
|
aid = issue.get("github_advisory_id") or issue.get("url") or identifier
|
|
package = issue.get("module_name") or issue.get("name") or "unknown"
|
|
affected = issue.get("vulnerable_versions") or "unknown"
|
|
fixed = issue.get("patched_versions") or "not provided"
|
|
print(f"PNPM_ADVISORY id={aid} package={package} severity={severity} affected={affected} fixed={fixed}")
|
|
summary = report.get("metadata", {}).get("vulnerabilities", {})
|
|
print(f"PNPM_ADVISORY_RECORDS={len(advisories)} COUNT_BY_SEVERITY={counts}")
|
|
print(f"PNPM_REPORTED_SEVERITIES={summary}")
|
|
if sum(counts.values()) != len(advisories):
|
|
raise ValueError("Inconsistent advisory record count")
|
|
|
|
|
|
def triage_npm(report: dict) -> None:
|
|
issues = report.get("vulnerabilities")
|
|
if not isinstance(issues, dict):
|
|
raise ValueError("Expected npm audit vulnerabilities map")
|
|
for name, entry in sorted(issues.items()):
|
|
if not isinstance(entry, dict):
|
|
raise ValueError(f"Invalid npm record: {name}")
|
|
via = entry.get("via", [])
|
|
advisories = [
|
|
(item.get("url") or item.get("source") or "unknown")
|
|
for item in via if isinstance(item, dict)
|
|
]
|
|
print(
|
|
f"NPM_VULNERABILITY package={name} severity={entry.get('severity', 'unknown')} "
|
|
f"affected={entry.get('range', 'unknown')} fix={entry.get('fixAvailable', 'unknown')} "
|
|
f"advisories={advisories}"
|
|
)
|
|
print(f"NPM_VULNERABLE_PACKAGE_COUNT={len(issues)}")
|
|
print(f"NPM_REPORTED_SEVERITIES={report.get('metadata', {}).get('vulnerabilities', {})}")
|
|
|
|
|
|
def main() -> None:
|
|
if len(sys.argv) != 3 or sys.argv[1] not in ("npm", "pnpm"):
|
|
raise SystemExit("Usage: advisory_triage.py npm|pnpm audit-report.json")
|
|
report = read_report(sys.argv[2])
|
|
if sys.argv[1] == "npm":
|
|
triage_npm(report)
|
|
else:
|
|
triage_pnpm(report)
|
|
print("ZITPLANE_ADVISORY_TRIAGE_COMPLETE")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except (ValueError, OSError, json.JSONDecodeError) as exc:
|
|
print(f"ZITPLANE_ADVISORY_TRIAGE_FAILED: {exc}", file=sys.stderr)
|
|
raise SystemExit(1) from exc
|