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.

20.5 KB
Raw Blame
"""Exercise release decisions while preserving open security alerts."""

# allow: no-sut-import — the SUT is the standalone release gate script.

from copy import deepcopy
from datetime import date, datetime, timezone
import importlib.util
import json
from pathlib import Path
import subprocess
import sys
from urllib.parse import parse_qs, urlsplit

import pytest
import yaml


ROOT = Path(__file__).resolve().parents[2]
REPOSITORY = "LearningCircuit/local-deep-research"
SCRIPT = ROOT / ".github/scripts/check_code_scanning_alerts.py"
spec = importlib.util.spec_from_file_location("release_alert_gate", SCRIPT)
gate = importlib.util.module_from_spec(spec)
spec.loader.exec_module(gate)
TODAY = date(2026, 9, 27)


@pytest.fixture
def alert():
    return {
        "number": 8273,
        "state": "open",
        "rule": {
            "id": "CVE-2026-87910-python",
            "security_severity_level": "medium",
        },
        "tool": {"name": "Grype"},
        "most_recent_instance": {
            "category": "grype-container",
            "location": {
                "path": "local-deep-research//usr/local/bin/python3.14",
            },
        },
    }


@pytest.fixture
def policy():
    return {
        "version": 1,
        "repository": REPOSITORY,
        "exceptions": [
            {
                "alert_number": 8273,
                "rule_id": "CVE-2026-87910-python",
                "tool": "Grype",
                "category": "grype-container",
                "path": "local-deep-research//usr/local/bin/python3.14",
                "severity": "medium",
                "expires_on": "2026-10-13",
                "reason": "Wait for a confirmed fixed upstream image.",
                "tracking_url": "https://github.com/LearningCircuit/local-deep-research/pull/6628",
            }
        ],
    }


@pytest.fixture
def exceptions(tmp_path, policy):
    path = tmp_path / "exceptions.json"
    path.write_text(json.dumps(policy))
    return gate.load_exceptions(path, REPOSITORY)


def test_checked_in_policy_is_valid():
    # Keep testing the mechanism after the temporary entry is removed.
    gate.load_exceptions(gate.POLICY_PATH, REPOSITORY)


def test_matching_exception_does_not_mutate_alert(alert, exceptions):
    original = deepcopy(alert)
    blocking, excepted, expired = gate.classify_alerts(
        [alert], exceptions, TODAY
    )

    assert blocking == []
    assert excepted == [alert]
    assert expired == []
    assert alert == original
    assert alert["state"] == "open"


@pytest.mark.parametrize(
    "today,expected_blocking",
    [
        (date(2026, 10, 12), False),
        (date(2026, 10, 13), True),
        (date(2026, 10, 14), True),
    ],
)
def test_expiry_boundary(alert, exceptions, today, expected_blocking):
    blocking, excepted, expired = gate.classify_alerts(
        [alert], exceptions, today
    )
    assert bool(blocking) is expected_blocking
    assert bool(excepted) is not expected_blocking
    assert expired == ([8273] if expected_blocking else [])


@pytest.mark.parametrize(
    "keys,value",
    [
        (("number",), 8274),
        (("rule", "id"), "CVE-2026-87910-other-package"),
        (("rule", "security_severity_level"), "high"),
        (("rule", "security_severity_level"), "critical"),
        (("tool", "name"), "CodeQL"),
        (("most_recent_instance", "category"), "grype-filesystem"),
        (("most_recent_instance", "location", "path"), "/usr/bin/python3.14"),
        (("most_recent_instance", "location", "path"), None),
        (("most_recent_instance", "location"), None),
        (("most_recent_instance", "location"), "invalid"),
        (("most_recent_instance",), None),
        (("most_recent_instance",), "invalid"),
        (("tool",), None),
        (("tool",), "invalid"),
    ],
)
def test_scope_drift_and_severity_increase_block(
    alert, exceptions, keys, value
):
    target = alert
    for key in keys[:-1]:
        target = target[key]
    target[keys[-1]] = value
    blocking, excepted, _ = gate.classify_alerts([alert], exceptions, TODAY)
    assert blocking == [alert]
    assert excepted == []


@pytest.mark.parametrize("severity", ["critical", "high", "medium"])
def test_unrelated_alerts_still_block(alert, exceptions, severity):
    other = deepcopy(alert)
    other["number"] = 8185
    other["rule"] = {
        "id": "py/stack-trace-exposure",
        "security_severity_level": severity,
    }
    other["tool"] = {"name": "CodeQL"}
    blocking, excepted, _ = gate.classify_alerts(
        [alert, other], exceptions, TODAY
    )
    assert blocking == [other]
    assert excepted == [alert]


