Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

36.1 KB
Raw Blame
"""Standalone report-policy tests; no application initialization required."""

# allow: no-sut-import — imports the standalone CI script directly.

import contextlib
import copy
import importlib.util
import io
import json
import os
import sys
import tempfile
import unittest
from pathlib import Path
from unittest import mock

ROOT = Path(__file__).resolve().parents[3]
SPEC = importlib.util.spec_from_file_location(
    "semgrep_report", ROOT / ".github/scripts/check_semgrep_report.py"
)
POLICY = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(POLICY)


# Scores GitHub may not read as a decimal (so the alert would get no level
# and never block a release), plus out-of-range values. float() accepts
# every string here except "invalid" and "".
BAD_SECURITY_SEVERITIES = (
    None,
    True,
    False,
    "NaN",
    "Infinity",
    float("nan"),
    float("inf"),
    "-1",
    -1,
    "11",
    11,
    "10.5",
    "invalid",
    "",
    "1e1",
    "+8",
    " 8 ",
    "8\n",
    "8.",
    ".8",
    "0_8",
    "\u0668",  # ARABIC-INDIC DIGIT EIGHT
    "\uff18",  # FULLWIDTH DIGIT EIGHT
    [8],
    {"score": 8},
)


class ReportTests(unittest.TestCase):
    def setUp(self):
        # Suppressed results are checked against the scanned source file.
        source_dir = tempfile.TemporaryDirectory()
        self.addCleanup(source_dir.cleanup)
        self.source_root = Path(source_dir.name)
        self._write_source(
            {4: "probe()  # nosemgrep: example, reason: reviewed probe"}
        )
        self.report = {
            "errors": [],
            "skipped_rules": [],
            "paths": {"scanned": ["example.py"]},
            "results": [],
        }
        self.sarif = {
            "version": "2.1.0",
            "runs": [
                {
                    "invocations": [
                        {
                            "executionSuccessful": True,
                            "toolExecutionNotifications": [],
                        }
                    ],
                    "results": [],
                    "tool": {
                        "driver": {
                            "rules": [
                                {
                                    "id": "example",
                                    "defaultConfiguration": {
                                        "level": "warning"
                                    },
                                }
                            ]
                        }
                    },
                }
            ],
        }

    def _with_finding(self, level=None, rule_level="warning"):
        """One consistent JSON/SARIF finding pair for rule ``example``."""
        report = copy.deepcopy(self.report)
        report["results"] = [
            {"check_id": "example", "extra": {"severity": "WARNING"}}
        ]
        sarif = copy.deepcopy(self.sarif)
        result = {"ruleId": "example"}
        if level is not None:
            result["level"] = level
        sarif["runs"][0]["tool"] = {
            "driver": {
                "rules": [
                    {
                        "id": "example",
                        "defaultConfiguration": {"level": rule_level},
                    }
                ]
            }
        }
        sarif["runs"][0]["results"] = [result]
        return report, sarif

    def _write_source(self, lines, name="example.py"):
        """Write ``name`` with ``lines`` ({number: text}), others as code."""
        last = max(8, *lines)
        text = "\n".join(
            lines.get(number, "probe()") for number in range(1, last + 1)
        )
        (self.source_root / name).write_text(text, encoding="utf-8")

    @staticmethod
    def _location(line, uri="example.py"):
        return [
            {
                "physicalLocation": {
                    "artifactLocation": {"uri": uri},
                    "region": {"startLine": line},
                }
            }
        ]

    def _suppressed_finding(self, line=4, uri="example.py"):
        """A JSON/SARIF pair whose only finding is source-suppressed."""
        report, sarif = self._with_finding()
        report["results"][0]["extra"]["is_ignored"] = True
        result = sarif["runs"][0]["results"][0]
        result["suppressions"] = [{"kind": "inSource"}]
        result["locations"] = self._location(line, uri)
        return report, sarif

    def _prepare(self, report, sarif):
        return POLICY.prepare_github_sarif(
            report, sarif, source_root=self.source_root
        )

    def _rejects(self, message, report, sarif):
        """Assert validation fails with ``message``, not some other check.

        Matching the message keeps each negative case pinned to the check it
        targets: a case another check also rejects would stay green if its
        own check were deleted.
        """
        with self.assertRaisesRegex(ValueError, message):
            POLICY.validate(report, sarif)

    def test_clean_scan(self):
        self.assertEqual(POLICY.validate(self.report, self.sarif), {})

    def test_scanner_errors_and_missing_error_status_fail(self):
        for errors in (None, [{"message": "scanner failure"}], "malformed"):
            with self.subTest(errors=errors):
                self._rejects(
                    "Semgrep reported errors",
                    {**self.report, "errors": errors},
                    self.sarif,
                )

    def test_empty_scan_scope_fails(self):
        self._rejects(
            "Semgrep did not report any scanned files",
            {**self.report, "paths": {"scanned": []}},
            self.sarif,
        )

    def test_scan_without_loaded_rules_fails(self):
        """Semgrep lists every loaded rule; none means nothing was checked."""
        for tool in (
            None,
            {},
            {"driver": {}},
            {"driver": {"rules": []}},
            {"driver": {"rules": "example"}},
        ):
            sarif = copy.deepcopy(self.sarif)
            if tool is None:
                del sarif["runs"][0]["tool"]
            else:
                sarif["runs"][0]["tool"] = tool
            with self.subTest(tool=tool):
                self._rejects(
                    "SARIF run lists no loaded rules", self.report, sarif
                )

    def test_finding_severities_are_preserved(self):
        self.report["results"] = [
            {"extra": {"severity": s, "is_ignored": True}}
            for s in ("ERROR", "WARNING", "INFO")
        ]
        self.assertEqual(
            POLICY.validate(self.report, self.sarif),
            {"ERROR": 1, "WARNING": 1, "INFO": 1},
        )

    def test_missing_sarif_run_or_failed_invocation_fails(self):
        self._rejects(
            "SARIF contains no scan runs",
            self.report,
            {**self.sarif, "runs": []},
        )
        # Each run otherwise passes (it has a loaded rule), so only the
        # invocation check under test can reject it.
        for invocation in (
            {"executionSuccessful": False},
            {},
            {"executionSuccessful": "true"},
        ):
            sarif = copy.deepcopy(self.sarif)
            sarif["runs"][0]["invocations"] = [invocation]
            with self.subTest(invocation=invocation):
                self._rejects(
                    "SARIF does not report successful scanner execution",
                    self.report,
                    sarif,
                )
        for invocations in ([], None):
            sarif = copy.deepcopy(self.sarif)
            if invocations is None:
                del sarif["runs"][0]["invocations"]
            else:
                sarif["runs"][0]["invocations"] = invocations
            with self.subTest(invocations=invocations):
                self._rejects(
                    "SARIF run has no scanner invocation", self.report, sarif
                )

    def test_sarif_findings_need_severity_metadata(self):
        self.sarif["runs"][0].update(
            {
                "tool": {
                    "driver": {
                        "rules": [
                            {
                                "id": "example",
                                "properties": {"security-severity": "8.0"},
                            }
                        ]
                    }
                },
                "results": [{"ruleId": "example"}],
            }
        )
        self.report["results"] = [
            {"check_id": "example", "extra": {"severity": "ERROR"}}
        ]
        self.assertEqual(POLICY.validate(self.report, self.sarif), {"ERROR": 1})
        for score in ("0", "4", "7.5", "10.0", 0, 8, 7.5, 10):
            good = copy.deepcopy(self.sarif)
            good["runs"][0]["tool"]["driver"]["rules"][0]["properties"][
                "security-severity"
            ] = score
            with self.subTest(score=score):
                self.assertEqual(
                    POLICY.validate(self.report, good), {"ERROR": 1}
                )
        for score in BAD_SECURITY_SEVERITIES:
            bad = copy.deepcopy(self.sarif)
            bad["runs"][0]["tool"]["driver"]["rules"][0]["properties"][
                "security-severity"
            ] = score
            with self.subTest(score=score):
                self._rejects(
                    "SARIF rule has an? (malformed|out-of-range) security "
                    "severity",
                    self.report,
                    bad,
                )

    def test_every_loaded_rule_score_is_checked(self):
        """A rule with no finding in this scan must still carry a valid score.

        Its alerts are what the release gate counts once it fires.
        """
        report, sarif = self._with_finding()
        rules = sarif["runs"][0]["tool"]["driver"]["rules"]
        rules.append(
            {"id": "quiet", "properties": {"security-severity": "7.5"}}
        )
        self.assertEqual(POLICY.validate(report, sarif), {"WARNING": 1})
        for score in BAD_SECURITY_SEVERITIES:
            rules[-1]["properties"]["security-severity"] = score
            with self.subTest(score=score):
                self._rejects(
                    "SARIF rule has an? (malformed|out-of-range) security "
                    "severity",
                    report,
                    sarif,
                )
        for rule, message in (
            (None, "SARIF rule descriptor is malformed"),
            ("quiet", "SARIF rule descriptor is malformed"),
            ({}, "SARIF rule descriptor is malformed"),
            ({"id": 1}, "SARIF rule descriptor is malformed"),
            (
                {"id": "quiet", "properties": []},
                "SARIF rule properties are malformed",
            ),
        ):
            rules[-1] = rule
            with self.subTest(rule=rule):
                self._rejects(message, report, sarif)

    def test_malformed_json_results_fail(self):
        for results, message in (
            (None, "Semgrep findings are missing or malformed"),
            ({}, "Semgrep findings are missing or malformed"),
            ("results", "Semgrep findings are missing or malformed"),
            ([None], "Semgrep finding is malformed"),
            ([{"extra": None}], "Semgrep finding has no metadata"),
        ):
            with self.subTest(results=results):
                self._rejects(
                    message, {**self.report, "results": results}, self.sarif
                )

    def test_unrecognized_severity_fails(self):
        report, sarif = self._with_finding()
        self.assertEqual(POLICY.validate(report, sarif), {"WARNING": 1})
        for severity in (None, "", "info", "CRITICAL ", "UNKNOWN"):
            report["results"][0]["extra"]["severity"] = severity
            with self.subTest(severity=severity):
                self._rejects(
                    "Semgrep finding has no recognized severity", report, sarif
                )

    def test_skipped_rules_fail(self):
        for skipped in (None, [{"rule_id": "example"}], "malformed"):
            report = {**self.report, "skipped_rules": skipped}
            with self.subTest(skipped=skipped):
                self._rejects("Semgrep skipped rules", report, self.sarif)
        report = dict(self.report)
        del report["skipped_rules"]
        self._rejects("Semgrep skipped rules", report, self.sarif)

    def test_malformed_scanned_paths_fail(self):
        for scanned, message in (
            ([""], "Semgrep reported a malformed scanned path"),
            ([None], "Semgrep reported a malformed scanned path"),
            (["example.py", ""], "Semgrep reported a malformed scanned path"),
            ("example.py", "Semgrep did not report any scanned files"),
        ):
            report = {**self.report, "paths": {"scanned": scanned}}
            with self.subTest(scanned=scanned):
                self._rejects(message, report, self.sarif)

    def test_non_native_sarif_version_fails(self):
        for version in (None, "2.0.0", "2.1", 2.1):
            with self.subTest(version=version):
                self._rejects(
                    "Expected native SARIF 2.1.0",
                    self.report,
                    {**self.sarif, "version": version},
                )

    def test_malformed_sarif_run_results_fail(self):
        for results in (None, {}, "results"):
            sarif = copy.deepcopy(self.sarif)
            sarif["runs"][0]["results"] = results
            with self.subTest(results=results):
                self._rejects(
                    "SARIF findings are missing or malformed",
                    self.report,
                    sarif,
                )

    def test_error_notifications_fail(self):
        for key in (
            "toolExecutionNotifications",
            "toolConfigurationNotifications",
        ):
            sarif = copy.deepcopy(self.sarif)
            sarif["runs"][0]["invocations"][0][key] = [
                {"level": "error", "message": {"text": "rule failed"}}
            ]
            with self.subTest(key=key):
                self._rejects(
                    f"SARIF reports an error in {key}", self.report, sarif
                )
        sarif = copy.deepcopy(self.sarif)
        sarif["runs"][0]["invocations"][0]["toolExecutionNotifications"] = [
            {"level": "note", "message": {"text": "informational"}}
        ]
        self.assertEqual(POLICY.validate(self.report, sarif), {})

    def test_non_object_entries_fail(self):
        """Each container check fails with its own message, not a crash."""
        for report in (None, [], "report"):
            with self.subTest(report=report):
                self._rejects(
                    "Semgrep JSON must be an object", report, self.sarif
                )
        sarif = copy.deepcopy(self.sarif)
        sarif["runs"][0]["invocations"] = [None]
        self._rejects("SARIF invocation is malformed", self.report, sarif)
        sarif = copy.deepcopy(self.sarif)
        sarif["runs"][0]["results"] = [None]
        self._rejects("SARIF finding is malformed", self.report, sarif)
        for key in (
            "toolExecutionNotifications",
            "toolConfigurationNotifications",
        ):
            # {} is not a list but iterates as empty; [None] is a list whose
            # entry is not an object.
            for notifications in ({}, [None]):
                sarif = copy.deepcopy(self.sarif)
                sarif["runs"][0]["invocations"][0][key] = notifications
                with self.subTest(key=key, notifications=notifications):
                    self._rejects(
                        f"SARIF {key} are malformed", self.report, sarif
                    )

    def test_finding_without_rule_descriptor_fails(self):
        # An explicit result level, so only the missing descriptor can fail.
        report, sarif = self._with_finding("warning")
        self.assertEqual(POLICY.validate(report, sarif), {"WARNING": 1})
        sarif["runs"][0]["results"][0]["ruleId"] = "unknown"
        report["results"][0]["check_id"] = "unknown"
        self._rejects("SARIF finding has no rule descriptor", report, sarif)

    def test_level_none_is_not_severity_information(self):
        for level, rule_level in (("none", "warning"), (None, "none")):
            report, sarif = self._with_finding(level, rule_level)
            with self.subTest(level=level, rule_level=rule_level):
                self._rejects(
                    "SARIF finding lacks severity information", report, sarif
                )
        report, sarif = self._with_finding("error", "none")
        self.assertEqual(POLICY.validate(report, sarif), {"WARNING": 1})

    def test_json_and_sarif_findings_must_agree(self):
        report, sarif = self._with_finding()
        truncated = copy.deepcopy(sarif)
        truncated["runs"][0]["results"] = []
        renamed = copy.deepcopy(report)
        renamed["results"][0]["check_id"] = "other"
        for bad_report, bad_sarif in (
            (report, truncated),
            (self.report, sarif),
            (renamed, sarif),
        ):
            self._rejects(
                "Semgrep JSON and SARIF reports disagree", bad_report, bad_sarif
            )
        # Suppressed findings are excluded on both sides.
        suppressed = copy.deepcopy(sarif)
        suppressed["runs"][0]["results"][0]["suppressions"] = [
            {"kind": "inSource"}
        ]
        ignored = copy.deepcopy(report)
        ignored["results"][0]["extra"]["is_ignored"] = True
        self.assertEqual(POLICY.validate(ignored, suppressed), {"WARNING": 1})
        self.assertEqual(POLICY.validate(ignored, truncated), {"WARNING": 1})

    def test_github_upload_preserves_unsuppressed_results_and_metadata(self):
        for suppression in (
            {"kind": "inSource"},
            {
                "kind": "inSource",
                "status": "accepted",
                "justification": "Reviewed",
            },
        ):
            with self.subTest(suppression=suppression):
                report, sarif = self._with_finding()
                run = sarif["runs"][0]
                run["automationDetails"] = {"id": "semgrep-security/"}
                kept = run["results"][0]
                kept.update(
                    {
                        "suppressions": [],
                        "message": {"text": "Must remain visible"},
                        "partialFingerprints": {
                            "primaryLocationLineHash": "abc"
                        },
                        "locations": self._location(8),
                        "codeFlows": [{"threadFlows": []}],
                    }
                )
                # Same rule as the retained result: rule-wide removal is wrong.
                removed = copy.deepcopy(kept)
                removed["suppressions"] = [suppression]
                removed["locations"][0]["physicalLocation"]["region"][
                    "startLine"
                ] = 4
                run["results"].insert(0, removed)
                # A different rule at the suppressed location must survive.
                other = copy.deepcopy(kept)
                other["ruleId"] = "another-rule"
                other["locations"][0]["physicalLocation"]["region"][
                    "startLine"
                ] = 4
                run["results"].append(other)
                descriptor = copy.deepcopy(run["tool"]["driver"]["rules"][0])
                descriptor["id"] = "another-rule"
                run["tool"]["driver"]["rules"].append(descriptor)
                other_json = copy.deepcopy(report["results"][0])
                other_json["check_id"] = "another-rule"
                report["results"].append(other_json)
                # Preserve other runs and repeated findings, not just a set of IDs.
                sarif["runs"].append(copy.deepcopy(run))
                report["results"] *= 2
                original_sarif = copy.deepcopy(sarif)
                original_report = copy.deepcopy(report)
                expected = copy.deepcopy(sarif)
                for output_run in expected["runs"]:
                    output_run["results"] = copy.deepcopy([kept, other])

                self.assertEqual(self._prepare(report, sarif), expected)
                self.assertEqual(sarif, original_sarif)
                self.assertEqual(report, original_report)

    def test_github_upload_without_suppressions_is_unchanged(self):
        report, sarif = self._with_finding()
        self.assertEqual(self._prepare(report, sarif), sarif)

    def test_all_suppressed_upload_keeps_rules_and_scan_metadata(self):
        _, sarif = self._suppressed_finding()
        upload = self._prepare(self.report, sarif)
        self.assertEqual(upload, self.sarif)

    def test_unknown_suppression_semantics_fail_closed(self):
        for suppressions in (
            None,
            True,
            {},
            "inSource",
            [None],
            [{}],
            [{"kind": "external"}],
            [{"kind": "inSource", "status": "rejected"}],
            [{"kind": "inSource", "status": "underReview"}],
            [{"kind": "inSource", "status": None}],
            [{"kind": "inSource"}, {"kind": "external"}],
        ):
            with self.subTest(suppressions=suppressions):
                _, sarif = self._with_finding()
                sarif["runs"][0]["results"][0]["suppressions"] = suppressions
                with self.assertRaisesRegex(ValueError, "suppression"):
                    self._prepare(self.report, sarif)

    def test_suppressed_results_are_validated_before_removal(self):
        _, sarif = self._with_finding(level="none", rule_level="none")
        sarif["runs"][0]["results"][0]["suppressions"] = [{"kind": "inSource"}]
        with self.assertRaisesRegex(ValueError, "lacks severity information"):
            self._prepare(self.report, sarif)

    def test_github_upload_rejects_incomplete_scans(self):
        report, sarif = self._with_finding()
        for bad_report, bad_sarif, reason in (
            (
                {**report, "errors": [{"message": "failure"}]},
                sarif,
                "reported errors",
            ),
            ({**report, "skipped_rules": ["example"]}, sarif, "skipped rules"),
            ({**report, "paths": {"scanned": []}}, sarif, "any scanned files"),
            (self.report, sarif, "reports disagree"),
        ):
            with self.subTest(reason=reason):
                with self.assertRaisesRegex(ValueError, reason):
                    self._prepare(bad_report, bad_sarif)

    def test_suppressions_need_a_reviewed_annotation_for_their_rule(self):
        """A bare or unreviewed marker must not drop a finding from GitHub.

        Semgrep 1.177.0 honours each of these (a rule-less match hides every
        rule on the line), but none names this rule with a reason.
        """
        for line in (
            "probe()  # nosemgrep",
            "probe()  # nosem",
            "probe()  # NOSEMGREP: example, reason: reviewed probe",
            "probe()  # nosem: example, reason: reviewed probe",
            "probe()  # nosemgrep: example",
            "probe()  # nosemgrep: example, reason:",
            "probe()  # nosemgrep: example, reason: 42",
            "probe()  # nosemgrep:example, reason: reviewed probe",
            "probe()  # nosemgrep: other, reason: reviewed probe",
            "probe()  # nosemgrep: other, x.example, reason: reviewed probe",
            "probe()  # nosemgrep: example, reason: reviewed, other",
            "probe()  # nosemgrep: example,reason: reviewed probe",
            "probe()  # nosemgrep: example reason: reviewed probe",
            "probe()  # nosemgrep: example, reason: reviewed  # nosemgrep",
            # The single-id form fails a strict scan when another rule
            # matches the line, so it is not a reviewed annotation.
            "probe()  # nosemgrep: example -- reviewed probe",
            "probe()  # nosemgrep: example, other -- reviewed probe",
            'probe("see nosemgrep docs")',
            "probe()  # NoSemantics",
            "probe()  # no\u017fem",
        ):
            with self.subTest(line=line):
                self._write_source({4: line})
                report, sarif = self._suppressed_finding()
                with self.assertRaisesRegex(
                    ValueError,
                    "no reviewed 'nosemgrep: example, reason: <reason>'",
                ):
                    self._prepare(report, sarif)

    def test_reviewed_annotations_on_the_line_or_the_line_above(self):
        expected = copy.deepcopy(self.sarif)
        for lines in (
            {4: "probe()  # nosemgrep: example, reason: reviewed probe"},
            {4: "probe()  // nosemgrep: example, reason: reviewed probe"},
            {3: "    # nosemgrep: example, reason: reviewed probe"},
            {3: "{# nosemgrep: example, reason: reviewed probe #}"},
            {3: "# nosemgrep: example, reason: reviewed probe\r"},
            # Several reviewed rules in one annotation; the result's exact
            # id must be one of them.
            {4: "probe()  # nosemgrep: other, example, reason: reviewed"},
            {3: "# nosemgrep: example, x.other, reason: reviewed probe"},
        ):
            with self.subTest(lines=lines):
                self._write_source(lines)
                report, sarif = self._suppressed_finding()
                self.assertEqual(self._prepare(report, sarif), expected)

    def test_previous_line_annotation_needs_semgreps_own_line_form(self):
        # Semgrep ignores a marker after code on the line above a finding,
        # so the finding here was suppressed by something else.
        for lines in (
            {3: "x = 1  # nosemgrep: example, reason: reviewed probe"},
            {2: "# nosemgrep: example, reason: reviewed probe"},
        ):
            with self.subTest(lines=lines):
                self._write_source(lines)
                report, sarif = self._suppressed_finding()
                with self.assertRaisesRegex(ValueError, "no reviewed"):
                    self._prepare(report, sarif)

    def test_source_lines_split_like_semgrep(self):
        # str.splitlines() would also split at U+2028 and shift line 4.
        (self.source_root / "example.py").write_text(
            "a\u2028b\nprobe()\nprobe()\n"
            "probe()  # nosemgrep: example, reason: reviewed probe\n",
            encoding="utf-8",
        )
        report, sarif = self._suppressed_finding()
        self.assertEqual(self._prepare(report, sarif), self.sarif)

    def test_unreadable_suppressed_locations_fail_closed(self):
        for uri, line, message in (
            ("missing.py", 4, "Cannot read the source"),
            ("example.py", 99, "beyond the end"),
            ("example.py", 0, "malformed location"),
            ("example.py", True, "malformed location"),
            ("example.py", "4", "malformed location"),
            ("file:///etc/passwd", 4, "malformed location"),
            ("example%2Epy", 4, "malformed location"),
            ("", 4, "malformed location"),
        ):
            with self.subTest(uri=uri, line=line):
                report, sarif = self._suppressed_finding(line, uri)
                with self.assertRaisesRegex(ValueError, message):
                    self._prepare(report, sarif)
        report, sarif = self._suppressed_finding()
        del sarif["runs"][0]["results"][0]["locations"]
        with self.assertRaisesRegex(ValueError, "has no source line"):
            self._prepare(report, sarif)

    def test_cli_rejects_unreviewed_suppressions_without_writing(self):
        self._write_source({4: "probe()  # nosemgrep"})
        report, sarif = self._suppressed_finding()
        with tempfile.TemporaryDirectory() as directory:
            root = Path(directory)
            report_path, sarif_path = root / "raw.json", root / "raw.sarif"
            upload_path = root / "github.sarif"
            report_path.write_text(json.dumps(report), encoding="utf-8")
            sarif_path.write_text(json.dumps(sarif), encoding="utf-8")
            out = io.StringIO()
            with (
                mock.patch.object(
                    sys,
                    "argv",
                    [
                        "check",
                        str(report_path),
                        str(sarif_path),
                        "--github-sarif-output",
                        str(upload_path),
                        "--source-root",
                        str(self.source_root),
                    ],
                ),
                contextlib.redirect_stdout(out),
            ):
                self.assertEqual(POLICY.main(), 1)
            self.assertIn("no reviewed", out.getvalue())
            self.assertFalse(upload_path.exists())

    def test_cli_creates_upload_and_preserves_raw_reports(self):
        report, sarif = self._with_finding()
        suppressed = {
            "ruleId": "example",
            "suppressions": [{"kind": "inSource"}],
            "locations": self._location(4),
        }
        sarif["runs"][0]["results"].append(suppressed)
        with tempfile.TemporaryDirectory() as directory:
            root = Path(directory)
            report_path, sarif_path = root / "raw.json", root / "raw.sarif"
            upload_path = root / "github.sarif"
            report_path.write_text(json.dumps(report), encoding="utf-8")
            sarif_path.write_text(json.dumps(sarif), encoding="utf-8")
            originals = report_path.read_bytes(), sarif_path.read_bytes()
            with (
                mock.patch.object(
                    sys,
                    "argv",
                    [
                        "check",
                        str(report_path),
                        str(sarif_path),
                        "--github-sarif-output",
                        str(upload_path),
                        "--source-root",
                        str(self.source_root),
                    ],
                ),
                contextlib.redirect_stdout(io.StringIO()),
            ):
                self.assertEqual(POLICY.main(), 0)
            expected = copy.deepcopy(sarif)
            expected["runs"][0]["results"].pop()
            self.assertEqual(json.loads(upload_path.read_text()), expected)
            self.assertEqual(
                (report_path.read_bytes(), sarif_path.read_bytes()), originals
            )

    def test_cli_rejects_raw_report_overwrites(self):
        with tempfile.TemporaryDirectory() as directory:
            root = Path(directory)
            report_path, sarif_path = root / "raw.json", root / "raw.sarif"
            report_path.write_text(json.dumps(self.report), encoding="utf-8")
            sarif_path.write_text(json.dumps(self.sarif), encoding="utf-8")
            alias = root / "alias.sarif"
            alias.symlink_to(sarif_path)
            # A hard link resolves to its own path; only the inode matches.
            hard_link = root / "hard-link.sarif"
            os.link(sarif_path, hard_link)
            originals = report_path.read_bytes(), sarif_path.read_bytes()
            for destination in (report_path, sarif_path, alias, hard_link):
                with self.subTest(destination=destination):
                    out = io.StringIO()
                    with (
                        mock.patch.object(
                            sys,
                            "argv",
                            [
                                "check",
                                str(report_path),
                                str(sarif_path),
                                "--github-sarif-output",
                                str(destination),
                            ],
                        ),
                        contextlib.redirect_stdout(out),
                    ):
                        self.assertEqual(POLICY.main(), 1)
                    self.assertIn(
                        "must not overwrite a raw report", out.getvalue()
                    )
                    self.assertEqual(
                        (report_path.read_bytes(), sarif_path.read_bytes()),
                        originals,
                    )

    def test_cli_does_not_write_upload_after_validation_failure(self):
        with tempfile.TemporaryDirectory() as directory:
            root = Path(directory)
            report_path, sarif_path = root / "raw.json", root / "raw.sarif"
            upload_path = root / "github.sarif"
            report_path.write_text(
                json.dumps({**self.report, "errors": ["failure"]})
            )
            sarif_path.write_text(json.dumps(self.sarif))
            with (
                mock.patch.object(
                    sys,
                    "argv",
                    [
                        "check",
                        str(report_path),
                        str(sarif_path),
                        "--github-sarif-output",
                        str(upload_path),
                    ],
                ),
                contextlib.redirect_stdout(io.StringIO()),
            ):
                self.assertEqual(POLICY.main(), 1)
            self.assertFalse(upload_path.exists())

    def test_hostile_input_reports_error_instead_of_traceback(self):
        """Hostile reports fail with an ``::error::`` line, not a traceback.

        The deeply nested input has two valid outcomes. Before Python 3.14
        the C JSON decoder bounds nesting by a fixed recursion count, so it
        raises ``RecursionError``. From 3.14 the bound is the C stack
        size, so with a large or unlimited ``ulimit -s`` (as in the CI test
        image, Python 3.14.8) the 100,000-deep list parses and is then
        rejected as not being SARIF. No depth is deterministic there, since
        an unlimited stack parses any depth that fits in memory. Either way
        the validator must report an error and exit 1; that is what matters.
        """
        with tempfile.TemporaryDirectory() as directory:
            root = Path(directory)
            report = root / "report.json"
            report.write_text(json.dumps(self.report), encoding="utf-8")
            deep = root / "deep.sarif"
            deep.write_text("[" * 100_000 + "]" * 100_000, encoding="utf-8")
            huge = copy.deepcopy(self.sarif)
            huge["runs"][0]["tool"] = {
                "driver": {
                    "rules": [
                        {
                            "id": "example",
                            "properties": {"security-severity": 10**400},
                        }
                    ]
                }
            }
            huge["runs"][0]["results"] = [{"ruleId": "example"}]
            overflow = root / "overflow.sarif"
            overflow.write_text(json.dumps(huge), encoding="utf-8")
            for sarif_path, reason in (
                (
                    deep,
                    r"RecursionError|ValueError: Expected native SARIF 2\.1\.0",
                ),
                (overflow, r"out-of-range security severity"),
            ):
                with self.subTest(sarif=sarif_path.name):
                    out = io.StringIO()
                    with (
                        mock.patch.object(
                            sys,
                            "argv",
                            ["check", str(report), str(sarif_path)],
                        ),
                        contextlib.redirect_stdout(out),
                    ):
                        self.assertEqual(POLICY.main(), 1)
                    self.assertRegex(
                        out.getvalue(),
                        rf"\A::error::Invalid Semgrep scan: [^\n]*(?:{reason})",
                    )
                    self.assertNotIn("Traceback", out.getvalue())

    def test_cli_accepts_consistent_reports(self):
        report, sarif = self._with_finding()
        with tempfile.TemporaryDirectory() as directory:
            root = Path(directory)
            (root / "r.json").write_text(json.dumps(report), encoding="utf-8")
            (root / "r.sarif").write_text(json.dumps(sarif), encoding="utf-8")
            out = io.StringIO()
            with (
                mock.patch.object(
                    sys,
                    "argv",
                    ["check", str(root / "r.json"), str(root / "r.sarif")],
                ),
                contextlib.redirect_stdout(out),
            ):
                self.assertEqual(POLICY.main(), 0)
            self.assertIn("1 findings", out.getvalue())


if __name__ == "__main__":
    unittest.main()