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.

54.7 KB
Raw Blame
"""Standalone release-policy tests using synthetic API responses."""

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

import importlib.util
import io
import json
import os
import re
import shutil
import subprocess
import sys
import tempfile
import unittest
from contextlib import redirect_stdout
from datetime import datetime, timedelta, timezone
from pathlib import Path
from unittest.mock import patch
from urllib.parse import urlencode

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

SHA = "a" * 40
REF = "refs/tags/v1.0.0"
WORKFLOW = ".github/workflows/release.yml"
START = "2026-01-01T00:00:00Z"


def analysis(**changes):
    return {
        "category": "semgrep-security",
        "commit_sha": SHA,
        "ref": REF,
        "analysis_key": f"{WORKFLOW}:semgrep-scan",
        "created_at": START,
        "error": "",
        "results_count": 0,
        **changes,
    }


def alert(**changes):
    record = {
        "number": 1,
        "state": "open",
        "tool": {"name": "CodeQL"},
        "rule": {"security_severity_level": "critical", "severity": None},
        **changes,
    }
    record["rule"].setdefault("id", "SAMPLE_RULE")
    record.setdefault(
        "most_recent_instance",
        {"location": {"path": "sample.py", "start_line": 1}},
    )
    return record


# --- R3: tie release-scan-policy.json to what release-gate.yml actually
# uploads. This is a plain-text (no PyYAML) walk, deliberately: the standalone
# suite runs in ci-policy-tests.yml on a bare `actions/setup-python` with no
# dependency install step, so a `yaml` import here would break that job.
_JOB_ID_RE = re.compile(r"^  ([A-Za-z0-9_-]+):\s*$")
_LOCAL_USES_RE = re.compile(r"^    uses:\s*(\$/\S+|\./\S+)\s*$")
_UPLOAD_MARKERS = ("upload-sarif", "codeql-action/analyze")
_CATEGORY_RE = re.compile(r"^\s*category:\s*(.+?)\s*(#.*)?$")

# Remote reusable "uploaders" whose SARIF category cannot be statically
# extracted from this repo (a third-party composite/Docker action that
# uploads internally, not a local `$/` reusable workflow with its own
# `upload-sarif`/`codeql-action/analyze` step to grep). Each entry must
# name why it's safe to trust rather than verify by parsing.
_REMOTE_SARIF_UPLOADERS = {
    # zizmorcore/zizmor-action (zizmor-security.yml) uploads its own SARIF
    # internally under a fixed "zizmor" category — confirmed against the
    # live Security tab, not discoverable by grepping this repo.
    "zizmor-scan": "zizmor",
}

# Remote reusable workflows (`owner/repo/.github/workflows/x.yml@sha`) that a
# gate-called local workflow delegates a whole job to. Their upload-sarif step
# lives in the other repository, so its category is recorded here instead of
# parsed. "{workflow}" stands for the top-level workflow file of the run.
_REMOTE_REUSABLE_SARIF_UPLOADERS = {
    # osv-scanner.yml -> google/osv-scanner-action's reusable workflow, whose
    # "Upload to code-scanning" step (v2.6.0) passes no `category:`. Code
    # scanning therefore files it under the default "<top-level workflow>:
    # <job id>" category, e.g. ".github/workflows/release-gate.yml:osv-scan"
    # for the scheduled release-gate run (confirmed against live analyses).
    "google/osv-scanner-action/.github/workflows/osv-scanner-reusable.yml": (
        "{workflow}:osv-scan"
    ),
}

# The local OSV workflow preserves the same terminal job id and implicit
# category as its former upstream reusable workflow. Do not generalize this
# exception to other uploads: #7220 tracks the caller/category alignment.
_LOCAL_DEFAULT_SARIF_UPLOADERS = {
    (".github/workflows/osv-scan-reusable.yml", "osv-scan"): (
        "{workflow}:osv-scan"
    ),
}

# Categories the gate uploads but release-scan-policy.json deliberately does
# not require, each with the reason.
_UPLOADED_BUT_NOT_IN_POLICY = {
    # The category embeds the calling workflow's path (release.yml for a tag
    # release, release-gate.yml for the scheduled run), which the policy's
    # fixed category names cannot express. OSV is still gated: the
    # osv-scan job fails on any vulnerability (fail-on-vuln defaults to
    # true) or scanner error, release-gate-summary requires every
    # dependency to succeed, and its open alerts on the release ref are
    # counted by the alert query like any other tool's.
    "{workflow}:osv-scan": "path-dependent default category",
}

_REMOTE_REUSABLE_RE = re.compile(
    r"^\s+uses:\s*[\"']?([\w.-]+/[\w.-]+/\.github/workflows/[^@\"'\s]+)@"
)


def _local_reusable_workflow_jobs(gate_text):
    """release-gate.yml job id -> the `$/...`/`./...` workflow file it calls."""
    jobs = {}
    current = None
    for line in gate_text.splitlines():
        job_match = _JOB_ID_RE.match(line)
        if job_match:
            current = job_match.group(1)
            continue
        if line and not line.startswith(" "):
            current = None
            continue
        uses_match = _LOCAL_USES_RE.match(line)
        if uses_match and current:
            jobs[current] = uses_match.group(1)[2:]  # strip "$/" / "./"
    return jobs


def _sarif_categories_in(text, source="workflow"):
    """Every `category:` value that follows an upload-sarif/analyze step.

    An upload step with no `category:` in reach is reported as a
    "<uncategorized ...>" placeholder rather than skipped, and so is a remote
    reusable workflow not listed in _REMOTE_REUSABLE_SARIF_UPLOADERS: either
    can upload SARIF this walk cannot name, so neither may silently vanish
    from the parity check.
    """
    lines = text.splitlines()
    found = []
    current_job = None
    for i, line in enumerate(lines):
        job_match = _JOB_ID_RE.match(line)
        if job_match:
            current_job = job_match.group(1)
        remote = _REMOTE_REUSABLE_RE.match(line)
        if remote:
            found.append(
                _REMOTE_REUSABLE_SARIF_UPLOADERS.get(
                    remote.group(1),
                    f"<unrecognised remote reusable workflow "
                    f"{remote.group(1)} in {source}>",
                )
            )
            continue
        if line.lstrip().startswith("#") or not any(
            marker in line for marker in _UPLOAD_MARKERS
        ):
            continue
        for follow in lines[i + 1 : i + 8]:
            match = _CATEGORY_RE.match(follow)
            if match:
                found.append(match.group(1).strip().strip("\"'"))
                break
            if re.match(r"^\s*-\s*(name|uses):", follow):
                break  # next step started with no category on this one
        else:
            match = None
        if not match:
            found.append(
                _LOCAL_DEFAULT_SARIF_UPLOADERS.get(
                    (source, current_job),
                    f"<uncategorized upload in {source} line {i + 1}>",
                )
            )
    return found