@pytest.mark.parametrize("severity", ["low", None])
def test_existing_nonblocking_severities_are_preserved(alert, severity):
    alert["rule"]["security_severity_level"] = severity
    alert["rule"]["severity"] = "note"
    gate.validate_alerts([alert])
    assert gate.classify_alerts([alert], {}, TODAY) == ([], [], [])


def test_informational_alert_without_security_severity_is_nonblocking(
    monkeypatch, cli
):
    # Native informational results remain advisory without a numerical score.
    alert = {
        "number": 101,
        "state": "open",
        "rule": {"id": "SAMPLE_NOTE", "severity": "note"},
        "tool": {"name": "Sample scanner"},
    }
    original = deepcopy(alert)
    _mock_pages(monkeypatch, [[alert], []])
    assert gate.main() == 0
    assert "Fetched 1 open alert(s)" in cli[0].read_text()
    assert "No open critical" in cli[0].read_text()
    assert cli[1].read_text() == "excepted_count=0\nrelease_deadline=none\n"
    assert alert == original


def test_empty_policy_restores_blocking(alert):
    assert gate.classify_alerts([alert], {}, TODAY) == ([alert], [], [])


def test_expired_entry_for_closed_alert_does_not_block(exceptions):
    # Only open alerts are fetched; stale entries do not grant anything.
    assert gate.classify_alerts([], exceptions, date(2027, 1, 1)) == (
        [],
        [],
        [],
    )


@pytest.mark.parametrize(
    "field,value",
    [
        ("expires_on", None),
        ("expires_on", ""),
        ("expires_on", "2026-13-01"),
        ("expires_on", "20261013"),
        ("severity", "*"),
        ("reason", " "),
        ("alert_number", True),
        ("alert_number", 0),
        ("tracking_url", "javascript:alert(1)"),
    ],
)
def test_invalid_exception_is_rejected(tmp_path, policy, field, value):
    policy["exceptions"][0][field] = value
    path = tmp_path / "exceptions.json"
    path.write_text(json.dumps(policy))
    with pytest.raises(ValueError):
        gate.load_exceptions(path, REPOSITORY)


@pytest.mark.parametrize(
    "change", ["missing", "unknown", "duplicate", "repository", "version"]
)
def test_policy_mistakes_fail_closed(tmp_path, policy, change):
    if change == "missing":
        del policy["exceptions"][0]["expires_on"]
    elif change == "unknown":
        policy["exceptions"][0]["expiry"] = "2026-10-13"
    elif change == "duplicate":
        policy["exceptions"].append(deepcopy(policy["exceptions"][0]))
    elif change == "repository":
        policy["repository"] = "another/repository"
    else:
        policy["version"] = True
    path = tmp_path / "exceptions.json"
    path.write_text(json.dumps(policy))
    with pytest.raises(ValueError):
        gate.load_exceptions(path, REPOSITORY)


def _mock_pages(monkeypatch, pages):
    responses = iter(pages)
    calls = []

    def run(args, **kwargs):
        calls.append(args)
        assert args[:4] == ["gh", "api", "--method", "GET"]
        assert f"page={len(calls)}" in args[-1]
        assert kwargs["check"] is True
        assert kwargs["timeout"] == 60
        response = next(responses)
        if isinstance(response, Exception):
            raise response
        return subprocess.CompletedProcess(args, 0, stdout=json.dumps(response))

    monkeypatch.setattr(gate.subprocess, "run", run)
    return calls


def test_all_pages_are_fetched_before_deciding(monkeypatch, alert, exceptions):
    other = deepcopy(alert)
    other["number"] = 9008
    calls = _mock_pages(monkeypatch, [[alert], [other], []])
    alerts = gate.fetch_alerts(REPOSITORY)
    blocking, excepted, _ = gate.classify_alerts(alerts, exceptions, TODAY)
    assert len(calls) == 3
    assert blocking == [other]
    assert excepted == [alert]


