sarif.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. """SARIF 2.1.0 for one scan: the log encoder."""
  2. from __future__ import annotations
  3. import hashlib
  4. import json
  5. import posixpath
  6. import re
  7. from dataclasses import dataclass
  8. from typing import TYPE_CHECKING, Final, NamedTuple
  9. from urllib.parse import quote
  10. from . import cwe, secret, source
  11. if TYPE_CHECKING:
  12. import uuid
  13. from collections.abc import Collection, Mapping, Sequence
  14. from .finding import Finding, Panel, Record
  15. SCHEMA_ID = (
  16. "https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json"
  17. )
  18. # The driver name GitHub keys alert identity on; renaming it orphans every open alert.
  19. TOOL_NAME = "Claude Security Plugin for Claude Code"
  20. TOOL_URI = "https://claude.com/product/claude-security"
  21. PROPERTY_BAG = "claudeSecurityPlugin"
  22. ID_PREFIX = "claude-security-plugin"
  23. FINDING_ID: Final = "claudeSecurityPluginFindingId"
  24. ID_VERSION = "v3"
  25. CONTEXT_LINES = 3
  26. SRCROOT = "%SRCROOT%"
  27. # error is SARIF's highest level, so CRITICAL and HIGH both map to it.
  28. LEVEL = {"CRITICAL": "error", "HIGH": "error", "MEDIUM": "warning", "LOW": "note"}
  29. @dataclass(frozen=True)
  30. class Scan:
  31. """The one scan a log describes: its identity, where it ran, and the repository it names."""
  32. id: uuid.UUID
  33. mode: str
  34. # The scan root below the repository top level, slash-terminated; "" when there is none.
  35. prefix: str
  36. # The credential-free https form of the repository's remote; None when there is not one.
  37. remote: str | None
  38. # The directories the scan was limited to, relative to the scan root; empty for all of it.
  39. scope: tuple[str, ...]
  40. # The commit the scanned tree was exactly at; None when it was dirty, unversioned or unknown.
  41. revision: str | None
  42. def log(
  43. findings: Sequence[Record],
  44. scan: Scan,
  45. tool_version: str | None,
  46. run_properties: Mapping[str, object],
  47. panels: Mapping[str, Panel],
  48. notifications: Sequence[Mapping[str, object]],
  49. ) -> dict[str, object]:
  50. """The SARIF 2.1.0 log for one scan: one run, one rule per category, one result per finding.
  51. `findings` are the records render_report built with placed(): each on the
  52. line its file places it on, carrying its id.
  53. """
  54. filed = [(item, category_of(item)) for item in findings]
  55. categories = list(dict.fromkeys(category for _, category in filed))
  56. index = {category: position for position, category in enumerate(categories)}
  57. driver: dict[str, object] = {
  58. "name": TOOL_NAME,
  59. "organization": "Anthropic",
  60. "informationUri": TOOL_URI,
  61. **({"version": tool_version} if tool_version else {}),
  62. "rules": [rule(category) for category in categories],
  63. }
  64. invocation: dict[str, object] = {"executionSuccessful": True}
  65. if notifications:
  66. invocation["toolExecutionNotifications"] = list(notifications)
  67. base_description = (
  68. "The top level of the scanned repository, or the scanned directory when the scan did "
  69. "not run inside a git checkout."
  70. )
  71. run: dict[str, object] = {
  72. "tool": {"driver": driver},
  73. "automationDetails": {"id": automation_id(scan), "guid": str(scan.id)},
  74. "invocations": [invocation],
  75. "originalUriBaseIds": {SRCROOT: {"description": {"text": base_description}}},
  76. "results": [
  77. result(item, category, index[category], scan, panels.get(item["id"]))
  78. for item, category in filed
  79. ],
  80. "properties": {
  81. PROPERTY_BAG: {
  82. **run_properties,
  83. "target_kind": "git-remote" if scan.remote else "local-path",
  84. }
  85. },
  86. }
  87. if scan.remote:
  88. provenance: dict[str, object] = {"repositoryUri": scan.remote}
  89. if scan.revision:
  90. provenance["revisionId"] = scan.revision
  91. run["versionControlProvenance"] = [provenance]
  92. return {"$schema": SCHEMA_ID, "version": "2.1.0", "runs": [run]}
  93. def automation_id(scan: Scan) -> str:
  94. """The run's automation id: the plugin prefix, the mode, the scan's extent if any, its id."""
  95. category = f"{ID_PREFIX}/{scan.mode}"
  96. if scan.scope:
  97. extent = ",".join(scan.prefix + entry.strip("/") for entry in sorted(scan.scope))
  98. else:
  99. extent = scan.prefix.rstrip("/")
  100. if extent:
  101. category += "/" + quote(uri_bytes(extent))
  102. return f"{category}/{scan.id}"
  103. def category_of(finding: Finding) -> cwe.Category | None:
  104. """The Simplified Mapping entry the finding's CWE rolls up to; None for Uncategorized."""
  105. return cwe.catalog.category(cwe.id_number(finding["cwe_id"]))
  106. def rule_id(category: cwe.Category | None) -> str:
  107. """A rule's id: its entry's CWE id (`CWE-89`), or `uncategorized`."""
  108. return category.id if category is not None else cwe.UNCATEGORIZED.lower()
  109. def rule(category: cwe.Category | None) -> dict[str, object]:
  110. """The reporting descriptor for one entry: the catalog's names, its page, its fixed tags."""
  111. help_text = (
  112. "Each alert's message names the finding's own CWE and states the impact, exploit "
  113. "scenario, preconditions and recommended fix; the finding appears under its F<n> id "
  114. "in CLAUDE-SECURITY-RESULTS.md."
  115. )
  116. if category is None:
  117. return {
  118. "id": rule_id(None),
  119. "name": cwe.UNCATEGORIZED,
  120. "shortDescription": {"text": cwe.UNCATEGORIZED},
  121. "fullDescription": {
  122. "text": "Findings whose CWE is not an entry of the CWE Simplified Mapping view "
  123. f"and rolls up to none, reported by {TOOL_NAME} from static review of the "
  124. "source."
  125. },
  126. "help": {"text": help_text},
  127. "properties": {"tags": ["security"]},
  128. }
  129. return {
  130. "id": category.id,
  131. "name": rule_name(category.name),
  132. "shortDescription": {"text": category.name},
  133. "fullDescription": {
  134. "text": f"{category.title} ({category.id}, CWE {cwe.catalog.version}): findings whose "
  135. f"CWE is this entry of the Simplified Mapping view or rolls up to it, reported by "
  136. f"{TOOL_NAME} from static review of the source."
  137. },
  138. "help": {"text": help_text},
  139. "helpUri": f"https://cwe.mitre.org/data/definitions/{category.number}.html",
  140. "properties": {"tags": ["security", f"external/cwe/cwe-{category.number}"]},
  141. }
  142. def placed(
  143. findings: Sequence[Finding],
  144. scan: Scan,
  145. sources: Mapping[str, str],
  146. *,
  147. refused_secrets: Collection[str],
  148. ) -> list[Record]:
  149. """Each finding as the products carry it: `line` moved to where its file places it, id added.
  150. `sources` is the text of each scanned file by the finding's `file`; a
  151. finding whose file is absent from it keeps its line and hashes the number.
  152. """
  153. texts = {repository_file(scan, file): text for file, text in sources.items()}
  154. secrets = {
  155. (path, line)
  156. for f in findings
  157. if secret.is_credential(f)
  158. for path, line in secret_lines(repository_path(scan, f), f, texts)
  159. } | {
  160. (path, line)
  161. for quote in refused_secrets
  162. for path, text in texts.items()
  163. for line in source.quoted_lines(text, quote, whole=False)
  164. }
  165. return [placed_one(f, scan, sources.get(f["file"]), secrets) for f in findings]
  166. def placed_one(
  167. finding: Finding, scan: Scan, text: str | None, secrets: Collection[tuple[str, int]]
  168. ) -> Record:
  169. """One finding placed in `text`, its file's content (None when unread), and given its id."""
  170. moved: Finding = finding
  171. code = None
  172. if text is not None:
  173. lines = source.normalized_lines(text)
  174. row = source.placing_row(lines, finding["line"], finding["snippet"])
  175. if row is not None:
  176. moved = {**finding, "line": row + 1}
  177. path = repository_path(scan, finding)
  178. near_secret = any(p == path and abs(row + 1 - n) <= CONTEXT_LINES for p, n in secrets)
  179. code = None if near_secret else code_at(lines, row)
  180. return {**moved, FINDING_ID: fingerprint(moved, scan, code)}
  181. def secret_lines(home: str, credential: Finding, texts: Mapping[str, str]) -> set[tuple[str, int]]:
  182. """Every (repository path, line) of `texts` on which the `credential` finding's quote occurs.
  183. `home` is the credential's own repository path and `texts` the scanned
  184. files by repository path. The quote is marked wherever it occurs in any
  185. file, with one restraint: a quote its own file shows only as part of a
  186. longer line is marked in the other files just where it is a whole line of
  187. theirs. The credential's own line is always marked, for one whose quote
  188. matched nothing or whose file was not read.
  189. """
  190. snippet = credential["snippet"]
  191. own = texts.get(home, "")
  192. found = bool(source.quoted_lines(own, snippet, whole=False))
  193. fragment = found and not source.quoted_lines(own, snippet, whole=True)
  194. return {
  195. (path, line)
  196. for path, text in texts.items()
  197. for line in source.quoted_lines(text, snippet, whole=fragment and path != home)
  198. } | {(home, credential["line"])}
  199. def fingerprint(finding: Finding, scan: Scan, code: str | None) -> str:
  200. """ID_VERSION, a colon, and a sha256 hex over the rule id, the repository path and `code`.
  201. `code` is the file's normalized lines around the placed one (code_at);
  202. None hashes the finding's line number in its place.
  203. """
  204. where: str | int = finding["line"] if code is None else code
  205. basis = [rule_id(category_of(finding)), repository_path(scan, finding), where]
  206. canonical = json.dumps(basis, separators=(",", ":"), ensure_ascii=True)
  207. return f"{ID_VERSION}:{hashlib.sha256(canonical.encode()).hexdigest()}"
  208. def code_at(lines: Sequence[str], row: int) -> str:
  209. """The normalized `lines` CONTEXT_LINES either side of `row`, joined; fewer at a file's ends."""
  210. return "\n".join(lines[max(row - CONTEXT_LINES, 0) : row + CONTEXT_LINES + 1])
  211. class Site(NamedTuple):
  212. """What one result stands for: a rule at a line of a file; the log holds one result per site."""
  213. rule: str
  214. path: str
  215. line: int
  216. def site(finding: Record, scan: Scan) -> Site | None:
  217. """The finding's site: its rule id, repository path and placed line; None for a line below 1."""
  218. if finding["line"] < 1:
  219. return None
  220. return Site(rule_id(category_of(finding)), repository_path(scan, finding), finding["line"])
  221. def result(
  222. finding: Record,
  223. category: cwe.Category | None,
  224. rule_index: int,
  225. scan: Scan,
  226. panel: Panel | None,
  227. ) -> dict[str, object]:
  228. """One result: the finding under its rule, its location, and its JSONL record, id included."""
  229. shown = secret.withheld(finding)
  230. record: dict[str, object] = {**shown}
  231. if panel is not None:
  232. record["verification"] = {"panel": panel}
  233. return {
  234. "ruleId": rule_id(category),
  235. "ruleIndex": rule_index,
  236. "level": LEVEL[finding["severity"]],
  237. "message": {"text": message(finding)},
  238. "locations": [location(shown, scan)],
  239. "properties": {PROPERTY_BAG: record},
  240. }
  241. def location(finding: Finding, scan: Scan) -> dict[str, object]:
  242. """A result's one location: the file relative to SRCROOT, the line, the snippet, the symbol.
  243. The line is the finding's placed line (placed()): where its quoted code
  244. sits in the file, or the line it declared when nothing places it.
  245. """
  246. line = finding["line"]
  247. region: dict[str, object] = {"startLine": max(line, 1)}
  248. if line >= 1 and finding["snippet"].strip():
  249. region["snippet"] = {"text": finding["snippet"]}
  250. place: dict[str, object] = {
  251. "physicalLocation": {
  252. "artifactLocation": {
  253. "uri": quote(uri_bytes(repository_path(scan, finding))),
  254. "uriBaseId": SRCROOT,
  255. },
  256. "region": region,
  257. }
  258. }
  259. if symbol := finding["symbol"].strip():
  260. place["logicalLocations"] = [{"name": symbol, "fullyQualifiedName": symbol}]
  261. return place
  262. def repository_path(scan: Scan, finding: Finding) -> str:
  263. """The finding's file relative to the repository top level, with any leading climb folded."""
  264. return repository_file(scan, finding["file"])
  265. def repository_file(scan: Scan, file: str) -> str:
  266. """A scan-root-relative `file`, as file_field carries it, made repository-relative."""
  267. return posixpath.normpath(scan.prefix + file)
  268. def uri_bytes(text: str) -> bytes:
  269. """`text` as the bytes its uri must name; byte-faithful for a filesystem name."""
  270. try:
  271. return text.encode("utf-8", "surrogateescape")
  272. except UnicodeEncodeError:
  273. return text.encode("utf-8", "surrogatepass")
  274. def notification(descriptor_id: str, level: str, text: str) -> dict[str, object]:
  275. """One invocation notification: its namespaced descriptor id, its level, and its message."""
  276. return {"descriptor": {"id": descriptor_id}, "level": level, "message": {"text": text}}
  277. def message(finding: Finding) -> str:
  278. """A result's message: the finding's prose, its stated parts labeled, then its ratings."""
  279. parts = [sentence(finding["title"]), sentence(finding["description"])]
  280. labeled = (
  281. ("Impact", finding["impact"]),
  282. ("Exploit scenario", finding["exploit_scenario"]),
  283. ("Preconditions", "; ".join(finding["preconditions"])),
  284. ("Recommendation", finding["recommendation"]),
  285. )
  286. parts += [f"{label}: {text}" for label, value in labeled if (text := sentence(value))]
  287. if finding["line"] < 1:
  288. parts.append("The exact line was not determined; see the description.")
  289. if secret.is_credential(finding):
  290. parts.append("The source line is not quoted because it holds the credential.")
  291. parts.append(
  292. f"{finding['cwe_id']}. Severity {finding['severity']}, confidence {finding['confidence']}."
  293. )
  294. return "\n\n".join(parts)
  295. def rule_name(category: str) -> str:
  296. """A category's common name in PascalCase, for a rule name: `SQLInjection`."""
  297. return "".join(word[:1].upper() + word[1:] for word in re.split(r"[^A-Za-z0-9]+", category))
  298. def sentence(text: str) -> str:
  299. """`text` stripped and closed with a period unless it already ends in punctuation."""
  300. text = text.strip()
  301. return text if not text or text[-1] in ".!?" else text + "."