summaryrefslogtreecommitdiff
path: root/plugins/claude-security/scripts
diff options
context:
space:
mode:
authorBryan Thompson <238056179+bryan-anthropic@users.noreply.github.com>2026-08-20 14:50:15 -0500
committerGitHub <noreply@github.com>2026-08-20 14:50:15 -0500
commit240c4b2c17b19868e880f2bfdd909144d67224e1 (patch)
tree9deddb18cf7c137cd56c93b25763eba0946a7ad6 /plugins/claude-security/scripts
parent81182783dc1c15ae70e8a489b1edee5450b1434d (diff)
parentcbe94d02bc8ea7375e13b39cc400e17eeabfcbee (diff)
Merge branch 'main' into add-unleashadd-unleash
Diffstat (limited to 'plugins/claude-security/scripts')
-rwxr-xr-xplugins/claude-security/scripts/keep-waiting.sh14
-rw-r--r--plugins/claude-security/scripts/lib/__init__.py1
-rw-r--r--plugins/claude-security/scripts/lib/absolute.py15
-rw-r--r--plugins/claude-security/scripts/lib/console.py17
-rw-r--r--plugins/claude-security/scripts/lib/cwe-categories.json1473
-rw-r--r--plugins/claude-security/scripts/lib/cwe.py79
-rw-r--r--plugins/claude-security/scripts/lib/finding.py241
-rw-r--r--plugins/claude-security/scripts/lib/plugin.py31
-rw-r--r--plugins/claude-security/scripts/lib/sarif.py373
-rw-r--r--plugins/claude-security/scripts/lib/secret.py32
-rw-r--r--plugins/claude-security/scripts/lib/strictjson.py83
-rwxr-xr-xplugins/claude-security/scripts/patch_artifacts.py136
-rwxr-xr-xplugins/claude-security/scripts/render_report.py995
-rwxr-xr-xplugins/claude-security/scripts/write_scan_meta.py284
14 files changed, 3159 insertions, 615 deletions
diff --git a/plugins/claude-security/scripts/keep-waiting.sh b/plugins/claude-security/scripts/keep-waiting.sh
new file mode 100755
index 00000000..8920d36d
--- /dev/null
+++ b/plugins/claude-security/scripts/keep-waiting.sh
@@ -0,0 +1,14 @@
+#!/usr/bin/env bash
+# Hold the Security Lead's turn while the scan workflow runs: wait, then tell the
+# caller to run this again unless the workflow's result has arrived.
+# Usage: keep-waiting.sh [SECONDS] (1 to 110, default 90; exits 2 on a bad value)
+# The wait stays under the Bash tool's two-minute default timeout, past which a
+# command is moved to the background and no longer holds the turn.
+seconds=${1:-90}
+case $seconds in '' | *[!0-9]*) seconds=0 ;; esac
+if [ "$seconds" -lt 1 ] || [ "$seconds" -gt 110 ]; then
+ echo "usage: keep-waiting.sh [SECONDS between 1 and 110]" >&2
+ exit 2
+fi
+sleep "$seconds"
+echo "Waited ${seconds}s. If the scan workflow's result has not arrived, run this same command again now; do not reply to the user or end your turn until it arrives."
diff --git a/plugins/claude-security/scripts/lib/__init__.py b/plugins/claude-security/scripts/lib/__init__.py
new file mode 100644
index 00000000..c180a3d7
--- /dev/null
+++ b/plugins/claude-security/scripts/lib/__init__.py
@@ -0,0 +1 @@
+"""Modules shared by the plugin's entry-point scripts."""
diff --git a/plugins/claude-security/scripts/lib/absolute.py b/plugins/claude-security/scripts/lib/absolute.py
new file mode 100644
index 00000000..45f28ed3
--- /dev/null
+++ b/plugins/claude-security/scripts/lib/absolute.py
@@ -0,0 +1,15 @@
+"""Whether a path is spelled absolutely on any platform, not just the one this runs on."""
+
+from __future__ import annotations
+
+import ntpath
+import os
+
+
+def spelled(path: str) -> bool:
+ """True for a rooted, drive-qualified or UNC path, whichever platform reads it."""
+ return (
+ os.path.isabs(path)
+ or path.replace("\\", "/").startswith("/")
+ or bool(ntpath.splitdrive(path)[0])
+ )
diff --git a/plugins/claude-security/scripts/lib/console.py b/plugins/claude-security/scripts/lib/console.py
new file mode 100644
index 00000000..b8a32635
--- /dev/null
+++ b/plugins/claude-security/scripts/lib/console.py
@@ -0,0 +1,17 @@
+"""The scripts' console output: undecodable path names and failed removals, printed readably."""
+
+from __future__ import annotations
+
+import io
+import sys
+
+
+def tolerate_undecodable_names() -> None:
+ """Make stdout print an undecodable path name as escapes instead of raising."""
+ if isinstance(sys.stdout, io.TextIOWrapper):
+ sys.stdout.reconfigure(errors="backslashreplace")
+
+
+def removal_failure_detail(error: OSError) -> object:
+ """The operator-readable reason a tree removal failed."""
+ return str(error) if error.strerror or not error.args else error.args[0]
diff --git a/plugins/claude-security/scripts/lib/cwe-categories.json b/plugins/claude-security/scripts/lib/cwe-categories.json
new file mode 100644
index 00000000..de95259d
--- /dev/null
+++ b/plugins/claude-security/scripts/lib/cwe-categories.json
@@ -0,0 +1,1473 @@
+{
+ "source": "Derived mechanically from the CWE 4.20 catalog published at https://cwe.mitre.org/.",
+ "cwe_version": "4.20",
+ "view": "CWE-1003",
+ "categories": {
+ "20": {
+ "name": "Improper Input Validation",
+ "title": "Improper Input Validation"
+ },
+ "22": {
+ "name": "Path Traversal",
+ "title": "Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')"
+ },
+ "59": {
+ "name": "Link Following",
+ "title": "Improper Link Resolution Before File Access ('Link Following')"
+ },
+ "74": {
+ "name": "Injection",
+ "title": "Improper Neutralization of Special Elements in Output Used by a Downstream Component ('Injection')"
+ },
+ "77": {
+ "name": "Command Injection",
+ "title": "Improper Neutralization of Special Elements used in a Command ('Command Injection')"
+ },
+ "78": {
+ "name": "OS Command Injection",
+ "title": "Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection')"
+ },
+ "79": {
+ "name": "Cross-site Scripting",
+ "title": "Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting')"
+ },
+ "88": {
+ "name": "Argument Injection",
+ "title": "Improper Neutralization of Argument Delimiters in a Command ('Argument Injection')"
+ },
+ "89": {
+ "name": "SQL Injection",
+ "title": "Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection')"
+ },
+ "91": {
+ "name": "XML Injection (aka Blind XPath Injection)",
+ "title": "XML Injection (aka Blind XPath Injection)"
+ },
+ "94": {
+ "name": "Code Injection",
+ "title": "Improper Control of Generation of Code ('Code Injection')"
+ },
+ "116": {
+ "name": "Improper Encoding or Escaping of Output",
+ "title": "Improper Encoding or Escaping of Output"
+ },
+ "119": {
+ "name": "Improper Restriction of Operations within the Bounds of a Memory Buffer",
+ "title": "Improper Restriction of Operations within the Bounds of a Memory Buffer"
+ },
+ "120": {
+ "name": "Classic Buffer Overflow",
+ "title": "Buffer Copy without Checking Size of Input ('Classic Buffer Overflow')"
+ },
+ "125": {
+ "name": "Out-of-bounds Read",
+ "title": "Out-of-bounds Read"
+ },
+ "129": {
+ "name": "Improper Validation of Array Index",
+ "title": "Improper Validation of Array Index"
+ },
+ "131": {
+ "name": "Incorrect Calculation of Buffer Size",
+ "title": "Incorrect Calculation of Buffer Size"
+ },
+ "134": {
+ "name": "Use of Externally-Controlled Format String",
+ "title": "Use of Externally-Controlled Format String"
+ },
+ "178": {
+ "name": "Improper Handling of Case Sensitivity",
+ "title": "Improper Handling of Case Sensitivity"
+ },
+ "190": {
+ "name": "Integer Overflow or Wraparound",
+ "title": "Integer Overflow or Wraparound"
+ },
+ "191": {
+ "name": "Integer Underflow (Wrap or Wraparound)",
+ "title": "Integer Underflow (Wrap or Wraparound)"
+ },
+ "193": {
+ "name": "Off-by-one Error",
+ "title": "Off-by-one Error"
+ },
+ "200": {
+ "name": "Exposure of Sensitive Information to an Unauthorized Actor",
+ "title": "Exposure of Sensitive Information to an Unauthorized Actor"
+ },
+ "203": {
+ "name": "Observable Discrepancy",
+ "title": "Observable Discrepancy"
+ },
+ "209": {
+ "name": "Generation of Error Message Containing Sensitive Information",
+ "title": "Generation of Error Message Containing Sensitive Information"
+ },
+ "212": {
+ "name": "Improper Removal of Sensitive Information Before Storage or Transfer",
+ "title": "Improper Removal of Sensitive Information Before Storage or Transfer"
+ },
+ "252": {
+ "name": "Unchecked Return Value",
+ "title": "Unchecked Return Value"
+ },
+ "269": {
+ "name": "Improper Privilege Management",
+ "title": "Improper Privilege Management"
+ },
+ "273": {
+ "name": "Improper Check for Dropped Privileges",
+ "title": "Improper Check for Dropped Privileges"
+ },
+ "276": {
+ "name": "Incorrect Default Permissions",
+ "title": "Incorrect Default Permissions"
+ },
+ "281": {
+ "name": "Improper Preservation of Permissions",
+ "title": "Improper Preservation of Permissions"
+ },
+ "287": {
+ "name": "Improper Authentication",
+ "title": "Improper Authentication"
+ },
+ "290": {
+ "name": "Authentication Bypass by Spoofing",
+ "title": "Authentication Bypass by Spoofing"
+ },
+ "294": {
+ "name": "Authentication Bypass by Capture-replay",
+ "title": "Authentication Bypass by Capture-replay"
+ },
+ "295": {
+ "name": "Improper Certificate Validation",
+ "title": "Improper Certificate Validation"
+ },
+ "306": {
+ "name": "Missing Authentication for Critical Function",
+ "title": "Missing Authentication for Critical Function"
+ },
+ "307": {
+ "name": "Improper Restriction of Excessive Authentication Attempts",
+ "title": "Improper Restriction of Excessive Authentication Attempts"
+ },
+ "311": {
+ "name": "Missing Encryption of Sensitive Data",
+ "title": "Missing Encryption of Sensitive Data"
+ },
+ "312": {
+ "name": "Cleartext Storage of Sensitive Information",
+ "title": "Cleartext Storage of Sensitive Information"
+ },
+ "319": {
+ "name": "Cleartext Transmission of Sensitive Information",
+ "title": "Cleartext Transmission of Sensitive Information"
+ },
+ "326": {
+ "name": "Inadequate Encryption Strength",
+ "title": "Inadequate Encryption Strength"
+ },
+ "327": {
+ "name": "Use of a Broken or Risky Cryptographic Algorithm",
+ "title": "Use of a Broken or Risky Cryptographic Algorithm"
+ },
+ "330": {
+ "name": "Use of Insufficiently Random Values",
+ "title": "Use of Insufficiently Random Values"
+ },
+ "331": {
+ "name": "Insufficient Entropy",
+ "title": "Insufficient Entropy"
+ },
+ "335": {
+ "name": "Incorrect Usage of Seeds in Pseudo-Random Number Generator (PRNG)",
+ "title": "Incorrect Usage of Seeds in Pseudo-Random Number Generator (PRNG)"
+ },
+ "338": {
+ "name": "Use of Cryptographically Weak Pseudo-Random Number Generator (PRNG)",
+ "title": "Use of Cryptographically Weak Pseudo-Random Number Generator (PRNG)"
+ },
+ "345": {
+ "name": "Insufficient Verification of Data Authenticity",
+ "title": "Insufficient Verification of Data Authenticity"
+ },
+ "346": {
+ "name": "Origin Validation Error",
+ "title": "Origin Validation Error"
+ },
+ "347": {
+ "name": "Improper Verification of Cryptographic Signature",
+ "title": "Improper Verification of Cryptographic Signature"
+ },
+ "352": {
+ "name": "Cross-Site Request Forgery (CSRF)",
+ "title": "Cross-Site Request Forgery (CSRF)"
+ },
+ "354": {
+ "name": "Improper Validation of Integrity Check Value",
+ "title": "Improper Validation of Integrity Check Value"
+ },
+ "362": {
+ "name": "Race Condition",
+ "title": "Concurrent Execution using Shared Resource with Improper Synchronization ('Race Condition')"
+ },
+ "367": {
+ "name": "Time-of-check Time-of-use (TOCTOU) Race Condition",
+ "title": "Time-of-check Time-of-use (TOCTOU) Race Condition"
+ },
+ "369": {
+ "name": "Divide By Zero",
+ "title": "Divide By Zero"
+ },
+ "384": {
+ "name": "Session Fixation",
+ "title": "Session Fixation"
+ },
+ "400": {
+ "name": "Uncontrolled Resource Consumption",
+ "title": "Uncontrolled Resource Consumption"
+ },
+ "401": {
+ "name": "Missing Release of Memory after Effective Lifetime",
+ "title": "Missing Release of Memory after Effective Lifetime"
+ },
+ "404": {
+ "name": "Improper Resource Shutdown or Release",
+ "title": "Improper Resource Shutdown or Release"
+ },
+ "407": {
+ "name": "Inefficient Algorithmic Complexity",
+ "title": "Inefficient Algorithmic Complexity"
+ },
+ "415": {
+ "name": "Double Free",
+ "title": "Double Free"
+ },
+ "416": {
+ "name": "Use After Free",
+ "title": "Use After Free"
+ },
+ "425": {
+ "name": "Forced Browsing",
+ "title": "Direct Request ('Forced Browsing')"
+ },
+ "426": {
+ "name": "Untrusted Search Path",
+ "title": "Untrusted Search Path"
+ },
+ "427": {
+ "name": "Uncontrolled Search Path Element",
+ "title": "Uncontrolled Search Path Element"
+ },
+ "428": {
+ "name": "Unquoted Search Path or Element",
+ "title": "Unquoted Search Path or Element"
+ },
+ "434": {
+ "name": "Unrestricted Upload of File with Dangerous Type",
+ "title": "Unrestricted Upload of File with Dangerous Type"
+ },
+ "436": {
+ "name": "Interpretation Conflict",
+ "title": "Interpretation Conflict"
+ },
+ "444": {
+ "name": "HTTP Request/Response Smuggling",
+ "title": "Inconsistent Interpretation of HTTP Requests ('HTTP Request/Response Smuggling')"
+ },
+ "459": {
+ "name": "Incomplete Cleanup",
+ "title": "Incomplete Cleanup"
+ },
+ "470": {
+ "name": "Unsafe Reflection",
+ "title": "Use of Externally-Controlled Input to Select Classes or Code ('Unsafe Reflection')"
+ },
+ "476": {
+ "name": "NULL Pointer Dereference",
+ "title": "NULL Pointer Dereference"
+ },
+ "494": {
+ "name": "Download of Code Without Integrity Check",
+ "title": "Download of Code Without Integrity Check"
+ },
+ "502": {
+ "name": "Deserialization of Untrusted Data",
+ "title": "Deserialization of Untrusted Data"
+ },
+ "521": {
+ "name": "Weak Password Requirements",
+ "title": "Weak Password Requirements"
+ },
+ "522": {
+ "name": "Insufficiently Protected Credentials",
+ "title": "Insufficiently Protected Credentials"
+ },
+ "532": {
+ "name": "Insertion of Sensitive Information into Log File",
+ "title": "Insertion of Sensitive Information into Log File"
+ },
+ "552": {
+ "name": "Files or Directories Accessible to External Parties",
+ "title": "Files or Directories Accessible to External Parties"
+ },
+ "565": {
+ "name": "Reliance on Cookies without Validation and Integrity Checking",
+ "title": "Reliance on Cookies without Validation and Integrity Checking"
+ },
+ "601": {
+ "name": "Open Redirect",
+ "title": "URL Redirection to Untrusted Site ('Open Redirect')"
+ },
+ "610": {
+ "name": "Externally Controlled Reference to a Resource in Another Sphere",
+ "title": "Externally Controlled Reference to a Resource in Another Sphere"
+ },
+ "611": {
+ "name": "Improper Restriction of XML External Entity Reference",
+ "title": "Improper Restriction of XML External Entity Reference"
+ },
+ "613": {
+ "name": "Insufficient Session Expiration",
+ "title": "Insufficient Session Expiration"
+ },
+ "617": {
+ "name": "Reachable Assertion",
+ "title": "Reachable Assertion"
+ },
+ "639": {
+ "name": "Authorization Bypass Through User-Controlled Key",
+ "title": "Authorization Bypass Through User-Controlled Key"
+ },
+ "640": {
+ "name": "Weak Password Recovery Mechanism for Forgotten Password",
+ "title": "Weak Password Recovery Mechanism for Forgotten Password"
+ },
+ "662": {
+ "name": "Improper Synchronization",
+ "title": "Improper Synchronization"
+ },
+ "665": {
+ "name": "Improper Initialization",
+ "title": "Improper Initialization"
+ },
+ "667": {
+ "name": "Improper Locking",
+ "title": "Improper Locking"
+ },
+ "668": {
+ "name": "Exposure of Resource to Wrong Sphere",
+ "title": "Exposure of Resource to Wrong Sphere"
+ },
+ "669": {
+ "name": "Incorrect Resource Transfer Between Spheres",
+ "title": "Incorrect Resource Transfer Between Spheres"
+ },
+ "670": {
+ "name": "Always-Incorrect Control Flow Implementation",
+ "title": "Always-Incorrect Control Flow Implementation"
+ },
+ "672": {
+ "name": "Operation on a Resource after Expiration or Release",
+ "title": "Operation on a Resource after Expiration or Release"
+ },
+ "674": {
+ "name": "Uncontrolled Recursion",
+ "title": "Uncontrolled Recursion"
+ },
+ "681": {
+ "name": "Incorrect Conversion between Numeric Types",
+ "title": "Incorrect Conversion between Numeric Types"
+ },
+ "682": {
+ "name": "Incorrect Calculation",
+ "title": "Incorrect Calculation"
+ },
+ "697": {
+ "name": "Incorrect Comparison",
+ "title": "Incorrect Comparison"
+ },
+ "704": {
+ "name": "Incorrect Type Conversion or Cast",
+ "title": "Incorrect Type Conversion or Cast"
+ },
+ "706": {
+ "name": "Use of Incorrectly-Resolved Name or Reference",
+ "title": "Use of Incorrectly-Resolved Name or Reference"
+ },
+ "732": {
+ "name": "Incorrect Permission Assignment for Critical Resource",
+ "title": "Incorrect Permission Assignment for Critical Resource"
+ },
+ "754": {
+ "name": "Improper Check for Unusual or Exceptional Conditions",
+ "title": "Improper Check for Unusual or Exceptional Conditions"
+ },
+ "755": {
+ "name": "Improper Handling of Exceptional Conditions",
+ "title": "Improper Handling of Exceptional Conditions"
+ },
+ "763": {
+ "name": "Release of Invalid Pointer or Reference",
+ "title": "Release of Invalid Pointer or Reference"
+ },
+ "770": {
+ "name": "Allocation of Resources Without Limits or Throttling",
+ "title": "Allocation of Resources Without Limits or Throttling"
+ },
+ "772": {
+ "name": "Missing Release of Resource after Effective Lifetime",
+ "title": "Missing Release of Resource after Effective Lifetime"
+ },
+ "776": {
+ "name": "XML Entity Expansion",
+ "title": "Improper Restriction of Recursive Entity References in DTDs ('XML Entity Expansion')"
+ },
+ "787": {
+ "name": "Out-of-bounds Write",
+ "title": "Out-of-bounds Write"
+ },
+ "798": {
+ "name": "Use of Hard-coded Credentials",
+ "title": "Use of Hard-coded Credentials"
+ },
+ "824": {
+ "name": "Access of Uninitialized Pointer",
+ "title": "Access of Uninitialized Pointer"
+ },
+ "829": {
+ "name": "Inclusion of Functionality from Untrusted Control Sphere",
+ "title": "Inclusion of Functionality from Untrusted Control Sphere"
+ },
+ "834": {
+ "name": "Excessive Iteration",
+ "title": "Excessive Iteration"
+ },
+ "835": {
+ "name": "Infinite Loop",
+ "title": "Loop with Unreachable Exit Condition ('Infinite Loop')"
+ },
+ "838": {
+ "name": "Inappropriate Encoding for Output Context",
+ "title": "Inappropriate Encoding for Output Context"
+ },
+ "843": {
+ "name": "Type Confusion",
+ "title": "Access of Resource Using Incompatible Type ('Type Confusion')"
+ },
+ "862": {
+ "name": "Missing Authorization",
+ "title": "Missing Authorization"
+ },
+ "863": {
+ "name": "Incorrect Authorization",
+ "title": "Incorrect Authorization"
+ },
+ "908": {
+ "name": "Use of Uninitialized Resource",
+ "title": "Use of Uninitialized Resource"
+ },
+ "909": {
+ "name": "Missing Initialization of Resource",
+ "title": "Missing Initialization of Resource"
+ },
+ "913": {
+ "name": "Improper Control of Dynamically-Managed Code Resources",
+ "title": "Improper Control of Dynamically-Managed Code Resources"
+ },
+ "916": {
+ "name": "Use of Password Hash With Insufficient Computational Effort",
+ "title": "Use of Password Hash With Insufficient Computational Effort"
+ },
+ "917": {
+ "name": "Expression Language Injection",
+ "title": "Improper Neutralization of Special Elements used in an Expression Language Statement ('Expression Language Injection')"
+ },
+ "918": {
+ "name": "Server-Side Request Forgery (SSRF)",
+ "title": "Server-Side Request Forgery (SSRF)"
+ },
+ "920": {
+ "name": "Improper Restriction of Power Consumption",
+ "title": "Improper Restriction of Power Consumption"
+ },
+ "922": {
+ "name": "Insecure Storage of Sensitive Information",
+ "title": "Insecure Storage of Sensitive Information"
+ },
+ "924": {
+ "name": "Improper Enforcement of Message Integrity During Transmission in a Communication Channel",
+ "title": "Improper Enforcement of Message Integrity During Transmission in a Communication Channel"
+ },
+ "1021": {
+ "name": "Improper Restriction of Rendered UI Layers or Frames",
+ "title": "Improper Restriction of Rendered UI Layers or Frames"
+ },
+ "1188": {
+ "name": "Initialization of a Resource with an Insecure Default",
+ "title": "Initialization of a Resource with an Insecure Default"
+ },
+ "1236": {
+ "name": "Improper Neutralization of Formula Elements in a CSV File",
+ "title": "Improper Neutralization of Formula Elements in a CSV File"
+ },
+ "1284": {
+ "name": "Improper Validation of Specified Quantity in Input",
+ "title": "Improper Validation of Specified Quantity in Input"
+ },
+ "1321": {
+ "name": "Prototype Pollution",
+ "title": "Improperly Controlled Modification of Object Prototype Attributes ('Prototype Pollution')"
+ },
+ "1333": {
+ "name": "Inefficient Regular Expression Complexity",
+ "title": "Inefficient Regular Expression Complexity"
+ }
+ },
+ "category_of": {
+ "5": 319,
+ "6": 330,
+ "7": 755,
+ "8": 668,
+ "9": 269,
+ "11": null,
+ "12": 755,
+ "13": 522,
+ "14": null,
+ "15": 610,
+ "20": 20,
+ "22": 22,
+ "23": 22,
+ "24": 22,
+ "25": 22,
+ "26": 22,
+ "27": 22,
+ "28": 22,
+ "29": 22,
+ "30": 22,
+ "31": 22,
+ "32": 22,
+ "33": 22,
+ "34": 22,
+ "35": 22,
+ "36": 22,
+ "37": 22,
+ "38": 22,
+ "39": 22,
+ "40": 22,
+ "41": 706,
+ "42": 706,
+ "43": 706,
+ "44": 706,
+ "45": 706,
+ "46": 706,
+ "47": 706,
+ "48": 706,
+ "49": 706,
+ "50": 706,
+ "51": 706,
+ "52": 706,
+ "53": 706,
+ "54": 706,
+ "55": 706,
+ "56": 706,
+ "57": 706,
+ "58": 706,
+ "59": 59,
+ "61": 59,
+ "62": 59,
+ "64": 59,
+ "65": 59,
+ "66": 706,
+ "67": 706,
+ "69": 706,
+ "72": 706,
+ "73": 610,
+ "74": 74,
+ "75": 74,
+ "76": 74,
+ "77": 77,
+ "78": 78,
+ "79": 79,
+ "80": 79,
+ "81": 79,
+ "82": 79,
+ "83": 79,
+ "84": 79,
+ "85": 79,
+ "86": 79,
+ "87": 79,
+ "88": 88,
+ "89": 89,
+ "90": 74,
+ "91": 91,
+ "93": 74,
+ "94": 94,
+ "95": 94,
+ "96": 94,
+ "97": 94,
+ "98": 829,
+ "99": 74,
+ "102": 20,
+ "103": null,
+ "104": null,
+ "105": 20,
+ "106": 20,
+ "107": null,
+ "108": 20,
+ "109": 20,
+ "110": null,
+ "111": null,
+ "112": 20,
+ "113": 436,
+ "114": 610,
+ "115": 436,
+ "116": 116,
+ "117": 116,
+ "118": null,
+ "119": 119,
+ "120": 120,
+ "121": 787,
+ "122": 787,
+ "123": 787,
+ "124": 787,
+ "125": 125,
+ "126": 125,
+ "127": 125,
+ "128": 682,
+ "129": 129,
+ "130": null,
+ "131": 131,
+ "134": 134,
+ "135": 682,
+ "138": null,
+ "140": null,
+ "141": null,
+ "142": null,
+ "143": null,
+ "144": null,
+ "145": null,
+ "146": null,
+ "147": 436,
+ "148": null,
+ "149": null,
+ "150": null,
+ "151": null,
+ "152": null,
+ "153": null,
+ "154": null,
+ "155": 706,
+ "156": null,
+ "157": null,
+ "158": null,
+ "159": null,
+ "160": 22,
+ "161": 706,
+ "162": 706,
+ "163": 706,
+ "164": 706,
+ "165": 706,
+ "166": null,
+ "167": null,
+ "168": null,
+ "170": null,
+ "172": null,
+ "173": null,
+ "174": null,
+ "175": null,
+ "176": null,
+ "177": null,
+ "178": 178,
+ "179": 20,
+ "180": 20,
+ "181": 20,
+ "182": null,
+ "183": 697,
+ "184": 697,
+ "185": 697,
+ "186": 697,
+ "187": 697,
+ "188": null,
+ "190": 190,
+ "191": 191,
+ "192": 681,
+ "193": 193,
+ "194": 681,
+ "195": 681,
+ "196": 681,
+ "197": 681,
+ "198": null,
+ "200": 200,
+ "201": 200,
+ "202": null,
+ "203": 203,
+ "204": 203,
+ "205": 203,
+ "206": 203,
+ "207": 203,
+ "208": 203,
+ "209": 209,
+ "210": 209,
+ "211": 209,
+ "212": 212,
+ "213": 200,
+ "214": 200,
+ "215": 200,
+ "219": 552,
+ "220": 552,
+ "221": 755,
+ "222": null,
+ "223": null,
+ "224": null,
+ "226": 459,
+ "228": null,
+ "229": null,
+ "230": null,
+ "231": null,
+ "232": null,
+ "233": null,
+ "234": null,
+ "235": null,
+ "236": null,
+ "237": null,
+ "238": null,
+ "239": null,
+ "240": null,
+ "241": null,
+ "242": null,
+ "243": 669,
+ "244": 459,
+ "245": null,
+ "246": null,
+ "248": 755,
+ "250": 269,
+ "252": 252,
+ "253": 754,
+ "256": 522,
+ "257": 522,
+ "258": 521,
+ "259": 798,
+ "260": 522,
+ "261": 522,
+ "262": 287,
+ "263": 287,
+ "266": 269,
+ "267": 269,
+ "268": 269,
+ "269": 269,
+ "270": 269,
+ "271": 269,
+ "272": 269,
+ "273": 273,
+ "274": 755,
+ "276": 276,
+ "277": 732,
+ "278": 732,
+ "279": 732,
+ "280": 755,
+ "281": 281,
+ "282": null,
+ "283": null,
+ "284": null,
+ "285": null,
+ "286": null,
+ "287": 287,
+ "288": 306,
+ "289": 287,
+ "290": 290,
+ "291": 290,
+ "293": 290,
+ "294": 294,
+ "295": 295,
+ "296": 295,
+ "297": 295,
+ "298": 295,
+ "299": 295,
+ "300": null,
+ "301": 287,
+ "302": 287,
+ "303": 287,
+ "304": 287,
+ "305": 287,
+ "306": 306,
+ "307": 307,
+ "308": 287,
+ "309": 287,
+ "311": 311,
+ "312": 312,
+ "313": 312,
+ "314": 312,
+ "315": 312,
+ "316": 312,
+ "317": 312,
+ "318": 312,
+ "319": 319,
+ "321": 798,
+ "322": 306,
+ "323": 330,
+ "324": 672,
+ "325": 327,
+ "326": 326,
+ "327": 327,
+ "328": 327,
+ "329": 330,
+ "330": 330,
+ "331": 331,
+ "332": 331,
+ "333": 331,
+ "334": 330,
+ "335": 335,
+ "336": 335,
+ "337": 335,
+ "338": 338,
+ "339": 335,
+ "340": 330,
+ "341": 330,
+ "342": 330,
+ "343": 330,
+ "344": 330,
+ "345": 345,
+ "346": 346,
+ "347": 347,
+ "348": 345,
+ "349": 345,
+ "350": 290,
+ "351": 345,
+ "352": 352,
+ "353": 345,
+ "354": 354,
+ "356": null,
+ "357": null,
+ "358": null,
+ "359": 200,
+ "360": 345,
+ "362": 362,
+ "363": 367,
+ "364": 362,
+ "366": 362,
+ "367": 367,
+ "368": 362,
+ "369": 369,
+ "370": 295,
+ "372": null,
+ "374": 668,
+ "375": 668,
+ "377": 668,
+ "378": 668,
+ "379": 668,
+ "382": null,
+ "383": null,
+ "384": 384,
+ "385": null,
+ "386": 706,
+ "390": 755,
+ "391": 754,
+ "392": 755,
+ "393": null,
+ "394": 754,
+ "395": 755,
+ "396": 755,
+ "397": null,
+ "400": 400,
+ "401": 401,
+ "402": 668,
+ "403": 668,
+ "404": 404,
+ "405": 400,
+ "406": 400,
+ "407": 407,
+ "408": 400,
+ "409": 400,
+ "410": null,
+ "412": 667,
+ "413": 667,
+ "414": 667,
+ "415": 415,
+ "416": 416,
+ "419": null,
+ "420": 306,
+ "421": 362,
+ "422": 345,
+ "424": 862,
+ "425": 425,
+ "426": 426,
+ "427": 427,
+ "428": 428,
+ "430": null,
+ "431": null,
+ "432": 362,
+ "433": 552,
+ "434": 434,
+ "435": null,
+ "436": 436,
+ "437": 436,
+ "439": null,
+ "440": 665,
+ "441": 610,
+ "444": 444,
+ "446": null,
+ "447": null,
+ "448": null,
+ "449": null,
+ "450": null,
+ "451": 1021,
+ "453": 1188,
+ "454": 665,
+ "455": 665,
+ "456": 909,
+ "457": 908,
+ "459": 459,
+ "460": 459,
+ "462": 74,
+ "463": null,
+ "464": null,
+ "466": 119,
+ "467": 131,
+ "468": 682,
+ "469": 682,
+ "470": 470,
+ "471": 668,
+ "472": 668,
+ "473": null,
+ "474": null,
+ "475": null,
+ "476": 476,
+ "477": null,
+ "478": 697,
+ "479": 662,
+ "480": 670,
+ "481": 670,
+ "482": 670,
+ "483": 670,
+ "484": 670,
+ "486": 697,
+ "487": null,
+ "488": 668,
+ "489": null,
+ "491": 668,
+ "492": 668,
+ "493": 668,
+ "494": 494,
+ "495": null,
+ "496": null,
+ "497": 200,
+ "498": 668,
+ "499": 668,
+ "500": 668,
+ "501": null,
+ "502": 502,
+ "506": null,
+ "507": null,
+ "508": null,
+ "509": null,
+ "510": null,
+ "511": null,
+ "512": null,
+ "514": null,
+ "515": null,
+ "520": 269,
+ "521": 521,
+ "522": 522,
+ "523": 522,
+ "524": 668,
+ "525": 668,
+ "526": 312,
+ "527": 552,
+ "528": 552,
+ "529": 552,
+ "530": 552,
+ "531": 200,
+ "532": 532,
+ "535": 209,
+ "536": 209,
+ "537": 209,
+ "538": 200,
+ "539": 552,
+ "540": 200,
+ "541": 200,
+ "543": 662,
+ "544": 755,
+ "546": null,
+ "547": null,
+ "548": 200,
+ "549": 522,
+ "550": 209,
+ "551": 863,
+ "552": 552,
+ "553": 552,
+ "554": 20,
+ "555": 522,
+ "556": 269,
+ "558": 662,
+ "560": null,
+ "561": null,
+ "562": null,
+ "563": null,
+ "564": 89,
+ "565": 565,
+ "566": 639,
+ "567": 662,
+ "568": 459,
+ "570": null,
+ "571": null,
+ "572": 662,
+ "573": 74,
+ "574": 662,
+ "575": null,
+ "576": null,
+ "577": null,
+ "578": null,
+ "579": null,
+ "580": null,
+ "581": 697,
+ "582": 668,
+ "583": 668,
+ "584": null,
+ "585": null,
+ "586": null,
+ "587": 330,
+ "588": 704,
+ "589": null,
+ "590": 763,
+ "591": 667,
+ "593": 287,
+ "594": null,
+ "595": 697,
+ "597": 670,
+ "598": 200,
+ "599": 295,
+ "600": 755,
+ "601": 601,
+ "602": 287,
+ "603": 287,
+ "605": null,
+ "606": 1284,
+ "607": null,
+ "608": 668,
+ "609": 667,
+ "610": 610,
+ "611": 611,
+ "612": null,
+ "613": 613,
+ "614": 319,
+ "615": 200,
+ "616": 345,
+ "617": 617,
+ "618": null,
+ "619": 668,
+ "620": 287,
+ "621": 913,
+ "622": 20,
+ "623": 269,
+ "624": 77,
+ "625": 697,
+ "626": 436,
+ "627": 913,
+ "628": null,
+ "636": 755,
+ "637": null,
+ "638": 862,
+ "639": 639,
+ "640": 640,
+ "641": 74,
+ "642": 668,
+ "643": 91,
+ "644": 116,
+ "645": 287,
+ "646": 345,
+ "647": 863,
+ "648": 269,
+ "649": 345,
+ "650": 436,
+ "651": 200,
+ "652": 91,
+ "653": 668,
+ "654": 287,
+ "655": null,
+ "656": null,
+ "657": null,
+ "662": 662,
+ "663": 662,
+ "664": null,
+ "665": 665,
+ "666": null,
+ "667": 667,
+ "668": 668,
+ "669": 669,
+ "670": 670,
+ "671": 798,
+ "672": 672,
+ "673": 426,
+ "674": 674,
+ "675": 667,
+ "676": 120,
+ "680": 190,
+ "681": 681,
+ "682": 682,
+ "683": null,
+ "684": 755,
+ "685": null,
+ "686": null,
+ "687": null,
+ "688": null,
+ "689": 362,
+ "690": 252,
+ "691": null,
+ "692": 697,
+ "693": null,
+ "694": 74,
+ "695": 662,
+ "696": 20,
+ "697": 697,
+ "698": 670,
+ "703": null,
+ "704": 704,
+ "705": 755,
+ "706": 706,
+ "707": null,
+ "708": null,
+ "710": null,
+ "732": 732,
+ "733": null,
+ "749": null,
+ "754": 754,
+ "755": 755,
+ "756": 755,
+ "757": null,
+ "758": 330,
+ "759": 916,
+ "760": 916,
+ "761": 763,
+ "762": 763,
+ "763": 763,
+ "764": 667,
+ "765": 667,
+ "766": 732,
+ "767": 668,
+ "768": null,
+ "770": 770,
+ "771": 400,
+ "772": 772,
+ "773": 400,
+ "774": 770,
+ "775": 772,
+ "776": 776,
+ "777": 697,
+ "778": null,
+ "779": 400,
+ "780": 327,
+ "781": 20,
+ "782": null,
+ "783": 670,
+ "784": 565,
+ "785": 120,
+ "786": 119,
+ "787": 787,
+ "788": 119,
+ "789": 770,
+ "790": null,
+ "791": null,
+ "792": null,
+ "793": null,
+ "794": null,
+ "795": null,
+ "796": null,
+ "797": null,
+ "798": 798,
+ "799": 307,
+ "804": 863,
+ "805": 119,
+ "806": 119,
+ "807": 287,
+ "820": 662,
+ "821": 662,
+ "822": 119,
+ "823": 119,
+ "824": 824,
+ "825": 119,
+ "826": null,
+ "827": 706,
+ "828": 362,
+ "829": 829,
+ "830": 829,
+ "831": 362,
+ "832": 667,
+ "833": 667,
+ "834": 834,
+ "835": 835,
+ "836": 287,
+ "837": null,
+ "838": 838,
+ "839": 697,
+ "841": null,
+ "842": null,
+ "843": 843,
+ "862": 862,
+ "863": 863,
+ "908": 908,
+ "909": 909,
+ "910": 672,
+ "911": null,
+ "912": null,
+ "913": 913,
+ "914": 913,
+ "915": 913,
+ "916": 916,
+ "917": 917,
+ "918": 918,
+ "920": 920,
+ "921": 922,
+ "922": 922,
+ "923": 290,
+ "924": 924,
+ "925": 346,
+ "926": null,
+ "927": 668,
+ "939": 862,
+ "940": 346,
+ "941": null,
+ "942": 863,
+ "943": 74,
+ "1004": 732,
+ "1007": null,
+ "1021": 1021,
+ "1022": 269,
+ "1023": 697,
+ "1024": 697,
+ "1025": 697,
+ "1037": null,
+ "1038": null,
+ "1039": 697,
+ "1041": null,
+ "1042": 400,
+ "1043": null,
+ "1044": null,
+ "1045": null,
+ "1046": 400,
+ "1047": null,
+ "1048": null,
+ "1049": 400,
+ "1050": 400,
+ "1051": 665,
+ "1052": 665,
+ "1053": null,
+ "1054": null,
+ "1055": null,
+ "1056": null,
+ "1057": null,
+ "1058": 662,
+ "1059": null,
+ "1060": null,
+ "1061": 732,
+ "1062": null,
+ "1063": 400,
+ "1064": null,
+ "1065": null,
+ "1066": null,
+ "1067": 400,
+ "1068": null,
+ "1069": null,
+ "1070": null,
+ "1071": null,
+ "1072": 400,
+ "1073": 400,
+ "1074": null,
+ "1075": null,
+ "1076": null,
+ "1077": 697,
+ "1078": null,
+ "1079": null,
+ "1080": null,
+ "1082": null,
+ "1083": null,
+ "1084": 400,
+ "1085": null,
+ "1086": null,
+ "1087": null,
+ "1088": 662,
+ "1089": 400,
+ "1090": null,
+ "1091": 772,
+ "1092": null,
+ "1093": null,
+ "1094": 400,
+ "1095": null,
+ "1096": 662,
+ "1097": null,
+ "1098": null,
+ "1099": null,
+ "1100": null,
+ "1101": null,
+ "1102": null,
+ "1103": null,
+ "1104": null,
+ "1105": null,
+ "1106": null,
+ "1107": null,
+ "1108": null,
+ "1109": null,
+ "1110": null,
+ "1111": null,
+ "1112": null,
+ "1113": null,
+ "1114": null,
+ "1115": null,
+ "1116": null,
+ "1117": null,
+ "1118": null,
+ "1119": null,
+ "1120": null,
+ "1121": null,
+ "1122": null,
+ "1123": null,
+ "1124": null,
+ "1125": null,
+ "1126": null,
+ "1127": null,
+ "1164": null,
+ "1173": 20,
+ "1174": 20,
+ "1176": 400,
+ "1177": 120,
+ "1188": 1188,
+ "1189": 668,
+ "1190": null,
+ "1191": null,
+ "1192": null,
+ "1193": null,
+ "1204": 330,
+ "1209": null,
+ "1220": null,
+ "1221": 665,
+ "1222": null,
+ "1223": 362,
+ "1224": null,
+ "1229": null,
+ "1230": null,
+ "1231": null,
+ "1232": 667,
+ "1233": 667,
+ "1234": 667,
+ "1235": 400,
+ "1236": 1236,
+ "1239": 459,
+ "1240": 327,
+ "1241": 330,
+ "1242": null,
+ "1243": null,
+ "1244": 863,
+ "1245": null,
+ "1246": 400,
+ "1247": null,
+ "1248": null,
+ "1249": null,
+ "1250": null,
+ "1251": null,
+ "1252": null,
+ "1253": null,
+ "1254": 697,
+ "1255": 203,
+ "1256": null,
+ "1257": null,
+ "1258": 212,
+ "1259": null,
+ "1260": null,
+ "1261": null,
+ "1262": null,
+ "1263": null,
+ "1264": 662,
+ "1265": 662,
+ "1266": 404,
+ "1267": null,
+ "1268": 269,
+ "1269": null,
+ "1270": null,
+ "1271": 909,
+ "1272": 459,
+ "1273": 200,
+ "1274": null,
+ "1275": null,
+ "1276": null,
+ "1277": null,
+ "1278": null,
+ "1279": 665,
+ "1280": null,
+ "1281": null,
+ "1282": 668,
+ "1283": null,
+ "1284": 1284,
+ "1285": 20,
+ "1286": 20,
+ "1287": 20,
+ "1288": 20,
+ "1289": 20,
+ "1290": null,
+ "1291": null,
+ "1292": null,
+ "1293": 345,
+ "1294": null,
+ "1295": 200,
+ "1296": null,
+ "1297": null,
+ "1298": 362,
+ "1299": 306,
+ "1300": 203,
+ "1301": 459,
+ "1302": null,
+ "1303": 203,
+ "1304": null,
+ "1310": null,
+ "1311": null,
+ "1312": null,
+ "1313": null,
+ "1314": 862,
+ "1315": null,
+ "1316": null,
+ "1317": null,
+ "1318": null,
+ "1319": null,
+ "1320": null,
+ "1321": 1321,
+ "1322": 834,
+ "1323": null,
+ "1325": 770,
+ "1326": null,
+ "1327": 668,
+ "1328": null,
+ "1329": null,
+ "1330": 459,
+ "1331": 668,
+ "1332": null,
+ "1333": 1333,
+ "1334": null,
+ "1335": 682,
+ "1336": 94,
+ "1338": null,
+ "1339": 682,
+ "1341": 415,
+ "1342": 459,
+ "1351": null,
+ "1357": null,
+ "1384": null,
+ "1385": 346,
+ "1386": 59,
+ "1389": 704,
+ "1390": 287,
+ "1391": 287,
+ "1392": 287,
+ "1393": 287,
+ "1394": 287,
+ "1395": null,
+ "1419": 665,
+ "1420": 669,
+ "1421": 669,
+ "1422": 669,
+ "1423": 669,
+ "1426": null,
+ "1427": 77,
+ "1428": 319,
+ "1429": null,
+ "1431": 200,
+ "1434": 665
+ }
+}
diff --git a/plugins/claude-security/scripts/lib/cwe.py b/plugins/claude-security/scripts/lib/cwe.py
new file mode 100644
index 00000000..1f7a38be
--- /dev/null
+++ b/plugins/claude-security/scripts/lib/cwe.py
@@ -0,0 +1,79 @@
+"""CWE data the plugin ships: the weaknesses, and the Simplified Mapping entry each belongs to."""
+
+from __future__ import annotations
+
+from dataclasses import dataclass
+from pathlib import Path
+from typing import TypedDict, cast
+
+from . import strictjson
+
+UNCATEGORIZED = "Uncategorized"
+
+
+class CategoryNames(TypedDict):
+ """One entry's names in cwe-categories.json."""
+
+ name: str
+ title: str
+
+
+class CatalogFile(TypedDict):
+ """The shape of cwe-categories.json."""
+
+ cwe_version: str
+ categories: dict[str, CategoryNames]
+ category_of: dict[str, int | None]
+
+
+@dataclass(frozen=True)
+class Category:
+ """One entry of the view: its CWE number, its common name, its full catalog title."""
+
+ number: int
+ name: str
+ title: str
+
+ @property
+ def id(self) -> str:
+ """The entry's CWE id, `CWE-<number>`."""
+ return f"CWE-{self.number}"
+
+
+@dataclass(frozen=True)
+class Catalog:
+ """One CWE release: the view's entries, and for every weakness the entry it rolls up to."""
+
+ version: str
+ categories: dict[int, Category]
+ category_of: dict[int, int | None]
+
+ @classmethod
+ def load(cls, path: Path) -> Catalog:
+ """The catalog stored at `path`."""
+ raw = cast("CatalogFile", strictjson.load(path))
+ return cls(
+ raw["cwe_version"],
+ {
+ int(number): Category(int(number), names["name"], names["title"])
+ for number, names in raw["categories"].items()
+ },
+ {int(number): category for number, category in raw["category_of"].items()},
+ )
+
+ def category(self, cwe: int) -> Category | None:
+ """The entry `cwe` rolls up to; None when it reaches none or is not a known weakness."""
+ number = self.category_of.get(cwe)
+ return self.categories[number] if number is not None else None
+
+ def defines(self, cwe: int) -> bool:
+ """Whether the release defines weakness `cwe` at all; category() is None either way."""
+ return cwe in self.category_of
+
+
+def id_number(cwe_id: str) -> int:
+ """The number of a canonical `CWE-<number>` id, the spelling every validated finding carries."""
+ return int(cwe_id.removeprefix("CWE-"))
+
+
+catalog = Catalog.load(Path(__file__).resolve().with_name("cwe-categories.json"))
diff --git a/plugins/claude-security/scripts/lib/finding.py b/plugins/claude-security/scripts/lib/finding.py
new file mode 100644
index 00000000..c6f0ffa1
--- /dev/null
+++ b/plugins/claude-security/scripts/lib/finding.py
@@ -0,0 +1,241 @@
+"""The Finding record every product carries, and build_finding, which validates a raw one."""
+
+from __future__ import annotations
+
+import os
+import re
+from typing import TypedDict
+
+from . import absolute, cwe
+from .strictjson import JsonMap, has_lone_surrogate, is_int, is_list, is_map, is_str
+
+
+class Panel(TypedDict):
+ """A validated panel round: the vote counts and the fixed voter count."""
+
+ true: int
+ false: int
+ voters: int
+
+
+class Finding(TypedDict):
+ """One validated finding: the JSONL record, whose field order is this class's order."""
+
+ id: str
+ title: str
+ impact: str
+ file: str
+ line: int
+ description: str
+ exploit_scenario: str
+ preconditions: list[str]
+ category: str
+ severity: str
+ confidence: str
+ recommendation: str
+ cwe_id: str
+ snippet: str
+ symbol: str
+
+
+SEVERITIES = ("HIGH", "MEDIUM", "LOW")
+CONFIDENCES = ("low", "medium", "high")
+CONFIDENCE_RANK = {"low": 1, "medium": 2, "high": 3}
+
+PANEL_VOTER_COUNT = 3
+PANEL_KEEP_QUORUM = 2
+
+# \Z, not $: `$` also matches before a trailing newline, and this names a file.
+FINDING_ID_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9_.-]{0,63}\Z")
+
+
+class FindingError(Exception):
+ """A refusal; the message names what a findings.json record got wrong."""
+
+
+def cwe_number(item: JsonMap, finding_id: str) -> int:
+ """A finding's CWE number; a cwe_id missing, unreadable or malformed is refused.
+
+ A well-formed id is accepted as declared, whether or not the pinned CWE
+ release defines it; one the release does not define files the finding
+ under Uncategorized, and the renderer discloses the substitution.
+ """
+ declared = text_field(item, "cwe_id", finding_id, required=True)
+ matched = re.fullmatch(
+ r"(?:CWE-)?0*([1-9][0-9]{0,4})", declared.strip().upper().replace("_", "-")
+ )
+ if not matched:
+ msg = f"finding {finding_id} cwe_id {declared!r} is not a CWE id such as CWE-89"
+ raise FindingError(msg)
+ return int(matched[1])
+
+
+def confidence_value(raw: object) -> str:
+ """A finding's stated confidence, normalized to low|medium|high; refuses others."""
+ if is_str(raw):
+ word = raw.strip().lower()
+ if word in CONFIDENCE_RANK:
+ return word
+ msg = f"confidence {raw!r} is not one of {'/'.join(CONFIDENCES)}"
+ raise FindingError(msg)
+
+
+def panel_complete(record: object) -> Panel | None:
+ """One round record's panel when the full voter count returned an integer tally, else None."""
+ if not is_map(record):
+ return None
+ panel = record.get("panel")
+ if not is_map(panel):
+ return None
+ panel_true = panel.get("true")
+ if not is_int(panel_true):
+ return None
+ if panel.get("voters") != PANEL_VOTER_COUNT:
+ return None
+ false_votes = panel.get("false")
+ return {
+ "true": panel_true,
+ "false": false_votes if is_int(false_votes) else 0,
+ "voters": PANEL_VOTER_COUNT,
+ }
+
+
+def vote_confidence_ceiling(record: object) -> str | None:
+ """A finding's vote-backed confidence: `high` if unanimous, `medium` if complete, else None."""
+ panel = panel_complete(record)
+ if panel is None:
+ return None
+ return "high" if panel["true"] >= PANEL_VOTER_COUNT else "medium"
+
+
+def text_field(item: JsonMap, key: str, finding_id: str, required: bool = False) -> str:
+ """One of a finding's text fields; absent or null reads as empty unless it is required."""
+ value = item.get(key)
+ if value is None:
+ text = ""
+ elif is_str(value):
+ text = value
+ else:
+ msg = f"finding {finding_id} {key} is {type(value).__name__}, not a string"
+ raise FindingError(msg)
+ if has_lone_surrogate(text):
+ msg = f"finding {finding_id} {key} contains an unpaired surrogate"
+ raise FindingError(msg)
+ if required and not text.strip():
+ msg = f"finding {finding_id} is missing required field {key!r}"
+ raise FindingError(msg)
+ return text
+
+
+def file_field(
+ item: JsonMap, finding_id: str, scan_root: str, scan_prefix: str, must_exist: bool
+) -> str:
+ """A finding's file relative to the scan root; a path that leaves the repository is refused.
+
+ `scan_prefix` is the scan root's path below the repository top level (`a/b/`,
+ or empty): a file may climb one directory per prefix component and no further.
+ A file spelled relative to the top level, absent under the scan root but
+ present under the top level, is respelled relative to the scan root.
+ With `must_exist` (a codebase scan, whose whole tree is still present when
+ the report renders), a path that exists neither under the scan root nor at
+ the repository top level is refused.
+ """
+ declared = text_field(item, "file", finding_id, required=True).strip()
+ depth = scan_prefix.count("/")
+ beyond = "repository" if depth else "scan root"
+ refusal = f"finding {finding_id} file {declared!r} escapes the {beyond}"
+ path = declared.replace("\\", "/")
+ prefix = scan_root.replace("\\", "/").rstrip("/") + "/"
+ if scan_root and path.startswith(prefix):
+ path = path[len(prefix) :].lstrip("/")
+ if os.path.isabs(path):
+ try:
+ path = os.path.relpath(os.path.realpath(path), scan_root).replace("\\", "/")
+ except (ValueError, OSError) as error:
+ raise FindingError(refusal) from error
+ parts = [part for part in path.split("/") if part and part != "."]
+ climb = next((i for i, part in enumerate(parts) if part != ".."), len(parts))
+ inside = parts[climb:]
+ if not inside or absolute.spelled(path) or ".." in inside or climb > depth:
+ raise FindingError(refusal)
+ if not os.path.lexists(os.path.join(scan_root, *parts)):
+ # A file only the repository top level holds was spelled relative to it, not the scan root.
+ if depth and not climb:
+ at_top = os.path.join(os.path.normpath(os.path.join(scan_root, "../" * depth)), *parts)
+ if os.path.lexists(at_top):
+ return os.path.relpath(at_top, scan_root).replace("\\", "/")
+ if must_exist:
+ msg = f"finding {finding_id} file {declared!r} does not exist in the scanned tree"
+ raise FindingError(msg)
+ return "/".join(parts)
+
+
+def build_finding(
+ raw: object,
+ index: int,
+ rounds_by_id: JsonMap,
+ scan_root: str,
+ scan_prefix: str,
+ must_exist: bool,
+) -> Finding:
+ """Validate one raw findings.json record into a Finding."""
+ if not is_map(raw):
+ msg = f"findings.json item {index} is not an object"
+ raise FindingError(msg)
+ numbered = f"F{index + 1}"
+ finding_id = text_field(raw, "id", numbered) or numbered
+ if not FINDING_ID_RE.match(finding_id):
+ msg = f"finding id {finding_id!r} is not a valid id"
+ raise FindingError(msg)
+
+ severity = str(raw.get("severity", "")).strip().upper()
+ if severity not in SEVERITIES:
+ msg = (
+ f"finding {finding_id} severity {raw.get('severity')!r} is not one of "
+ f"{'/'.join(SEVERITIES)}"
+ )
+ raise FindingError(msg)
+
+ confidence = confidence_value(raw.get("confidence"))
+ ceiling = vote_confidence_ceiling(rounds_by_id.get(finding_id))
+ if ceiling is not None and CONFIDENCE_RANK[confidence] > CONFIDENCE_RANK[ceiling]:
+ confidence = ceiling
+
+ line = raw.get("line", 0)
+ if is_str(line) and re.fullmatch(r"\s*-?[0-9]{1,15}\s*", line):
+ line = int(line)
+ if not is_int(line):
+ msg = f"finding {finding_id} line {raw.get('line')!r} is not an integer"
+ raise FindingError(msg)
+
+ preconditions: list[str] = []
+ declared = raw.get("preconditions")
+ if declared is not None:
+ if not is_list(declared):
+ msg = f"finding {finding_id} preconditions must be a list"
+ raise FindingError(msg)
+ preconditions = [item for item in declared if is_str(item)]
+ if len(preconditions) != len(declared) or any(map(has_lone_surrogate, preconditions)):
+ msg = f"finding {finding_id} preconditions must be a list of strings"
+ raise FindingError(msg)
+
+ number = cwe_number(raw, finding_id)
+ category = cwe.catalog.category(number)
+
+ return {
+ "id": finding_id,
+ "title": text_field(raw, "title", finding_id, required=True),
+ "impact": text_field(raw, "impact", finding_id),
+ "file": file_field(raw, finding_id, scan_root, scan_prefix, must_exist),
+ "line": line,
+ "description": text_field(raw, "description", finding_id, required=True),
+ "exploit_scenario": text_field(raw, "exploit_scenario", finding_id, required=True),
+ "preconditions": preconditions,
+ "category": category.name if category is not None else cwe.UNCATEGORIZED,
+ "severity": severity,
+ "confidence": confidence,
+ "recommendation": text_field(raw, "recommendation", finding_id),
+ "cwe_id": f"CWE-{number}",
+ "snippet": text_field(raw, "snippet", finding_id),
+ "symbol": text_field(raw, "symbol", finding_id),
+ }
diff --git a/plugins/claude-security/scripts/lib/plugin.py b/plugins/claude-security/scripts/lib/plugin.py
new file mode 100644
index 00000000..c5daa153
--- /dev/null
+++ b/plugins/claude-security/scripts/lib/plugin.py
@@ -0,0 +1,31 @@
+"""The plugin's fixed names, and its own manifest."""
+
+from __future__ import annotations
+
+import re
+from pathlib import Path
+
+from . import strictjson
+from .strictjson import is_map, is_str
+
+NAME = "claude-security"
+ROOT = Path(__file__).resolve().parents[2]
+RUN_DIR_NAME = ".claude-security-run"
+MODES = ("scan", "changes", "commit")
+REPORT_DIR_PREFIX = "CLAUDE-SECURITY-"
+# \Z, not $: `$` also matches before a trailing newline, and this id names product files.
+SHA_RE = re.compile(r"^[0-9a-fA-F]{7,64}\Z")
+
+
+def version() -> str | None:
+ """The non-blank version string in the plugin's manifest, or None when there is not one."""
+ try:
+ manifest = strictjson.load(ROOT / ".claude-plugin" / "plugin.json")
+ except (OSError, ValueError):
+ return None
+ if not is_map(manifest):
+ return None
+ declared = manifest.get("version")
+ if not is_str(declared):
+ return None
+ return declared.strip() or None
diff --git a/plugins/claude-security/scripts/lib/sarif.py b/plugins/claude-security/scripts/lib/sarif.py
new file mode 100644
index 00000000..f30d0afa
--- /dev/null
+++ b/plugins/claude-security/scripts/lib/sarif.py
@@ -0,0 +1,373 @@
+"""SARIF 2.1.0 for one scan: the log encoder."""
+
+from __future__ import annotations
+
+import hashlib
+import json
+import posixpath
+import re
+from bisect import bisect_right
+from dataclasses import dataclass
+from itertools import accumulate
+from typing import TYPE_CHECKING, NamedTuple
+from urllib.parse import quote
+
+from . import cwe, secret
+
+if TYPE_CHECKING:
+ import uuid
+ from collections.abc import Mapping, Sequence
+
+ from .finding import Finding, Panel
+
+SCHEMA_ID = (
+ "https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json"
+)
+# The driver name GitHub keys alert identity on; renaming it orphans every open alert.
+TOOL_NAME = "Claude Security Plugin for Claude Code"
+TOOL_URI = "https://claude.com/product/claude-security"
+PROPERTY_BAG = "claudeSecurityPlugin"
+ID_PREFIX = "claude-security-plugin"
+FINGERPRINT_KEY = ID_PREFIX + "/v2"
+CONTEXT_LINES = 3
+SRCROOT = "%SRCROOT%"
+LEVEL = {"HIGH": "error", "MEDIUM": "warning", "LOW": "note"}
+
+
+@dataclass(frozen=True)
+class Scan:
+ """The one scan a log describes: its identity, where it ran, and the repository it names."""
+
+ id: uuid.UUID
+ mode: str
+ # The scan root below the repository top level, slash-terminated; "" when there is none.
+ prefix: str
+ # The credential-free https form of the repository's remote; None when there is not one.
+ remote: str | None
+ # The directories the scan was limited to, relative to the scan root; empty for all of it.
+ scope: tuple[str, ...]
+ # The commit the scanned tree was exactly at; None when it was dirty, unversioned or unknown.
+ revision: str | None
+
+
+def log(
+ findings: Sequence[Finding],
+ scan: Scan,
+ tool_version: str | None,
+ run_properties: Mapping[str, object],
+ panels: Mapping[str, Panel],
+ sources: Mapping[str, str],
+ notifications: Sequence[Mapping[str, object]],
+) -> dict[str, object]:
+ """The SARIF 2.1.0 log for one scan: one run, one rule per category, one result per finding.
+
+ `sources` is the text of each scanned file a finding names, keyed by the
+ finding's `file`; a finding whose file is absent from it is fingerprinted
+ on its own quote of the code instead.
+ """
+ filed = [(item, category_of(item)) for item in findings]
+ categories = list(dict.fromkeys(category for _, category in filed))
+ index = {category: position for position, category in enumerate(categories)}
+ driver: dict[str, object] = {
+ "name": TOOL_NAME,
+ "organization": "Anthropic",
+ "informationUri": TOOL_URI,
+ **({"version": tool_version} if tool_version else {}),
+ "rules": [rule(category) for category in categories],
+ }
+ invocation: dict[str, object] = {"executionSuccessful": True}
+ if notifications:
+ invocation["toolExecutionNotifications"] = list(notifications)
+ base_description = (
+ "The top level of the scanned repository, or the scanned directory when the scan did "
+ "not run inside a git checkout."
+ )
+ run: dict[str, object] = {
+ "tool": {"driver": driver},
+ "automationDetails": {"id": automation_id(scan), "guid": str(scan.id)},
+ "invocations": [invocation],
+ "originalUriBaseIds": {SRCROOT: {"description": {"text": base_description}}},
+ "results": [
+ result(
+ item,
+ category,
+ index[category],
+ scan,
+ panels.get(item["id"]),
+ sources.get(item["file"]),
+ )
+ for item, category in filed
+ ],
+ "properties": {
+ PROPERTY_BAG: {
+ **run_properties,
+ "target_kind": "git-remote" if scan.remote else "local-path",
+ }
+ },
+ }
+ if scan.remote:
+ provenance: dict[str, object] = {"repositoryUri": scan.remote}
+ if scan.revision:
+ provenance["revisionId"] = scan.revision
+ run["versionControlProvenance"] = [provenance]
+ return {"$schema": SCHEMA_ID, "version": "2.1.0", "runs": [run]}
+
+
+def automation_id(scan: Scan) -> str:
+ """The run's automation id: the plugin prefix, the mode, the scan's extent if any, its id."""
+ category = f"{ID_PREFIX}/{scan.mode}"
+ if scan.scope:
+ extent = ",".join(scan.prefix + entry.strip("/") for entry in sorted(scan.scope))
+ else:
+ extent = scan.prefix.rstrip("/")
+ if extent:
+ category += "/" + quote(uri_bytes(extent))
+ return f"{category}/{scan.id}"
+
+
+def category_of(finding: Finding) -> cwe.Category | None:
+ """The Simplified Mapping entry the finding's CWE rolls up to; None for Uncategorized."""
+ return cwe.catalog.category(cwe.id_number(finding["cwe_id"]))
+
+
+def rule_id(category: cwe.Category | None) -> str:
+ """A rule's id: its entry's CWE id (`CWE-89`), or `uncategorized`."""
+ return category.id if category is not None else cwe.UNCATEGORIZED.lower()
+
+
+def rule(category: cwe.Category | None) -> dict[str, object]:
+ """The reporting descriptor for one entry: the catalog's names, its page, its fixed tags."""
+ help_text = (
+ "Each alert's message names the finding's own CWE and states the impact, exploit "
+ "scenario, preconditions and recommended fix; the finding appears under its F<n> id "
+ "in CLAUDE-SECURITY-RESULTS.md."
+ )
+ if category is None:
+ return {
+ "id": rule_id(None),
+ "name": cwe.UNCATEGORIZED,
+ "shortDescription": {"text": cwe.UNCATEGORIZED},
+ "fullDescription": {
+ "text": "Findings whose CWE is not an entry of the CWE Simplified Mapping view "
+ f"and rolls up to none, reported by {TOOL_NAME} from static review of the "
+ "source."
+ },
+ "help": {"text": help_text},
+ "properties": {"tags": ["security"]},
+ }
+ return {
+ "id": category.id,
+ "name": rule_name(category.name),
+ "shortDescription": {"text": category.name},
+ "fullDescription": {
+ "text": f"{category.title} ({category.id}, CWE {cwe.catalog.version}): findings whose "
+ f"CWE is this entry of the Simplified Mapping view or rolls up to it, reported by "
+ f"{TOOL_NAME} from static review of the source."
+ },
+ "help": {"text": help_text},
+ "helpUri": f"https://cwe.mitre.org/data/definitions/{category.number}.html",
+ "properties": {"tags": ["security", f"external/cwe/cwe-{category.number}"]},
+ }
+
+
+def fingerprint(
+ finding: Finding, category: cwe.Category | None, scan: Scan, source: str | None
+) -> str:
+ """The finding's partial fingerprint: a sha256 over its rule, its path and the code it names.
+
+ `source` is the text of the finding's file. The code is the file's own
+ lines around the one that places the finding (see code_at); when the file
+ was not read, or no line of it places the finding, the finding's symbol
+ and snippet stand in for them, and the line when it has neither. A
+ hard-coded credential finding is the exception: its symbol and the number
+ of the line that places it stand in for the code always, so no text of a
+ file that holds a credential enters the hash.
+ """
+ parts = [scan.remote or "", rule_id(category), repository_path(scan, finding)]
+ symbol = finding["symbol"].strip()
+ snippet = quoted_line(finding)
+ if secret.is_credential(finding):
+ lines = None if source is None else normalized_lines(source)
+ placed = None if lines is None else placing_row(lines, finding["line"], snippet)
+ parts += [symbol, str(finding["line"] if placed is None else placed + 1)]
+ return hashlib.sha256(json.dumps(parts).encode()).hexdigest()
+ code = None if source is None else code_at(source, finding["line"], snippet)
+ if code is not None:
+ parts.append(code)
+ else:
+ parts += [symbol, snippet]
+ if not symbol and not snippet:
+ parts.append(str(finding["line"]))
+ return hashlib.sha256(json.dumps(parts).encode()).hexdigest()
+
+
+class Site(NamedTuple):
+ """What one result stands for: a rule at a line of a file; the log holds one result per site."""
+
+ rule: str
+ path: str
+ line: int
+
+
+def site(finding: Finding, scan: Scan, source: str | None) -> Site | None:
+ """The finding's site: its rule id, its repository path, and the line that places it.
+
+ The line is the one of `source`, the finding's file, that places the
+ finding (placing_row), so two findings that quote one line of code are one
+ site whatever lines they declare; it is the declared line when the file
+ was not read or no line of it places the finding. A finding left with no
+ line (it declared none, line < 1, and none places it) has no site: None.
+ """
+ line = finding["line"]
+ if source is not None:
+ row = placing_row(normalized_lines(source), line, quoted_line(finding))
+ if row is not None:
+ line = row + 1
+ if line < 1:
+ return None
+ return Site(rule_id(category_of(finding)), repository_path(scan, finding), line)
+
+
+def quoted_line(finding: Finding) -> str:
+ """The finding's snippet with its whitespace normalized, the form placing_row looks for."""
+ return " ".join(finding["snippet"].split())
+
+
+def normalized_lines(source: str) -> list[str]:
+ """A file's lines, split on the newline alone, each with its whitespace normalized."""
+ return [" ".join(each.split()) for each in source.split("\n")]
+
+
+def placing_row(lines: Sequence[str], line: int, quoted: str) -> int | None:
+ """The index into the normalized `lines` of the one placing a finding; None when none does.
+
+ The finding is placed on the line nearest its declared `line` where
+ `quoted`, its normalized snippet, appears, whitespace aside, and on the
+ declared line itself when it appears nowhere.
+ """
+ declared = line - 1
+ at = min(
+ (min(max(declared, first), last) for first, last in occurrences(lines, quoted)),
+ key=lambda row: abs(row - declared),
+ default=declared,
+ )
+ return at if 0 <= at < len(lines) else None
+
+
+def code_at(source: str, line: int, quoted: str) -> str | None:
+ """The normalized lines of `source` around the one placing a finding; None when none does."""
+ lines = normalized_lines(source)
+ at = placing_row(lines, line, quoted)
+ if at is None:
+ return None
+ return "\n".join(lines[max(at - CONTEXT_LINES, 0) : at + CONTEXT_LINES + 1])
+
+
+def occurrences(lines: Sequence[str], quoted: str) -> list[tuple[int, int]]:
+ """The (first, last) index into the normalized `lines` of each occurrence of `quoted`."""
+ if not quoted:
+ return []
+ filled = [row for row, line in enumerate(lines) if line]
+ starts = list(accumulate((len(lines[row]) + 1 for row in filled), initial=0))
+ flat = " ".join(lines[row] for row in filled)
+
+ def row_at(offset: int) -> int:
+ return filled[bisect_right(starts, offset) - 1]
+
+ return [
+ (row_at(found.start()), row_at(found.end() - 1))
+ for found in re.finditer(re.escape(quoted), flat)
+ ]
+
+
+def result(
+ finding: Finding,
+ category: cwe.Category | None,
+ rule_index: int,
+ scan: Scan,
+ panel: Panel | None,
+ source: str | None,
+) -> dict[str, object]:
+ """One result: the finding under its rule, its partial fingerprint, and its JSONL record."""
+ shown = secret.withheld(finding)
+ record: dict[str, object] = {**shown}
+ if panel is not None:
+ record["verification"] = {"panel": panel}
+ return {
+ "ruleId": rule_id(category),
+ "ruleIndex": rule_index,
+ "level": LEVEL[finding["severity"]],
+ "message": {"text": message(finding)},
+ "locations": [location(shown, scan)],
+ "partialFingerprints": {FINGERPRINT_KEY: fingerprint(finding, category, scan, source)},
+ "properties": {PROPERTY_BAG: record},
+ }
+
+
+def location(finding: Finding, scan: Scan) -> dict[str, object]:
+ """A result's one location: the file relative to SRCROOT, the line, the snippet, the symbol."""
+ line = finding["line"]
+ region: dict[str, object] = {"startLine": max(line, 1)}
+ if line >= 1 and finding["snippet"].strip():
+ region["snippet"] = {"text": finding["snippet"]}
+ place: dict[str, object] = {
+ "physicalLocation": {
+ "artifactLocation": {
+ "uri": quote(uri_bytes(repository_path(scan, finding))),
+ "uriBaseId": SRCROOT,
+ },
+ "region": region,
+ }
+ }
+ if symbol := finding["symbol"].strip():
+ place["logicalLocations"] = [{"name": symbol, "fullyQualifiedName": symbol}]
+ return place
+
+
+def repository_path(scan: Scan, finding: Finding) -> str:
+ """The finding's file relative to the repository top level, with any leading climb folded."""
+ return posixpath.normpath(scan.prefix + finding["file"])
+
+
+def uri_bytes(text: str) -> bytes:
+ """`text` as the bytes its uri must name; byte-faithful for a filesystem name."""
+ try:
+ return text.encode("utf-8", "surrogateescape")
+ except UnicodeEncodeError:
+ return text.encode("utf-8", "surrogatepass")
+
+
+def notification(descriptor_id: str, level: str, text: str) -> dict[str, object]:
+ """One invocation notification: its namespaced descriptor id, its level, and its message."""
+ return {"descriptor": {"id": descriptor_id}, "level": level, "message": {"text": text}}
+
+
+def message(finding: Finding) -> str:
+ """A result's message: the finding's prose, its stated parts labeled, then its ratings."""
+ parts = [sentence(finding["title"]), sentence(finding["description"])]
+ labeled = (
+ ("Impact", finding["impact"]),
+ ("Exploit scenario", finding["exploit_scenario"]),
+ ("Preconditions", "; ".join(finding["preconditions"])),
+ ("Recommendation", finding["recommendation"]),
+ )
+ parts += [f"{label}: {text}" for label, value in labeled if (text := sentence(value))]
+ if finding["line"] < 1:
+ parts.append("The exact line was not determined; see the description.")
+ if secret.is_credential(finding):
+ parts.append("The source line is not quoted because it holds the credential.")
+ parts.append(
+ f"{finding['cwe_id']}. Severity {finding['severity']}, confidence {finding['confidence']}."
+ )
+ return "\n\n".join(parts)
+
+
+def rule_name(category: str) -> str:
+ """A category's common name in PascalCase, for a rule name: `SQLInjection`."""
+ return "".join(word[:1].upper() + word[1:] for word in re.split(r"[^A-Za-z0-9]+", category))
+
+
+def sentence(text: str) -> str:
+ """`text` stripped and closed with a period unless it already ends in punctuation."""
+ text = text.strip()
+ return text if not text or text[-1] in ".!?" else text + "."
diff --git a/plugins/claude-security/scripts/lib/secret.py b/plugins/claude-security/scripts/lib/secret.py
new file mode 100644
index 00000000..9bf90a3a
--- /dev/null
+++ b/plugins/claude-security/scripts/lib/secret.py
@@ -0,0 +1,32 @@
+"""The snippet of a hard-coded credential finding, withheld from what the products carry.
+
+The line such a finding quotes is the credential itself, and the JSONL and
+SARIF files exist to leave the machine (a code scanning upload, a CI
+artifact), so neither file quotes it: the finding's file, line and symbol
+still locate the code, and the SARIF result's message says why no line is
+quoted. Only the emitted copy changes: the finding is still placed on the
+snippet as the researcher quoted it, and its fingerprint hashes no text of
+the file (see sarif.fingerprint).
+"""
+
+from __future__ import annotations
+
+from typing import TYPE_CHECKING
+
+from . import cwe
+
+if TYPE_CHECKING:
+ from .finding import Finding
+
+# CWE's Simplified Mapping entry Use of Hard-coded Credentials; CWE-259, 321 and 671 roll up to it.
+CREDENTIALS = 798
+
+
+def is_credential(finding: Finding) -> bool:
+ """Whether the finding's CWE rolls up to Use of Hard-coded Credentials."""
+ return cwe.catalog.category_of.get(cwe.id_number(finding["cwe_id"])) == CREDENTIALS
+
+
+def withheld(finding: Finding) -> Finding:
+ """`finding` as the products carry it: a hard-coded credential's snippet is empty."""
+ return {**finding, "snippet": ""} if is_credential(finding) else finding
diff --git a/plugins/claude-security/scripts/lib/strictjson.py b/plugins/claude-security/scripts/lib/strictjson.py
new file mode 100644
index 00000000..2c5a9900
--- /dev/null
+++ b/plugins/claude-security/scripts/lib/strictjson.py
@@ -0,0 +1,83 @@
+"""JSON in and out of the scripts, stricter than the json module in both directions.
+
+Input is model-written and output is read by other tools, so load() refuses
+what json.load admits but a conforming reader does not (NaN, Infinity, a
+number that overflows to infinity, nesting past the interpreter's limit), and
+text() keeps non-ASCII readable while escaping what UTF-8 or a JSONL line
+reader cannot take (an unpaired surrogate, the Unicode line separators).
+"""
+
+from __future__ import annotations
+
+import json
+import math
+import re
+from collections.abc import Mapping
+from typing import TYPE_CHECKING, NoReturn, cast
+
+if TYPE_CHECKING:
+ import os
+ from typing import TypeGuard
+
+JsonMap = Mapping[str, object]
+
+# The separators a naive line reader would split a JSONL record on.
+_SEPARATOR_ESCAPES = {0x85: "\\u0085", 0x2028: "\\u2028", 0x2029: "\\u2029"}
+# The code units UTF-8 cannot encode; surrogateescape mints them from bytes.
+_SURROGATES = re.compile(r"[\ud800-\udfff]")
+
+
+def is_map(value: object) -> TypeGuard[JsonMap]:
+ """Whether `value` is a JSON object."""
+ return isinstance(value, dict)
+
+
+def is_list(value: object) -> TypeGuard[list[object]]:
+ """Whether `value` is a JSON array."""
+ return isinstance(value, list)
+
+
+def is_str(value: object) -> TypeGuard[str]:
+ """Whether `value` is a JSON string."""
+ return isinstance(value, str)
+
+
+def is_int(value: object) -> TypeGuard[int]:
+ """Whether `value` is a JSON integer; JSON's booleans are not numbers."""
+ return isinstance(value, int) and not isinstance(value, bool)
+
+
+def has_lone_surrogate(value: str) -> bool:
+ """Whether `value` holds an unpaired surrogate, the one code unit UTF-8 cannot encode."""
+ return _SURROGATES.search(value) is not None
+
+
+def _refuse(token: str) -> NoReturn:
+ msg = f"{token} is not JSON"
+ raise ValueError(msg)
+
+
+def _finite(digits: str) -> float:
+ value = float(digits)
+ if not math.isfinite(value):
+ msg = f"{digits} is out of range"
+ raise ValueError(msg)
+ return value
+
+
+def load(path: str | os.PathLike[str]) -> object:
+ """The JSON value in `path`; OSError if unreadable, ValueError if not strict JSON."""
+ with open(path, encoding="utf-8") as handle:
+ try:
+ return cast("object", json.load(handle, parse_constant=_refuse, parse_float=_finite))
+ except RecursionError as error:
+ msg = "nested too deeply"
+ raise ValueError(msg) from error
+
+
+def text(value: object, indent: int | None = None) -> str:
+ """`value` as UTF-8-encodable JSON text: non-ASCII kept, finite only, separators escaped."""
+ dumped = json.dumps(value, ensure_ascii=False, allow_nan=False, indent=indent)
+ return _SURROGATES.sub(
+ lambda match: f"\\u{ord(match.group()):04x}", dumped.translate(_SEPARATOR_ESCAPES)
+ )
diff --git a/plugins/claude-security/scripts/patch_artifacts.py b/plugins/claude-security/scripts/patch_artifacts.py
index f11d87ae..73ef5149 100755
--- a/plugins/claude-security/scripts/patch_artifacts.py
+++ b/plugins/claude-security/scripts/patch_artifacts.py
@@ -19,14 +19,13 @@ Usage:
patch_artifacts.py --remove-scratch <workspace>
Exits 0 on success (declined findings included), 1 on a refusal naming what is
-wrong, 2 on a usage error. Python 3.9-compatible, stdlib only.
+wrong, 2 on a usage error.
+Python 3.9-compatible, stdlib only.
"""
from __future__ import annotations
import argparse
-import contextlib
-import json
import os
import pathlib
import re
@@ -35,11 +34,13 @@ import shutil
import stat
import subprocess
import sys
-import tempfile
-from typing import TYPE_CHECKING, TypedDict, cast
+from typing import TYPE_CHECKING, TypedDict
-sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
-from render_report import HEX_RE, RenderError, as_map, atomic_write
+# The lib/ package lives next to this script. Python normally adds a script's own
+# directory to the import path, but not under -P or PYTHONSAFEPATH, so we add it here.
+sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
+from lib import console, plugin, strictjson
+from lib.strictjson import JsonMap, is_list, is_map, is_str
if TYPE_CHECKING:
from collections.abc import Callable
@@ -48,14 +49,12 @@ if TYPE_CHECKING:
FINDING_ID_PATTERN = "F[0-9]{1,9}"
FINDING_ID_RE = re.compile(rf"^{FINDING_ID_PATTERN}\Z")
-SURROGATE_RE = re.compile(r"[\ud800-\udfff]")
REGULAR_FILE_MODE = "100644"
# \Z, not $: `$` also matches before a trailing newline, and this is a fence.
-REPORT_DIR_RE = re.compile(r"^CLAUDE-SECURITY-[0-9][0-9-]*\Z")
+REPORT_DIR_RE = re.compile(rf"^{re.escape(plugin.REPORT_DIR_PREFIX)}[0-9][0-9-]*\Z")
PATCHES_DIR_NAME = "patches"
SCRATCH_NAME_RE = re.compile(rf"^scratch-{FINDING_ID_PATTERN}\Z")
PATCH_DIR_RE = re.compile(r"^patch-[0-9][0-9-]*\Z")
-RUN_DIR_NAME = ".claude-security-run"
DIFF_HEADER = "diff --git "
CLAIM_KEYS = ("targeted", "no_new_vulnerability", "behaviour_unchanged")
CLAIM_LABELS = {
@@ -101,6 +100,15 @@ class Unit(TypedDict):
recommendation: str
+class Args(argparse.Namespace):
+ """The parsed command line."""
+
+ patch_dir: str = ""
+ patches_dir: str = ""
+ scan_root: str = ""
+ base: str = ""
+
+
class PatchError(Exception):
"""The run record or a raw diff is malformed; the caller must correct it."""
@@ -121,12 +129,11 @@ def field(value: object, what: str) -> str:
"""A record field as text; None reads as empty."""
if value is None:
return ""
- if not isinstance(value, str):
+ if not is_str(value):
msg = f"{what} must be a string"
raise PatchError(msg)
- lone = SURROGATE_RE.search(value)
- if lone:
- msg = f"{what} contains an unpaired surrogate ({lone.group(0)!r}); it is not valid text"
+ if strictjson.has_lone_surrogate(value):
+ msg = f"{what} contains an unpaired surrogate; it is not valid text"
raise PatchError(msg)
return value
@@ -140,20 +147,24 @@ def field_list(value: object, what: str) -> list[str]:
"""A list-of-strings record field."""
if value is None:
return []
- if not isinstance(value, list):
+ if not is_list(value):
msg = f"{what} must be a list of strings"
raise PatchError(msg)
- items = cast("list[object]", value)
- return [field(item, f"{what}[{index}]") for index, item in enumerate(items)]
+ return [field(item, f"{what}[{index}]") for index, item in enumerate(value)]
def build_claims(raw: object, unit_id: str, status: str) -> dict[str, Claim]:
"""Validate the three named claims. A written patch needs all three CONFIDENT."""
- claims_map = as_map(raw) or {}
+ claims_map: JsonMap = {}
+ if raw is not None:
+ if not is_map(raw):
+ msg = f"{unit_id}: claims must be an object keyed by claim name"
+ raise PatchError(msg)
+ claims_map = raw
out: dict[str, Claim] = {}
for key in CLAIM_KEYS:
- claim = as_map(claims_map.get(key))
- if claim is None:
+ claim = claims_map.get(key)
+ if not is_map(claim):
if status == "patch_written":
msg = f"{unit_id}: status is patch_written but claim {key!r} is missing"
raise PatchError(msg)
@@ -179,10 +190,9 @@ def build_claims(raw: object, unit_id: str, status: str) -> dict[str, Claim]:
return out
-def build_unit(raw: object, index: int) -> Unit:
+def build_unit(item: object, index: int) -> Unit:
"""Validate one unit from patches.json into the shape the writers use."""
- item = as_map(raw)
- if item is None:
+ if not is_map(item):
msg = f"patches.json unit {index} is not an object"
raise PatchError(msg)
unit_id = field(item.get("id"), f"unit {index} id")
@@ -227,20 +237,18 @@ def load_units(patch_dir: str) -> list[Unit]:
"""Read and validate patches.json (an object with a `units` array)."""
path = os.path.join(patch_dir, "patches.json")
try:
- with open(path, encoding="utf-8") as handle:
- raw = cast("object", json.load(handle))
+ raw = strictjson.load(path)
except OSError as error:
msg = "patches.json is missing from the patch directory. Write it before running this."
raise PatchError(msg) from error
except ValueError as error:
msg = f"patches.json is not valid JSON: {error}"
raise PatchError(msg) from error
- record = as_map(raw)
- units_raw: object = record.get("units") if record is not None else raw
- if not isinstance(units_raw, list):
+ units_raw = raw.get("units") if is_map(raw) else raw
+ if not is_list(units_raw):
msg = 'patches.json must be an object with a "units" array'
raise PatchError(msg)
- units = [build_unit(item, i) for i, item in enumerate(cast("list[object]", units_raw))]
+ units = [build_unit(item, i) for i, item in enumerate(units_raw)]
seen: set[str] = set()
for unit in units:
if unit["id"] in seen:
@@ -273,21 +281,6 @@ def read_diff(patch_dir: str, unit_id: str, required: bool) -> bytes | None:
return data
-def atomic_write_bytes(path: str, data: bytes) -> None:
- """Byte-faithful counterpart of render_report.atomic_write."""
- handle, temp = tempfile.mkstemp(dir=os.path.dirname(path), prefix=".render.")
- try:
- with os.fdopen(handle, "wb") as out:
- out.write(data)
- out.flush()
- os.fsync(out.fileno())
- os.replace(temp, path)
- except BaseException:
- with contextlib.suppress(OSError):
- os.unlink(temp)
- raise
-
-
def display_name(name: str | None) -> str | None:
"""A `--- `/`+++ ` line's file name for display: a/ or b/ dropped, None for /dev/null."""
if name is None:
@@ -398,7 +391,7 @@ def apply_check(top: str | None, patch_path: str) -> str:
def diffstat_lines(stats: list[DiffStat] | None) -> list[str]:
- """Diffstat as markdown bullets, or a one-line fallback when git was unavailable."""
+ """Diffstat as markdown bullets, or a one-line note when there is no diff to size."""
if stats is None:
return ["- _(no attempt diff was saved)_"]
if not stats:
@@ -592,7 +585,7 @@ def jsonl(
"apply_check": checks.get(unit["id"]),
"decline_reason": unit["decline_reason"] or None,
}
- rows.append(json.dumps(record, ensure_ascii=False, sort_keys=False))
+ rows.append(strictjson.text(record))
return "\n".join(rows) + ("\n" if rows else "")
@@ -631,7 +624,8 @@ def ensure_gitignore(report_dir: str) -> str:
except OSError:
return "open"
return "present" if "*" in (line.strip() for line in existing.splitlines()) else "open"
- atomic_write(path, "*\n")
+ with open(path, "w", encoding="utf-8", newline="\n") as out:
+ out.write("*\n")
return "written"
@@ -640,7 +634,7 @@ def contained_relpath(target: str, root: str) -> str | None:
rel = os.path.relpath(os.path.realpath(target), os.path.realpath(root))
if rel == ".." or rel.startswith(".." + os.sep) or os.path.isabs(rel):
return None
- return rel
+ return rel.replace(os.sep, "/")
def report_path_from_root(report_dir: str, top: str | None, fallback: str) -> str:
@@ -691,7 +685,7 @@ def run(patch_dir: str, patches_dir: str, scan_root: str, base: str) -> int:
if written and diff is not None:
patch_path = os.path.join(patches_dir, f"{unit['id']}.patch")
header = header_comment(unit, base, report_ref)
- atomic_write_bytes(patch_path, header.encode("utf-8") + diff)
+ pathlib.Path(patch_path).write_bytes(header.encode("utf-8") + diff)
check = apply_check(top, patch_path)
checks[unit["id"]] = check
note = note_written(unit, stats, check, report_ref)
@@ -700,13 +694,16 @@ def run(patch_dir: str, patches_dir: str, scan_root: str, base: str) -> int:
else:
note = note_declined(unit, stats)
print(f"{unit['id']}: no patch ({unit['status']}) -> {unit['id']}.md")
- atomic_write(os.path.join(patches_dir, f"{unit['id']}.md"), note)
+ note_path = os.path.join(patches_dir, f"{unit['id']}.md")
+ with open(note_path, "w", encoding="utf-8", newline="\n") as out:
+ out.write(note)
produced.add(f"{unit['id']}.md")
- index_text = index_markdown(units, base, report_dir_name, report_ref)
- atomic_write(os.path.join(patches_dir, "PATCHES.md"), index_text)
- atomic_write(
- os.path.join(patches_dir, "patches.jsonl"), jsonl(units, base, stats_by_id, checks)
- )
+ index_path = os.path.join(patches_dir, "PATCHES.md")
+ with open(index_path, "w", encoding="utf-8", newline="\n") as out:
+ out.write(index_markdown(units, base, report_dir_name, report_ref))
+ jsonl_path = os.path.join(patches_dir, "patches.jsonl")
+ with open(jsonl_path, "w", encoding="utf-8", newline="\n") as out:
+ out.write(jsonl(units, base, stats_by_id, checks))
for name in clear_stale_products(patches_dir, produced):
print(f"removed stale {name} (not produced by this run)")
swept, warnings = remove_workspaces_in(patch_dir)
@@ -749,8 +746,8 @@ def refuse_reason(path: str) -> str | None:
top = os.path.dirname(run)
if not PATCH_DIR_RE.match(os.path.basename(run)):
return "it is not inside a patch-<timestamp> run directory"
- if os.path.basename(top) != RUN_DIR_NAME:
- return f"its run directory is not inside {RUN_DIR_NAME}/"
+ if os.path.basename(top) != plugin.RUN_DIR_NAME:
+ return f"its run directory is not inside {plugin.RUN_DIR_NAME}/"
if not os.path.isdir(os.path.join(leaf, ".git")):
return "it holds no .git directory of its own"
return None
@@ -779,7 +776,7 @@ def remove_workspace(path: str) -> None:
try:
shutil.rmtree(target, onerror=clear_readonly)
except OSError as error:
- detail = error.args[0] if error.args else error
+ detail = console.removal_failure_detail(error)
msg = f"could not remove {path!r}: {detail}"
raise PatchError(msg) from error
@@ -820,12 +817,12 @@ def remove_patch_run(patch_dir: str) -> tuple[list[str], list[str]]:
run_dir = os.path.dirname(target)
if not PATCH_DIR_RE.match(os.path.basename(target)):
return removed, [f"left {patch_dir!r} in place: its name is not patch-<timestamp>"]
- if os.path.basename(run_dir) != RUN_DIR_NAME:
- return removed, [f"left {patch_dir!r} in place: it is not inside {RUN_DIR_NAME}/"]
+ if os.path.basename(run_dir) != plugin.RUN_DIR_NAME:
+ return removed, [f"left {patch_dir!r} in place: it is not inside {plugin.RUN_DIR_NAME}/"]
try:
shutil.rmtree(target, onerror=clear_readonly)
except OSError as error:
- detail = error.args[0] if error.args else error
+ detail = console.removal_failure_detail(error)
return removed, [f"could not remove {patch_dir!r}: {detail}"]
removed.append(target)
try:
@@ -855,23 +852,20 @@ def main(argv: list[str]) -> int:
parser.add_argument("patches_dir", help="the report's patches/ directory to write into")
parser.add_argument("scan_root", help="the user's repository root (for git apply --check)")
parser.add_argument("--base", required=True, help="the revision every patch applies to")
- args = parser.parse_args(argv)
- patch_dir = str(cast("object", args.patch_dir))
- patches_dir = str(cast("object", args.patches_dir))
- scan_root = str(cast("object", args.scan_root))
- base = str(cast("object", args.base))
- for label, path in (("patch dir", patch_dir), ("patches dir", patches_dir)):
+ args = parser.parse_args(argv, namespace=Args())
+ for label, path in (("patch dir", args.patch_dir), ("patches dir", args.patches_dir)):
if not os.path.isdir(path):
die_usage(f"{label} is not a directory: {path}")
- if not HEX_RE.match(base):
- die_usage(f"--base {base!r} is not a hex revision id")
+ if not plugin.SHA_RE.match(args.base):
+ die_usage(f"--base {args.base!r} is not a hex revision id")
try:
- return run(patch_dir, patches_dir, scan_root, base)
- except (PatchError, RenderError) as error:
+ return run(args.patch_dir, args.patches_dir, args.scan_root, args.base)
+ except PatchError as error:
die(str(error))
except OSError as error:
die(f"could not read or write the report's files: {error}")
if __name__ == "__main__":
+ console.tolerate_undecodable_names()
sys.exit(main(sys.argv[1:]))
diff --git a/plugins/claude-security/scripts/render_report.py b/plugins/claude-security/scripts/render_report.py
index a87b1922..c31bec27 100755
--- a/plugins/claude-security/scripts/render_report.py
+++ b/plugins/claude-security/scripts/render_report.py
@@ -2,41 +2,62 @@
"""Render a scan's machine-readable artifacts from its run directory.
Writes CLAUDE-SECURITY-RESULTS.jsonl (one finding per line, fields in a fixed
-order) and the CLAUDE-SECURITY-REVISION-<tag>.json stamp, places the report
-markdown beside them, then removes the scan's run directory now that its
-records are rendered. Filenames, JSONL field order, and verification.status
-semantics are stable across releases.
+order), CLAUDE-SECURITY-RESULTS.sarif (the same findings as a SARIF 2.1.0 log)
+and the CLAUDE-SECURITY-REVISION-<tag>.json stamp, places the report markdown
+beside them, then removes the scan's run directory now that its records are
+rendered. Findings that name one rule at one line of a file are one record in
+every product (see one_per_site). Filenames, JSONL field order, and
+verification.status semantics are stable across releases.
-Usage: render_report.py <run-dir> [--products-dir <dir>]
+Usage:
+ render_report.py <run_dir> [--products-dir <dir>]
+
+Exits 0 on success, 1 on a refusal naming what is wrong, 2 on a usage error.
Python 3.9-compatible, stdlib only.
"""
from __future__ import annotations
-import contextlib
-import json
+import argparse
+import ntpath
import os
import re
import shutil
import sys
-import tempfile
-from collections.abc import Mapping
+import uuid
+from collections import Counter
from datetime import datetime, timezone
-from typing import NoReturn, TypedDict, cast
+from pathlib import Path
+from typing import TYPE_CHECKING, NamedTuple, TypedDict
+
+# The lib/ package lives next to this script. Python normally adds a script's own
+# directory to the import path, but not under -P or PYTHONSAFEPATH, so we add it here.
+sys.path.insert(0, str(Path(__file__).resolve().parent))
+from lib import absolute, console, cwe, plugin, sarif, secret, strictjson
+from lib.finding import (
+ CONFIDENCES,
+ PANEL_KEEP_QUORUM,
+ PANEL_VOTER_COUNT,
+ SEVERITIES,
+ Finding,
+ FindingError,
+ build_finding,
+ panel_complete,
+)
+from lib.strictjson import JsonMap, is_int, is_list, is_map, is_str
-JsonMap = Mapping[str, object]
-Finding = dict[str, object]
+if TYPE_CHECKING:
+ from collections.abc import Mapping, Sequence
-class Panel(TypedDict, total=False):
- """A validated panel round: an int vote count and the fixed voter count."""
+class _ResearcherCounts(TypedDict, total=False):
+ """The two verification counts a vote record may omit."""
- true: int
- false: int
- voters: int
+ researchers_dispatched: int
+ researchers_returned: int
-class VerificationSummary(TypedDict, total=False):
+class VerificationSummary(_ResearcherCounts):
"""The stamp's `verification` object; every path names why if not verified."""
status: str
@@ -45,229 +66,86 @@ class VerificationSummary(TypedDict, total=False):
panel_votes: int
panel_reviewed_findings: int
panel_quorum_findings: int
- unreviewed_candidate_sites: object
+ unreviewed_candidate_sites: int
+ incomplete_panel_candidates: int
attested_findings: int
reason: str | None
- researchers_dispatched: int
- researchers_returned: int
-REPORT_FIELDS = (
- "id",
- "title",
- "impact",
- "file",
- "line",
- "description",
- "exploit_scenario",
- "preconditions",
- "category",
- "severity",
- "confidence",
- "recommendation",
- "cwe_id",
- "snippet",
- "symbol",
-)
+class Meta(NamedTuple):
+ """The scan meta a render reads back: the scan itself, and the stamp fields beside it."""
-SEPARATOR_ESCAPES = {0x85: "\\u0085", 0x2028: "\\u2028", 0x2029: "\\u2029"}
+ scan: sarif.Scan
+ scan_root: str
+ revision: object
+ revision_source: str
+ model: object
+ effort: object
-SEVERITIES = ("HIGH", "MEDIUM", "LOW")
-CONFIDENCES = ("low", "medium", "high")
-CONFIDENCE_RANK = {"low": 1, "medium": 2, "high": 3}
-PANEL_VOTER_COUNT = 3
-PANEL_KEEP_QUORUM = 2
+class Rendered(NamedTuple):
+ """A completed render: the findings, their verification, and the stamp's tag."""
-REVISION_PREFIX = "CLAUDE-SECURITY-REVISION-"
-RUN_DIR_NAME = ".claude-security-run"
-# \Z, not $: `$` also matches before a trailing newline, and this names a file.
-HEX_RE = re.compile(r"^[0-9a-fA-F]{7,64}\Z")
-FINDING_ID_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9_.-]{0,63}\Z")
-
-CATEGORY_ALIASES = {
- "sqli": "sql-injection",
- "sql injection": "sql-injection",
- "rce": "command-injection",
- "command execution": "command-injection",
- "cmdi": "command-injection",
- "xss": "xss",
- "cross-site scripting": "xss",
- "csrf": "csrf",
- "cross-site request forgery": "csrf",
- "ssrf": "ssrf",
- "path traversal": "path-traversal",
- "directory traversal": "path-traversal",
- "idor": "idor",
- "authz bypass": "improper-authorization",
- "authn bypass": "auth-bypass",
- "hardcoded credentials": "hardcoded-secret",
- "hardcoded password": "hardcoded-secret",
- "secret": "hardcoded-secret",
- "weak cryptography": "weak-crypto",
- "insecure randomness": "weak-randomness",
- "uaf": "use-after-free",
- "oob read": "out-of-bounds-read",
- "oob write": "out-of-bounds-write",
- "denial of service": "dos",
- "prototype pollution": "prototype-pollution",
-}
+ findings: list[Finding]
+ verification: VerificationSummary
+ tag: str
-class RenderError(Exception):
- """A refusal; the message names what the caller must fix."""
+REVISION_PREFIX = "CLAUDE-SECURITY-REVISION-"
+JSONL_NAME = "CLAUDE-SECURITY-RESULTS.jsonl"
+SARIF_NAME = "CLAUDE-SECURITY-RESULTS.sarif"
+SANITIZED_REMOTE_RE = re.compile(
+ r"https://[a-z0-9.-]+(?::[0-9]+)?/(?:[A-Za-z0-9._~/-]|%[0-9A-F]{2})+\Z"
+)
+# Set only by workflows/scan.js (its PROVENANCE) on each vote record it computes.
+VOTES_PROVENANCE = "workflows/scan.js"
-def as_map(value: object) -> JsonMap | None:
- """The value as a str-keyed mapping, or None when it is not one."""
- if isinstance(value, dict):
- return cast("JsonMap", value)
- return None
+class Args(argparse.Namespace):
+ """The parsed command line."""
+ run_dir: str = ""
+ products_dir: str | None = None
-def die(message: str) -> NoReturn:
- sys.stderr.write(f"render_report.py: {message}\n")
- sys.exit(1)
+class RenderError(Exception):
+ """A refusal; the message names what the caller must fix."""
-def read_json(run_dir: str, name: str, required: bool = True) -> object:
- path = os.path.join(run_dir, name)
+
+def read_json(run_dir: str, name: str) -> object:
+ """The JSON value in a run file the render requires; a missing or malformed one is a refusal."""
try:
- with open(path, encoding="utf-8") as handle:
- return cast("object", json.load(handle))
- except OSError as error:
- if required:
- msg = f"{name} is missing from the run directory. Write it before running this script."
- raise RenderError(msg) from error
- return None
+ return strictjson.load(os.path.join(run_dir, name))
+ except FileNotFoundError as error:
+ msg = f"{name} is missing from the run directory. Write it before running this script."
+ raise RenderError(msg) from error
except ValueError as error:
msg = f"{name} is not valid JSON: {error}"
raise RenderError(msg) from error
-def normalize_category(raw: object) -> str:
- """Lowercase/slugify a category and fold known synonyms."""
- text = str(raw or "").strip().lower()
- if text in CATEGORY_ALIASES:
- return CATEGORY_ALIASES[text]
- slug = re.sub(r"[^a-z0-9]+", "-", text).strip("-")
- return CATEGORY_ALIASES.get(slug, slug)
-
-
-def confidence_value(raw: object) -> str:
- """A finding's stated confidence, normalized to low|medium|high; refuses others."""
- if isinstance(raw, str):
- word = raw.strip().lower()
- if word in CONFIDENCE_RANK:
- return word
- msg = "confidence {!r} is not one of {}".format(raw, "/".join(CONFIDENCES))
- raise RenderError(msg)
-
-
-def panel_complete(record: object) -> Panel | None:
- """The validated panel dict for one round record, or None.
-
- A complete panel has `voters` equal to PANEL_VOTER_COUNT and an integer
- `true` vote count.
- """
- round_record = as_map(record)
- if round_record is None:
- return None
- panel = as_map(round_record.get("panel"))
- if panel is None:
- return None
- panel_true = panel.get("true")
- if not isinstance(panel_true, int) or isinstance(panel_true, bool):
- return None
- if panel.get("voters") != PANEL_VOTER_COUNT:
+def read_votes(run_dir: str) -> JsonMap | None:
+ """The workflow's vote record, or None when votes.json is absent or not marked as its own."""
+ try:
+ raw = strictjson.load(os.path.join(run_dir, "votes.json"))
+ except FileNotFoundError:
return None
- panel_false = panel.get("false")
- return {
- "true": panel_true,
- "false": panel_false if isinstance(panel_false, int) else 0,
- "voters": PANEL_VOTER_COUNT,
- }
-
-
-def vote_confidence_ceiling(rounds: object) -> str | None:
- """The vote-backed confidence ceiling for one finding, or None.
-
- A unanimous panel yields `high`; a keep quorum below unanimity yields
- `medium`. None means no usable vote record.
- """
- panel = panel_complete(rounds)
- if panel is None:
+ except ValueError as error:
+ msg = f"votes.json is not valid JSON: {error}"
+ raise RenderError(msg) from error
+ if not is_map(raw):
+ raise RenderError("votes.json must be a JSON object mapping the vote record")
+ if raw.get("provenance") != VOTES_PROVENANCE:
return None
- return "high" if panel.get("true", 0) >= PANEL_VOTER_COUNT else "medium"
-
-
-def build_finding(raw: object, index: int, rounds_by_id: JsonMap) -> Finding:
- """Validate one finding into exactly REPORT_FIELDS, in order."""
- item = as_map(raw)
- if item is None:
- msg = f"findings.json item {index} is not an object"
- raise RenderError(msg)
- finding_id = str(item.get("id") or f"F{index + 1}")
- if not FINDING_ID_RE.match(finding_id):
- msg = f"finding id {finding_id!r} is not a valid id"
- raise RenderError(msg)
-
- for required in ("title", "file", "description", "exploit_scenario"):
- if not item.get(required):
- msg = f"finding {finding_id} is missing required field {required!r}"
- raise RenderError(msg)
-
- severity = str(item.get("severity", "")).strip().upper()
- if severity not in SEVERITIES:
- msg = "finding {} severity {!r} is not one of {}".format(
- finding_id, item.get("severity"), "/".join(SEVERITIES)
- )
- raise RenderError(msg)
+ return raw
- confidence = confidence_value(item.get("confidence"))
- ceiling = vote_confidence_ceiling(rounds_by_id.get(finding_id))
- if ceiling is not None and CONFIDENCE_RANK[confidence] > CONFIDENCE_RANK[ceiling]:
- confidence = ceiling
- raw_line = item.get("line", 0)
+def read_source(scan_root: str, file: str) -> str | None:
+ """The text of a scanned file a finding names; None when it cannot be read."""
try:
- line = int(raw_line) if isinstance(raw_line, (int, float, str)) else int(str(raw_line))
- except (TypeError, ValueError, OverflowError) as error:
- msg = "finding {} line {!r} is not an integer".format(finding_id, item.get("line"))
- raise RenderError(msg) from error
-
- preconditions_raw: object = item.get("preconditions") or []
- if not isinstance(preconditions_raw, list):
- msg = f"finding {finding_id} preconditions must be a list"
- raise RenderError(msg)
-
- cwe = item.get("cwe_id")
- if cwe:
- text = str(cwe).strip().upper().replace("_", "-")
- if re.match(r"^\d{1,5}$", text):
- text = "CWE-" + text
- cwe = text if re.match(r"^CWE-\d{1,5}$", text) else None
- else:
- cwe = None
-
- finding = {
- "id": finding_id,
- "title": item.get("title"),
- "impact": item.get("impact") or "",
- "file": item.get("file"),
- "line": line,
- "description": item.get("description"),
- "exploit_scenario": item.get("exploit_scenario"),
- "preconditions": [str(p) for p in cast("list[object]", preconditions_raw)],
- "category": normalize_category(item.get("category")),
- "severity": severity,
- "confidence": confidence,
- "recommendation": item.get("recommendation") or "",
- "cwe_id": cwe,
- "snippet": item.get("snippet") or "",
- "symbol": item.get("symbol") or "",
- }
- return {k: finding[k] for k in REPORT_FIELDS}
+ return Path(scan_root, file).read_bytes().decode("utf-8", "surrogateescape")
+ except (OSError, ValueError):
+ return None
def read_coverage(run_dir: str) -> tuple[JsonMap | None, str]:
@@ -279,16 +157,12 @@ def read_coverage(run_dir: str) -> tuple[JsonMap | None, str]:
"""
name = "coverage.json"
try:
- raw = read_json(run_dir, name, required=False)
- except RenderError:
- return None, "unreadable"
- if raw is None:
- present = os.path.exists(os.path.join(run_dir, name))
- return None, ("unreadable" if present else "unavailable")
- cov = as_map(raw)
- if cov is None:
+ raw = strictjson.load(os.path.join(run_dir, name))
+ except FileNotFoundError:
+ return None, "unavailable"
+ except (OSError, ValueError):
return None, "unreadable"
- return cov, name
+ return (raw, name) if is_map(raw) else (None, "unreadable")
COVERAGE_TEXT_CAP = 300
@@ -296,73 +170,84 @@ COVERAGE_TEXT_CAP = 300
def coverage_text(value: object, cap: int = COVERAGE_TEXT_CAP) -> str | None:
"""A coverage string, trimmed to `cap`, or None when the value is not a string."""
- if not isinstance(value, str):
+ if not is_str(value):
return None
if len(value) > cap:
return value[:cap] + f"...[+{len(value) - cap} chars]"
return value
-def skipped_components(raw: object) -> list[dict[str, object]] | None:
+def coverage_texts(raw: object, cap: int) -> list[str]:
+ """The strings among a coverage list, each trimmed to `cap`; anything else is dropped."""
+ items: list[object] = raw if is_list(raw) else []
+ return [text for item in items if (text := coverage_text(item, cap))]
+
+
+def tree_relative(path: str, scan_root: str) -> str | None:
+ """A skipped path relative to the scan root; None for an absolute one that is not inside it."""
+ if not absolute.spelled(path):
+ return path
+ try:
+ relative = os.path.relpath(os.path.realpath(path), scan_root).replace("\\", "/")
+ except (ValueError, OSError):
+ return None
+ return None if relative == ".." or relative.startswith("../") else relative
+
+
+def skipped_component(item: JsonMap, scan_root: str) -> dict[str, object]:
+ paths = (tree_relative(path, scan_root) for path in coverage_texts(item.get("paths"), 200))
+ return {
+ "name": coverage_text(item.get("name"), 100) or "",
+ "paths": [path for path in paths if path is not None],
+ "reason": coverage_text(item.get("reason")) or "",
+ }
+
+
+def skipped_components(raw: object, scan_root: str) -> list[dict[str, object]] | None:
"""coverage.skippedComponents as [{name, paths, reason}], or None when unusable."""
- if not isinstance(raw, list):
+ if not is_list(raw):
return None
- out: list[dict[str, object]] = []
- for entry in cast("list[object]", raw):
- item = as_map(entry)
- if item is None:
- continue
- paths_raw = item.get("paths")
- paths_in: list[object] = (
- cast("list[object]", paths_raw) if isinstance(paths_raw, list) else []
- )
- paths = [text for text in (coverage_text(p, 200) for p in paths_in) if text]
- out.append({
- "name": coverage_text(item.get("name"), 100) or "",
- "paths": paths,
- "reason": coverage_text(item.get("reason")) or "",
- })
- return out
+ return [skipped_component(entry, scan_root) for entry in raw if is_map(entry)]
def coverage_enum(value: object, allowed: tuple[str, ...]) -> str | None:
"""A coverage enum field, or None when absent or not one of the known values."""
- return value if isinstance(value, str) and value in allowed else None
+ return value if is_str(value) and value in allowed else None
+
+def coverage_count(value: object) -> int | None:
+ """A coverage count field, or None when absent or not an integer."""
+ return value if is_int(value) else None
-def run_shape(coverage: JsonMap | None, source: str, effort: object) -> dict[str, object]:
+
+def run_shape(
+ coverage: JsonMap | None, source: str, effort: object, scan_root: str
+) -> dict[str, object]:
"""What shape actually ran, distinct from the effort tier that was asked."""
shape: dict[str, object] = {"requested_effort": effort, "collapsed": None, "source": source}
if coverage is None:
return shape
- shape["collapsed"] = coverage.get("collapsed")
- shape["diff_files"] = coverage.get("diffFiles")
- shape["diff_lines"] = coverage.get("diffLines")
- shape["scope_files"] = coverage.get("scopeFiles")
- shape["empty_diff"] = bool(coverage.get("emptyDiff"))
- shape["empty_scope"] = bool(coverage.get("emptyScope"))
- shape["researchers_dispatched"] = coverage.get("researchersDispatched")
- shape["skipped_components"] = skipped_components(coverage.get("skippedComponents"))
- shape["completeness_check_outcome"] = coverage_enum(
- coverage.get("completenessCheckOutcome"),
- ("checked", "partial", "not-checkable", "not-applicable"),
- )
- unaccounted_raw = coverage.get("unaccountedTopLevelDirs")
- unaccounted_in: list[object] = (
- cast("list[object]", unaccounted_raw) if isinstance(unaccounted_raw, list) else []
- )
- shape["unaccounted_top_level_dirs"] = [
- text for text in (coverage_text(x, 200) for x in unaccounted_in) if text
- ]
- shape["inventory_fallback"] = coverage_enum(
- coverage.get("inventoryFallback"),
- ("inventory-failed", "empty-partition", "incomplete-partition"),
- )
- top_count = coverage.get("topLevelCount")
- shape["top_level_dir_count"] = (
- top_count if isinstance(top_count, int) and not isinstance(top_count, bool) else None
- )
- return shape
+ return {
+ **shape,
+ "collapsed": coverage_enum(coverage.get("collapsed"), ("small-diff", "small-scope")),
+ "diff_files": coverage_count(coverage.get("diffFiles")),
+ "diff_lines": coverage_count(coverage.get("diffLines")),
+ "scope_files": coverage_count(coverage.get("scopeFiles")),
+ "empty_diff": bool(coverage.get("emptyDiff")),
+ "empty_scope": bool(coverage.get("emptyScope")),
+ "researchers_dispatched": coverage_count(coverage.get("researchersDispatched")),
+ "skipped_components": skipped_components(coverage.get("skippedComponents"), scan_root),
+ "completeness_check_outcome": coverage_enum(
+ coverage.get("completenessCheckOutcome"),
+ ("checked", "partial", "not-checkable", "not-applicable"),
+ ),
+ "unaccounted_top_level_dirs": coverage_texts(coverage.get("unaccountedTopLevelDirs"), 200),
+ "inventory_fallback": coverage_enum(
+ coverage.get("inventoryFallback"),
+ ("inventory-failed", "empty-partition", "incomplete-partition"),
+ ),
+ "top_level_dir_count": coverage_count(coverage.get("topLevelCount")),
+ }
def verification_summary(
@@ -372,174 +257,335 @@ def verification_summary(
) -> VerificationSummary:
"""Compute the stamp's verification object from the vote record.
- status is 'verified' only when the vote record proves the panel ran for
- every finding the report contains; otherwise 'unverified' with a `reason`.
- votes_present is False when votes.json was absent from the run directory.
+ status is 'verified' only when the vote record proves a complete panel
+ round for every finding the report contains and for every other candidate
+ it holds a round for; otherwise 'unverified' with a `reason`.
+ `incomplete_panel_candidates` counts the unreported candidates whose round
+ is not complete. votes_present is False when read_votes returned None.
"""
- rounds = as_map(votes.get("rounds")) or {}
- panel_reviewed = 0
- panel_quorum = 0
- incomplete: list[str] = []
-
- for finding in findings:
- finding_id = str(finding.get("id", ""))
- panel = panel_complete(rounds.get(finding_id))
- if panel is None:
- incomplete.append(finding_id)
- continue
- panel_reviewed += 1
- if panel.get("true", 0) >= PANEL_KEEP_QUORUM:
- panel_quorum += 1
+ raw_rounds = votes.get("rounds")
+ rounds: JsonMap = raw_rounds if is_map(raw_rounds) else {}
+ panels = [(f["id"], panel_complete(rounds.get(f["id"]))) for f in findings]
+ incomplete = sorted(finding_id for finding_id, panel in panels if panel is None)
+ reviewed = [panel for _, panel in panels if panel is not None]
+ quorum = sum(panel["true"] >= PANEL_KEEP_QUORUM for panel in reviewed)
+ reported = {f["id"] for f in findings}
+ dropped_incomplete = sorted(
+ round_id
+ for round_id, record in rounds.items()
+ if round_id not in reported and panel_complete(record) is None
+ )
def as_count(key: str) -> int:
"""A vote count as a non-negative int; a wrong shape is a refusal."""
- value = votes.get(key, 0)
- if isinstance(value, bool) or not isinstance(value, int) or value < 0:
+ raw = votes.get(key, 0)
+ if not is_int(raw) or raw < 0:
msg = (
- f"votes.json field {key!r} is not a non-negative integer ({value!r}); the "
+ f"votes.json field {key!r} is not a non-negative integer ({raw!r}); the "
"vote record is malformed"
)
raise RenderError(msg)
- return value
+ return raw
- def optional_count(key: str) -> int | None:
- """A count that may be absent: None when so, else as_count's contract."""
- if key not in votes:
- return None
- return as_count(key)
+ candidates = as_count("candidates")
+ dispatched = as_count("researchers_dispatched") if "researchers_dispatched" in votes else None
+ returned = as_count("researchers_returned") if "researchers_returned" in votes else None
- candidates_recorded = "candidates" in votes
- researchers_dispatched = optional_count("researchers_dispatched")
- researchers_returned = optional_count("researchers_returned")
-
- summary: dict[str, object] = {
- "status": "verified",
- "candidates": as_count("candidates"),
- "candidates_deduped": as_count("candidates_deduped"),
- "panel_votes": as_count("panel_votes"),
- "panel_reviewed_findings": panel_reviewed,
- "panel_quorum_findings": panel_quorum,
- "unreviewed_candidate_sites": as_count("unreviewed_candidate_sites"),
- "attested_findings": 0,
- "reason": None,
- }
- if researchers_dispatched is not None:
- summary["researchers_dispatched"] = researchers_dispatched
- if researchers_returned is not None:
- summary["researchers_returned"] = researchers_returned
-
- reportable: list[Finding] = findings
+ reason: str | None = None
if not votes_present:
- summary["status"] = "unverified"
- summary["reason"] = (
- "votes.json is absent from the run directory: the verification "
- "pipeline left no vote record, so nothing about this report can be "
- "attested"
+ reason = (
+ "votes.json is absent from the run directory or is not the scan workflow's record: "
+ "the verification pipeline left no vote record, so nothing about this report can "
+ "be attested"
)
- elif not candidates_recorded:
- summary["status"] = "unverified"
- summary["reason"] = (
- "votes.json has no 'candidates' field: the vote record does not "
- "prove the pipeline ran, so nothing about this report can be attested"
+ elif "candidates" not in votes:
+ reason = (
+ "votes.json has no 'candidates' field: the vote record does not prove the pipeline "
+ "ran, so nothing about this report can be attested"
)
- elif researchers_dispatched and researchers_returned == 0:
- summary["status"] = "unverified"
- summary["reason"] = (
- f"{researchers_dispatched} research agent(s) were dispatched but none returned; "
- "the scan examined nothing"
+ elif dispatched and returned == 0:
+ reason = (
+ f"{dispatched} research agent(s) were dispatched but none returned; the scan "
+ "examined nothing"
)
elif incomplete:
- summary["status"] = "unverified"
- summary["reason"] = (
+ reason = (
f"these findings have no complete {PANEL_VOTER_COUNT}-voter panel round: "
- f"{', '.join(sorted(incomplete))}"
+ f"{', '.join(incomplete)}"
)
- elif reportable and panel_quorum != len(reportable):
- summary["status"] = "unverified"
- summary["reason"] = (
- f"{len(reportable) - panel_quorum} of {len(reportable)} reported findings did not "
- "reach the keep quorum, so the report contains findings the panel rejected"
+ elif findings and quorum != len(findings):
+ reason = (
+ f"{len(findings) - quorum} of {len(findings)} reported findings did not reach the "
+ "keep quorum, so the report contains findings the panel rejected"
)
- elif not findings and not votes.get("rounds") and summary["candidates"]:
- summary["status"] = "unverified"
- summary["reason"] = f"{summary['candidates']} candidates were recorded but none was paneled"
- elif not findings and rounds and not any(panel_complete(record) for record in rounds.values()):
- summary["status"] = "unverified"
- summary["reason"] = (
+ elif not findings and not rounds and candidates:
+ reason = f"{candidates} candidates were recorded but none was paneled"
+ elif not findings and rounds and not any(map(panel_complete, rounds.values())):
+ reason = (
f"{len(rounds)} panel round(s) were dispatched but none completed a full "
f"{PANEL_VOTER_COUNT}-voter review; no candidate was actually verified"
)
- return cast("VerificationSummary", cast("object", summary))
+ elif dropped_incomplete:
+ reason = (
+ f"{len(dropped_incomplete)} candidate(s) were dropped without a complete "
+ f"{PANEL_VOTER_COUNT}-voter panel round: {', '.join(dropped_incomplete)}"
+ )
+ summary: VerificationSummary = {
+ "status": "verified" if reason is None else "unverified",
+ "candidates": candidates,
+ "candidates_deduped": as_count("candidates_deduped"),
+ "panel_votes": as_count("panel_votes"),
+ "panel_reviewed_findings": len(reviewed),
+ "panel_quorum_findings": quorum,
+ "unreviewed_candidate_sites": as_count("unreviewed_candidate_sites"),
+ "incomplete_panel_candidates": len(dropped_incomplete),
+ "attested_findings": 0,
+ "reason": reason,
+ }
+ if dispatched is not None:
+ summary["researchers_dispatched"] = dispatched
+ if returned is not None:
+ summary["researchers_returned"] = returned
+ return summary
def revision_tag(revision: object) -> str:
"""The stamp's filename tag: <sha12>[-dirty], or UNVERSIONED."""
- rev = as_map(revision) or {}
- sha = rev.get("commit") or rev.get("head")
+ if not is_map(revision):
+ msg = f"the run's revision {revision!r} is not an object, so it cannot name the stamp file"
+ raise RenderError(msg)
+ sha = revision.get("commit") or revision.get("head")
if not sha:
return "UNVERSIONED"
- if not (isinstance(sha, str) and HEX_RE.match(sha)):
+ if not is_str(sha) or not plugin.SHA_RE.match(sha):
msg = f"the run's revision {sha!r} is not a hex commit id, so it cannot name the stamp file"
raise RenderError(msg)
- return sha[:12] + ("" if rev.get("dirty") is False else "-dirty")
+ return sha[:12] + ("" if revision.get("dirty") is False else "-dirty")
+
+
+def show_prefix_shaped(prefix: str) -> bool:
+ """Whether `prefix` is what `git rev-parse --show-prefix` prints: empty, or `a/b/`."""
+ if not prefix:
+ return True
+ return (
+ prefix.endswith("/")
+ and "\\" not in prefix
+ and not ntpath.splitdrive(prefix)[0]
+ and all(segment not in {"", ".", ".."} for segment in prefix.split("/")[:-1])
+ )
-def atomic_write(path: str, text: str) -> None:
- """Write `text` atomically: a temp file in the same directory, then replace."""
- directory = os.path.dirname(path)
- handle, temp = tempfile.mkstemp(dir=directory, prefix=".render.")
+def scan_of(meta: JsonMap) -> Meta:
+ """The scan meta the run records, every field shape-checked; a wrong one is a refusal."""
+ scan_id = meta.get("scan_id")
try:
- with os.fdopen(handle, "w", encoding="utf-8") as out:
- out.write(text)
- out.flush()
- os.fsync(out.fileno())
- os.replace(temp, path)
- except BaseException:
- with contextlib.suppress(OSError):
- os.unlink(temp)
- raise
-
-
-def jsonl_line(finding: Finding) -> str:
- """One finding, fixed field order, separators escaped."""
- text = json.dumps(finding, ensure_ascii=False, sort_keys=False)
- return text.translate(SEPARATOR_ESCAPES)
-
-
-def render(run_dir: str, products_dir: str) -> tuple[list[Finding], VerificationSummary, str]:
- meta_raw = read_json(run_dir, "scan-meta.json")
- findings_raw = read_json(run_dir, "findings.json")
- votes: object = read_json(run_dir, "votes.json", required=False)
- coverage, coverage_source = read_coverage(run_dir)
- votes_present = votes is not None
- if votes is None:
- votes = {}
-
- if not isinstance(findings_raw, list):
- raise RenderError("findings.json must be a JSON array (use [] for no findings)")
- meta = as_map(meta_raw)
- if meta is None:
- raise RenderError("scan-meta.json must be a JSON object")
- votes_map = as_map(votes)
- if votes_map is None:
- raise RenderError("votes.json must be a JSON object mapping the vote record")
- rounds_raw = votes_map.get("rounds")
- rounds_by_id: JsonMap = {} if rounds_raw is None else (as_map(rounds_raw) or {})
- if rounds_raw is not None and not isinstance(rounds_raw, dict):
- kind = type(rounds_raw).__name__
- msg = f"votes.json 'rounds' must be an object keyed by finding id, not {kind}"
+ value = uuid.UUID(scan_id) if is_str(scan_id) else None
+ except ValueError:
+ value = None
+ if value is None or value.version is None or not 1 <= value.version <= 5:
+ msg = (
+ f"scan-meta.json scan_id {scan_id!r} is not a version 1-5 UUID; "
+ "rerun write_scan_meta.py to mint one"
+ )
+ raise RenderError(msg)
+ mode = meta.get("mode")
+ if not is_str(mode) or mode not in plugin.MODES:
+ msg = f"scan-meta.json mode {mode!r} is not a scan mode; rerun write_scan_meta.py"
+ raise RenderError(msg)
+ scan_root = meta.get("scan_root")
+ if not is_str(scan_root) or not scan_root.strip():
+ msg = f"scan-meta.json scan_root {scan_root!r} is not a path; rerun write_scan_meta.py"
+ raise RenderError(msg)
+ prefix = meta.get("scan_prefix")
+ if prefix is None:
+ prefix = ""
+ if not is_str(prefix) or not show_prefix_shaped(prefix):
+ msg = (
+ f"scan-meta.json scan_prefix {prefix!r} is not a path prefix; rerun write_scan_meta.py"
+ )
+ raise RenderError(msg)
+ remote = meta.get("remote")
+ if remote is not None and (not is_str(remote) or not SANITIZED_REMOTE_RE.match(remote)):
+ msg = (
+ f"scan-meta.json remote {remote!r} is not a sanitized repository URL; "
+ "rerun write_scan_meta.py"
+ )
+ raise RenderError(msg)
+ scope = meta.get("scope", [])
+ entries = [entry for entry in scope if is_str(entry)] if is_list(scope) else []
+ if not is_list(scope) or len(entries) != len(scope):
+ msg = (
+ f"scan-meta.json scope {scope!r} is not the list of paths the scan covered; "
+ "rerun write_scan_meta.py"
+ )
raise RenderError(msg)
- findings = [
- build_finding(raw, i, rounds_by_id)
- for i, raw in enumerate(cast("list[object]", findings_raw))
+ revision: object = meta.get("revision")
+ if revision is None:
+ revision = {}
+ revision_source = meta.get("revision_source", "self-reported")
+ if not is_str(revision_source):
+ msg = (
+ f"scan-meta.json revision_source {revision_source!r} does not name what vouches for "
+ "the revision; rerun write_scan_meta.py"
+ )
+ raise RenderError(msg)
+ clean_commit: str | None = None
+ if is_map(revision) and revision.get("dirty") is False:
+ commit = revision.get("commit")
+ clean_commit = commit if is_str(commit) and plugin.SHA_RE.match(commit) else None
+ scan = sarif.Scan(
+ id=value,
+ mode=mode,
+ prefix=prefix,
+ remote=remote,
+ scope=tuple(entries),
+ revision=clean_commit,
+ )
+ return Meta(scan, scan_root, revision, revision_source, meta.get("model"), meta.get("effort"))
+
+
+def jsonl_text(findings: Sequence[Finding]) -> str:
+ """The findings as JSONL: one record per line as the products carry it, findings.json order."""
+ return "".join(strictjson.text(secret.withheld(item)) + "\n" for item in findings)
+
+
+def strength(finding: Finding) -> tuple[int, int]:
+ """A finding's rank among those at one site: severity first, then confidence."""
+ return -SEVERITIES.index(finding["severity"]), CONFIDENCES.index(finding["confidence"])
+
+
+def one_per_site(
+ findings: Sequence[Finding], scan: sarif.Scan, sources: Mapping[str, str]
+) -> tuple[list[Finding], list[str]]:
+ """The findings reduced to one per site, and one disclosure sentence per finding merged away.
+
+ A site is a rule at a line of a file (sarif.site), which is what a result
+ stands for to a SARIF or JSONL consumer, so the products carry one record
+ for it: of the findings at one site the strongest is kept, the first of
+ them in findings.json order when they tie, and each of the others is
+ named in a sentence with the finding it was merged into. A finding with
+ no site, one whose line was never determined, is kept as it is.
+ """
+ sites = [sarif.site(item, scan, sources.get(item["file"])) for item in findings]
+ by_site: dict[sarif.Site, list[Finding]] = {}
+ for item, where in zip(findings, sites):
+ if where is not None:
+ by_site.setdefault(where, []).append(item)
+ kept = {where: max(group, key=strength) for where, group in by_site.items()}
+ merged = [
+ f"finding {other['id']} names the same site as finding {kept[where]['id']}, "
+ f"{where.path}:{where.line} under rule {where.rule}; merged into it"
+ for where, group in by_site.items()
+ for other in group
+ if other is not kept[where]
+ ]
+ unmerged = [
+ item if where is None else kept[where]
+ for item, where in zip(findings, sites)
+ if where is None or item is by_site[where][0]
+ ]
+ return unmerged, merged
+
+
+def unrecognized_cwes(findings: Sequence[Finding]) -> list[str]:
+ """One disclosure sentence per finding whose declared CWE the pinned release does not define."""
+ return [
+ f"finding {item['id']} cwe_id {item['cwe_id']} is not a weakness in "
+ f"CWE {cwe.catalog.version}; filed as Uncategorized"
+ for item in findings
+ if not cwe.catalog.defines(cwe.id_number(item["cwe_id"]))
]
- seen = {}
- for finding in findings:
- if finding["id"] in seen:
- msg = "finding id {!r} appears twice in findings.json".format(finding["id"])
+
+def notifications_of(
+ shape: Mapping[str, object],
+ verification: VerificationSummary,
+ merged: Sequence[str],
+ unrecognized: Sequence[str],
+ symlinks: Sequence[str],
+ revision: object,
+) -> list[dict[str, object]]:
+ """The invocation notifications: what was skipped, capped, merged, mislabeled or unverified.
+
+ The sentences of `merged` (one_per_site) are disclosed at level note, those
+ of `unrecognized` (unrecognized_cwes) at level warning. `symlinks` names
+ the root-level symbolic links the scan's extent left out unfollowed.
+ """
+ note = sarif.notification
+ skipped = shape.get("skipped_components")
+ notes = [
+ note("coverage/skipped-component", "note", f"Skipped component {s['name']}: {s['reason']}")
+ for s in (skipped if is_list(skipped) else [])
+ if is_map(s)
+ ]
+ if is_map(revision) and revision.get("sparse") is True:
+ absent = revision.get("not_checked_out_dirs")
+ names = ", ".join(d for d in (absent if is_list(absent) else []) if is_str(d))
+ text = "Sparse checkout: only the checked-out part of the repository was scanned"
+ if names:
+ text += f"; tracked top-level directories not checked out: {names}"
+ notes.append(note("coverage/sparse-checkout", "note", text))
+ unaccounted = shape.get("unaccounted_top_level_dirs")
+ if is_list(unaccounted) and unaccounted:
+ names = ", ".join(d for d in unaccounted if is_str(d))
+ text = f"Top-level directories the accepted partition left unaccounted: {names}"
+ notes.append(note("coverage/unaccounted-top-level-dirs", "note", text))
+ if symlinks:
+ names = ", ".join(symlinks)
+ text = f"Root-level symbolic links not followed, left out of the scan's extent: {names}"
+ notes.append(note("coverage/unfollowed-symlinks", "note", text))
+ if unreviewed := verification["unreviewed_candidate_sites"]:
+ text = f"{unreviewed} candidate site(s) were recorded but never reviewed by the panel"
+ notes.append(note("coverage/unverified-by-cap", "warning", text))
+ if dropped := verification["incomplete_panel_candidates"]:
+ text = (
+ f"{dropped} candidate(s) were dropped without a complete "
+ f"{PANEL_VOTER_COUNT}-voter panel round"
+ )
+ notes.append(note("verification/incomplete-panel", "warning", text))
+ notes += [note("finding/merged", "note", text) for text in merged]
+ notes += [note("cwe/unrecognized", "warning", text) for text in unrecognized]
+ if verification["status"] == "unverified":
+ notes.append(note("verification/unverified", "error", verification["reason"] or ""))
+ return notes
+
+
+def render(run_dir: str, products_dir: str) -> Rendered:
+ """Read the run's records, validate them, build every product, then write them, stamp last."""
+ meta = read_json(run_dir, "scan-meta.json")
+ if not is_map(meta):
+ raise RenderError("scan-meta.json must be a JSON object")
+ findings_in = read_json(run_dir, "findings.json")
+ if not is_list(findings_in):
+ raise RenderError("findings.json must be a JSON array (use [] for no findings)")
+ coverage, coverage_source = read_coverage(run_dir)
+ votes_raw = read_votes(run_dir)
+ votes: JsonMap = {} if votes_raw is None else votes_raw
+ rounds_raw = votes.get("rounds")
+ rounds_by_id: JsonMap = {}
+ if rounds_raw is not None:
+ if not is_map(rounds_raw):
+ kind = type(rounds_raw).__name__
+ msg = f"votes.json 'rounds' must be an object keyed by finding id, not {kind}"
raise RenderError(msg)
- seen[finding["id"]] = True
+ rounds_by_id = rounds_raw
+ scan, scan_root, revision, revision_source, model, effort = scan_of(meta)
+ tag = revision_tag(revision)
+ built = [
+ build_finding(raw, i, rounds_by_id, scan_root, scan.prefix, scan.mode == "scan")
+ for i, raw in enumerate(findings_in)
+ ]
+ counted = Counter(f["id"] for f in built)
+ repeated = sorted(finding_id for finding_id, count in counted.items() if count > 1)
+ if repeated:
+ msg = f"findings.json uses these finding ids more than once: {', '.join(repeated)}"
+ raise RenderError(msg)
+ sources = {
+ path: text
+ for path in {f["file"] for f in built}
+ if (text := read_source(scan_root, path)) is not None
+ }
+ findings, merged = one_per_site(built, scan, sources)
markdown_path = os.path.join(run_dir, "CLAUDE-SECURITY-RESULTS.md")
if not os.path.isfile(markdown_path):
@@ -548,36 +594,26 @@ def render(run_dir: str, products_dir: str) -> tuple[list[Finding], Verification
"report before running this script."
)
with open(markdown_path, encoding="utf-8", newline="") as handle:
- markdown = handle.read()
-
- counts: dict[str, int] = dict.fromkeys(SEVERITIES, 0)
- for finding in findings:
- counts[str(finding.get("severity", ""))] += 1
-
- verification = verification_summary(findings, votes_map, votes_present=votes_present)
- revision: object = meta.get("revision") or {}
- tag = revision_tag(revision)
-
- atomic_write(
- os.path.join(products_dir, "CLAUDE-SECURITY-RESULTS.jsonl"),
- "".join(jsonl_line(f) + "\n" for f in findings),
- )
- markdown_out = os.path.join(products_dir, "CLAUDE-SECURITY-RESULTS.md")
- if os.path.realpath(markdown_path) != os.path.realpath(markdown_out):
- atomic_write(markdown_out, markdown)
- os.unlink(markdown_path)
+ try:
+ markdown = handle.read()
+ except UnicodeDecodeError as error:
+ msg = f"CLAUDE-SECURITY-RESULTS.md is not valid UTF-8: {error}"
+ raise RenderError(msg) from error
+ counts = Counter(f["severity"] for f in findings)
+ verification = verification_summary(findings, votes, votes_present=votes_raw is not None)
+ shape = run_shape(coverage, coverage_source, effort, scan_root)
stamp: dict[str, object] = {
"generated_at": datetime.now(timezone.utc).replace(microsecond=0).isoformat(),
- "scan_root": meta.get("scan_root"),
- "products_dir": products_dir,
- "mode": meta.get("mode"),
- "scope": meta.get("scope") or [],
+ "scan_id": str(scan.id),
+ "mode": scan.mode,
+ "scan_prefix": scan.prefix,
+ "scope": list(scan.scope),
"revision": revision,
- "revision_source": meta.get("revision_source") or "self-reported",
- "model": meta.get("model"),
- "effort": meta.get("effort"),
- "run_shape": run_shape(coverage, coverage_source, meta.get("effort")),
+ "revision_source": revision_source,
+ "model": model,
+ "effort": effort,
+ "run_shape": shape,
"findings": {
"total": len(findings),
"high": counts["HIGH"],
@@ -586,66 +622,101 @@ def render(run_dir: str, products_dir: str) -> tuple[list[Finding], Verification
},
"verification": verification,
}
+
+ jsonl = jsonl_text(findings)
+ run_properties = {k: v for k, v in stamp.items() if k != "model" or v is not None}
+ panels = {
+ f["id"]: panel for f in findings if (panel := panel_complete(rounds_by_id.get(f["id"])))
+ }
+ unrecognized = unrecognized_cwes(findings)
+ for text in merged + unrecognized:
+ sys.stderr.write(f"render_report.py: {text}\n")
+ symlinks = coverage_texts(meta.get("unfollowed_symlinks"), 200)
+ notifications = notifications_of(shape, verification, merged, unrecognized, symlinks, revision)
+ sarif_log = sarif.log(
+ findings, scan, plugin.version(), run_properties, panels, sources, notifications
+ )
+ sarif_doc = strictjson.text(sarif_log, indent=2) + "\n"
for stale in os.listdir(products_dir):
if stale.startswith(REVISION_PREFIX) and stale.endswith(".json"):
os.unlink(os.path.join(products_dir, stale))
- atomic_write(
- os.path.join(products_dir, f"{REVISION_PREFIX}{tag}.json"),
- json.dumps(stamp, indent=2) + "\n",
- )
+ with open(os.path.join(products_dir, JSONL_NAME), "w", encoding="utf-8", newline="\n") as out:
+ out.write(jsonl)
+ with open(os.path.join(products_dir, SARIF_NAME), "w", encoding="utf-8", newline="\n") as out:
+ out.write(sarif_doc)
+ markdown_out = os.path.join(products_dir, "CLAUDE-SECURITY-RESULTS.md")
+ relocated = os.path.realpath(markdown_path) != os.path.realpath(markdown_out)
+ if relocated:
+ with open(markdown_out, "w", encoding="utf-8", newline="\n") as out:
+ out.write(markdown)
+ stamp_path = os.path.join(products_dir, f"{REVISION_PREFIX}{tag}.json")
+ with open(stamp_path, "w", encoding="utf-8", newline="\n") as out:
+ out.write(strictjson.text(stamp, indent=2) + "\n")
+ if relocated:
+ os.unlink(markdown_path)
- return findings, verification, tag
+ return Rendered(findings, verification, tag)
def remove_run_dir(run_dir: str, products_dir: str) -> str:
"""Remove the scan's run directory once rendered; returns a one-line status."""
target = os.path.normpath(os.path.abspath(run_dir))
- if os.path.basename(target) != RUN_DIR_NAME:
- return f"kept {run_dir} (not a {RUN_DIR_NAME} run directory)"
+ if os.path.basename(target) != plugin.RUN_DIR_NAME:
+ return f"kept {run_dir} (not a {plugin.RUN_DIR_NAME} run directory)"
if os.path.realpath(target) == os.path.realpath(products_dir):
return f"kept {run_dir} (it holds the products)"
try:
shutil.rmtree(target)
except OSError as error:
- detail = error.args[0] if error.args else error
+ detail = console.removal_failure_detail(error)
return f"WARNING: could not remove run directory {run_dir}: {detail}"
return f"removed run directory {run_dir}"
+def argument_parser() -> argparse.ArgumentParser:
+ """The command line: which run directory to render, and where its products go."""
+ parser = argparse.ArgumentParser(
+ prog="render_report.py",
+ description="Render a scan's machine-readable artifacts from its run directory.",
+ )
+ parser.add_argument("run_dir", help="the run directory holding the scan's records")
+ parser.add_argument(
+ "--products-dir", help="where the products are written (default: the run directory)"
+ )
+ return parser
+
+
def main(argv: list[str]) -> int:
- products_dir: str | None = None
- args = list(argv)
- if len(args) == 3 and args[1] == "--products-dir":
- products_dir = args.pop(2)
- args.pop(1)
- if len(args) != 1:
- die("usage: render_report.py <run-dir> [--products-dir <dir>]")
- run_dir = args[0]
- if not os.path.isdir(run_dir):
- die(f"not a directory: {run_dir}")
- products_dir = products_dir or run_dir
+ parser = argument_parser()
+ args = parser.parse_args(argv, namespace=Args())
+ if not os.path.isdir(args.run_dir):
+ parser.error(f"not a directory: {args.run_dir}")
+ products_dir = args.products_dir or args.run_dir
if not os.path.isdir(products_dir):
- die(f"products directory is not a directory: {products_dir}")
+ parser.error(f"products directory is not a directory: {products_dir}")
try:
- findings, verification, tag = render(run_dir, products_dir)
- except RenderError as error:
- die(str(error))
+ rendered = render(args.run_dir, products_dir)
+ except (RenderError, FindingError) as error:
+ sys.stderr.write(f"render_report.py: {error}\n")
+ return 1
except OSError as error:
- die(f"could not read or write the report's files: {error}")
- removal = remove_run_dir(run_dir, products_dir)
+ sys.stderr.write(f"render_report.py: could not read or write the report's files: {error}\n")
+ return 1
+ removal = remove_run_dir(args.run_dir, products_dir)
+ count = len(rendered.findings)
+ stamp_name = f"{REVISION_PREFIX}{rendered.tag}.json"
print(
- f"wrote CLAUDE-SECURITY-RESULTS.jsonl ({len(findings)} finding"
- f"{'' if len(findings) == 1 else 's'}) and {REVISION_PREFIX}{tag}.json "
- f"into {products_dir}"
+ f"wrote {JSONL_NAME}, {SARIF_NAME} ({count} finding{'' if count == 1 else 's'}) "
+ f"and {stamp_name} into {products_dir}"
)
- print(f"stamp: {REVISION_PREFIX}{tag}.json")
- print(f"verification.status: {verification.get('status')}")
- reason = verification.get("reason")
- if reason:
+ print(f"stamp: {stamp_name}")
+ print(f"verification.status: {rendered.verification['status']}")
+ if reason := rendered.verification["reason"]:
print(f"verification.reason: {reason}")
print(removal)
return 0
if __name__ == "__main__":
+ console.tolerate_undecodable_names()
sys.exit(main(sys.argv[1:]))
diff --git a/plugins/claude-security/scripts/write_scan_meta.py b/plugins/claude-security/scripts/write_scan_meta.py
index 6cf3bd83..f75b9e31 100755
--- a/plugins/claude-security/scripts/write_scan_meta.py
+++ b/plugins/claude-security/scripts/write_scan_meta.py
@@ -1,33 +1,40 @@
#!/usr/bin/env python3
"""Write scan-meta.json for a run: the record of what was scanned.
-Captures the revision from git itself and, for a whole-repository scan, the
-tree's top-level directories, printed as a JSON array on a `top_level_dirs:`
-line and recorded in the meta file.
+Mints the scan's id and captures, from git itself: the revision, the scan
+root's path within the repository, the credential-free https form of its
+remote and, for a whole-repository scan, the tree's top-level directories,
+printed as a JSON array on a `top_level_dirs:` line and recorded in the meta
+file with any root-level symbolic links left out of them.
Usage:
write_scan_meta.py <run_dir> <scan_root> --mode scan|changes|commit
--effort low|medium|high|max [--scope a,b] [--base <ref>]
[--merge-base <sha>] [--commit <sha>]
-Exits 0 on success. A caller error prints a one-line diagnostic to stderr and
-exits non-zero without writing the file.
+Exits 0 on success, 1 on a refusal naming what is wrong, 2 on a usage error;
+the file is written only on success.
+Python 3.9-compatible, stdlib only.
"""
from __future__ import annotations
import argparse
-import json
import os
+import re
+import stat
import subprocess
import sys
-from typing import TypedDict, cast
+import uuid
+from pathlib import Path
+from typing import Literal, NamedTuple, TypedDict
+from urllib.parse import quote, unquote, urlsplit
-sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
-from render_report import RenderError, atomic_write
+# The lib/ package lives next to this script. Python normally adds a script's own
+# directory to the import path, but not under -P or PYTHONSAFEPATH, so we add it here.
+sys.path.insert(0, str(Path(__file__).resolve().parent))
+from lib import absolute, console, plugin, strictjson
-PLUGIN_NAME = "claude-security"
-REPORT_DIR_PREFIX = "CLAUDE-SECURITY-"
GIT_ENV = dict(os.environ, GIT_TERMINAL_PROMPT="0")
@@ -39,30 +46,27 @@ class Revision(TypedDict, total=False):
parent: str | None
branch: str | None
dirty: bool | None
+ sparse: Literal[True]
+ not_checked_out_dirs: list[str]
base: str | None
merge_base: str | None
-class Options(TypedDict):
- """The parsed, typed command line -- argparse hands back untyped attributes."""
+class Args(argparse.Namespace):
+ """The parsed command line."""
- run_dir: str
- scan_root: str
- mode: str
- effort: str
- scope: str
- base: str | None
- merge_base: str | None
- commit: str | None
+ run_dir: str = ""
+ scan_root: str = ""
+ mode: str = ""
+ effort: str = ""
+ scope: str = ""
+ base: str | None = None
+ merge_base: str | None = None
+ commit: str | None = None
class MetaError(Exception):
- """An input error the caller must correct."""
-
-
-def _opt_str(value: object) -> str | None:
- """An argparse optional as str-or-None, typed."""
- return None if value is None else str(value)
+ """A refusal: the command line was well-formed but the run cannot be recorded."""
def git(cwd: str, *args: str) -> str | None:
@@ -80,33 +84,109 @@ def git(cwd: str, *args: str) -> str | None:
return None
if out.returncode != 0:
return None
- return out.stdout.decode("utf-8", "replace").rstrip("\r\n")
+ return out.stdout.decode("utf-8", "surrogateescape").rstrip("\r\n")
+
+
+class Extent(NamedTuple):
+ """The scan target's top-level directories, its root-level symbolic links, and
+ the tracked top-level directories its working tree does not hold."""
+
+ dirs: list[str]
+ symlinks: list[str]
+ absent: list[str]
-def top_level_dirs(scan_root: str) -> list[str] | None:
+def tree_extent(scan_root: str) -> Extent | None:
"""The scan target's top-level directories, computed from the tree itself.
Inside a git work tree the tracked files decide; where nothing is tracked
- the immediate subdirectories do. `.git` and `CLAUDE-SECURITY-*` report
- directories are excluded. None when the tree could not be listed.
+ the immediate subdirectories do. Entries are classified without following
+ symbolic links, so nothing outside the checkout is read: a root-level
+ symbolic link is never one of the directories, and is named in `symlinks`
+ so the report can say it was not followed. `.git` and `CLAUDE-SECURITY-*`
+ report directories are excluded. None when the tree could not be listed.
"""
names: set[str] = set()
+ symlinks: set[str] = set()
listing = git(scan_root, "ls-files", "-z")
if listing:
for path in listing.split("\0"):
top, sep, _rest = path.partition("/")
if sep and top:
names.add(top)
- elif path and os.path.isdir(os.path.join(scan_root, path)):
- names.add(path)
+ elif path:
+ try:
+ mode = os.lstat(os.path.join(scan_root, path)).st_mode
+ except OSError:
+ continue
+ if stat.S_ISLNK(mode):
+ symlinks.add(path)
+ elif stat.S_ISDIR(mode):
+ names.add(path)
else:
try:
with os.scandir(scan_root) as entries:
- names.update(entry.name for entry in entries if entry.is_dir(follow_symlinks=False))
+ for entry in entries:
+ if entry.is_symlink():
+ symlinks.add(entry.name)
+ elif entry.is_dir(follow_symlinks=False):
+ names.add(entry.name)
except OSError:
return None
names.discard(".git")
- return sorted(n for n in names if not n.startswith(REPORT_DIR_PREFIX))
+ kept = sorted(n for n in names if not n.startswith(plugin.REPORT_DIR_PREFIX))
+ on_disk = {n for n in kept if os.path.lexists(os.path.join(scan_root, n))}
+ dirs = [n for n in kept if n in on_disk]
+ return Extent(dirs, sorted(symlinks), [n for n in kept if n not in on_disk])
+
+
+def sparse_checkout(scan_root: str, extent: Extent | None) -> list[str] | None:
+ """The tracked top-level directories a sparse checkout left out; None when it is not one."""
+ if git(scan_root, "config", "--bool", "core.sparseCheckout") != "true":
+ return None
+ return extent.absent if extent else []
+
+
+REMOTE_SCHEMES = frozenset({"http", "https", "ssh", "git", "git+ssh"})
+
+
+def sanitize_remote(url: str | None) -> str | None:
+ """`url` as a credential-free https URL naming the same repository, or None.
+
+ Userinfo, query and fragment are stripped; the scheme becomes https and the
+ host lowercase; a port survives only from an http or https URL; scp-like
+ `user@host:path` is read as ssh; a trailing `/` or `.git` is dropped, so
+ the ssh and https spellings of one repository come out equal. A URL that
+ does not name a hosted repository is None.
+ """
+ text = (url or "").strip()
+ if not text or "[" in text or "]" in text or strictjson.has_lone_surrogate(text):
+ return None
+ if "://" in text:
+ try:
+ parts = urlsplit(text)
+ port = parts.port if parts.scheme.lower() in {"http", "https"} else None
+ except ValueError:
+ return None
+ if parts.scheme.lower() not in REMOTE_SCHEMES:
+ return None
+ host = (parts.hostname or "").lower()
+ location = host if port is None else f"{host}:{port}"
+ path = parts.path
+ else:
+ # Userinfo splits off first: an optional user@ group backtracks and leaks the secret.
+ rest = text.rpartition("@")[2]
+ matched = re.match(r"([^@:/\\]{2,}):(.*)", rest)
+ if not matched:
+ return None
+ location = matched[1].lower()
+ path = matched[2]
+ if not re.fullmatch(r"[a-z0-9.-]+(?::\d+)?", location):
+ return None
+ path = quote(unquote(path.strip("/"))).removesuffix(".git").rstrip("/")
+ if not path:
+ return None
+ return f"https://{location}/{path}"
def worktree_dirty(scan_root: str) -> bool | None:
@@ -118,20 +198,19 @@ def worktree_dirty(scan_root: str) -> bool | None:
if len(line) < len("XY P"):
continue
path = line[3:].split(" -> ")[-1]
- top = path.split("/", 1)[0]
- if top.startswith(REPORT_DIR_PREFIX):
+ if any(part.startswith(plugin.REPORT_DIR_PREFIX) for part in path.split("/")[:-1]):
continue
return True
return False
-def capture_revision(scan_root: str, opts: Options) -> Revision:
+def capture_revision(scan_root: str, opts: Args) -> Revision:
versioned = git(scan_root, "rev-parse", "--is-inside-work-tree") == "true"
- if opts["mode"] == "commit":
+ if opts.mode == "commit":
if not versioned:
msg = f"--mode commit needs a git repository; {scan_root!r} is not one"
raise MetaError(msg)
- commit_arg = opts["commit"] or ""
+ commit_arg = opts.commit or ""
sha = git(scan_root, "rev-parse", "--verify", "--quiet", commit_arg + "^{commit}")
if not sha:
msg = f"--commit {commit_arg!r} does not resolve to a commit"
@@ -151,81 +230,122 @@ def capture_revision(scan_root: str, opts: Options) -> Revision:
"branch": git(scan_root, "rev-parse", "--abbrev-ref", "HEAD"),
"dirty": worktree_dirty(scan_root),
}
- if opts["mode"] == "changes":
- revision["base"] = opts["base"]
- revision["merge_base"] = opts["merge_base"]
+ if opts.mode == "changes":
+ revision["base"] = opts.base
+ revision["merge_base"] = opts.merge_base
return revision
-def parse_options(argv: list[str]) -> Options:
+def scoped(entry: str, scan_root: str) -> str:
+ """A scope entry relative to the scan root; an absolute one that is not inside it is refused."""
+ if not absolute.spelled(entry):
+ return entry
+ literal = os.path.abspath(entry)
+ parent, name = os.path.split(literal)
+ try:
+ resolutions = [
+ literal,
+ os.path.join(os.path.realpath(parent), name),
+ os.path.realpath(entry),
+ ]
+ except OSError:
+ resolutions = [literal]
+ for resolved in resolutions:
+ try:
+ relative = os.path.relpath(resolved, scan_root).replace("\\", "/")
+ except ValueError:
+ continue
+ if relative != ".." and not relative.startswith("../"):
+ return relative
+ msg = f"--scope entry {entry!r} is not inside the scan root {scan_root!r}"
+ raise MetaError(msg)
+
+
+def parse_options(argv: list[str]) -> Args:
+ """The parsed command line; anything wrong with it is argparse's exit 2."""
ap = argparse.ArgumentParser(prog="write_scan_meta")
ap.add_argument("run_dir")
ap.add_argument("scan_root")
- ap.add_argument("--mode", required=True, choices=["scan", "changes", "commit"])
+ ap.add_argument("--mode", required=True, choices=plugin.MODES)
ap.add_argument("--effort", required=True, choices=["low", "medium", "high", "max"])
- ap.add_argument("--scope", default="")
- ap.add_argument("--base", default=None)
- ap.add_argument("--merge-base", dest="merge_base", default=None)
- ap.add_argument("--commit", default=None)
- ns = ap.parse_args(argv)
- return {
- "run_dir": str(cast("object", ns.run_dir)),
- "scan_root": str(cast("object", ns.scan_root)),
- "mode": str(cast("object", ns.mode)),
- "effort": str(cast("object", ns.effort)),
- "scope": str(cast("object", ns.scope)),
- "base": _opt_str(cast("object", ns.base)),
- "merge_base": _opt_str(cast("object", ns.merge_base)),
- "commit": _opt_str(cast("object", ns.commit)),
- }
+ ap.add_argument("--scope")
+ ap.add_argument("--base")
+ ap.add_argument("--merge-base", dest="merge_base")
+ ap.add_argument("--commit")
+ opts = ap.parse_args(argv, namespace=Args())
+ if opts.mode == "commit" and not opts.commit:
+ ap.error("--mode commit requires --commit <sha>")
+ if not os.path.isdir(opts.run_dir):
+ ap.error(f"run directory does not exist: {opts.run_dir}")
+ return opts
def main(argv: list[str]) -> int:
opts = parse_options(argv)
- if opts["mode"] == "commit" and not opts["commit"]:
- msg = "--mode commit requires --commit <sha>"
- raise MetaError(msg)
-
- run_dir = os.path.realpath(os.path.abspath(opts["run_dir"]))
- if not os.path.isdir(run_dir):
- msg = f"run directory does not exist: {run_dir}"
- raise MetaError(msg)
- scan_root = os.path.realpath(os.path.abspath(opts["scan_root"]))
+ run_dir = os.path.realpath(os.path.abspath(opts.run_dir))
+ scan_root = os.path.realpath(os.path.abspath(opts.scan_root))
revision = capture_revision(scan_root, opts)
- scope = [s.strip() for s in opts["scope"].split(",") if s.strip()]
+ extent = tree_extent(scan_root)
+ absent = sparse_checkout(scan_root, extent) if revision.get("versioned") else None
+ if absent is not None:
+ revision["sparse"] = True
+ revision["not_checked_out_dirs"] = absent
+ scan_prefix = (
+ git(scan_root, "rev-parse", "--show-prefix") if revision.get("versioned") else None
+ )
+ remote = (
+ sanitize_remote(git(scan_root, "remote", "get-url", "origin"))
+ if revision.get("versioned")
+ else None
+ )
+ scope = [scoped(entry.strip(), scan_root) for entry in opts.scope.split(",") if entry.strip()]
if scope and all(s in {".", "./"} for s in scope):
scope = []
- whole_repo = opts["mode"] == "scan" and not scope
- top_level = top_level_dirs(scan_root) if whole_repo else None
- if whole_repo and top_level is None:
+ whole_repo = opts.mode == "scan" and not scope
+ if whole_repo and extent is None:
sys.stderr.write(f"write_scan_meta: could not list {scan_root}; top_level_dirs unknown\n")
+ top_level, symlinks = (extent.dirs, extent.symlinks) if whole_repo and extent else (None, None)
+ if symlinks:
+ sys.stderr.write(
+ "write_scan_meta: root-level symbolic links not followed, "
+ f"left out of top_level_dirs: {', '.join(symlinks)}\n"
+ )
meta: dict[str, object] = {
+ "scan_id": str(uuid.uuid4()),
"scan_root": scan_root,
+ "scan_prefix": scan_prefix,
+ "remote": remote,
"run_dir": run_dir,
- "flow": "scan" if opts["mode"] == "scan" else "changes",
- "agent": f"{PLUGIN_NAME}:{PLUGIN_NAME}",
- "mode": opts["mode"],
+ "flow": "scan" if opts.mode == "scan" else "changes",
+ "agent": f"{plugin.NAME}:{plugin.NAME}",
+ "mode": opts.mode,
"scope": scope,
- "effort": opts["effort"],
+ "effort": opts.effort,
"model": None,
"revision": revision,
"revision_source": "self-reported",
"top_level_dirs": top_level,
+ "unfollowed_symlinks": symlinks,
}
path = os.path.join(run_dir, "scan-meta.json")
- atomic_write(path, json.dumps(meta, indent=2) + "\n")
+ with open(path, "w", encoding="utf-8", newline="\n") as out:
+ out.write(strictjson.text(meta, indent=2) + "\n")
sys.stdout.write(f"scan-meta.json written: {path}\n")
sys.stdout.write(f"revision: {revision.get('commit') or 'UNVERSIONED'}\n")
- sys.stdout.write(f"top_level_dirs: {json.dumps(top_level)}\n")
+ if absent is not None:
+ listed = strictjson.text(absent)
+ sys.stdout.write(f"sparse checkout: top-level directories not checked out: {listed}\n")
+ sys.stdout.write(f"top_level_dirs: {strictjson.text(top_level)}\n")
return 0
if __name__ == "__main__":
+ console.tolerate_undecodable_names()
try:
sys.exit(main(sys.argv[1:]))
- except (MetaError, RenderError) as error:
+ except MetaError as error:
sys.stderr.write(f"write_scan_meta: {error}\n")
- sys.exit(2)
+ sys.exit(1)
except OSError as error:
sys.stderr.write(f"write_scan_meta: could not write the run's output: {error}\n")
- sys.exit(2)
+ sys.exit(1)