@pytest.mark.parametrize("state", ["fixed", "dismissed"])
def test_history_accepts_both_terminal_states_on_every_page(
    monkeypatch, alert, state
):
    fixed = {**deepcopy(alert), "state": "fixed"}
    dismissed = {**deepcopy(alert), "number": 9008, "state": "dismissed"}
    # A page with only the other terminal state must not end pagination.
    pages = [dismissed, fixed] if state == "fixed" else [fixed, dismissed]
    original = deepcopy(pages)
    calls = _mock_pages(monkeypatch, [[pages[0]], [pages[1]], []])
    result = gate.fetch_alerts(
        REPOSITORY, state, ref="refs/heads/main", tool_name="Sample scanner"
    )
    assert result == pages == original
    assert len(calls) == 3
    for args in calls:
        query = parse_qs(urlsplit(args[-1]).query)
        assert query["state"] == [state]
        assert query["ref"] == ["refs/heads/main"]
        assert query["tool_name"] == ["Sample scanner"]


@pytest.mark.parametrize("state", ["fixed", "dismissed"])
@pytest.mark.parametrize("returned", ["open", "unknown", "closed", None])
def test_history_rejects_nonterminal_or_unrecognized_records(
    monkeypatch, alert, state, returned
):
    malformed = {**deepcopy(alert), "state": returned}
    _mock_pages(monkeypatch, [[malformed]])
    with pytest.raises(ValueError, match="Malformed"):
        gate.fetch_alerts(REPOSITORY, state)


@pytest.mark.parametrize("state", ["fixed", "dismissed"])
def test_open_query_still_rejects_every_terminal_state(
    monkeypatch, alert, state
):
    _mock_pages(monkeypatch, [[{**deepcopy(alert), "state": state}]])
    with pytest.raises(ValueError, match="Malformed"):
        gate.fetch_alerts(REPOSITORY)


@pytest.fixture
def cli(monkeypatch, tmp_path, policy):
    class FrozenDateTime(datetime):
        @classmethod
        def now(cls, tz=None):
            assert tz == timezone.utc
            return datetime(2026, 9, 27, tzinfo=tz)

    monkeypatch.setattr(gate, "datetime", FrozenDateTime)
    path = tmp_path / "exceptions.json"
    path.write_text(json.dumps(policy))
    monkeypatch.setattr(
        sys,
        "argv",
        [
            str(SCRIPT),
            "--repository",
            REPOSITORY,
            "--exceptions",
            str(path),
            "--evidence-deadline",
            "none",
        ],
    )
    summary = tmp_path / "summary.md"
    output = tmp_path / "output.txt"
    monkeypatch.setenv("GITHUB_STEP_SUMMARY", str(summary))
    monkeypatch.setenv("GITHUB_OUTPUT", str(output))
    return summary, output


@pytest.mark.parametrize("historical_state", ["fixed", "dismissed"])
@pytest.mark.parametrize("age_days,expected_exit", [(20, 1), (3, 0)])
def test_cli_retains_original_age_across_terminal_history_states(
    monkeypatch, cli, alert, historical_state, age_days, expected_exit
):
    current = {
        **deepcopy(alert),
        "number": 9008,
        "created_at": "2026-09-26T00:00:00Z",
    }
    old = {
        **deepcopy(current),
        "number": 9009,
        "state": historical_state,
        "created_at": f"2026-09-{27 - age_days:02}T00:00:00Z",
    }
    opposite_query = "fixed" if historical_state == "dismissed" else "dismissed"
    calls = []

    def run(args, **kwargs):
        assert args[:4] == ["gh", "api", "--method", "GET"]
        assert kwargs["check"] is True
        query = parse_qs(urlsplit(args[-1]).query)
        state = query["state"][0]
        calls.append((state, query["page"][0]))
        rows = []
        if query["page"] == ["1"]:
            if state == "open":
                rows = [current]
            elif state == opposite_query:
                rows = [old]
        return subprocess.CompletedProcess(args, 0, stdout=json.dumps(rows))

    monkeypatch.setattr(gate.subprocess, "run", run)
    assert gate.main() == expected_exit
    assert ("fixed", "1") in calls and ("dismissed", "1") in calls
    if expected_exit:
        assert not cli[1].exists()
        assert "Release blocked: 1 alert(s)." in cli[0].read_text()
    else:
        assert "release_deadline=2026-10-08T00:00:00Z" in cli[1].read_text()
    assert current["state"] == "open"
    assert old["state"] == historical_state