def _expand_matrix_language(category, workflow_text):
    if "matrix.python" in category:
        legs = re.findall(
            r"- python: ['\"]([0-9.]+)['\"]\s+distribution: (\w+)",
            workflow_text,
        )
        return [
            category.replace("${{ matrix.python }}", version).replace(
                "${{ matrix.distribution }}", distribution
            )
            for version, distribution in legs
        ] or [category]
    if "matrix.language" not in category:
        return [category]
    languages = re.findall(
        r"^\s*-\s*language:\s*([\w.-]+)", workflow_text, re.MULTILINE
    )
    return [
        category.replace("${{matrix.language}}", lang).replace(
            "${{ matrix.language }}", lang
        )
        for lang in languages
    ] or [category]


def _release_gate_sarif_categories():
    """Every SARIF category reachable from release-gate.yml, discovered the
    same way the policy's author verified it by hand: walk each job's local
    reusable workflow and read off its upload-sarif/analyze category, plus
    the recorded category of any remote reusable workflow it calls.
    Uploads the walk cannot name come back as "<...>" placeholders.
    """
    gate_text = (ROOT / ".github/workflows/release-gate.yml").read_text(
        encoding="utf-8"
    )
    categories = {
        category
        for raw in _sarif_categories_in(gate_text)
        for category in _expand_matrix_language(raw, gate_text)
    }
    pending = list(_local_reusable_workflow_jobs(gate_text).items())
    visited = set()
    while pending:
        job_id, rel_path = pending.pop()
        if rel_path in visited:
            continue
        visited.add(rel_path)
        workflow_text = (ROOT / rel_path).read_text(encoding="utf-8")
        pending.extend(_local_reusable_workflow_jobs(workflow_text).items())
        expanded = set()
        for raw_category in _sarif_categories_in(workflow_text, rel_path):
            expanded.update(
                _expand_matrix_language(raw_category, workflow_text)
            )
        if not expanded and job_id in _REMOTE_SARIF_UPLOADERS:
            expanded = {_REMOTE_SARIF_UPLOADERS[job_id]}
        categories.update(expanded)
    return categories


