77 lines
3.2 KiB
Python
77 lines
3.2 KiB
Python
"""Fail-closed production advisory gate: reject Moderate, High and Critical.
|
|
|
|
Registry report must be complete and match the expected format.
|
|
This is CI-only; report data comes from npm/pnpm and never changes dependencies.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
import sys
|
|
|
|
LEVELS = ("moderate", "high", "critical")
|
|
ALL = ("info", "low", "moderate", "high", "critical")
|
|
|
|
|
|
def main() -> int:
|
|
if len(sys.argv) != 3 or sys.argv[1] not in ("npm", "pnpm"):
|
|
raise ValueError("Usage: security_advisory_gate.py npm|pnpm report.json")
|
|
manager, file = sys.argv[1:]
|
|
report = json.loads(Path(file).read_text(encoding="utf-8"))
|
|
if not isinstance(report, dict) or report.get("error"):
|
|
raise ValueError("Registry error or malformed audit report")
|
|
metadata = report.get("metadata")
|
|
if not isinstance(metadata, dict):
|
|
raise ValueError("Audit metadata missing")
|
|
counts = metadata.get("vulnerabilities")
|
|
if not isinstance(counts, dict):
|
|
raise ValueError("Audit vulnerability metadata missing")
|
|
for severity in ALL:
|
|
value = counts.get(severity)
|
|
if type(value) is not int or value < 0:
|
|
raise ValueError(f"Missing/non-integer {severity} audit count")
|
|
|
|
if manager == "npm":
|
|
packages = report.get("vulnerabilities")
|
|
if not isinstance(packages, dict):
|
|
raise ValueError("npm audit vulnerable package map missing")
|
|
discovered = {severity: 0 for severity in ALL}
|
|
for name, record in packages.items():
|
|
if not isinstance(record, dict) or record.get("severity") not in ALL:
|
|
raise ValueError(f"Invalid npm package vulnerability record: {name}")
|
|
discovered[record["severity"]] += 1
|
|
if any(counts[key] != discovered[key] for key in ALL):
|
|
raise ValueError("npm metadata and individual vulnerable package counts differ")
|
|
else:
|
|
records = report.get("advisories")
|
|
if not isinstance(records, dict):
|
|
raise ValueError("pnpm audit advisory map missing")
|
|
discovered = {severity: 0 for severity in ALL}
|
|
for identifier, record in records.items():
|
|
if not isinstance(record, dict) or record.get("severity") not in ALL:
|
|
raise ValueError(f"Invalid pnpm advisory record: {identifier}")
|
|
discovered[record["severity"]] += 1
|
|
if any(counts[key] != discovered[key] for key in ALL):
|
|
raise ValueError("pnpm metadata and individual advisory counts differ")
|
|
|
|
print(f"ZITPLANE_{manager.upper()}_ADVISORY_COUNTS={counts}", flush=True)
|
|
open_count = sum(counts[key] for key in LEVELS)
|
|
if open_count:
|
|
print(
|
|
f"ZITPLANE_{manager.upper()}_SECURITY_ACCEPTANCE_BLOCKED="
|
|
f"{open_count} Moderate/High/Critical findings; see preceding triage records",
|
|
file=sys.stderr,
|
|
flush=True,
|
|
)
|
|
return 1
|
|
print(f"ZITPLANE_{manager.upper()}_ZERO_MODERATE_HIGH_CRITICAL_PASS")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
sys.exit(main())
|
|
except (ValueError, OSError, KeyError, json.JSONDecodeError) as exc:
|
|
print(f"ZITPLANE_ADVISORY_GATE_INVALID_REPORT: {exc}", file=sys.stderr)
|
|
sys.exit(1)
|