def test_cli_reports_exception_without_claiming_no_alerts(
    monkeypatch, cli, alert, capsys
):
    summary, output = cli
    _mock_pages(monkeypatch, [[alert], []])
    assert gate.main() == 0
    report = summary.read_text()
    assert "| Medium | 1 | 1 | 0 |" in report
    assert "These alerts remain open" in report
    assert "2026-10-13" in report
    assert "No open critical" not in report
    assert output.read_text() == (
        "excepted_count=1\nrelease_deadline=2026-10-13\n"
    )
    assert (
        "::warning::Release exception for open alert #8273"
        in capsys.readouterr().out
    )


@pytest.mark.parametrize(
    "batch", [{"message": "error"}, [None], [{"number": 3}]]
)
def test_bad_api_data_never_passes(monkeypatch, cli, batch):
    _mock_pages(monkeypatch, [batch])
    assert gate.main() == 1
    assert not cli[1].exists()


@pytest.mark.parametrize("severity", ["unknown", "", 5, True, [], {}])
def test_malformed_alert_severity_fails(monkeypatch, cli, alert, severity):
    alert["rule"]["security_severity_level"] = severity
    _mock_pages(monkeypatch, [[alert]])
    assert gate.main() == 1


def test_api_failure_after_first_page_never_grants_exception(
    monkeypatch, cli, alert
):
    failure = subprocess.CalledProcessError(1, ["gh", "api"])
    _mock_pages(monkeypatch, [[alert], failure])
    assert gate.main() == 1
    assert not cli[1].exists()


@pytest.mark.parametrize("channel", ["stderr", "output"])
def test_api_failure_preserves_status_without_echoing_private_diagnostics(
    monkeypatch, cli, capsys, channel
):
    failure = subprocess.CalledProcessError(
        1,
        ["gh", "api"],
        **{channel: "HTTP 403 rate limit\n::warning::injected%\r" + "x" * 3000},
    )
    _mock_pages(monkeypatch, [failure])
    assert gate.main() == 1
    report = capsys.readouterr().out
    assert "HTTP 403" in report and "rate limit" in report
    assert "injected" not in report
    assert "x" * 2001 not in report
    assert len(report.splitlines()) == 1
    assert not cli[1].exists()


def test_missing_policy_fails_before_api(monkeypatch, cli, tmp_path):
    monkeypatch.setattr(
        sys,
        "argv",
        [
            str(SCRIPT),
            "--repository",
            REPOSITORY,
            "--exceptions",
            str(tmp_path / "missing.json"),
            "--evidence-deadline",
            "none",
        ],
    )
    calls = _mock_pages(monkeypatch, [])
    assert gate.main() == 1
    assert calls == []


def test_cli_unrelated_blocker_returns_failure(monkeypatch, cli, alert):
    other = deepcopy(alert)
    other["number"] = 9008
    _mock_pages(monkeypatch, [[alert, other], []])
    assert gate.main() == 1
    assert "| Medium | 2 | 1 | 1 |" in cli[0].read_text()
    assert not cli[1].exists(), "A blocked gate must not grant a deadline"


def test_publication_deadline_uses_earliest_applied_exception(
    alert, exceptions
):
    second = deepcopy(alert)
    second["number"] = 42
    exceptions[42] = {**exceptions[8273], "expires_on": "2026-10-05"}
    # An unused/closed alert's entry must not constrain this release.
    exceptions[99] = {"expires_on": "2020-01-01"}
    assert (
        gate.publication_deadline([alert, second], exceptions) == "2026-10-05"
    )
    assert gate.publication_deadline([alert], exceptions) == "2026-10-13"
    assert gate.publication_deadline([], exceptions) == "none"


@pytest.mark.parametrize(
    "previous,current,expected",
    [
        ("none", "none", "none"),
        ("2026-10-05", "none", "2026-10-05"),
        ("none", "2026-10-13", "2026-10-13"),
        ("2026-10-05", "2026-10-13", "2026-10-05"),
        ("2026-10-13", "2026-10-05", "2026-10-05"),
    ],
)
def test_earliest_deadline_retains_both_gate_decisions(
    previous, current, expected
):
    assert gate.earliest_deadline(previous, current, TODAY) == expected


@pytest.mark.parametrize(
    "previous,expected",
    [
        ("none", "2026-10-13"),
        ("2026-10-05", "2026-10-05"),
        ("2026-10-20", "2026-10-13"),
    ],
)
def test_cli_carries_earliest_deadline_from_either_query(
    monkeypatch, cli, alert, previous, expected
):
    monkeypatch.setattr(sys, "argv", [*sys.argv[:-1], previous])
    _mock_pages(monkeypatch, [[alert], []])
    assert gate.main() == 0
    assert cli[1].read_text() == (
        f"excepted_count=1\nrelease_deadline={expected}\n"
    )
    assert f"`{expected}`" in cli[0].read_text()