class EvidenceTests(unittest.TestCase):
    def check(self, records):
        return POLICY.check_analyses(
            records,
            {"semgrep-security": "semgrep-scan"},
            SHA,
            REF,
            WORKFLOW,
            POLICY.timestamp(START),
        )

    def test_clean_current_analysis_is_valid(self):
        self.assertEqual(self.check([analysis()]), [])

    def test_wrong_commit_ref_workflow_and_stale_results_are_not_evidence(self):
        for changes in (
            {"ref": "refs/heads/main"},
            {"analysis_key": ".github/workflows/semgrep.yml:semgrep-scan"},
            {"created_at": "2025-12-31T23:59:59Z"},
            {"category": "unrelated"},
        ):
            with self.subTest(changes=changes):
                self.assertEqual(
                    self.check([analysis(**changes)]), ["semgrep-security"]
                )

    def test_superseding_commit_cannot_supply_release_alert_state(self):
        with self.assertRaisesRegex(ValueError, "another commit"):
            self.check(
                [
                    analysis(),
                    analysis(
                        commit_sha="b" * 40, created_at="2026-01-01T01:00:00Z"
                    ),
                ]
            )

    def test_scan_errors_and_missing_counts_fail(self):
        for changes in (
            {"error": "scan failed"},
            {"error": None},
            {"results_count": None},
            {"results_count": -1},
        ):
            with self.subTest(changes=changes), self.assertRaises(ValueError):
                self.check([analysis(**changes)])

    def test_latest_analysis_controls_rerun_result(self):
        failed = analysis(error="failed")
        passed = analysis(created_at="2026-01-01T01:00:00Z")
        self.assertEqual(self.check([failed, passed]), [])
        with self.assertRaises(ValueError):
            self.check(
                [
                    passed,
                    analysis(created_at="2026-01-01T02:00:00Z", error="failed"),
                ]
            )

    def test_each_category_is_required(self):
        self.assertEqual(self.check([]), ["semgrep-security"])
        with self.assertRaises(ValueError):
            POLICY.check_analyses(
                [], {}, SHA, REF, WORKFLOW, POLICY.timestamp(START)
            )

    def test_alert_severity_and_unscored_fallback(self):
        for security, level, expected in (
            ("critical", "note", True),
            ("high", "error", True),
            ("medium", "warning", True),
            ("low", "error", False),
            (None, "error", True),
            (None, "warning", True),
            (None, "note", False),
            (None, "none", False),
        ):
            with self.subTest(security=security, level=level):
                self.assertEqual(
                    POLICY.blocking_alert(
                        {
                            "state": "open",
                            "rule": {
                                "security_severity_level": security,
                                "severity": level,
                            },
                        }
                    ),
                    expected,
                )
        for rule in ({}, {"security_severity_level": "unknown"}):
            with self.assertRaises(ValueError):
                POLICY.blocking_alert({"state": "open", "rule": rule})

    def test_pagination_and_malformed_api_response(self):
        responses = iter([[analysis()] * 100, [analysis()]])
        self.assertEqual(
            sum(
                len(page)
                for page in POLICY.pages(
                    "endpoint", {}, lambda _: next(responses)
                )
            ),
            101,
        )
        with self.assertRaises(ValueError):
            list(
                POLICY.pages("endpoint", {}, lambda _: {"message": "forbidden"})
            )

    def test_recent_analysis_pagination_stops_before_old_runs(self):
        calls = []

        def fetch(endpoint):
            calls.append(endpoint)
            return [analysis(created_at="2025-12-31T00:00:00Z")] * 100

        self.assertEqual(
            POLICY.recent_analyses(
                "endpoint", REF, POLICY.timestamp(START), fetch
            ),
            [],
        )
        self.assertEqual(len(calls), 1)

    def test_end_to_end_polling_and_ref_scoped_alerts(self):
        expected = json.loads(
            (ROOT / ".github/release-scan-policy.json").read_text(
                encoding="utf-8"
            )
        )
        complete = [
            analysis(category=category, analysis_key=f"{WORKFLOW}:{job}")
            for category, job in expected.items()
        ]
        calls, sleeps = [], []
        analyses = iter([[], complete])

        def fetch(endpoint):
            calls.append(endpoint)
            if "/actions/runs/" in endpoint:
                return {"head_sha": SHA, "created_at": START, "path": WORKFLOW}
            if "/analyses?" in endpoint:
                return next(analyses)
            self.assertIn("state=open", endpoint)
            self.assertIn("ref=refs%2Ftags%2Fv1.0.0", endpoint)
            return []

        with (
            tempfile.TemporaryDirectory() as directory,
            patch.dict(
                os.environ,
                {
                    "GITHUB_REPOSITORY": "owner/repo",
                    "GITHUB_SHA": SHA,
                    "GITHUB_REF": REF,
                    "GITHUB_RUN_ID": "1",
                    "GITHUB_STEP_SUMMARY": str(Path(directory) / "summary"),
                },
            ),
        ):
            POLICY.evaluate(fetch, sleeps.append, lambda: 0)
            self.assertEqual(sleeps, [15])
            self.assertIn(
                "Open blocking alerts: **0**",
                Path(os.environ["GITHUB_STEP_SUMMARY"]).read_text(
                    encoding="utf-8"
                ),
            )
            ticks = iter([0, 301])
            with self.assertRaisesRegex(
                ValueError, "No fresh successful analysis"
            ):
                POLICY.evaluate(
                    lambda endpoint: (
                        {"head_sha": SHA, "created_at": START, "path": WORKFLOW}
                        if "/actions/runs/" in endpoint
                        else []
                    ),
                    sleeps.append,
                    lambda: next(ticks),
                )

    @staticmethod
    def _run_env(directory):
        return {
            "GITHUB_REPOSITORY": "owner/repo",
            "GITHUB_SHA": SHA,
            "GITHUB_REF": REF,
            "GITHUB_RUN_ID": "1",
            "GITHUB_STEP_SUMMARY": str(Path(directory) / "summary"),
        }

    def test_evaluate_raises_and_names_blocking_alerts_but_allows_low_severity(
        self,
    ):
        """B1: one scored-critical and one unscored-error alert must block;
        a low-severity control must not. Kills both `if blocking:` ->
        `if False:` and `blocking = []` mutants, which would swallow the
        blocking alerts and let evaluate() return normally.
        """
        expected = json.loads(
            (ROOT / ".github/release-scan-policy.json").read_text(
                encoding="utf-8"
            )
        )
        complete = [
            analysis(category=category, analysis_key=f"{WORKFLOW}:{job}")
            for category, job in expected.items()
        ]
        alerts = [
            alert(
                number=101,
                tool={"name": "CodeQL"},
                rule={"security_severity_level": "critical", "severity": None},
            ),
            alert(
                number=102,
                tool={"name": "Bandit"},
                rule={"security_severity_level": None, "severity": "error"},
            ),
            # Non-blocking control: open, low-scored severity.
            alert(
                number=103,
                tool={"name": "Semgrep"},
                rule={"security_severity_level": "low", "severity": "error"},
            ),
        ]

        def fetch(endpoint):
            if "/actions/runs/" in endpoint:
                return {"head_sha": SHA, "created_at": START, "path": WORKFLOW}
            if "/analyses?" in endpoint:
                return complete
            return alerts

        # Bounded ticks (not a constant monotonic): if a category ever fails
        # to match, this fails the polling deadline instead of looping
        # forever under a no-op sleep.
        ticks = iter([0, 301])
        with (
            tempfile.TemporaryDirectory() as directory,
            patch.dict(os.environ, self._run_env(directory)),
        ):
            with self.assertRaisesRegex(
                ValueError,
                r"2 blocking code-scanning alerts remain open: "
                r"alert #101 \(CodeQL\), alert #102 \(Bandit\)",
            ) as caught:
                POLICY.evaluate(
                    fetch, lambda seconds: None, lambda: next(ticks)
                )
            self.assertNotIn("#103", str(caught.exception))
            summary = Path(os.environ["GITHUB_STEP_SUMMARY"]).read_text(
                encoding="utf-8"
            )
            self.assertIn("Open blocking alerts: **2**", summary)

    def test_main_fails_for_every_evaluate_exception_class(self):
        """B2: main() must report and return nonzero for every exception
        class evaluate() can raise. Kills the `return 1` -> `return 0`
        mutant, which would report an error yet exit success.
        """
        cases = (
            ValueError("release scan policy violated"),
            subprocess.CalledProcessError(
                1, ["gh", "api", "x"], output="", stderr="not found"
            ),
            json.JSONDecodeError("Expecting value", "", 0),
            subprocess.TimeoutExpired(cmd="gh api x", timeout=60),
        )
        for exc in cases:
            with self.subTest(exc=type(exc).__name__):
                buffer = io.StringIO()
                with (
                    patch.object(POLICY, "evaluate", side_effect=exc),
                    redirect_stdout(buffer),
                ):
                    self.assertEqual(POLICY.main(), 1)
                self.assertIn(
                    "::error::Release scan evidence failed:", buffer.getvalue()
                )

    def test_main_returns_zero_on_success(self):
        with patch.object(POLICY, "evaluate", return_value=None):
            self.assertEqual(POLICY.main(), 0)

    def test_main_reports_status_without_native_private_diagnostics(self):
        token = "ghp_" + "a" * 40
        stderr = f"HTTP 403: {token} SYNTHETIC_PRIVATE_DIAGNOSTIC " + (
            "z" * 600
        )
        exc = subprocess.CalledProcessError(
            1, ["gh", "api", "x"], output="", stderr=stderr
        )
        buffer = io.StringIO()
        with (
            patch.object(POLICY, "evaluate", side_effect=exc),
            redirect_stdout(buffer),
        ):
            self.assertEqual(POLICY.main(), 1)
        output = buffer.getvalue()
        self.assertIn("HTTP 403", output)
        self.assertNotIn(token, output)
        self.assertNotIn("SYNTHETIC_PRIVATE_DIAGNOSTIC", output)
        self.assertNotIn("z" * 600, output)

    def test_main_escapes_untrusted_text_in_the_failure_annotation(self):
        """A tool name or gh stderr must stay inside the one ::error:: line.

        Unescaped, a newline in either starts a forged workflow command.
        """
        tool = {"name": "Grype\n::add-mask::deadbeef 100%"}
        with self.assertRaises(ValueError) as caught:
            POLICY.blocking_alert(alert(state="fixed", tool=tool))
        api_error = subprocess.CalledProcessError(
            1, ["gh", "api", "x"], output="", stderr="403\r\n::warning::x"
        )
        for exc, escaped in (
            (caught.exception, "policy or context validation failed"),
            (api_error, "API request failed"),
        ):
            with self.subTest(exc=type(exc).__name__):
                buffer = io.StringIO()
                with (
                    patch.object(POLICY, "evaluate", side_effect=exc),
                    redirect_stdout(buffer),
                ):
                    self.assertEqual(POLICY.main(), 1)
                lines = buffer.getvalue().splitlines()
                self.assertEqual(len(lines), 1, lines)
                self.assertTrue(
                    lines[0].startswith(
                        "::error::Release scan evidence failed:"
                    )
                )
                self.assertIn(escaped, lines[0])

    def test_evaluate_rejects_a_run_for_a_different_commit(self):
        """R1: head_sha != GITHUB_SHA must be rejected."""

        def fetch(endpoint):
            if "/actions/runs/" in endpoint:
                return {
                    "head_sha": "b" * 40,
                    "created_at": START,
                    "path": WORKFLOW,
                }
            return []

        # Bounded ticks: evaluate() must raise before ever touching the
        # polling loop, but a non-advancing monotonic would hang forever
        # under a no-op sleep if that check ever regressed.
        ticks = iter([0, 301])
        with (
            tempfile.TemporaryDirectory() as directory,
            patch.dict(os.environ, self._run_env(directory)),
        ):
            with self.assertRaisesRegex(
                ValueError, "does not match the release commit"
            ):
                POLICY.evaluate(
                    fetch, lambda seconds: None, lambda: next(ticks)
                )

    def test_blocking_alert_rejects_a_non_open_state(self):
        """R1: blocking_alert must reject any state other than 'open'."""
        with self.assertRaisesRegex(ValueError, "unexpected alert state"):
            POLICY.blocking_alert(alert(state="dismissed"))

    def test_timestamp_rejects_a_naive_value(self):
        """R1: API timestamps without a UTC offset must be rejected rather
        than silently compared as naive datetimes.
        """
        with self.assertRaisesRegex(ValueError, "no timezone"):
            POLICY.timestamp("2026-01-01T00:00:00")

    def test_analysis_from_a_first_attempt_survives_a_later_rerun(self):
        """R1: `started` must come from the run's created_at (the original
        run, stable across reruns), not run_started_at (the current
        attempt). A first-attempt analysis created well before a later
        rerun's run_started_at must still be accepted. If evaluate() used
        run_started_at instead, this analysis would look stale and the
        category would come back missing, which times out immediately below
        (monotonic ticks straight past the deadline on the first check).
        """
        expected = json.loads(
            (ROOT / ".github/release-scan-policy.json").read_text(
                encoding="utf-8"
            )
        )
        original_created = "2026-01-01T00:00:00Z"
        analysis_created = "2026-01-01T00:30:00Z"
        rerun_started = "2026-01-01T05:00:00Z"
        complete = [
            analysis(
                category=category,
                analysis_key=f"{WORKFLOW}:{job}",
                created_at=analysis_created,
            )
            for category, job in expected.items()
        ]

        def fetch(endpoint):
            if "/actions/runs/" in endpoint:
                return {
                    "head_sha": SHA,
                    "created_at": original_created,
                    "run_started_at": rerun_started,
                    "path": WORKFLOW,
                }
            if "/analyses?" in endpoint:
                return complete
            return []

        ticks = iter([0, 301])
        with (
            tempfile.TemporaryDirectory() as directory,
            patch.dict(os.environ, self._run_env(directory)),
        ):
            # Must not raise: the analysis is accepted on the first pass.
            POLICY.evaluate(fetch, lambda seconds: None, lambda: next(ticks))

    def test_blocking_alert_messages_name_the_alert_and_tool(self):
        """R5: an unrecognised/null severity must name the offending alert
        number and tool, not just the category of failure.
        """
        with self.assertRaisesRegex(ValueError, r"alert #77 \(Bandit\)"):
            POLICY.blocking_alert(
                alert(
                    number=77,
                    tool={"name": "Bandit"},
                    rule={"security_severity_level": "unscored"},
                )
            )
        with self.assertRaisesRegex(ValueError, r"alert #88 \(Semgrep\)"):
            POLICY.blocking_alert(
                alert(
                    number=88,
                    tool={"name": "Semgrep"},
                    rule={"security_severity_level": None, "severity": "huge"},
                )
            )

    def test_policy_categories_match_every_sarif_upload_reachable_from_gate(
        self,
    ):
        """R3: every release-scan-policy.json category must correspond to an
        actual upload-sarif/analyze category reachable from
        release-gate.yml, and vice versa. Discovery walks each gate job's
        local reusable workflow; remote uploaders (a third-party action, or a
        remote reusable workflow such as OSV's) resolve only through the
        explicit allowlists above, and anything else it cannot name fails
        as a placeholder. Uploads deliberately left out of the policy must
        be listed in _UPLOADED_BUT_NOT_IN_POLICY. A category dropped from
        either side fails this test.
        """
        discovered = _release_gate_sarif_categories()
        policy = json.loads(
            (ROOT / ".github/release-scan-policy.json").read_text(
                encoding="utf-8"
            )
        )
        self.assertTrue(discovered, "SARIF category discovery found nothing")
        self.assertFalse(set(policy) & set(_UPLOADED_BUT_NOT_IN_POLICY))
        self.assertEqual(
            discovered,
            set(policy) | set(_UPLOADED_BUT_NOT_IN_POLICY),
            "release-scan-policy.json categories (plus the documented "
            "exclusions) must exactly match the SARIF categories reachable "
            "from release-gate.yml",
        )

    def test_sarif_discovery_never_skips_an_unnamed_upload(self):
        """R3: an upload with no `category:` and an unlisted remote
        reusable workflow must surface as placeholders, not disappear.
        """
        text = "\n".join(
            (
                "jobs:",
                "  a:",
                "    steps:",
                "      # upload-sarif is mentioned only in this comment",
                "      - uses: github/codeql-action/upload-sarif@abc",
                "        with:",
                "          sarif_file: x.sarif",
                "      - name: next",
                "        uses: github/codeql-action/upload-sarif@abc",
                "        with:",
                "          category: named",
                "  b:",
                "    uses: other/repo/.github/workflows/scan.yml@abc",
                "  c:",
                '    uses: "google/osv-scanner-action/.github/workflows/'
                'osv-scanner-reusable.yml@abc" # v2',
            )
        )
        self.assertEqual(
            _sarif_categories_in(text, "w.yml"),
            [
                "<uncategorized upload in w.yml line 5>",
                "named",
                "<unrecognised remote reusable workflow "
                "other/repo/.github/workflows/scan.yml in w.yml>",
                "{workflow}:osv-scan",
            ],
        )

    @staticmethod
    def _reviewed_alert_and_entry():
        finding = alert(
            rule={
                "id": "fixture-rule",
                "security_severity_level": "medium",
                "severity": "warning",
            },
            most_recent_instance={
                "category": "fixture-category",
                "location": {"path": "fixture.py"},
            },
        )
        entry = {
            "alert_number": 1,
            "rule_id": "fixture-rule",
            "tool": "CodeQL",
            "category": "fixture-category",
            "path": "fixture.py",
            "severity": "medium",
            "expires_on": (
                datetime.now(timezone.utc).date() + timedelta(days=7)
            ).isoformat(),
            "reason": "Synthetic reviewed fixture",
            "tracking_url": "https://github.com/owner/repo/issues/1",
        }
        return finding, entry

    def _evaluate_reviewed(
        self, alerts, entries, *, fresh=True, repository="owner/repo", ref=REF
    ):
        expected = json.loads(
            (ROOT / ".github/release-scan-policy.json").read_text()
        )
        complete = [
            analysis(
                category=category, analysis_key=f"{WORKFLOW}:{job}", ref=ref
            )
            for category, job in expected.items()
        ]
        self.reviewed_api_calls = []

        def fetch(endpoint):
            self.reviewed_api_calls.append(endpoint)
            if "/actions/runs/" in endpoint:
                return {"head_sha": SHA, "created_at": START, "path": WORKFLOW}
            if "/analyses?" in endpoint:
                return complete if fresh else []
            self.assertIn(urlencode({"ref": ref}), endpoint)
            return alerts

        with (
            tempfile.TemporaryDirectory() as directory,
            patch.dict(
                os.environ,
                {
                    **self._run_env(directory),
                    "GITHUB_REF": ref,
                    "GITHUB_OUTPUT": str(Path(directory) / "output"),
                },
            ),
        ):
            path = Path(directory) / "exceptions.json"
            path.write_text(
                json.dumps(
                    {
                        "version": 1,
                        "repository": repository,
                        "exceptions": entries,
                    }
                )
            )
            ticks = iter([0, 301])
            POLICY.evaluate(
                fetch,
                lambda _: self.fail("unexpected polling"),
                lambda: next(ticks),
                exception_policy=path,
            )
            self.reviewed_output = Path(os.environ["GITHUB_OUTPUT"]).read_text()
            return Path(os.environ["GITHUB_STEP_SUMMARY"]).read_text()

    def test_reviewed_exception_remains_open_after_fresh_evidence(self):
        finding, entry = self._reviewed_alert_and_entry()
        before = json.loads(json.dumps(finding))
        summary = self._evaluate_reviewed([finding], [entry])
        self.assertIn("Open blocking alerts: **0**", summary)
        self.assertIn("Reviewed, unexpired exceptions: **1**", summary)
        self.assertEqual(finding, before)
        self.assertEqual(finding["state"], "open")
        self.assertEqual(
            self.reviewed_output, f"release_deadline={entry['expires_on']}\n"
        )

    def test_later_alert_query_cannot_discard_an_applied_deadline(self):
        finding, entry = self._reviewed_alert_and_entry()
        checker = POLICY._exception_checker()
        deadline_spec = importlib.util.spec_from_file_location(
            "deadline", ROOT / ".github/scripts/check_release_deadline.py"
        )
        deadline = importlib.util.module_from_spec(deadline_spec)
        deadline_spec.loader.exec_module(deadline)
        for case, ref, later_alerts in (
            ("same alert", REF, [finding]),
            ("different ref", REF, []),
            ("resolved between checks", "refs/heads/main", []),
        ):
            with self.subTest(case=case):
                self._evaluate_reviewed([finding], [entry], ref=ref)
                carried = self.reviewed_output.strip().split("=", 1)[1]
                responses = iter([later_alerts, []] if later_alerts else [[]])

                def run(args, **kwargs):
                    self.assertEqual(args[:4], ["gh", "api", "--method", "GET"])
                    self.assertNotIn("ref=", args[-1])
                    return subprocess.CompletedProcess(
                        args, 0, stdout=json.dumps(next(responses))
                    )

                with tempfile.TemporaryDirectory() as directory:
                    path = Path(directory) / "exceptions.json"
                    path.write_text(
                        json.dumps(
                            {
                                "version": 1,
                                "repository": "owner/repo",
                                "exceptions": [entry],
                            }
                        )
                    )
                    output = Path(directory) / "output"
                    with (
                        patch.dict(
                            os.environ,
                            {
                                **self._run_env(directory),
                                "GITHUB_OUTPUT": str(output),
                            },
                        ),
                        patch.object(
                            checker.subprocess, "run", side_effect=run
                        ),
                        patch.object(
                            sys,
                            "argv",
                            [
                                "checker",
                                "--repository",
                                "owner/repo",
                                "--exceptions",
                                str(path),
                                "--evidence-deadline",
                                carried,
                            ],
                        ),
                    ):
                        self.assertEqual(checker.main(), 0)
                    outputs = dict(
                        line.split("=", 1)
                        for line in output.read_text().splitlines()
                    )
                    self.assertEqual(
                        outputs["release_deadline"], entry["expires_on"]
                    )
                    self.assertEqual(
                        outputs["excepted_count"], str(len(later_alerts))
                    )
                    with self.assertRaisesRegex(ValueError, "expired"):
                        deadline.check_deadline(
                            outputs["release_deadline"],
                            datetime.fromisoformat(entry["expires_on"]).replace(
                                tzinfo=timezone.utc
                            ),
                        )

    def test_fresh_evidence_without_applied_exceptions_emits_explicit_none(
        self,
    ):
        self._evaluate_reviewed([], [])
        self.assertEqual(self.reviewed_output, "release_deadline=none\n")

    def test_empty_policy_keeps_scored_and_unscored_alerts_blocking(self):
        finding, _ = self._reviewed_alert_and_entry()
        native = {
            **finding,
            "number": 2,
            "rule": {
                "id": "native-rule",
                "security_severity_level": None,
                "severity": "warning",
            },
        }
        for current in (finding, native):
            with (
                self.subTest(rule=current["rule"]),
                self.assertRaisesRegex(
                    ValueError, "blocking code-scanning alerts"
                ),
            ):
                self._evaluate_reviewed([current], [])

    def test_expired_reviewed_exception_blocks_again(self):
        finding, entry = self._reviewed_alert_and_entry()
        entry["expires_on"] = "2000-01-01"
        with self.assertRaisesRegex(
            ValueError, "blocking code-scanning alerts"
        ):
            self._evaluate_reviewed([finding], [entry])

    def test_reviewed_exception_requires_every_identity_field(self):
        finding, entry = self._reviewed_alert_and_entry()
        for field, replacement in (
            ("alert_number", 2),
            ("rule_id", "other-rule"),
            ("tool", "OtherTool"),
            ("category", "other-category"),
            ("path", "other.py"),
            ("severity", "high"),
        ):
            with (
                self.subTest(field=field),
                self.assertRaisesRegex(
                    ValueError, "blocking code-scanning alerts"
                ),
            ):
                self._evaluate_reviewed(
                    [finding], [{**entry, field: replacement}]
                )

    def test_reviewed_exception_does_not_cover_unrelated_or_native_alerts(self):
        finding, entry = self._reviewed_alert_and_entry()
        for other in (
            {**finding, "number": 2},
            {
                **finding,
                "number": 2,
                "rule": {
                    "id": "native-rule",
                    "security_severity_level": None,
                    "severity": "error",
                },
            },
        ):
            with (
                self.subTest(rule=other["rule"]),
                self.assertRaisesRegex(
                    ValueError, "1 blocking code-scanning alert"
                ),
            ):
                self._evaluate_reviewed([finding, other], [entry])

    def test_reviewed_exception_cannot_replace_fresh_analyses(self):
        finding, entry = self._reviewed_alert_and_entry()
        with self.assertRaisesRegex(ValueError, "No fresh successful analysis"):
            self._evaluate_reviewed([finding], [entry], fresh=False)
        self.assertFalse(
            any("/alerts?" in endpoint for endpoint in self.reviewed_api_calls)
        )

    def test_reviewed_policy_repository_and_schema_fail_before_api_calls(self):
        finding, entry = self._reviewed_alert_and_entry()
        with self.assertRaisesRegex(ValueError, "repository mismatch"):
            self._evaluate_reviewed([finding], [entry], repository="other/repo")
        self.assertEqual(self.reviewed_api_calls, [])
        with self.assertRaisesRegex(ValueError, "missing or unknown fields"):
            self._evaluate_reviewed([finding], [{**entry, "unexpected": True}])
        self.assertEqual(self.reviewed_api_calls, [])


