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.

22.0 KB
Raw Blame
"""Standalone tests for the retire.js JSON to SARIF converter."""

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

import copy
import importlib.util
import io
import json
import tempfile
import unittest
from contextlib import redirect_stderr, redirect_stdout
from pathlib import Path

ROOT = Path(__file__).resolve().parents[3]


def _load(name, relative):
    spec = importlib.util.spec_from_file_location(name, ROOT / relative)
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)
    return module


CONVERTER = _load("retire_sarif", ".github/scripts/retire_json_to_sarif.py")
POLICY = _load(
    "release_scan_for_retire",
    ".github/scripts/check_release_scan_evidence.py",
)

RUNNER_ROOT = "/home/runner/work/local-deep-research/local-deep-research"

# A real `npx [email protected] --severity medium --outputformat json` report
# (exit status 13) for DOMPurify 2.0.0 and jQuery 3.4.1, trimmed to two
# files and four vulnerabilities, with the scratch directory replaced by a
# runner checkout path.
REAL_REPORT = {
    "version": "5.2.4",
    "start": "2026-09-26T12:14:07.084Z",
    "data": [
        {
            "file": f"{RUNNER_ROOT}/node_modules/dompurify/dist/purify.js",
            "results": [
                {
                    "version": "2.0.0",
                    "component": "DOMPurify",
                    "npmname": "dompurify",
                    "detection": "filecontent",
                    "vulnerabilities": [
                        {
                            "info": [
                                "https://github.com/cure53/DOMPurify/releases"
                            ],
                            "below": "2.0.3",
                            "severity": "medium",
                            "identifiers": {
                                "summary": "Fixed an mXSS-based bypass "
                                "caused by nested forms inside MathML",
                                "githubID": "GHSA-chqj-j4fh-rw7m",
                                "CVE": ["CVE-2019-16728"],
                            },
                            "cwe": ["CWE-79"],
                        },
                        {
                            "info": [
                                "https://github.com/cure53/DOMPurify/releases"
                            ],
                            "below": "2.0.7",
                            "severity": "high",
                            "identifiers": {
                                "summary": "possible to bypass the package "
                                "sanitization through Mutation XSS",
                                "githubID": "GHSA-mjjq-c88q-qhr6",
                            },
                            "cwe": ["CWE-79"],
                        },
                        {
                            "info": [
                                "https://github.com/cure53/DOMPurify/releases"
                            ],
                            "below": "2.0.16",
                            "severity": "low",
                            "identifiers": {
                                "summary": "Fixed an mXSS-based bypass "
                                "caused by nested forms inside MathML",
                                "retid": "28",
                            },
                            "cwe": ["CWE-79"],
                        },
                    ],
                    "licenses": ["MPL-2.0 OR Apache-2.0"],
                }
            ],
        },
        {
            "file": f"{RUNNER_ROOT}/node_modules/jquery/dist/jquery.min.js",
            "results": [
                {
                    "version": "3.4.1",
                    "component": "jquery",
                    "npmname": "jquery",
                    "detection": "filecontent",
                    "vulnerabilities": [
                        {
                            "info": [
                                "https://blog.jquery.com/2020/04/10/"
                                "jquery-3-5-0-released/"
                            ],
                            "below": "3.5.0",
                            "atOrAbove": "1.0.3",
                            "severity": "medium",
                            "identifiers": {
                                "summary": "passing HTML containing <option> "
                                "elements from untrusted sources - even after "
                                "sanitizing it - to one of jQuery's DOM "
                                "manipulation methods (i.e. .html(), "
                                ".append(), and others) may execute "
                                "untrusted code.",
                                "CVE": ["CVE-2020-11023"],
                                "issue": "4647",
                                "githubID": "GHSA-jpcq-cgw6-v4j6",
                            },
                            "cwe": ["CWE-79"],
                        }
                    ],
                    "licenses": ["MIT"],
                }
            ],
        },
    ],
    "messages": [],
    "errors": [],
    "time": 2.244,
}