@pytest.mark.parametrize(
    "previous", ["", "20261013", "invalid", "2026-09-27", "2026-09-26"]
)
def test_invalid_or_expired_evidence_deadline_fails_before_api(
    monkeypatch, cli, previous
):
    monkeypatch.setattr(sys, "argv", [*sys.argv[:-1], previous])
    calls = _mock_pages(monkeypatch, [])
    assert gate.main() == 1
    assert calls == []
    assert not cli[1].exists()


def test_missing_evidence_deadline_is_not_unlimited(monkeypatch, cli):
    monkeypatch.setattr(sys, "argv", sys.argv[:-2])
    calls = _mock_pages(monkeypatch, [])
    with pytest.raises(SystemExit) as error:
        gate.main()
    assert error.value.code == 2
    assert calls == []
    assert not cli[1].exists()


def test_evidence_expiring_during_alert_fetch_does_not_grant_output(
    monkeypatch, cli
):
    class AdvancingDateTime(datetime):
        times = iter(
            [
                datetime(2026, 10, 12, 23, 59, tzinfo=timezone.utc),
                datetime(2026, 10, 13, tzinfo=timezone.utc),
                datetime(2026, 10, 13, tzinfo=timezone.utc),
            ]
        )

        @classmethod
        def now(cls, tz=None):
            assert tz == timezone.utc
            return next(cls.times)

    monkeypatch.setattr(gate, "datetime", AdvancingDateTime)
    monkeypatch.setattr(sys, "argv", [*sys.argv[:-1], "2026-10-13"])
    _mock_pages(monkeypatch, [[]])
    assert gate.main() == 1
    assert not cli[1].exists()


def test_workflow_keeps_scans_and_alert_gate_required():
    workflow = yaml.safe_load(
        (ROOT / ".github/workflows/release-gate.yml").read_text()
    )
    job = workflow["jobs"]["check-code-scanning-alerts"]
    assert job["permissions"] == {
        "contents": "read",
        "actions": "read",
        "security-events": "read",
    }
    assert "grype-scan" in job["needs"]
    assert not job.get("continue-on-error")
    steps = job["steps"]
    evidence = next(
        step
        for step in steps
        if "check_release_scan_evidence.py" in step.get("run", "")
    )
    assert not evidence.get("continue-on-error")
    assert evidence["id"] == "scan-evidence"
    assert (
        "--exceptions .github/release-security-exceptions.json"
        in evidence["run"]
    )
    check = next(step for step in steps if step.get("id") == "alert-check")
    assert steps.index(evidence) < steps.index(check)
    assert not check.get("continue-on-error")
    assert ".github/scripts/check_code_scanning_alerts.py" in check["run"]
    assert '--repository "$GITHUB_REPOSITORY"' in check["run"]
    assert '--evidence-deadline "$EVIDENCE_DEADLINE"' in check["run"]
    assert check["env"]["EVIDENCE_DEADLINE"] == (
        "${{ steps.scan-evidence.outputs.release_deadline }}"
    )
    assert any(
        step.get("uses", "").startswith("actions/checkout@") for step in steps
    )
    summary = workflow["jobs"]["release-gate-summary"]
    assert "check-code-scanning-alerts" in summary["needs"]
    assert "grype-scan" in summary["needs"]
    required_results = next(
        step
        for step in summary["steps"]
        if "GATE_RESULTS" in step.get("env", {})
        and "EXCEPTED_ALERT_COUNT" not in step.get("env", {})
    )
    assert summary["if"] == "always()"
    assert "if" not in required_results
    assert not required_results.get("continue-on-error")
    reporting = next(
        step
        for step in summary["steps"]
        if "EXCEPTED_ALERT_COUNT" in step.get("env", {})
    )
    assert summary["steps"].index(required_results) < summary["steps"].index(
        reporting
    )
    assert "excepted_count" in reporting["env"]["EXCEPTED_ALERT_COUNT"]
    assert reporting["env"]["RELEASE_DEADLINE"] == (
        "${{ needs.check-code-scanning-alerts.outputs.release_deadline }}"
    )
    assert 'if [ "$RELEASE_DEADLINE" != "none" ]' in reporting["run"]
    assert (
        "Applied exceptions retain their original deadline" in reporting["run"]
    )