~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Age, visibility and native-report contracts for release findings."""
# allow: no-sut-import — the SUTs are standalone release policy scripts.
from copy import deepcopy
from datetime import datetime, timedelta, timezone
import importlib.util
import json
from pathlib import Path
import sys
from urllib.parse import parse_qs, urlsplit
import pytest
SCRIPTS = Path(__file__).resolve().parents[2] / ".github/scripts"
NOW = datetime(2026, 10, 29, 12, tzinfo=timezone.utc)
def load(name):
spec = importlib.util.spec_from_file_location(name, SCRIPTS / f"{name}.py")
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
gate = load("check_code_scanning_alerts")
deadline = load("check_release_deadline")
native = load("check_release_report_age")
reports = load("release_native_reports")
evidence = load("check_release_scan_evidence")
def alert(age=timedelta(days=1), severity="high", *, state="open", number=1):
return {
"number": number,
"state": state,
"created_at": (NOW - age).isoformat(),
"rule": {
"id": "CVE-2099-0001",
"security_severity_level": severity,
"severity": "warning",
},
"tool": {"name": "sample-scanner"},
"most_recent_instance": {
"category": "sample",
"location": {"path": "package-lock.json", "start_line": 1},
},
}
@pytest.mark.parametrize("severity", ["high", "medium", None])
@pytest.mark.parametrize(
"age,allowed",
[
(timedelta(days=14, microseconds=-1), True),
(timedelta(days=14), False),
(timedelta(days=15), False),
],
)
def test_noncritical_findings_have_one_exact_window(severity, age, allowed):
item = alert(age, severity)
before = deepcopy(item)
candidates, _, _ = gate.classify_alerts([item], {}, NOW.date())
blocking, graced = gate.apply_grace(candidates, NOW)
assert bool(graced) == allowed
assert bool(blocking) != allowed
assert item == before and item["state"] == "open"
def test_critical_has_no_automatic_grace():
item = alert(timedelta(seconds=1), "critical")
assert gate.apply_grace([item], NOW) == ([item], [])
def test_critical_cannot_use_an_explicit_exception_either():
item = alert(severity="critical")
entry = {
"alert_number": 1,
"rule_id": item["rule"]["id"],
"tool": "sample-scanner",
"category": "sample",
"path": "package-lock.json",
"severity": "critical",
"expires_on": "2099-01-01",
}
assert gate.classify_alerts([item], {1: entry}, NOW.date()) == (
[item],
[],
[],
)
@pytest.mark.parametrize(
"severity,level", [("low", "error"), (None, "note"), (None, "none")]
)
def test_advisory_findings_stay_advisory(severity, level):
item = alert(timedelta(days=100), severity)
item["rule"]["severity"] = level
assert gate.classify_alerts([item], {}, NOW.date()) == ([], [], [])
@pytest.mark.parametrize(
"created",
[
None,
"",
"invalid",
"2026-10-28",
"2026-10-28T12:00:00",
"2026-10-30T12:00:00Z",
],
)
def test_unprovable_or_future_age_cannot_grant_time(created):
item = alert()
item["created_at"] = created
assert gate.apply_grace([item], NOW) == ([item], [])
def test_reruns_reopens_and_image_paths_do_not_reset_a_vulnerability_clock():
current = alert(number=2)
current["updated_at"] = NOW.isoformat()
current["most_recent_instance"]["location"]["path"] = "image-b/Python"
previous = alert(timedelta(days=20), state="fixed")
original = deepcopy(current)
aged = gate.retain_first_detection([current], [previous])
assert gate.apply_grace(aged, NOW) == (aged, [])
assert current == original
def test_invalid_old_history_is_not_discarded_to_extend_grace():
current = alert()
previous = alert(state="dismissed", number=2)
previous["created_at"] = None
aged = gate.retain_first_detection([current], [previous])
assert gate.apply_grace(aged, NOW) == (aged, [])
def test_an_expired_explicit_exception_cannot_restart_as_automatic_grace():
item = alert()
assert gate.apply_grace([item], NOW, expired=[1]) == ([item], [])
def test_the_exact_expiry_is_carried_across_disappearing_alerts_and_approval():
item = alert(timedelta(days=13))
expiry = gate.publication_deadline([], {}, [item])
assert expiry == "2026-10-30T12:00:00Z"
assert gate.earliest_deadline(expiry, "none", NOW) == expiry
deadline.check_deadline(
expiry, datetime(2026, 10, 30, 11, 59, 59, tzinfo=timezone.utc)
)
with pytest.raises(ValueError, match="expired"):
deadline.check_deadline(
expiry, datetime(2026, 10, 30, 12, tzinfo=timezone.utc)
)
def test_legacy_exception_dates_and_exact_grace_timestamps_choose_the_earliest():
assert (
gate.earliest_deadline("2026-10-30", "2026-10-30T12:00:00Z", NOW)
== "2026-10-30"
)
assert (
gate.earliest_deadline("2026-10-31", "2026-10-30T12:00:00Z", NOW)
== "2026-10-30T12:00:00Z"
)
def report(severity="high"):
return reports.sarif(
"sample-scanner", [("CVE-2099-0001", severity, "package-lock.json")]
)
def test_native_critical_blocks_without_waiting_for_history():
def forbidden(*args):
pytest.fail(
"Critical findings must not consult history for a grace period"
)
with pytest.raises(ValueError, match="Critical"):
native.evaluate(
report("critical"),
Path.cwd(),
"example/project",
now=NOW,
fetch=forbidden,
)
def test_native_empty_report_needs_no_history():
def forbidden(*args):
pytest.fail("Empty complete reports have no grace decision")
assert native.evaluate(
reports.sarif("sample-scanner", []),
Path.cwd(),
"example/project",
now=NOW,
fetch=forbidden,
) == ("none", 0)
def test_native_findings_use_persisted_first_detection():
calls = []
def fetch(repository, state):
calls.append((repository, state))
return [alert()] if state == "open" else []
assert native.evaluate(
report(), Path.cwd(), "example/project", now=NOW, fetch=fetch
) == ("2026-11-11T12:00:00Z", 1)
assert calls == [
("example/project", state) for state in ("open", "fixed", "dismissed")
]
def test_native_history_must_be_indexed_and_cannot_be_replaced_with_now():
sleeps = []
with pytest.raises(ValueError, match="first-detection"):
native.evaluate(
report(),
Path.cwd(),
"example/project",
now=NOW,
fetch=lambda *args: [],
sleep=sleeps.append,
)
assert sleeps == [5] * 6
def test_native_index_retry_refreshes_cached_history():
ready = False
def refresh():
nonlocal ready
ready = True
assert (
native.evaluate(
report(),
Path.cwd(),
"example/project",
now=NOW,
fetch=lambda _, state: (
[alert()] if ready and state == "open" else []
),
refresh=refresh,
sleep=lambda _: None,
)[1]
== 1
)
@pytest.mark.parametrize("score", [True, False, "NaN", "inf", -1, 11])
def test_native_invalid_security_scores_cannot_reduce_a_finding(score):
data = report()
data["runs"][0]["tool"]["driver"]["rules"][0]["properties"][
"security-severity"
] = score
with pytest.raises(ValueError, match="score"):
native.report_findings(data, Path.cwd())
def test_result_metadata_cannot_reduce_a_critical_rule_score():
data = report("critical")
data["runs"][0]["results"][0]["properties"] = {"security-severity": "2.0"}
with pytest.raises(ValueError, match="Critical"):
native.evaluate(data, Path.cwd(), "example/project", now=NOW)
def test_zero_security_score_retains_native_sarif_level():
data = report()
data["runs"][0]["tool"]["driver"]["rules"][0]["properties"][
"security-severity"
] = "0.0"
finding = native.report_findings(data, Path.cwd())[0]
assert finding["rule"]["security_severity_level"] is None
assert gate.blocking_severity(finding)
def test_native_reopened_old_finding_blocks():
def fetch(repository, state):
return (
[alert(timedelta(days=20), state="fixed")]
if state == "fixed"
else []
)
with pytest.raises(ValueError, match="14-day"):
native.evaluate(
report(), Path.cwd(), "example/project", now=NOW, fetch=fetch
)
def test_native_noncritical_score_cannot_hide_an_indexed_critical_alert():
# The native report scores the finding high (< 9), but GitHub's open
# alert for the same scanner/advisory key is critical: no grace applies,
# even though the recorded first detection is only one day old.
def fetch(repository, state):
return [alert(severity="critical")] if state == "open" else []
with pytest.raises(ValueError, match="Critical indexed"):
native.evaluate(
report("high"), Path.cwd(), "example/project", now=NOW, fetch=fetch
)
def test_native_first_detection_ignores_non_blocking_history_order():
# A low-scored record for the same advisory key listed first must not
# break the first-detection lookup; the earliest date still applies.
def fetch(repository, state):
if state == "open":
return [
alert(timedelta(days=2), severity="low", number=2),
alert(timedelta(days=1)),
]
return []
assert native.evaluate(
report(), Path.cwd(), "example/project", now=NOW, fetch=fetch
) == ("2026-11-10T12:00:00Z", 1)
def test_native_first_detection_with_only_non_blocking_history():
def fetch(repository, state):
return [alert(severity="low")] if state == "open" else []
assert native.evaluate(
report(), Path.cwd(), "example/project", now=NOW, fetch=fetch
) == ("2026-11-11T12:00:00Z", 1)
def test_an_incomplete_scan_never_becomes_a_clean_report():
data = reports.sarif("sample-scanner", [])
data["runs"][0]["invocations"][0]["executionSuccessful"] = False
with pytest.raises(ValueError, match="complete"):
native.report_findings(data, Path.cwd())
@pytest.mark.parametrize(
"data",
[
{},
{"error": "synthetic diagnostic"},
{"auditReportVersion": 2},
{"auditReportVersion": 2, "vulnerabilities": {}, "error": {}},
],
)
def test_npm_operational_failures_are_not_findings(data):
with pytest.raises(ValueError):
reports.npm_findings(data, "package-lock.json")
def test_npm_transitive_findings_keep_critical_severity_and_stable_identity():
data = {
"auditReportVersion": 2,
"vulnerabilities": {
"parent": {"severity": "critical", "via": ["child"]},
"child": {
"severity": "high",
"via": [
{
"url": "https://github.com/advisories/GHSA-aaaa-bbbb-cccc",
"severity": "high",
}
],
},
},
"metadata": {
"vulnerabilities": {
"info": 0,
"low": 0,
"moderate": 0,
"high": 1,
"critical": 1,
"total": 2,
}
},
}
rows = reports.npm_findings(data, "package-lock.json")
assert ("GHSA-aaaa-bbbb-cccc", "critical", "package-lock.json") in rows
assert len(rows) == 2
def test_zap_report_discards_http_samples_and_tracks_only_plugin_identity():
data = {
"@version": "2.17.0",
"site": [
{
"alerts": [
{
"pluginid": "10001",
"riskcode": "2",
"instances": [{"evidence": "SYNTHETIC_PRIVATE_MARKER"}],
}
]
}
],
}
generated = reports.zap_report(data)
assert "SYNTHETIC_PRIVATE_MARKER" not in str(generated)
assert generated["runs"][0]["results"][0]["ruleId"] == "ZAP-10001"
@pytest.mark.parametrize(
"data",
[
{},
{"@version": "2", "site": []},
{"@version": "2", "site": [{"alerts": "invalid"}]},
],
)
def test_zap_missing_or_malformed_targets_fail(data):
with pytest.raises(ValueError):
reports.zap_report(data)
def test_checkov_parsing_errors_remain_fatal():
with pytest.raises(ValueError, match="parsing"):
reports.validate_checkov(
{
"summary": {"parsing_errors": 1},
"results": {"parsing_errors": ["synthetic"]},
}
)
reports.validate_checkov(
{"summary": {"parsing_errors": 0}, "results": {"parsing_errors": []}}
)
@pytest.mark.parametrize(
"old_default_history,allowed", [(False, True), (True, False)]
)
def test_fresh_release_ref_uses_original_default_branch_history(
monkeypatch, tmp_path, old_default_history, allowed
):
class Clock(datetime):
@classmethod
def now(cls, tz=None):
return NOW
monkeypatch.setattr(evidence, "datetime", Clock)
repo, sha, ref = "example/project", "a" * 40, "refs/tags/v1.0.0"
for key, value in {
"GITHUB_REPOSITORY": repo,
"GITHUB_SHA": sha,
"GITHUB_REF": ref,
"GITHUB_RUN_ID": "1",
"GITHUB_STEP_SUMMARY": str(tmp_path / "summary"),
"GITHUB_OUTPUT": str(tmp_path / "output"),
}.items():
monkeypatch.setenv(key, value)
expected = json.loads(
(SCRIPTS.parent / "release-scan-policy.json").read_text(
encoding="utf-8"
)
)
calls = []
def fetch(endpoint):
calls.append(endpoint)
if "/actions/runs/" in endpoint:
return {
"head_sha": sha,
"created_at": NOW.isoformat(),
"path": ".github/workflows/release.yml",
}
if "/analyses?" in endpoint:
return [
{
"category": category,
"commit_sha": sha,
"ref": ref,
"analysis_key": f".github/workflows/release.yml:{job}",
"created_at": NOW.isoformat(),
"error": "",
"results_count": 1,
}
for category, job in expected.items()
]
query = parse_qs(urlsplit(endpoint).query)
if query["state"] == ["open"] and "ref" in query:
return [alert()]
if (
query["state"] == ["fixed"]
and "ref" not in query
and old_default_history
):
return [alert(timedelta(days=20), state="fixed", number=2)]
return []
if allowed:
evidence.evaluate(fetch=fetch)
assert (tmp_path / "output").read_text(
encoding="utf-8"
) == "release_deadline=2026-11-11T12:00:00Z\n"
else:
with pytest.raises(ValueError, match="blocking code-scanning"):
evidence.evaluate(fetch=fetch)
assert not (tmp_path / "output").exists()
assert any("state=fixed" in call and "ref=" not in call for call in calls)
@pytest.mark.parametrize("expire_during_lookup", [False, True])
def test_native_cli_binds_current_ref_and_does_not_emit_an_expired_decision(
monkeypatch, tmp_path, expire_during_lookup
):
class Clock(datetime):
calls = 0
@classmethod
def now(cls, tz=None):
cls.calls += 1
return NOW + (
timedelta(seconds=2)
if expire_during_lookup and cls.calls > 1
else timedelta()
)
monkeypatch.setattr(native, "datetime", Clock)
monkeypatch.setattr(native, "policy_module", lambda: gate)
upload = "11111111-1111-4111-8111-111111111111"
repo, sha, ref = "example/project", "a" * 40, "refs/tags/v1.0.0"
values = {
"GITHUB_REPOSITORY": repo,
"GITHUB_SHA": sha,
"GITHUB_REF": ref,
"GITHUB_JOB": "sample",
"GITHUB_WORKFLOW_REF": repo + "/.github/workflows/release.yml@" + ref,
"GITHUB_OUTPUT": str(tmp_path / "output"),
}
for key, value in values.items():
monkeypatch.setenv(key, value)
path = tmp_path / "native.sarif"
path.write_text(json.dumps(report()), encoding="utf-8")
monkeypatch.setattr(
sys,
"argv",
[
"policy",
"--report",
str(path),
"--sarif-id",
upload,
"--category",
"sample-release",
],
)
original = native.verify_upload
def api(endpoint):
if "/sarifs/" in endpoint:
return {"processing_status": "complete"}
return [
{
"sarif_id": upload,
"commit_sha": sha,
"ref": ref,
"analysis_key": ".github/workflows/release.yml:sample",
"category": "sample-release",
"tool": {"name": "sample-scanner"},
"error": "",
"results_count": 1,
}
]
monkeypatch.setattr(
native, "verify_upload", lambda *args: original(*args, fetch=api)
)
calls = []
def history(repository, state, ref=None, tool_name=None):
calls.append((repository, state, ref, tool_name))
return (
[alert(timedelta(days=14, seconds=-1))]
if state == "open" and ref == values["GITHUB_REF"]
else []
)
monkeypatch.setattr(gate, "fetch_alerts", history)
assert native.main() == int(expire_during_lookup)
assert (tmp_path / "output").exists() != expire_during_lookup
assert {call[2] for call in calls} == {None, values["GITHUB_REF"]}
if not expire_during_lookup:
assert (tmp_path / "output").read_text(
encoding="utf-8"
) == "release_deadline=2026-10-29T12:00:01Z\n"
class FrozenClock(datetime):
@classmethod
def now(cls, tz=None):
return NOW
def post_gate_alert(category, *, latest_state="open", age=timedelta(days=20)):
record = alert(age)
record["most_recent_instance"] = {
**record["most_recent_instance"],
"category": category,
"state": latest_state,
}
return record
def test_gate_ignores_alerts_open_only_in_post_gate_publication_categories():
lookups = []
def instances(rows):
def fetch(number):
lookups.append(number)
return rows
return fetch
only_prerelease = [{"state": "open", "category": "trivy-prerelease"}]
assert (
gate.gate_alerts(
[post_gate_alert("trivy-prerelease")], instances(only_prerelease)
)
== []
)
# Fixed in the gate-time scan, still open only after publication.
assert (
gate.gate_alerts(
[post_gate_alert("container-security", latest_state="fixed")],
instances(
[
{"state": "fixed", "category": "container-security"},
{"state": "open", "category": "trivy-promotion"},
]
),
)
== []
)
# Still open in a gate-time category: it keeps blocking.
mixed = [
{"state": "open", "category": "trivy-promotion"},
{"state": "open", "category": "container-security"},
]
assert (
len(
gate.gate_alerts(
[post_gate_alert("trivy-promotion")], instances(mixed)
)
)
== 1
)
# Unprovable instance data stays in the blocking set.
assert (
len(
gate.gate_alerts(
[post_gate_alert("trivy-prerelease")], instances([])
)
)
== 1
)
lookups.clear()
assert (
len(
gate.gate_alerts(
[post_gate_alert("container-security")],
instances(only_prerelease),
)
)
== 1
)
assert lookups == []
@pytest.mark.parametrize(
"category,allowed",
[
("trivy-prerelease", True),
("trivy-promotion", True),
("container-security", False),
],
)
def test_release_evidence_cannot_deadlock_on_stale_post_gate_alerts(
monkeypatch, tmp_path, category, allowed
):
class Clock(datetime):
@classmethod
def now(cls, tz=None):
return NOW
monkeypatch.setattr(evidence, "datetime", Clock)
repo, sha, ref = "example/project", "a" * 40, "refs/heads/main"
for key, value in {
"GITHUB_REPOSITORY": repo,
"GITHUB_SHA": sha,
"GITHUB_REF": ref,
"GITHUB_RUN_ID": "1",
"GITHUB_STEP_SUMMARY": str(tmp_path / "summary"),
"GITHUB_OUTPUT": str(tmp_path / "output"),
}.items():
monkeypatch.setenv(key, value)
expected = json.loads(
(SCRIPTS.parent / "release-scan-policy.json").read_text(
encoding="utf-8"
)
)
def fetch(endpoint):
if "/actions/runs/" in endpoint:
return {
"head_sha": sha,
"created_at": NOW.isoformat(),
"path": ".github/workflows/release.yml",
}
if "/analyses?" in endpoint:
return [
{
"category": name,
"commit_sha": sha,
"ref": ref,
"analysis_key": f".github/workflows/release.yml:{job}",
"created_at": NOW.isoformat(),
"error": "",
"results_count": 1,
}
for name, job in expected.items()
]
if "/instances?" in endpoint:
query = parse_qs(urlsplit(endpoint).query)
instance_refs.append(query.get("ref"))
rows = [
{"state": "open", "category": category, "ref": ref},
# A stale gate-time row on another ref must not count.
{
"state": "open",
"category": "container-security",
"ref": "refs/heads/release/rehearsal",
},
]
return (
[r for r in rows if r["ref"] in query.get("ref", [r["ref"]])]
if query["page"] == ["1"]
else []
)
query = parse_qs(urlsplit(endpoint).query)
if query["state"] == ["open"] and "ref" in query:
return [post_gate_alert(category)]
return []
instance_refs = []
if allowed:
evidence.evaluate(fetch=fetch)
assert (tmp_path / "output").read_text(
encoding="utf-8"
) == "release_deadline=none\n"
assert instance_refs and all(r == [ref] for r in instance_refs)
else:
with pytest.raises(ValueError, match="blocking code-scanning"):
evidence.evaluate(fetch=fetch)
@pytest.mark.parametrize(
"category,status",
[
("trivy-prerelease", 0),
("trivy-promotion", 0),
("container-security", 1),
],
)
def test_final_alert_check_ignores_stale_post_gate_alerts(
monkeypatch, tmp_path, category, status
):
policy = tmp_path / "exceptions.json"
policy.write_text(
json.dumps(
{"version": 1, "repository": "example/project", "exceptions": []}
),
encoding="utf-8",
)
monkeypatch.setattr(
gate,
"fetch_alerts",
lambda repository, state="open", ref=None, tool_name=None: (
[post_gate_alert(category)] if state == "open" else []
),
)
rows = [
{"ref": "refs/heads/main", "category": category, "state": "open"},
{
"ref": "refs/heads/main",
"category": "container-security",
"state": "fixed",
},
# Stale gate-time rows on a tag and a rehearsal branch.
{
"ref": "refs/tags/v1.0.0",
"category": "container-security",
"state": "open",
},
{
"ref": "refs/heads/release/v1.0.0-rehearsal",
"category": "container-security",
"state": "open",
},
]
monkeypatch.setattr(
gate,
"fetch_instances",
lambda repository, number, ref=None: [
row for row in rows if ref is None or row["ref"] == ref
],
)
event = tmp_path / "event.json"
event.write_text(
json.dumps({"repository": {"default_branch": "main"}}),
encoding="utf-8",
)
monkeypatch.setenv("GITHUB_EVENT_PATH", str(event))
monkeypatch.delenv("GITHUB_STEP_SUMMARY", raising=False)
monkeypatch.delenv("GITHUB_OUTPUT", raising=False)
monkeypatch.setattr(
sys,
"argv",
[
"check_code_scanning_alerts.py",
"--repository",
"example/project",
"--exceptions",
str(policy),
"--evidence-deadline",
"none",
],
)
assert gate.main() == status
def test_unprovable_instance_state_keeps_an_alert_blocking():
for state in (None, "OPEN", "unknown"):
row = {"category": "container-security"}
if state is not None:
row["state"] = state
rows = [{"state": "open", "category": "trivy-prerelease"}, row]
assert (
len(
gate.gate_alerts(
[post_gate_alert("trivy-prerelease")], lambda _: rows
)
)
== 1
), state
closed = [
{"state": "open", "category": "trivy-prerelease"},
{"state": "dismissed", "category": "container-security"},
]
assert (
gate.gate_alerts(
[post_gate_alert("trivy-prerelease")], lambda _: closed
)
== []
)
def test_fetch_instances_reads_only_the_requested_ref(monkeypatch):
urls = []
def run(argv, **kwargs):
urls.append(argv[-1])
page = parse_qs(urlsplit(argv[-1]).query)["page"]
rows = [
{"ref": "refs/heads/main", "state": "open"},
{"ref": "refs/tags/v1.0.0", "state": "open"},
]
return type(
"Done", (), {"stdout": json.dumps(rows if page == ["1"] else [])}
)
monkeypatch.setattr(gate.subprocess, "run", run)
rows = gate.fetch_instances("example/project", 7, ref="refs/heads/main")
assert rows == [{"ref": "refs/heads/main", "state": "open"}]
assert parse_qs(urlsplit(urls[0]).query)["ref"] == ["refs/heads/main"]
def test_excluded_post_gate_alerts_still_back_date_the_same_finding(
monkeypatch, tmp_path
):
"""A gate-time alert for the same advisory inherits the earlier detection
of an alert the gate then excludes as post-gate only."""
policy = tmp_path / "exceptions.json"
policy.write_text(
json.dumps(
{"version": 1, "repository": "example/project", "exceptions": []}
),
encoding="utf-8",
)
stale = post_gate_alert("trivy-prerelease")
current = post_gate_alert("container-security", age=timedelta(days=1))
current["number"] = 2
current["most_recent_instance"]["location"] = {
"path": "another/image/path",
"start_line": 1,
}
monkeypatch.setattr(
gate,
"fetch_alerts",
lambda repository, state="open", ref=None, tool_name=None: (
[stale, current] if state == "open" else []
),
)
monkeypatch.setattr(
gate,
"fetch_instances",
lambda repository, number, ref=None: [
{"ref": ref, "category": "trivy-prerelease", "state": "open"}
],
)
monkeypatch.setattr(
gate, "default_branch_ref", lambda repository: "refs/heads/main"
)
# Pin the clock: the unordered variant must grace the 1-day alert.
monkeypatch.setattr(gate, "datetime", FrozenClock)
monkeypatch.delenv("GITHUB_STEP_SUMMARY", raising=False)
monkeypatch.delenv("GITHUB_OUTPUT", raising=False)
monkeypatch.setattr(
sys,
"argv",
[
"check_code_scanning_alerts.py",
"--repository",
"example/project",
"--exceptions",
str(policy),
"--evidence-deadline",
"none",
],
)
# Back-dated to 20 days, the gate-time alert is past its window.
assert gate.main() == 1
def test_open_advisory_alerts_back_date_a_later_blocking_finding():
# An open low alert for the same advisory proves the earlier detection.
earlier = alert(timedelta(days=20), severity="low", number=2)
retained = gate.retain_first_detection([alert(), earlier], [])
assert retained[0]["first_detected_at"] == NOW - timedelta(days=20)
def test_final_check_back_dates_from_fixed_history(monkeypatch, tmp_path):
"""main() must fetch fixed history for a graced tool and inherit its
earlier detection: 10 days ago, not the reopened alert's 1 day."""
policy = tmp_path / "exceptions.json"
policy.write_text(
json.dumps(
{"version": 1, "repository": "example/project", "exceptions": []}
),
encoding="utf-8",
)
history_calls = []
def fetch_alerts(repository, state="open", ref=None, tool_name=None):
if state == "open":
return [alert()]
history_calls.append((state, tool_name))
if state == "fixed" and tool_name == "sample-scanner":
return [alert(timedelta(days=10), state="fixed", number=2)]
return []
monkeypatch.setattr(gate, "fetch_alerts", fetch_alerts)
monkeypatch.setattr(gate, "datetime", FrozenClock)
output = tmp_path / "output"
monkeypatch.setenv("GITHUB_OUTPUT", str(output))
monkeypatch.delenv("GITHUB_STEP_SUMMARY", raising=False)
monkeypatch.setattr(
sys,
"argv",
[
"check_code_scanning_alerts.py",
"--repository",
"example/project",
"--exceptions",
str(policy),
"--evidence-deadline",
"none",
],
)
assert gate.main() == 0
assert ("fixed", "sample-scanner") in history_calls
assert "release_deadline=2026-11-02T12:00:00Z\n" in output.read_text(
encoding="utf-8"
)
def test_publication_deadline_uses_first_detection_over_created_at():
graced = {**alert(), "first_detected_at": NOW - timedelta(days=10)}
assert gate.publication_deadline([], {}, [graced]) == (
"2026-11-02T12:00:00Z"
)
assert gate.publication_deadline([], {}, [alert()]) == (
"2026-11-11T12:00:00Z"
)
def _tag_evidence(monkeypatch, tmp_path, open_on_ref, open_default, rows):
"""Run the evidence check for a tag release (not the default branch)."""
monkeypatch.setattr(evidence, "datetime", FrozenClock)
sha, ref = "a" * 40, "refs/tags/v1.0.0"
for key, value in {
"GITHUB_REPOSITORY": "example/project",
"GITHUB_SHA": sha,
"GITHUB_REF": ref,
"GITHUB_RUN_ID": "1",
"GITHUB_STEP_SUMMARY": str(tmp_path / "summary"),
"GITHUB_OUTPUT": str(tmp_path / "output"),
}.items():
monkeypatch.setenv(key, value)
expected = json.loads(
(SCRIPTS.parent / "release-scan-policy.json").read_text(
encoding="utf-8"
)
)
def fetch(endpoint):
if "/actions/runs/" in endpoint:
return {
"head_sha": sha,
"created_at": NOW.isoformat(),
"path": ".github/workflows/release.yml",
}
if "/analyses?" in endpoint:
return [
{
"category": name,
"commit_sha": sha,
"ref": ref,
"analysis_key": f".github/workflows/release.yml:{job}",
"created_at": NOW.isoformat(),
"error": "",
"results_count": 1,
}
for name, job in expected.items()
]
query = parse_qs(urlsplit(endpoint).query)
if query["page"] != ["1"]:
return []
if "/instances?" in endpoint:
assert query["ref"] == [ref]
return rows
if query["state"] == ["open"]:
return open_on_ref if query.get("ref") == [ref] else open_default
return []
evidence.evaluate(fetch=fetch)
return (tmp_path / "output").read_text(encoding="utf-8")
def test_tag_evidence_back_dates_before_excluding_post_gate_alerts(
monkeypatch, tmp_path
):
stale = post_gate_alert("trivy-prerelease")
current = post_gate_alert("container-security", age=timedelta(days=1))
current["number"] = 2
current["most_recent_instance"]["location"] = {
"path": "another/image/path",
"start_line": 1,
}
rows = [
{
"ref": "refs/tags/v1.0.0",
"category": "trivy-prerelease",
"state": "open",
}
]
with pytest.raises(ValueError, match="blocking code-scanning"):
_tag_evidence(monkeypatch, tmp_path, [stale, current], [], rows)
def test_tag_evidence_uses_open_default_branch_history(monkeypatch, tmp_path):
"""A tag release inherits the default branch's earlier open detection."""
assert (
_tag_evidence(
monkeypatch,
tmp_path,
[alert()],
[alert(timedelta(days=10), number=2)],
[],
)
== "release_deadline=2026-11-02T12:00:00Z\n"
)