# A real clean report (exit status 0) for this repository's checkout.
EMPTY_REPORT = {
    "version": "5.2.4",
    "start": "2026-09-26T12:12:41.179Z",
    "data": [],
    "messages": [],
    "errors": [],
    "time": 2.22,
}


def _vulnerability(severity=None, **identifiers):
    vulnerability = {
        "info": ["https://example.invalid/advisory"],
        "below": "2.0.0",
        "identifiers": {"summary": "an advisory", **identifiers},
    }
    if severity is not None:
        vulnerability["severity"] = severity
    return vulnerability


def _report(*vulnerabilities, file="node_modules/lib/dist/lib.js"):
    return {
        "version": "5.2.4",
        "data": [
            {
                "file": file,
                "results": [
                    {
                        "version": "1.0.0",
                        "component": "lib",
                        "vulnerabilities": list(vulnerabilities),
                    }
                ],
            }
        ],
        "errors": [],
    }


def _results_by_rule(sarif):
    return {r["ruleId"]: r for r in sarif["runs"][0]["results"]}


def _rules(sarif):
    return {r["id"]: r for r in sarif["runs"][0]["tool"]["driver"]["rules"]}


class ConvertTests(unittest.TestCase):
    def test_real_report_becomes_sarif_with_relative_locations(self):
        sarif = CONVERTER.convert(REAL_REPORT, RUNNER_ROOT)
        self.assertEqual(sarif["version"], "2.1.0")
        run = sarif["runs"][0]
        self.assertEqual(run["tool"]["driver"]["name"], "retire.js")
        self.assertEqual(run["tool"]["driver"]["version"], "5.2.4")
        results = _results_by_rule(sarif)
        self.assertEqual(
            sorted(results),
            [
                "GHSA-chqj-j4fh-rw7m",
                "GHSA-jpcq-cgw6-v4j6",
                "GHSA-mjjq-c88q-qhr6",
                "RETIRE-28",
            ],
        )
        self.assertEqual(sorted(_rules(sarif)), sorted(results))
        self.assertEqual(
            {
                rule: (r["level"], r["properties"]["security-severity"])
                for rule, r in results.items()
            },
            {
                "GHSA-chqj-j4fh-rw7m": ("warning", "5.5"),
                "GHSA-jpcq-cgw6-v4j6": ("warning", "5.5"),
                "GHSA-mjjq-c88q-qhr6": ("error", "8.0"),
                "RETIRE-28": ("note", "2.0"),
            },
        )
        location = results["GHSA-jpcq-cgw6-v4j6"]["locations"][0][
            "physicalLocation"
        ]["artifactLocation"]
        self.assertEqual(
            location["uri"], "node_modules/jquery/dist/jquery.min.js"
        )
        self.assertEqual(location["uriBaseId"], "%SRCROOT%")
        self.assertIn(
            "CVE-2020-11023", results["GHSA-jpcq-cgw6-v4j6"]["message"]["text"]
        )
        # No absolute runner path may reach code scanning.
        self.assertNotIn(RUNNER_ROOT, json.dumps(sarif))
        self.assertNotIn('"/', json.dumps(run["results"]))

    def test_empty_report_is_a_valid_sarif_with_no_results(self):
        sarif = CONVERTER.convert(EMPTY_REPORT, RUNNER_ROOT)
        self.assertEqual(sarif["version"], "2.1.0")
        self.assertEqual(len(sarif["runs"]), 1)
        self.assertEqual(sarif["runs"][0]["results"], [])
        self.assertEqual(sarif["runs"][0]["tool"]["driver"]["rules"], [])
        self.assertEqual(
            sarif["runs"][0]["tool"]["driver"]["name"], "retire.js"
        )

    def test_severity_mapping(self):
        expected = {
            "critical": ("error", "9.5"),
            "high": ("error", "8.0"),
            "medium": ("warning", "5.5"),
            "low": ("note", "2.0"),
            # retire.js's schema allows "none": an unscored note.
            "none": ("note", None),
            # retire.js treats a missing severity as critical.
            None: ("error", "9.5"),
        }
        for severity, (level, score) in expected.items():
            with self.subTest(severity=severity):
                sarif = CONVERTER.convert(
                    _report(_vulnerability(severity, githubID="GHSA-1")), "/r"
                )
                result = sarif["runs"][0]["results"][0]
                rule = sarif["runs"][0]["tool"]["driver"]["rules"][0]
                self.assertEqual(result["level"], level)
                self.assertEqual(
                    result["properties"].get("security-severity"), score
                )
                self.assertEqual(rule["defaultConfiguration"]["level"], level)
                self.assertEqual(
                    rule["properties"].get("security-severity"), score
                )

    def test_null_severity_is_critical_like_retire(self):
        # retire.js 5.2.4 uses `v.severity ?? 'critical'`, so an explicit
        # JSON null is critical too, not an unknown severity.
        vulnerability = _vulnerability(githubID="GHSA-1")
        vulnerability["severity"] = None
        sarif = CONVERTER.convert(_report(vulnerability), "/r")
        result = sarif["runs"][0]["results"][0]
        self.assertEqual(result["level"], "error")
        self.assertEqual(result["properties"]["security-severity"], "9.5")

    def test_a_none_severity_advisory_does_not_reject_the_report(self):
        report = copy.deepcopy(REAL_REPORT)
        report["data"][1]["results"][0]["vulnerabilities"].append(
            _vulnerability("none", githubID="GHSA-none")
        )
        sarif = CONVERTER.convert(report, RUNNER_ROOT)
        results = _results_by_rule(sarif)
        self.assertEqual(len(results), 5)
        none = results["GHSA-none"]
        self.assertEqual(none["level"], "note")
        self.assertNotIn("security-severity", none["properties"])
        self.assertNotIn("none vulnerability", none["message"]["text"])
        self.assertIn("rates severity none", none["message"]["text"])
        rule = _rules(sarif)["GHSA-none"]
        self.assertNotIn("security-severity", rule["properties"])

    def test_mapping_blocks_exactly_what_the_release_gate_blocks(self):
        """The alert gate must block critical/high/medium, never low."""

        def github_band(score):
            # GitHub's documented security-severity bands.
            score = float(score)
            if score >= 9.0:
                return "critical"
            if score >= 7.0:
                return "high"
            if score >= 4.0:
                return "medium"
            return "low"

        for severity in ("critical", "high", "medium", "low", "none"):
            with self.subTest(severity=severity):
                sarif = CONVERTER.convert(
                    _report(_vulnerability(severity, githubID="GHSA-1")), "/r"
                )
                rule = sarif["runs"][0]["tool"]["driver"]["rules"][0]
                score = rule["properties"].get("security-severity")
                if severity == "none":
                    # Unscored: GitHub reports no security severity level,
                    # so only the SARIF-level fallback applies.
                    self.assertIsNone(score)
                    bands = (None,)
                else:
                    band = github_band(score)
                    self.assertEqual(band, severity)
                    bands = (band, None)
                blocks = severity not in {"low", "none"}
                for scored in bands:
                    alert = {
                        "number": 1,
                        "state": "open",
                        "tool": {"name": "retire.js"},
                        "rule": {
                            "security_severity_level": scored,
                            "severity": rule["defaultConfiguration"]["level"],
                        },
                    }
                    self.assertEqual(POLICY.blocking_alert(alert), blocks)

    def test_rule_takes_the_highest_severity_of_a_shared_identifier(self):
        sarif = CONVERTER.convert(
            _report(
                _vulnerability("low", githubID="GHSA-1"),
                _vulnerability("high", githubID="GHSA-1"),
            ),
            "/r",
        )
        rule = _rules(sarif)["GHSA-1"]
        self.assertEqual(rule["properties"]["security-severity"], "8.0")

    def test_a_repeated_finding_keeps_its_highest_severity(self):
        for order in (("low", "high"), ("high", "low")):
            with self.subTest(order=order):
                sarif = CONVERTER.convert(
                    _report(
                        *(
                            _vulnerability(severity, githubID="GHSA-1")
                            for severity in order
                        )
                    ),
                    "/r",
                )
                results = sarif["runs"][0]["results"]
                self.assertEqual(len(results), 1)
                result = results[0]
                self.assertEqual(result["level"], "error")
                self.assertEqual(
                    result["properties"]["security-severity"], "8.0"
                )
                self.assertIn(
                    "known high vulnerability", result["message"]["text"]
                )
        # The upgrade replaces the result in place: order is preserved.
        sarif = CONVERTER.convert(
            _report(
                _vulnerability("low", githubID="GHSA-1"),
                _vulnerability("medium", githubID="GHSA-2"),
                _vulnerability("critical", githubID="GHSA-1"),
            ),
            "/r",
        )
        self.assertEqual(
            [(r["ruleId"], r["level"]) for r in sarif["runs"][0]["results"]],
            [("GHSA-1", "error"), ("GHSA-2", "warning")],
        )

    def test_a_repeated_finding_in_one_file_is_reported_once(self):
        vulnerability = _vulnerability("medium", githubID="GHSA-1")
        report = _report(vulnerability, vulnerability)
        report["data"].append(copy.deepcopy(report["data"][0]))
        sarif = CONVERTER.convert(report, "/r")
        self.assertEqual(len(sarif["runs"][0]["results"]), 1)
        report["data"][1]["file"] = "node_modules/lib/dist/lib.min.js"
        sarif = CONVERTER.convert(report, "/r")
        self.assertEqual(len(sarif["runs"][0]["results"]), 2)

    def test_rule_identifier_preference(self):
        cases = (
            ({"githubID": "GHSA-a", "CVE": ["CVE-1"]}, "GHSA-a"),
            ({"CVE": ["CVE-1", "CVE-2"], "retid": "7"}, "CVE-1"),
            ({"retid": "7", "issue": "9"}, "RETIRE-7"),
            ({"issue": "9"}, "issue-9"),
        )
        for identifiers, rule_id in cases:
            with self.subTest(identifiers=identifiers):
                sarif = CONVERTER.convert(
                    _report(_vulnerability("medium", **identifiers)), "/r"
                )
                self.assertEqual(list(_results_by_rule(sarif)), [rule_id])
        sarif = CONVERTER.convert(_report(_vulnerability("medium")), "/r")
        self.assertRegex(
            sarif["runs"][0]["results"][0]["ruleId"],
            r"^RETIRE-lib-[0-9a-f]{16}$",
        )

    def test_relative_and_absolute_paths_share_one_uri(self):
        for file in ("/r/a/b.js", "a/b.js", "/r/a/./c/../b.js"):
            with self.subTest(file=file):
                sarif = CONVERTER.convert(
                    _report(_vulnerability("low", retid="1"), file=file), "/r"
                )
                uri = sarif["runs"][0]["results"][0]["locations"][0][
                    "physicalLocation"
                ]["artifactLocation"]["uri"]
                self.assertEqual(uri, "a/b.js")

    def test_malformed_reports_raise(self):
        def mutate(change):
            report = copy.deepcopy(REAL_REPORT)
            change(report)
            return report

        def set_vuln(key, value):
            def change(report):
                vuln = report["data"][0]["results"][0]["vulnerabilities"][0]
                vuln[key] = value

            return change

        malformed = {
            "not an object": [],
            "no version": mutate(lambda r: r.pop("version")),
            "data missing": mutate(lambda r: r.pop("data")),
            "data not a list": mutate(lambda r: r.update(data={})),
            "entry not an object": mutate(lambda r: r.update(data=["x"])),
            "file missing": mutate(lambda r: r["data"][0].pop("file")),
            "file outside root": mutate(
                lambda r: r["data"][0].update(file="/etc/passwd.js")
            ),
            "file escapes root": mutate(
                lambda r: r["data"][0].update(file="../outside.js")
            ),
            "results not a list": mutate(
                lambda r: r["data"][0].update(results={})
            ),
            "component missing": mutate(
                lambda r: r["data"][0]["results"][0].pop("component")
            ),
            "version not a string": mutate(
                lambda r: r["data"][0]["results"][0].update(version=2)
            ),
            "vulnerabilities not a list": mutate(
                lambda r: r["data"][0]["results"][0].update(vulnerabilities={})
            ),
            "unknown severity": mutate(set_vuln("severity", "moderate")),
            "identifiers not a map": mutate(set_vuln("identifiers", [])),
            "info not a list": mutate(set_vuln("info", "https://x")),
            "errors not a list": mutate(lambda r: r.update(errors="boom")),
        }
        for name, report in malformed.items():
            with self.subTest(name=name):
                with self.assertRaises(CONVERTER.ReportError):
                    CONVERTER.convert(report, RUNNER_ROOT)


