Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions scripts/sarif-summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
semgrep-other, other. `vuln-high` in --fail-on blocks high and critical.

`upload-copy` writes SARIF copies for GitHub code scanning: results without a
source location are dropped (code scanning rejects them), so are
vulnerabilities a valid statement in the --vex file accepts (the report does
not count them either), and only the first run per tool is kept. A run whose
results are all accepted is still written, empty: that upload closes the
alerts of earlier uploads.
source location are dropped (code scanning rejects them), so are results
suppressed in the source (code scanning ignores SARIF suppressions and would
open an alert for a reviewed `nosemgrep`) and vulnerabilities a valid
statement in the --vex file accepts (the report counts neither), and only the
first run per tool is kept. A run whose results are all suppressed or accepted
is still written, empty: that upload closes the alerts of earlier uploads.

Standard library only; run with `python3 -I`.
"""
Expand Down Expand Up @@ -139,6 +140,14 @@ def location_of(result):
return ""


def suppressed_in_source(result):
"""True for a result the scanner saw suppressed in the source (`nosemgrep`).

Such suppressions stay in the SARIF; one without a status, or accepted,
takes the result out. A rejected one does not."""
return any((s or {}).get("status", "accepted") == "accepted" for s in result.get("suppressions") or [])


def rule_of(rules, rule_list, result):
rule = rules.get(result.get("ruleId"))
if rule is None and isinstance(result.get("ruleIndex"), int) and result["ruleIndex"] < len(rule_list):
Expand Down Expand Up @@ -217,9 +226,7 @@ def collect(directory, vex):
for run in doc.get("runs") or []:
tool, rules, rule_list = rule_index(run)
for result in run.get("results") or []:
# In-source suppressions (`nosemgrep`) stay in the SARIF; one
# without a status, or accepted, takes the result out.
if any((s or {}).get("status", "accepted") == "accepted" for s in result.get("suppressions") or []):
if suppressed_in_source(result):
continue
rule = rule_of(rules, rule_list, result)
category = classify(tool, rule, result)
Expand Down Expand Up @@ -345,7 +352,7 @@ def cmd_upload_copy(args):
for path in sorted(glob.glob(os.path.join(args.dir, "**", "*.sarif"), recursive=True)):
with open(path, encoding="utf-8") as fh:
doc = json.load(fh)
runs, accepted = [], 0
runs, accepted, suppressed = [], 0, 0
for run in doc.get("runs") or []:
tool, rules, rule_list = rule_index(run)
results = run.get("results") or []
Expand All @@ -357,6 +364,9 @@ def cmd_upload_copy(args):
seen_tools.add(tool)
kept = []
for result in located:
if suppressed_in_source(result):
suppressed += 1
continue
rule = rule_of(rules, rule_list, result)
match = vex_match(classify(tool, rule, result), rule, result, vex)
if match and match[1][2] >= now:
Expand All @@ -370,7 +380,8 @@ def cmd_upload_copy(args):
name = os.path.basename(path)
with open(os.path.join(args.out, name), "w", encoding="utf-8") as fh:
json.dump(doc, fh)
print(f"upload copy: {name} ({sum(len(r['results']) for r in runs)} located results, {accepted} accepted left out)")
print(f"upload copy: {name} ({sum(len(r['results']) for r in runs)} located results, "
f"{suppressed} suppressed in source and {accepted} accepted left out)")
return 0


Expand Down
16 changes: 14 additions & 2 deletions scripts/test_sarif_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,19 @@ def test_vex_accepted_results_are_left_out(self):
[result("GO-1", "error"), result("GO-2", "error", line=2), result("GO-3", "error", line=3)]))
suppressed = result("r1")
suppressed["suppressions"] = [{"kind": "inSource"}]
c.add("s.sarif", sarif("Semgrep OSS", [{"id": "r1"}], [suppressed, result("r1", line=2)]))
rejected = result("r3", line=3)
rejected["suppressions"] = [{"kind": "inSource", "status": "rejected"}]
c.add("s.sarif", sarif("Semgrep OSS", [{"id": "r1"}, {"id": "r2"}, {"id": "r3"}],
[suppressed, result("r2", line=2), rejected]))
vex = c.file("vex.json", json.dumps({"statements": [
{"vulnerability": {"name": "GO-1"}, "status": "not_affected",
"justification": "vulnerable_code_not_in_execute_path", "timestamp": "2026-06-01T00:00:00Z"},
{"vulnerability": {"name": "GO-3"}, "status": "not_affected",
"justification": "vulnerable_code_not_in_execute_path", "timestamp": "2025-09-01T00:00:00Z"}]}))
copies = self.upload(c, vex)
self.assertEqual(copies["gv.sarif"], ["GO-2", "GO-3"], "a valid VEX statement leaves the result out, an expired one does not")
self.assertEqual(copies["s.sarif"], ["r1", "r1"], "in-source suppressions stay as they are")
self.assertEqual(copies["s.sarif"], ["r2", "r3"],
"an in-source suppression leaves the result out (code scanning ignores SARIF suppressions), a rejected one does not")
self.assertEqual(self.upload(c)["gv.sarif"], ["GO-1", "GO-2", "GO-3"], "without --vex nothing is accepted")

def test_run_with_only_accepted_results_is_uploaded_empty(self):
Expand All @@ -244,6 +248,14 @@ def test_run_with_only_accepted_results_is_uploaded_empty(self):
"justification": "vulnerable_code_not_in_execute_path", "timestamp": "2026-06-01T00:00:00Z"}]}))
self.assertEqual(self.upload(c, vex), {"gv.sarif": []})

def test_run_with_only_source_suppressed_results_is_uploaded_empty(self):
# Same reason: an earlier upload may have opened the alert.
c = self.case()
suppressed = result("r1")
suppressed["suppressions"] = [{"kind": "inSource"}]
c.add("s.sarif", sarif("Semgrep OSS", [{"id": "r1"}], [suppressed]))
self.assertEqual(self.upload(c), {"s.sarif": []})


if __name__ == "__main__":
unittest.main(verbosity=1)
Loading