render_report.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. #!/usr/bin/env python3
  2. """Render a scan's machine-readable artifacts from its run directory.
  3. Writes CLAUDE-SECURITY-RESULTS.jsonl (one finding per line, fields in a fixed
  4. order), CLAUDE-SECURITY-RESULTS.sarif (the same findings as a SARIF 2.1.0 log)
  5. and the CLAUDE-SECURITY-REVISION-<tag>.json stamp, places the report markdown
  6. beside them, then removes the scan's run directory now that its records are
  7. rendered. Findings that name one rule at one line of a file are one record in
  8. every product (see one_per_site). Filenames, JSONL field order, and
  9. verification.status semantics are stable across releases.
  10. Usage:
  11. render_report.py <run_dir> [--products-dir <dir>]
  12. Exits 0 on success, 1 on a refusal naming what is wrong, 2 on a usage error.
  13. Python 3.9-compatible, stdlib only.
  14. """
  15. from __future__ import annotations
  16. import argparse
  17. import ntpath
  18. import os
  19. import re
  20. import shutil
  21. import sys
  22. import uuid
  23. from collections import Counter
  24. from datetime import datetime, timezone
  25. from pathlib import Path
  26. from typing import TYPE_CHECKING, NamedTuple, TypedDict
  27. # The lib/ package lives next to this script. Python normally adds a script's own
  28. # directory to the import path, but not under -P or PYTHONSAFEPATH, so we add it here.
  29. sys.path.insert(0, str(Path(__file__).resolve().parent))
  30. from lib import absolute, console, cwe, plugin, sarif, secret, strictjson
  31. from lib.finding import (
  32. CONFIDENCES,
  33. PANEL_KEEP_QUORUM,
  34. PANEL_VOTER_COUNT,
  35. SEVERITIES,
  36. Finding,
  37. FindingError,
  38. build_finding,
  39. panel_complete,
  40. )
  41. from lib.strictjson import JsonMap, is_int, is_list, is_map, is_str
  42. if TYPE_CHECKING:
  43. from collections.abc import Mapping, Sequence
  44. class _ResearcherCounts(TypedDict, total=False):
  45. """The two verification counts a vote record may omit."""
  46. researchers_dispatched: int
  47. researchers_returned: int
  48. class VerificationSummary(_ResearcherCounts):
  49. """The stamp's `verification` object; every path names why if not verified."""
  50. status: str
  51. candidates: int
  52. candidates_deduped: int
  53. panel_votes: int
  54. panel_reviewed_findings: int
  55. panel_quorum_findings: int
  56. unreviewed_candidate_sites: int
  57. incomplete_panel_candidates: int
  58. attested_findings: int
  59. reason: str | None
  60. class Meta(NamedTuple):
  61. """The scan meta a render reads back: the scan itself, and the stamp fields beside it."""
  62. scan: sarif.Scan
  63. scan_root: str
  64. revision: object
  65. revision_source: str
  66. model: object
  67. effort: object
  68. class Rendered(NamedTuple):
  69. """A completed render: the findings, their verification, and the stamp's tag."""
  70. findings: list[Finding]
  71. verification: VerificationSummary
  72. tag: str
  73. REVISION_PREFIX = "CLAUDE-SECURITY-REVISION-"
  74. JSONL_NAME = "CLAUDE-SECURITY-RESULTS.jsonl"
  75. SARIF_NAME = "CLAUDE-SECURITY-RESULTS.sarif"
  76. SANITIZED_REMOTE_RE = re.compile(
  77. r"https://[a-z0-9.-]+(?::[0-9]+)?/(?:[A-Za-z0-9._~/-]|%[0-9A-F]{2})+\Z"
  78. )
  79. # Set only by workflows/scan.js (its PROVENANCE) on each vote record it computes.
  80. VOTES_PROVENANCE = "workflows/scan.js"
  81. class Args(argparse.Namespace):
  82. """The parsed command line."""
  83. run_dir: str = ""
  84. products_dir: str | None = None
  85. class RenderError(Exception):
  86. """A refusal; the message names what the caller must fix."""
  87. def read_json(run_dir: str, name: str) -> object:
  88. """The JSON value in a run file the render requires; a missing or malformed one is a refusal."""
  89. try:
  90. return strictjson.load(os.path.join(run_dir, name))
  91. except FileNotFoundError as error:
  92. msg = f"{name} is missing from the run directory. Write it before running this script."
  93. raise RenderError(msg) from error
  94. except ValueError as error:
  95. msg = f"{name} is not valid JSON: {error}"
  96. raise RenderError(msg) from error
  97. def read_votes(run_dir: str) -> JsonMap | None:
  98. """The workflow's vote record, or None when votes.json is absent or not marked as its own."""
  99. try:
  100. raw = strictjson.load(os.path.join(run_dir, "votes.json"))
  101. except FileNotFoundError:
  102. return None
  103. except ValueError as error:
  104. msg = f"votes.json is not valid JSON: {error}"
  105. raise RenderError(msg) from error
  106. if not is_map(raw):
  107. raise RenderError("votes.json must be a JSON object mapping the vote record")
  108. if raw.get("provenance") != VOTES_PROVENANCE:
  109. return None
  110. return raw
  111. def read_source(scan_root: str, file: str) -> str | None:
  112. """The text of a scanned file a finding names; None when it cannot be read."""
  113. try:
  114. return Path(scan_root, file).read_bytes().decode("utf-8", "surrogateescape")
  115. except (OSError, ValueError):
  116. return None
  117. def read_coverage(run_dir: str) -> tuple[JsonMap | None, str]:
  118. """The optional coverage.json for the informational run_shape field.
  119. Returns (map_or_None, source): source is "coverage.json" when the file
  120. is a usable object, "unavailable" when it is absent, and "unreadable" when
  121. it exists but is not a usable object.
  122. """
  123. name = "coverage.json"
  124. try:
  125. raw = strictjson.load(os.path.join(run_dir, name))
  126. except FileNotFoundError:
  127. return None, "unavailable"
  128. except (OSError, ValueError):
  129. return None, "unreadable"
  130. return (raw, name) if is_map(raw) else (None, "unreadable")
  131. COVERAGE_TEXT_CAP = 300
  132. def coverage_text(value: object, cap: int = COVERAGE_TEXT_CAP) -> str | None:
  133. """A coverage string, trimmed to `cap`, or None when the value is not a string."""
  134. if not is_str(value):
  135. return None
  136. if len(value) > cap:
  137. return value[:cap] + f"...[+{len(value) - cap} chars]"
  138. return value
  139. def coverage_texts(raw: object, cap: int) -> list[str]:
  140. """The strings among a coverage list, each trimmed to `cap`; anything else is dropped."""
  141. items: list[object] = raw if is_list(raw) else []
  142. return [text for item in items if (text := coverage_text(item, cap))]
  143. def tree_relative(path: str, scan_root: str) -> str | None:
  144. """A skipped path relative to the scan root; None for an absolute one that is not inside it."""
  145. if not absolute.spelled(path):
  146. return path
  147. try:
  148. relative = os.path.relpath(os.path.realpath(path), scan_root).replace("\\", "/")
  149. except (ValueError, OSError):
  150. return None
  151. return None if relative == ".." or relative.startswith("../") else relative
  152. def skipped_component(item: JsonMap, scan_root: str) -> dict[str, object]:
  153. paths = (tree_relative(path, scan_root) for path in coverage_texts(item.get("paths"), 200))
  154. return {
  155. "name": coverage_text(item.get("name"), 100) or "",
  156. "paths": [path for path in paths if path is not None],
  157. "reason": coverage_text(item.get("reason")) or "",
  158. }
  159. def skipped_components(raw: object, scan_root: str) -> list[dict[str, object]] | None:
  160. """coverage.skippedComponents as [{name, paths, reason}], or None when unusable."""
  161. if not is_list(raw):
  162. return None
  163. return [skipped_component(entry, scan_root) for entry in raw if is_map(entry)]
  164. def coverage_enum(value: object, allowed: tuple[str, ...]) -> str | None:
  165. """A coverage enum field, or None when absent or not one of the known values."""
  166. return value if is_str(value) and value in allowed else None
  167. def coverage_count(value: object) -> int | None:
  168. """A coverage count field, or None when absent or not an integer."""
  169. return value if is_int(value) else None
  170. def run_shape(
  171. coverage: JsonMap | None, source: str, effort: object, scan_root: str
  172. ) -> dict[str, object]:
  173. """What shape actually ran, distinct from the effort tier that was asked."""
  174. shape: dict[str, object] = {"requested_effort": effort, "collapsed": None, "source": source}
  175. if coverage is None:
  176. return shape
  177. return {
  178. **shape,
  179. "collapsed": coverage_enum(coverage.get("collapsed"), ("small-diff", "small-scope")),
  180. "diff_files": coverage_count(coverage.get("diffFiles")),
  181. "diff_lines": coverage_count(coverage.get("diffLines")),
  182. "scope_files": coverage_count(coverage.get("scopeFiles")),
  183. "empty_diff": bool(coverage.get("emptyDiff")),
  184. "empty_scope": bool(coverage.get("emptyScope")),
  185. "researchers_dispatched": coverage_count(coverage.get("researchersDispatched")),
  186. "skipped_components": skipped_components(coverage.get("skippedComponents"), scan_root),
  187. "completeness_check_outcome": coverage_enum(
  188. coverage.get("completenessCheckOutcome"),
  189. ("checked", "partial", "not-checkable", "not-applicable"),
  190. ),
  191. "unaccounted_top_level_dirs": coverage_texts(coverage.get("unaccountedTopLevelDirs"), 200),
  192. "inventory_fallback": coverage_enum(
  193. coverage.get("inventoryFallback"),
  194. ("inventory-failed", "empty-partition", "incomplete-partition"),
  195. ),
  196. "top_level_dir_count": coverage_count(coverage.get("topLevelCount")),
  197. }
  198. def verification_summary(
  199. findings: list[Finding],
  200. votes: JsonMap,
  201. votes_present: bool = True,
  202. ) -> VerificationSummary:
  203. """Compute the stamp's verification object from the vote record.
  204. status is 'verified' only when the vote record proves a complete panel
  205. round for every finding the report contains and for every other candidate
  206. it holds a round for; otherwise 'unverified' with a `reason`.
  207. `incomplete_panel_candidates` counts the unreported candidates whose round
  208. is not complete. votes_present is False when read_votes returned None.
  209. """
  210. raw_rounds = votes.get("rounds")
  211. rounds: JsonMap = raw_rounds if is_map(raw_rounds) else {}
  212. panels = [(f["id"], panel_complete(rounds.get(f["id"]))) for f in findings]
  213. incomplete = sorted(finding_id for finding_id, panel in panels if panel is None)
  214. reviewed = [panel for _, panel in panels if panel is not None]
  215. quorum = sum(panel["true"] >= PANEL_KEEP_QUORUM for panel in reviewed)
  216. reported = {f["id"] for f in findings}
  217. dropped_incomplete = sorted(
  218. round_id
  219. for round_id, record in rounds.items()
  220. if round_id not in reported and panel_complete(record) is None
  221. )
  222. def as_count(key: str) -> int:
  223. """A vote count as a non-negative int; a wrong shape is a refusal."""
  224. raw = votes.get(key, 0)
  225. if not is_int(raw) or raw < 0:
  226. msg = (
  227. f"votes.json field {key!r} is not a non-negative integer ({raw!r}); the "
  228. "vote record is malformed"
  229. )
  230. raise RenderError(msg)
  231. return raw
  232. candidates = as_count("candidates")
  233. dispatched = as_count("researchers_dispatched") if "researchers_dispatched" in votes else None
  234. returned = as_count("researchers_returned") if "researchers_returned" in votes else None
  235. reason: str | None = None
  236. if not votes_present:
  237. reason = (
  238. "votes.json is absent from the run directory or is not the scan workflow's record: "
  239. "the verification pipeline left no vote record, so nothing about this report can "
  240. "be attested"
  241. )
  242. elif "candidates" not in votes:
  243. reason = (
  244. "votes.json has no 'candidates' field: the vote record does not prove the pipeline "
  245. "ran, so nothing about this report can be attested"
  246. )
  247. elif dispatched and returned == 0:
  248. reason = (
  249. f"{dispatched} research agent(s) were dispatched but none returned; the scan "
  250. "examined nothing"
  251. )
  252. elif incomplete:
  253. reason = (
  254. f"these findings have no complete {PANEL_VOTER_COUNT}-voter panel round: "
  255. f"{', '.join(incomplete)}"
  256. )
  257. elif findings and quorum != len(findings):
  258. reason = (
  259. f"{len(findings) - quorum} of {len(findings)} reported findings did not reach the "
  260. "keep quorum, so the report contains findings the panel rejected"
  261. )
  262. elif not findings and not rounds and candidates:
  263. reason = f"{candidates} candidates were recorded but none was paneled"
  264. elif not findings and rounds and not any(map(panel_complete, rounds.values())):
  265. reason = (
  266. f"{len(rounds)} panel round(s) were dispatched but none completed a full "
  267. f"{PANEL_VOTER_COUNT}-voter review; no candidate was actually verified"
  268. )
  269. elif dropped_incomplete:
  270. reason = (
  271. f"{len(dropped_incomplete)} candidate(s) were dropped without a complete "
  272. f"{PANEL_VOTER_COUNT}-voter panel round: {', '.join(dropped_incomplete)}"
  273. )
  274. summary: VerificationSummary = {
  275. "status": "verified" if reason is None else "unverified",
  276. "candidates": candidates,
  277. "candidates_deduped": as_count("candidates_deduped"),
  278. "panel_votes": as_count("panel_votes"),
  279. "panel_reviewed_findings": len(reviewed),
  280. "panel_quorum_findings": quorum,
  281. "unreviewed_candidate_sites": as_count("unreviewed_candidate_sites"),
  282. "incomplete_panel_candidates": len(dropped_incomplete),
  283. "attested_findings": 0,
  284. "reason": reason,
  285. }
  286. if dispatched is not None:
  287. summary["researchers_dispatched"] = dispatched
  288. if returned is not None:
  289. summary["researchers_returned"] = returned
  290. return summary
  291. def revision_tag(revision: object) -> str:
  292. """The stamp's filename tag: <sha12>[-dirty], or UNVERSIONED."""
  293. if not is_map(revision):
  294. msg = f"the run's revision {revision!r} is not an object, so it cannot name the stamp file"
  295. raise RenderError(msg)
  296. sha = revision.get("commit") or revision.get("head")
  297. if not sha:
  298. return "UNVERSIONED"
  299. if not is_str(sha) or not plugin.SHA_RE.match(sha):
  300. msg = f"the run's revision {sha!r} is not a hex commit id, so it cannot name the stamp file"
  301. raise RenderError(msg)
  302. return sha[:12] + ("" if revision.get("dirty") is False else "-dirty")
  303. def show_prefix_shaped(prefix: str) -> bool:
  304. """Whether `prefix` is what `git rev-parse --show-prefix` prints: empty, or `a/b/`."""
  305. if not prefix:
  306. return True
  307. return (
  308. prefix.endswith("/")
  309. and "\\" not in prefix
  310. and not ntpath.splitdrive(prefix)[0]
  311. and all(segment not in {"", ".", ".."} for segment in prefix.split("/")[:-1])
  312. )
  313. def scan_of(meta: JsonMap) -> Meta:
  314. """The scan meta the run records, every field shape-checked; a wrong one is a refusal."""
  315. scan_id = meta.get("scan_id")
  316. try:
  317. value = uuid.UUID(scan_id) if is_str(scan_id) else None
  318. except ValueError:
  319. value = None
  320. if value is None or value.version is None or not 1 <= value.version <= 5:
  321. msg = (
  322. f"scan-meta.json scan_id {scan_id!r} is not a version 1-5 UUID; "
  323. "rerun write_scan_meta.py to mint one"
  324. )
  325. raise RenderError(msg)
  326. mode = meta.get("mode")
  327. if not is_str(mode) or mode not in plugin.MODES:
  328. msg = f"scan-meta.json mode {mode!r} is not a scan mode; rerun write_scan_meta.py"
  329. raise RenderError(msg)
  330. scan_root = meta.get("scan_root")
  331. if not is_str(scan_root) or not scan_root.strip():
  332. msg = f"scan-meta.json scan_root {scan_root!r} is not a path; rerun write_scan_meta.py"
  333. raise RenderError(msg)
  334. prefix = meta.get("scan_prefix")
  335. if prefix is None:
  336. prefix = ""
  337. if not is_str(prefix) or not show_prefix_shaped(prefix):
  338. msg = (
  339. f"scan-meta.json scan_prefix {prefix!r} is not a path prefix; rerun write_scan_meta.py"
  340. )
  341. raise RenderError(msg)
  342. remote = meta.get("remote")
  343. if remote is not None and (not is_str(remote) or not SANITIZED_REMOTE_RE.match(remote)):
  344. msg = (
  345. f"scan-meta.json remote {remote!r} is not a sanitized repository URL; "
  346. "rerun write_scan_meta.py"
  347. )
  348. raise RenderError(msg)
  349. scope = meta.get("scope", [])
  350. entries = [entry for entry in scope if is_str(entry)] if is_list(scope) else []
  351. if not is_list(scope) or len(entries) != len(scope):
  352. msg = (
  353. f"scan-meta.json scope {scope!r} is not the list of paths the scan covered; "
  354. "rerun write_scan_meta.py"
  355. )
  356. raise RenderError(msg)
  357. revision: object = meta.get("revision")
  358. if revision is None:
  359. revision = {}
  360. revision_source = meta.get("revision_source", "self-reported")
  361. if not is_str(revision_source):
  362. msg = (
  363. f"scan-meta.json revision_source {revision_source!r} does not name what vouches for "
  364. "the revision; rerun write_scan_meta.py"
  365. )
  366. raise RenderError(msg)
  367. clean_commit: str | None = None
  368. if is_map(revision) and revision.get("dirty") is False:
  369. commit = revision.get("commit")
  370. clean_commit = commit if is_str(commit) and plugin.SHA_RE.match(commit) else None
  371. scan = sarif.Scan(
  372. id=value,
  373. mode=mode,
  374. prefix=prefix,
  375. remote=remote,
  376. scope=tuple(entries),
  377. revision=clean_commit,
  378. )
  379. return Meta(scan, scan_root, revision, revision_source, meta.get("model"), meta.get("effort"))
  380. def jsonl_text(findings: Sequence[Finding]) -> str:
  381. """The findings as JSONL: one record per line as the products carry it, findings.json order."""
  382. return "".join(strictjson.text(secret.withheld(item)) + "\n" for item in findings)
  383. def strength(finding: Finding) -> tuple[int, int]:
  384. """A finding's rank among those at one site: severity first, then confidence."""
  385. return -SEVERITIES.index(finding["severity"]), CONFIDENCES.index(finding["confidence"])
  386. def one_per_site(
  387. findings: Sequence[Finding], scan: sarif.Scan, sources: Mapping[str, str]
  388. ) -> tuple[list[Finding], list[str]]:
  389. """The findings reduced to one per site, and one disclosure sentence per finding merged away.
  390. A site is a rule at a line of a file (sarif.site), which is what a result
  391. stands for to a SARIF or JSONL consumer, so the products carry one record
  392. for it: of the findings at one site the strongest is kept, the first of
  393. them in findings.json order when they tie, and each of the others is
  394. named in a sentence with the finding it was merged into. A finding with
  395. no site, one whose line was never determined, is kept as it is.
  396. """
  397. sites = [sarif.site(item, scan, sources.get(item["file"])) for item in findings]
  398. by_site: dict[sarif.Site, list[Finding]] = {}
  399. for item, where in zip(findings, sites):
  400. if where is not None:
  401. by_site.setdefault(where, []).append(item)
  402. kept = {where: max(group, key=strength) for where, group in by_site.items()}
  403. merged = [
  404. f"finding {other['id']} names the same site as finding {kept[where]['id']}, "
  405. f"{where.path}:{where.line} under rule {where.rule}; merged into it"
  406. for where, group in by_site.items()
  407. for other in group
  408. if other is not kept[where]
  409. ]
  410. unmerged = [
  411. item if where is None else kept[where]
  412. for item, where in zip(findings, sites)
  413. if where is None or item is by_site[where][0]
  414. ]
  415. return unmerged, merged
  416. def unrecognized_cwes(findings: Sequence[Finding]) -> list[str]:
  417. """One disclosure sentence per finding whose declared CWE the pinned release does not define."""
  418. return [
  419. f"finding {item['id']} cwe_id {item['cwe_id']} is not a weakness in "
  420. f"CWE {cwe.catalog.version}; filed as Uncategorized"
  421. for item in findings
  422. if not cwe.catalog.defines(cwe.id_number(item["cwe_id"]))
  423. ]
  424. def notifications_of(
  425. shape: Mapping[str, object],
  426. verification: VerificationSummary,
  427. merged: Sequence[str],
  428. unrecognized: Sequence[str],
  429. symlinks: Sequence[str],
  430. revision: object,
  431. ) -> list[dict[str, object]]:
  432. """The invocation notifications: what was skipped, capped, merged, mislabeled or unverified.
  433. The sentences of `merged` (one_per_site) are disclosed at level note, those
  434. of `unrecognized` (unrecognized_cwes) at level warning. `symlinks` names
  435. the root-level symbolic links the scan's extent left out unfollowed.
  436. """
  437. note = sarif.notification
  438. skipped = shape.get("skipped_components")
  439. notes = [
  440. note("coverage/skipped-component", "note", f"Skipped component {s['name']}: {s['reason']}")
  441. for s in (skipped if is_list(skipped) else [])
  442. if is_map(s)
  443. ]
  444. if is_map(revision) and revision.get("sparse") is True:
  445. absent = revision.get("not_checked_out_dirs")
  446. names = ", ".join(d for d in (absent if is_list(absent) else []) if is_str(d))
  447. text = "Sparse checkout: only the checked-out part of the repository was scanned"
  448. if names:
  449. text += f"; tracked top-level directories not checked out: {names}"
  450. notes.append(note("coverage/sparse-checkout", "note", text))
  451. unaccounted = shape.get("unaccounted_top_level_dirs")
  452. if is_list(unaccounted) and unaccounted:
  453. names = ", ".join(d for d in unaccounted if is_str(d))
  454. text = f"Top-level directories the accepted partition left unaccounted: {names}"
  455. notes.append(note("coverage/unaccounted-top-level-dirs", "note", text))
  456. if symlinks:
  457. names = ", ".join(symlinks)
  458. text = f"Root-level symbolic links not followed, left out of the scan's extent: {names}"
  459. notes.append(note("coverage/unfollowed-symlinks", "note", text))
  460. if unreviewed := verification["unreviewed_candidate_sites"]:
  461. text = f"{unreviewed} candidate site(s) were recorded but never reviewed by the panel"
  462. notes.append(note("coverage/unverified-by-cap", "warning", text))
  463. if dropped := verification["incomplete_panel_candidates"]:
  464. text = (
  465. f"{dropped} candidate(s) were dropped without a complete "
  466. f"{PANEL_VOTER_COUNT}-voter panel round"
  467. )
  468. notes.append(note("verification/incomplete-panel", "warning", text))
  469. notes += [note("finding/merged", "note", text) for text in merged]
  470. notes += [note("cwe/unrecognized", "warning", text) for text in unrecognized]
  471. if verification["status"] == "unverified":
  472. notes.append(note("verification/unverified", "error", verification["reason"] or ""))
  473. return notes
  474. def render(run_dir: str, products_dir: str) -> Rendered:
  475. """Read the run's records, validate them, build every product, then write them, stamp last."""
  476. meta = read_json(run_dir, "scan-meta.json")
  477. if not is_map(meta):
  478. raise RenderError("scan-meta.json must be a JSON object")
  479. findings_in = read_json(run_dir, "findings.json")
  480. if not is_list(findings_in):
  481. raise RenderError("findings.json must be a JSON array (use [] for no findings)")
  482. coverage, coverage_source = read_coverage(run_dir)
  483. votes_raw = read_votes(run_dir)
  484. votes: JsonMap = {} if votes_raw is None else votes_raw
  485. rounds_raw = votes.get("rounds")
  486. rounds_by_id: JsonMap = {}
  487. if rounds_raw is not None:
  488. if not is_map(rounds_raw):
  489. kind = type(rounds_raw).__name__
  490. msg = f"votes.json 'rounds' must be an object keyed by finding id, not {kind}"
  491. raise RenderError(msg)
  492. rounds_by_id = rounds_raw
  493. scan, scan_root, revision, revision_source, model, effort = scan_of(meta)
  494. tag = revision_tag(revision)
  495. built = [
  496. build_finding(raw, i, rounds_by_id, scan_root, scan.prefix, scan.mode == "scan")
  497. for i, raw in enumerate(findings_in)
  498. ]
  499. counted = Counter(f["id"] for f in built)
  500. repeated = sorted(finding_id for finding_id, count in counted.items() if count > 1)
  501. if repeated:
  502. msg = f"findings.json uses these finding ids more than once: {', '.join(repeated)}"
  503. raise RenderError(msg)
  504. sources = {
  505. path: text
  506. for path in {f["file"] for f in built}
  507. if (text := read_source(scan_root, path)) is not None
  508. }
  509. findings, merged = one_per_site(built, scan, sources)
  510. markdown_path = os.path.join(run_dir, "CLAUDE-SECURITY-RESULTS.md")
  511. if not os.path.isfile(markdown_path):
  512. raise RenderError(
  513. "CLAUDE-SECURITY-RESULTS.md is missing. Write the human-readable "
  514. "report before running this script."
  515. )
  516. with open(markdown_path, encoding="utf-8", newline="") as handle:
  517. try:
  518. markdown = handle.read()
  519. except UnicodeDecodeError as error:
  520. msg = f"CLAUDE-SECURITY-RESULTS.md is not valid UTF-8: {error}"
  521. raise RenderError(msg) from error
  522. counts = Counter(f["severity"] for f in findings)
  523. verification = verification_summary(findings, votes, votes_present=votes_raw is not None)
  524. shape = run_shape(coverage, coverage_source, effort, scan_root)
  525. stamp: dict[str, object] = {
  526. "generated_at": datetime.now(timezone.utc).replace(microsecond=0).isoformat(),
  527. "scan_id": str(scan.id),
  528. "mode": scan.mode,
  529. "scan_prefix": scan.prefix,
  530. "scope": list(scan.scope),
  531. "revision": revision,
  532. "revision_source": revision_source,
  533. "model": model,
  534. "effort": effort,
  535. "run_shape": shape,
  536. "findings": {
  537. "total": len(findings),
  538. "high": counts["HIGH"],
  539. "medium": counts["MEDIUM"],
  540. "low": counts["LOW"],
  541. },
  542. "verification": verification,
  543. }
  544. jsonl = jsonl_text(findings)
  545. run_properties = {k: v for k, v in stamp.items() if k != "model" or v is not None}
  546. panels = {
  547. f["id"]: panel for f in findings if (panel := panel_complete(rounds_by_id.get(f["id"])))
  548. }
  549. unrecognized = unrecognized_cwes(findings)
  550. for text in merged + unrecognized:
  551. sys.stderr.write(f"render_report.py: {text}\n")
  552. symlinks = coverage_texts(meta.get("unfollowed_symlinks"), 200)
  553. notifications = notifications_of(shape, verification, merged, unrecognized, symlinks, revision)
  554. sarif_log = sarif.log(
  555. findings, scan, plugin.version(), run_properties, panels, sources, notifications
  556. )
  557. sarif_doc = strictjson.text(sarif_log, indent=2) + "\n"
  558. for stale in os.listdir(products_dir):
  559. if stale.startswith(REVISION_PREFIX) and stale.endswith(".json"):
  560. os.unlink(os.path.join(products_dir, stale))
  561. with open(os.path.join(products_dir, JSONL_NAME), "w", encoding="utf-8", newline="\n") as out:
  562. out.write(jsonl)
  563. with open(os.path.join(products_dir, SARIF_NAME), "w", encoding="utf-8", newline="\n") as out:
  564. out.write(sarif_doc)
  565. markdown_out = os.path.join(products_dir, "CLAUDE-SECURITY-RESULTS.md")
  566. relocated = os.path.realpath(markdown_path) != os.path.realpath(markdown_out)
  567. if relocated:
  568. with open(markdown_out, "w", encoding="utf-8", newline="\n") as out:
  569. out.write(markdown)
  570. stamp_path = os.path.join(products_dir, f"{REVISION_PREFIX}{tag}.json")
  571. with open(stamp_path, "w", encoding="utf-8", newline="\n") as out:
  572. out.write(strictjson.text(stamp, indent=2) + "\n")
  573. if relocated:
  574. os.unlink(markdown_path)
  575. return Rendered(findings, verification, tag)
  576. def remove_run_dir(run_dir: str, products_dir: str) -> str:
  577. """Remove the scan's run directory once rendered; returns a one-line status."""
  578. target = os.path.normpath(os.path.abspath(run_dir))
  579. if os.path.basename(target) != plugin.RUN_DIR_NAME:
  580. return f"kept {run_dir} (not a {plugin.RUN_DIR_NAME} run directory)"
  581. if os.path.realpath(target) == os.path.realpath(products_dir):
  582. return f"kept {run_dir} (it holds the products)"
  583. try:
  584. shutil.rmtree(target)
  585. except OSError as error:
  586. detail = console.removal_failure_detail(error)
  587. return f"WARNING: could not remove run directory {run_dir}: {detail}"
  588. return f"removed run directory {run_dir}"
  589. def argument_parser() -> argparse.ArgumentParser:
  590. """The command line: which run directory to render, and where its products go."""
  591. parser = argparse.ArgumentParser(
  592. prog="render_report.py",
  593. description="Render a scan's machine-readable artifacts from its run directory.",
  594. )
  595. parser.add_argument("run_dir", help="the run directory holding the scan's records")
  596. parser.add_argument(
  597. "--products-dir", help="where the products are written (default: the run directory)"
  598. )
  599. return parser
  600. def main(argv: list[str]) -> int:
  601. parser = argument_parser()
  602. args = parser.parse_args(argv, namespace=Args())
  603. if not os.path.isdir(args.run_dir):
  604. parser.error(f"not a directory: {args.run_dir}")
  605. products_dir = args.products_dir or args.run_dir
  606. if not os.path.isdir(products_dir):
  607. parser.error(f"products directory is not a directory: {products_dir}")
  608. try:
  609. rendered = render(args.run_dir, products_dir)
  610. except (RenderError, FindingError) as error:
  611. sys.stderr.write(f"render_report.py: {error}\n")
  612. return 1
  613. except OSError as error:
  614. sys.stderr.write(f"render_report.py: could not read or write the report's files: {error}\n")
  615. return 1
  616. removal = remove_run_dir(args.run_dir, products_dir)
  617. count = len(rendered.findings)
  618. stamp_name = f"{REVISION_PREFIX}{rendered.tag}.json"
  619. print(
  620. f"wrote {JSONL_NAME}, {SARIF_NAME} ({count} finding{'' if count == 1 else 's'}) "
  621. f"and {stamp_name} into {products_dir}"
  622. )
  623. print(f"stamp: {stamp_name}")
  624. print(f"verification.status: {rendered.verification['status']}")
  625. if reason := rendered.verification["reason"]:
  626. print(f"verification.reason: {reason}")
  627. print(removal)
  628. return 0
  629. if __name__ == "__main__":
  630. console.tolerate_undecodable_names()
  631. sys.exit(main(sys.argv[1:]))