Skip to content

Commit b82ae90

Browse files
ralyodioclaude
andcommitted
ci: add ThreatCrush security scan
Installs threatcrush-scan@1.1.0 from the sh1pt Actions Store. Scans pull requests for hardcoded credentials, injection, SSRF, unsafe deserialisation and dependency tampering; uploads SARIF to the Security tab. Report-only — it will not fail a pull request. Set the pack's failOn input to critical,high once the existing findings are triaged. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 1cd8588 commit b82ae90

2 files changed

Lines changed: 505 additions & 0 deletions

File tree

.github/threatcrush-to-sarif.py

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
#!/usr/bin/env python3
2+
"""Convert ThreatCrush terminal output to SARIF 2.1.0.
3+
4+
Compatibility shim for CLI versions older than native ``--format sarif``.
5+
When the CLI can emit SARIF itself the workflow uses that and never runs this
6+
file; parsing a human-readable stream is strictly worse and exists only so a
7+
repository is not left unscanned while waiting for a release.
8+
9+
It **fails closed**. If it cannot recognise the output it exits non-zero and
10+
dumps what it saw. Emitting empty SARIF instead would report "0 findings",
11+
which is indistinguishable from a clean scan and is the single most expensive
12+
thing a security tool can get wrong.
13+
14+
Three details of the format, each of which is load-bearing:
15+
16+
* Severity is bare for ``CRITICAL`` and bracketed for ``[HIGH]``/``[MEDIUM]``/
17+
``[LOW]``. One regex shape misses half the findings.
18+
* ``File:`` paths are relative to the scan root, not the repository root. Left
19+
unprefixed, every finding resolves to nothing in the consumer's view of the
20+
repo. Hence ``--path-prefix``.
21+
* Whole-file findings report line ``:0``. SARIF requires ``startLine >= 1``.
22+
23+
``Code:`` lines are redacted excerpts of the match. They are skipped rather
24+
than parsed, both because matching them would double-count every finding and
25+
because a redacted excerpt tells a reader nothing the ``Info:`` line does not.
26+
"""
27+
28+
from __future__ import annotations
29+
30+
import argparse
31+
import json
32+
import re
33+
import sys
34+
35+
ANSI = re.compile(r"\x1b\[[0-9;]*[A-Za-z]")
36+
37+
# ` CRITICAL AWS Access Key` / ` [HIGH] Sensitive File`
38+
SEVERITY_LINE = re.compile(r"^\s*(?:\[(CRITICAL|HIGH|MEDIUM|LOW|INFO)\]|(CRITICAL))\s+(.+?)\s*$")
39+
FILE_LINE = re.compile(r"^\s*File:\s*(.+?):(\d+)\s*$")
40+
INFO_LINE = re.compile(r"^\s*Info:\s*(.+?)\s*$")
41+
42+
# Proof that a scan ran to completion. Without one of these we are looking at a
43+
# crash, a help screen, or an unrecognised release — never at a clean result.
44+
FOOTER = re.compile(r"^\s*(?:\d+\s+issue\(s\)\s+found|.*No security issues found)")
45+
46+
LEVELS = {"CRITICAL": "error", "HIGH": "error", "MEDIUM": "warning", "LOW": "note", "INFO": "none"}
47+
SECURITY_SEVERITY = {"CRITICAL": "9.0", "HIGH": "7.0", "MEDIUM": "5.0", "LOW": "3.0", "INFO": "1.0"}
48+
RANK = {"info": 0, "low": 1, "medium": 2, "high": 3, "critical": 4}
49+
50+
51+
class Unrecognised(Exception):
52+
"""The output did not look like a completed ThreatCrush scan."""
53+
54+
55+
def rule_id(title: str) -> str:
56+
"""Derive a stable rule id from a finding title.
57+
58+
Old CLIs print `AWS Access Key`, not `secret-aws-access-key`. Slugifying
59+
keeps SARIF results groupable and keeps fingerprints stable across runs,
60+
which is what stops the Security tab treating every run as brand-new alerts.
61+
"""
62+
slug = re.sub(r"[^a-z0-9]+", "-", title.lower()).strip("-")
63+
return f"threatcrush-{slug}" if slug else "threatcrush-finding"
64+
65+
66+
def parse(text: str) -> list[dict]:
67+
lines = ANSI.sub("", text).splitlines()
68+
if not any(FOOTER.match(line) for line in lines):
69+
raise Unrecognised("no scan-completion footer found")
70+
71+
findings: list[dict] = []
72+
pending: dict | None = None
73+
74+
for line in lines:
75+
severity_match = SEVERITY_LINE.match(line)
76+
if severity_match:
77+
severity = severity_match.group(1) or severity_match.group(2)
78+
pending = {"severity": severity.upper(), "title": severity_match.group(3).strip()}
79+
continue
80+
81+
if pending is None:
82+
continue
83+
84+
file_match = FILE_LINE.match(line)
85+
if file_match:
86+
pending["file"] = file_match.group(1).strip()
87+
pending["line"] = int(file_match.group(2))
88+
continue
89+
90+
info_match = INFO_LINE.match(line)
91+
if info_match and "file" in pending:
92+
pending["message"] = info_match.group(1).strip()
93+
findings.append(pending)
94+
pending = None
95+
96+
return findings
97+
98+
99+
def to_sarif(findings: list[dict], prefix: str, version: str) -> dict:
100+
rules: dict[str, dict] = {}
101+
results = []
102+
103+
for finding in findings:
104+
rid = rule_id(finding["title"])
105+
rules.setdefault(
106+
rid,
107+
{
108+
"id": rid,
109+
"name": rid,
110+
"shortDescription": {"text": finding["title"]},
111+
"fullDescription": {"text": finding["title"]},
112+
"defaultConfiguration": {"level": LEVELS[finding["severity"]]},
113+
"properties": {
114+
"tags": ["security", "threatcrush"],
115+
"security-severity": SECURITY_SEVERITY[finding["severity"]],
116+
},
117+
},
118+
)
119+
120+
uri = finding["file"].lstrip("./")
121+
if prefix:
122+
uri = f"{prefix.strip('/')}/{uri}"
123+
124+
results.append(
125+
{
126+
"ruleId": rid,
127+
"level": LEVELS[finding["severity"]],
128+
"message": {"text": finding.get("message", finding["title"])},
129+
"locations": [
130+
{
131+
"physicalLocation": {
132+
"artifactLocation": {"uri": uri, "uriBaseId": "%SRCROOT%"},
133+
# Clamped: SARIF rejects 0, and a whole-file finding
134+
# has no line to report.
135+
"region": {"startLine": max(1, finding["line"])},
136+
}
137+
}
138+
],
139+
"partialFingerprints": {
140+
"primaryLocationLineHash": f"{rid}:{uri}:{max(1, finding['line'])}"
141+
},
142+
"properties": {"severity": finding["severity"].lower()},
143+
}
144+
)
145+
146+
return {
147+
"$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json",
148+
"version": "2.1.0",
149+
"runs": [
150+
{
151+
"tool": {
152+
"driver": {
153+
"name": "ThreatCrush",
154+
"version": version,
155+
"informationUri": "https://threatcrush.com",
156+
"rules": list(rules.values()),
157+
}
158+
},
159+
"results": results,
160+
"columnKind": "utf16CodeUnits",
161+
}
162+
],
163+
}
164+
165+
166+
def main() -> int:
167+
parser = argparse.ArgumentParser(description=__doc__)
168+
parser.add_argument("--input", required=True, help="captured `threatcrush scan` output")
169+
parser.add_argument("--output", required=True, help="SARIF file to write")
170+
parser.add_argument("--path-prefix", default="", help="prepended to every file URI")
171+
parser.add_argument("--tool-version", default="unknown")
172+
parser.add_argument("--fail-on", default="", help="comma-separated severities that exit 1")
173+
args = parser.parse_args()
174+
175+
with open(args.input, encoding="utf-8", errors="replace") as handle:
176+
text = handle.read()
177+
178+
try:
179+
findings = parse(text)
180+
except Unrecognised as err:
181+
print(f"error: unrecognised ThreatCrush output ({err})", file=sys.stderr)
182+
print("--- first 40 lines ---", file=sys.stderr)
183+
for line in ANSI.sub("", text).splitlines()[:40]:
184+
print(line, file=sys.stderr)
185+
return 2
186+
187+
with open(args.output, "w", encoding="utf-8") as handle:
188+
json.dump(to_sarif(findings, args.path_prefix, args.tool_version), handle, indent=2)
189+
handle.write("\n")
190+
191+
print(f"converted {len(findings)} finding(s) to {args.output}")
192+
193+
thresholds = [s.strip().lower() for s in args.fail_on.split(",") if s.strip()]
194+
if thresholds:
195+
unknown = [s for s in thresholds if s not in RANK]
196+
if unknown:
197+
# Silently ignoring a typo produces a gate that never fires, which
198+
# looks exactly like a passing build.
199+
print(f"error: unknown severity in --fail-on: {', '.join(unknown)}", file=sys.stderr)
200+
return 2
201+
floor = min(RANK[s] for s in thresholds)
202+
if any(RANK[f["severity"].lower()] >= floor for f in findings):
203+
print(f"::error::findings at or above {args.fail_on}")
204+
return 1
205+
206+
return 0
207+
208+
209+
if __name__ == "__main__":
210+
sys.exit(main())

0 commit comments

Comments
 (0)