def _step_run_script(workflow_text, step_id):
    """The `run: |` block of the step with `id: <step_id>`, dedented.

    Plain-text extraction (no PyYAML, see the R3 note above): find the step,
    then take every following line indented deeper than its `run:` key.
    """
    lines = workflow_text.splitlines()
    step_start = next(
        i
        for i, line in enumerate(lines)
        if re.match(rf"^\s+id:\s*{re.escape(step_id)}\s*$", line)
    )
    run_index = next(
        i
        for i in range(step_start + 1, len(lines))
        if re.match(r"^\s+run:\s*\|\s*$", lines[i])
    )
    key_indent = len(lines[run_index]) - len(lines[run_index].lstrip())
    body = []
    for line in lines[run_index + 1 :]:
        if line.strip() and len(line) - len(line.lstrip()) <= key_indent:
            break
        body.append(line)
    indent = min(len(ln) - len(ln.lstrip()) for ln in body if ln.strip())
    return "\n".join(ln[indent:] for ln in body).rstrip() + "\n"


_FAKE_NPX = """#!/bin/sh
printf '%s\\n' "$@" > "$FAKE_ARGS"
out=""
while [ "$#" -gt 0 ]; do
  if [ "$1" = "--outputpath" ]; then out="$2"; fi
  shift
done
echo "fake retire.js output"
echo "::error::from a scanned file"
if [ -n "$FAKE_REPORT" ]; then
  printf '%s' "$FAKE_REPORT" | sed "s|@ROOT@|$PWD|g" > "$out"
fi
exit "$FAKE_EXIT"
"""