class MainTests(unittest.TestCase):
    def run_main(self, report_text, *flags, existing_output=False):
        with tempfile.TemporaryDirectory() as directory:
            work = Path(directory)
            report = work / "retire-results.json"
            output = work / "retire-results.sarif"
            if report_text is not None:
                report.write_text(report_text, encoding="utf-8")
            if existing_output:
                output.write_text("stale", encoding="utf-8")
            stderr = io.StringIO()
            with redirect_stdout(io.StringIO()), redirect_stderr(stderr):
                code = CONVERTER.main(
                    [*flags, "--root", str(work), str(report), str(output)]
                )
            written = (
                output.read_text(encoding="utf-8") if output.exists() else None
            )
            leftovers = sorted(p.name for p in work.iterdir())
            return code, written, stderr.getvalue(), leftovers

    def test_clean_report_writes_an_empty_sarif(self):
        code, written, _, leftovers = self.run_main(json.dumps(EMPTY_REPORT))
        self.assertEqual(code, 0)
        self.assertEqual(json.loads(written)["runs"][0]["results"], [])
        self.assertEqual(
            leftovers, ["retire-results.json", "retire-results.sarif"]
        )

    def test_findings_report_writes_results(self):
        report = _report(_vulnerability("high", githubID="GHSA-1"))
        code, written, _, _ = self.run_main(
            json.dumps(report), "--require-findings"
        )
        self.assertEqual(code, 0)
        self.assertEqual(len(json.loads(written)["runs"][0]["results"]), 1)

    def test_require_findings_accepts_a_none_severity_only_report(self):
        # With --severity none, retire.js exits 13 for a "none" advisory.
        report = _report(_vulnerability("none", githubID="GHSA-1"))
        code, written, _, _ = self.run_main(
            json.dumps(report), "--require-findings"
        )
        self.assertEqual(code, 0)
        results = json.loads(written)["runs"][0]["results"]
        self.assertEqual([r["level"] for r in results], ["note"])

    def test_require_findings_rejects_an_empty_report(self):
        code, written, stderr, _ = self.run_main(
            json.dumps(EMPTY_REPORT), "--require-findings"
        )
        self.assertEqual(code, 1)
        self.assertIsNone(written)
        self.assertIn("::error::", stderr)

    def test_malformed_input_fails_and_writes_nothing(self):
        cases = {
            "missing file": None,
            "empty file": "",
            "not json": "{not json",
            "truncated": json.dumps(REAL_REPORT)[:-10],
            "wrong shape": json.dumps({"version": "5.2.4", "data": {}}),
        }
        for name, text in cases.items():
            with self.subTest(name=name):
                code, written, stderr, _ = self.run_main(
                    text, existing_output=True
                )
                self.assertEqual(code, 1)
                # A stale SARIF from an earlier step must not survive either.
                self.assertIsNone(written)
                self.assertIn("::error::", stderr)

    def test_error_messages_cannot_inject_workflow_commands(self):
        self.assertEqual(
            CONVERTER._workflow_command_data("a%b\r\n::error::c"),
            "a%25b%0D%0A::error::c",
        )
        report = copy.deepcopy(EMPTY_REPORT)
        report["errors"] = ["Could not parse file: a.js\n::error::injected"]
        code, _, stderr, _ = self.run_main(json.dumps(report))
        self.assertEqual(code, 0)
        self.assertEqual(stderr.count("\n"), 1)
        self.assertIn("%0A::error::injected", stderr)


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