_RETIRE_CONVERTER = ".github/scripts/retire_json_to_sarif.py"

# retire.js 5.2.4 JSON report shapes (see test_release_scan_retire_sarif.py
# for a real report). @ROOT@ is replaced with the step's working directory,
# as retire.js reports absolute paths.
_EMPTY_REPORT = json.dumps(
    {"version": "5.2.4", "data": [], "messages": [], "errors": []}
)


def _retire_report(*severities):
    return json.dumps(
        {
            "version": "5.2.4",
            "data": [
                {
                    "file": "@ROOT@/node_modules/jquery/dist/jquery.js",
                    "results": [
                        {
                            "version": "3.4.1",
                            "component": "jquery",
                            "vulnerabilities": [
                                {
                                    "severity": severity,
                                    "identifiers": {
                                        "summary": "an advisory",
                                        "githubID": f"GHSA-{index}",
                                    },
                                }
                                for index, severity in enumerate(severities)
                            ],
                        }
                    ],
                }
            ],
            "messages": [],
            "errors": [],
        }
    )


_FINDINGS_REPORT = _retire_report("medium", "high")
_LOW_ONLY_REPORT = _retire_report("low")

# Every option retire.js 5.2.4 accepts (lib/cli.js, the commander
# definitions), and the values its --outputformat and --severity accept.
# 5.x rejects --node/-n outright and has no --js option or sarif format.
# Re-verify these against the new cli.js before bumping the pinned version.
RETIRE_5_2_4_OPTIONS = {
    "-V": False,
    "--version": False,
    "-h": False,
    "--help": False,
    "-v": False,
    "--verbose": False,
    "-c": False,
    "--nocache": False,
    "--jspath": True,
    "--path": True,
    "--jsrepo": True,
    "--cachedir": True,
    "--proxy": True,
    "--outputformat": True,
    "--outputpath": True,
    "--ignore": True,
    "--ignorefile": True,
    "--severity": True,
    "--exitwith": True,
    "--colors": False,
    "--insecure": False,
    "--ext": True,
    "--cacert": True,
    "--includeOsv": False,
    "--deep": False,
}
RETIRE_5_2_4_OUTPUT_FORMATS = {
    "text",
    "json",
    "jsonsimple",
    "depcheck",
    "cyclonedx",
    "cyclonedxJSON",
    "cyclonedxJSON1_6",
}
RETIRE_5_2_4_SEVERITIES = {"none", "low", "medium", "high", "critical"}


@unittest.skipUnless(shutil.which("bash"), "needs bash")
class RetireScanStepTests(unittest.TestCase):
    """R4: retirejs.yml's scan step must report retire.js's own exit status.

    The step script is run exactly as GitHub Actions runs a step with no
    `shell:` key (`bash -e`, no pipefail) and as `shell: bash` would
    (`bash -eo pipefail`), with a fake `npx` standing in for retire.js and
    the real JSON-to-SARIF converter. An empty "retirejs" SARIF upload is
    fresh evidence to the release gate and marks every open retirejs alert
    as fixed, so it may only follow a scan that completed cleanly (exit 0)
    with a well-formed report; findings (exit 13) are converted, uploaded
    and flag the job; any other status, a missing report or a malformed
    report fails the step with nothing to upload.
    """

    SHELLS = (
        ("bash", "-e"),
        ("bash", "--noprofile", "--norc", "-eo", "pipefail"),
    )

    @classmethod
    def setUpClass(cls):
        cls.script = _step_run_script(
            (ROOT / ".github/workflows/retirejs.yml").read_text(
                encoding="utf-8"
            ),
            "retire-scan",
        )

    def run_step(self, shell, exit_code, report=""):
        with tempfile.TemporaryDirectory() as directory:
            work = Path(directory)
            bin_dir = work / "bin"
            bin_dir.mkdir()
            npx = bin_dir / "npx"
            npx.write_text(_FAKE_NPX, encoding="utf-8")
            npx.chmod(0o755)
            converter = work / _RETIRE_CONVERTER
            converter.parent.mkdir(parents=True)
            shutil.copyfile(ROOT / _RETIRE_CONVERTER, converter)
            script = work / "step.sh"
            script.write_text(self.script, encoding="utf-8")
            env_file, output_file = work / "env", work / "output"
            args_file = work / "npx-args"
            env_file.touch()
            output_file.touch()
            proc = subprocess.run(
                [*shell, str(script)],
                cwd=work,
                env={
                    **os.environ,
                    "PATH": f"{bin_dir}{os.pathsep}{os.environ['PATH']}",
                    "GITHUB_ENV": str(env_file),
                    "GITHUB_OUTPUT": str(output_file),
                    "FAKE_EXIT": str(exit_code),
                    "FAKE_REPORT": report,
                    "FAKE_ARGS": str(args_file),
                },
                capture_output=True,
                text=True,
                timeout=60,
            )
            sarif = work / "retire-results.sarif"
            self.npx_args = args_file.read_text(encoding="utf-8").split("\n")
            self.step_log = proc.stdout + proc.stderr
            return (
                proc.returncode,
                env_file.read_text(encoding="utf-8"),
                output_file.read_text(encoding="utf-8"),
                json.loads(sarif.read_text(encoding="utf-8"))
                if sarif.exists()
                else None,
            )

    def test_clean_scan_uploads_an_empty_sarif(self):
        for shell in self.SHELLS:
            with self.subTest(shell=shell):
                code, env, output, sarif = self.run_step(
                    shell, 0, _EMPTY_REPORT
                )
                self.assertEqual(code, 0)
                self.assertNotIn("RETIRE_FOUND_ISSUES", env)
                self.assertIn("sarif_exists=true", output)
                self.assertEqual(sarif["version"], "2.1.0")
                run = sarif["runs"][0]
                self.assertEqual(run["tool"]["driver"]["name"], "retire.js")
                self.assertEqual(run["results"], [])

    def test_clean_scan_still_uploads_low_severity_findings(self):
        for shell in self.SHELLS:
            with self.subTest(shell=shell):
                code, env, output, sarif = self.run_step(
                    shell, 0, _LOW_ONLY_REPORT
                )
                self.assertEqual(code, 0)
                self.assertNotIn("RETIRE_FOUND_ISSUES", env)
                self.assertIn("sarif_exists=true", output)
                results = sarif["runs"][0]["results"]
                self.assertEqual([r["level"] for r in results], ["note"])

    def test_findings_are_converted_uploaded_and_flag_the_job(self):
        for shell in self.SHELLS:
            with self.subTest(shell=shell):
                code, env, output, sarif = self.run_step(
                    shell, 13, _FINDINGS_REPORT
                )
                self.assertEqual(code, 0)
                self.assertIn("RETIRE_FOUND_ISSUES=true", env)
                self.assertIn("sarif_exists=true", output)
                results = sarif["runs"][0]["results"]
                self.assertEqual(
                    sorted(r["level"] for r in results), ["error", "warning"]
                )
                self.assertEqual(
                    {
                        r["locations"][0]["physicalLocation"][
                            "artifactLocation"
                        ]["uri"]
                        for r in results
                    },
                    {"node_modules/jquery/dist/jquery.js"},
                )

    def test_missing_empty_or_malformed_reports_fail_the_step(self):
        cases = (
            (0, ""),
            (0, "{not json"),
            (0, '{"version": "5.2.4", "data": {}}'),
            (13, ""),
            (13, "{not json"),
            # Exit 13 promises findings; an empty report contradicts it.
            (13, _EMPTY_REPORT),
        )
        for shell in self.SHELLS:
            for exit_code, report in cases:
                with self.subTest(
                    shell=shell, exit_code=exit_code, report=report
                ):
                    code, _, output, sarif = self.run_step(
                        shell, exit_code, report
                    )
                    self.assertNotEqual(code, 0)
                    self.assertNotIn("sarif_exists", output)
                    self.assertIsNone(sarif)
                    if not report:
                        # The case statement, not the converter, rejects a
                        # scan that left no report behind.
                        self.assertIn("wrote no JSON report", self.step_log)

    def test_scanner_failure_fails_the_step_and_uploads_nothing(self):
        for shell in self.SHELLS:
            for exit_code, report in (
                (1, ""),
                (9, ""),
                (1, _EMPTY_REPORT),
                (1, _FINDINGS_REPORT),
            ):
                with self.subTest(
                    shell=shell, exit_code=exit_code, report=report
                ):
                    code, env, output, sarif = self.run_step(
                        shell, exit_code, report
                    )
                    self.assertNotEqual(code, 0)
                    self.assertNotIn("sarif_exists", output)
                    self.assertNotIn("RETIRE_FOUND_ISSUES", env)
                    # Nothing may be converted for a failed scan.
                    self.assertIsNone(sarif)

    def test_console_output_is_shown_with_workflow_commands_stopped(self):
        """retire.js echoes scanned file text; it must not run as commands."""
        for exit_code, report in ((0, _EMPTY_REPORT), (1, "")):
            with self.subTest(exit_code=exit_code):
                self.run_step(self.SHELLS[0], exit_code, report)
                lines = self.step_log.splitlines()
                stops = [
                    i
                    for i, line in enumerate(lines)
                    if line.startswith("::stop-commands::")
                ]
                self.assertEqual(len(stops), 1)
                token = lines[stops[0]].removeprefix("::stop-commands::")
                self.assertRegex(token, r"^[0-9a-f]{32}$")
                resume = lines.index(f"::{token}::")
                output = (
                    lines.index("fake retire.js output"),
                    lines.index("::error::from a scanned file"),
                )
                for index in output:
                    self.assertLess(stops[0], index)
                    self.assertLess(index, resume)
                # The token appears only in the stop and resume lines.
                self.assertEqual(self.step_log.count(token), 2)
        tokens = set()
        for _ in range(2):
            self.run_step(self.SHELLS[0], 0, _EMPTY_REPORT)
            tokens.add(
                re.search(r"^::stop-commands::(\S+)$", self.step_log, re.M)[1]
            )
        self.assertEqual(len(tokens), 2)
        # The wrapped cat is the step's only display of retire's output.
        self.assertEqual(len(re.findall(r"\bcat\b", self.script)), 1)
        self.assertIn(
            'echo "::stop-commands::${STOP_TOKEN}"\n'
            "cat retire-output.txt\n"
            'echo "::${STOP_TOKEN}::"\n',
            self.script,
        )

    def test_invocation_uses_only_options_retire_5_2_4_accepts(self):
        self.run_step(self.SHELLS[0], 0, _EMPTY_REPORT)
        args = [a for a in self.npx_args if a]
        self.assertEqual(args[0], "[email protected]")
        options = {}
        index = 1
        while index < len(args):
            option = args[index]
            self.assertIn(option, RETIRE_5_2_4_OPTIONS)
            if RETIRE_5_2_4_OPTIONS[option]:
                index += 1
                options[option] = args[index]
            else:
                options[option] = True
            index += 1
        self.assertIn(options["--outputformat"], RETIRE_5_2_4_OUTPUT_FORMATS)
        self.assertEqual(options["--outputformat"], "json")
        self.assertEqual(options["--outputpath"], "retire-results.json")
        self.assertIn(options["--severity"], RETIRE_5_2_4_SEVERITIES)
        self.assertEqual(options.get("--exitwith", "13"), "13")
        # Every retire invocation in the workflow is the one exercised here.
        workflow = (ROOT / ".github/workflows/retirejs.yml").read_text(
            encoding="utf-8"
        )
        self.assertEqual(len(re.findall(r"\bnpx\s+retire", workflow)), 1)


# The native invocation and exit/report contracts are exercised by
# test_osv_scan.py. Both event wrappers must use that guarded implementation.
OSV_SCAN_WORKFLOWS = (
    ".github/workflows/osv-scanner.yml",
    ".github/workflows/osv-scanner-scheduled.yml",
)


class OsvScanArgsTests(unittest.TestCase):
    def test_workflow_walk_does_not_treat_local_actions_as_workflows(self):
        text = (
            "jobs:\n"
            "  scan:\n"
            "    uses: ./.github/workflows/osv-scan-reusable.yml\n"
            "  other:\n"
            "    steps:\n"
            "      - name: Install\n"
            "        uses: ./.github/actions/install-gitleaks\n"
        )
        self.assertEqual(
            _local_reusable_workflow_jobs(text),
            {"scan": ".github/workflows/osv-scan-reusable.yml"},
        )

    def test_both_callers_use_the_complete_scan_workflow(self):
        for rel_path in OSV_SCAN_WORKFLOWS:
            with self.subTest(workflow=rel_path):
                text = (ROOT / rel_path).read_text(encoding="utf-8")
                self.assertEqual(
                    list(_local_reusable_workflow_jobs(text).values()),
                    [".github/workflows/osv-scan-reusable.yml"],
                )
                self.assertNotIn("scan-args:", text)
                self.assertIn("ref: ${{ github.sha }}", text)

    def test_default_category_is_limited_to_the_known_osv_job(self):
        text = (
            "jobs:\n"
            "  osv-scan:\n"
            "    steps:\n"
            "      - uses: github/codeql-action/upload-sarif@abc\n"
            "        with:\n"
            "          sarif_file: results.sarif\n"
        )
        source = ".github/workflows/osv-scan-reusable.yml"
        self.assertEqual(
            _sarif_categories_in(text, source), ["{workflow}:osv-scan"]
        )
        self.assertIn(
            "<uncategorized", _sarif_categories_in(text, "other.yml")[0]
        )
        self.assertIn(
            "<uncategorized",
            _sarif_categories_in(text.replace("osv-scan:", "other:"), source)[
                0
            ],
        )


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