"""Whether a candidate image improves on the pinned one (#1308). Trivy lists the packages of both, netOrk's matcher in kb-server judges both in one call (so against one state of its data, kb-server#155), and the verdict is "improved" when a critical or high CVE with a fix is closed by a patch, Debian made a point release, or a component the image is built for moved. Everything else is "same": the pin stays. A candidate that is worse than the pin anywhere, on an older base-os, with a Debian point release backwards or with a new critical or high CVE that has a fix, is "regressed" whatever else it brings (NetOrk/image-tools#3, netork#2523). A matcher that cannot be asked is never an improvement: no pin moves on a guess. """ from __future__ import annotations import json import subprocess import pytest from image_tools.verify import Verdict, base_os_version, judge, read_changes, render, verify CLOSED = { "cve": "CVE-2026-1111", "package": "openssl", "severity": "high", "installed": "3.5.1-1", "fixed": "3.5.1-1+deb13u1", } NEW = {"cve": "CVE-2026-2222", "package": "nginx", "severity": "critical", "installed": "1.26.3-4"} #: What netork#2523's candidate brought back: zlib of the older base-os. ZLIB = { "cve": "CVE-2026-76844", "package": "zlib", "severity": "high", "installed": "1:1.3.dfsg+really1.3.1-1", "fixed": "1:1.3.dfsg+really1.3.1-1+deb13u1", } PINNED_BASE, OLDER_BASE = "13-20261108.1", "13-20261025.1" def _answer(closed=(), new=()): return { "closed": list(closed), "new": list(new), "current": {"critical": 1, "high": 4}, "candidate": {"critical": 1, "high": 3}, } class TestJudge: def test_a_closed_cve_is_an_improvement(self): verdict = judge(_answer(closed=[CLOSED]), "13.7", "13.7") assert verdict.improved assert verdict.closed == [CLOSED] assert not verdict.point_release def test_nothing_closed_is_the_same(self): assert not judge(_answer(), "13.7", "13.7").improved def test_a_point_release_is_an_improvement_on_its_own(self): verdict = judge(_answer(), "13.7", "13.8") assert verdict.improved assert verdict.point_release == ("13.7", "13.8") def test_new_cves_do_not_block_they_are_reported(self): verdict = judge(_answer(closed=[CLOSED], new=[NEW]), "13.7", "13.7") assert verdict.improved assert verdict.new == [NEW] def test_new_cves_alone_are_no_improvement(self): assert not judge(_answer(new=[NEW]), "13.7", "13.7").improved @pytest.mark.parametrize("severity", ["medium", "low", "unknown"]) def test_only_critical_and_high_count(self, severity): """kb-server already filters; a weaker one slipping through moves nothing.""" closed = dict(CLOSED, severity=severity) assert not judge(_answer(closed=[closed]), "13.7", "13.7").improved def test_a_closed_cve_without_a_fix_is_not_a_patch(self): """Gone without a fixed version means the data changed, not the image.""" closed = dict(CLOSED, fixed="") assert not judge(_answer(closed=[closed]), "13.7", "13.7").improved def test_an_older_base_os_is_a_regression_whatever_else_closed(self): verdict = judge(_answer(closed=[CLOSED]), "13.7", "13.7", base_os=(PINNED_BASE, OLDER_BASE)) assert not verdict.improved assert verdict.base_os == (PINNED_BASE, OLDER_BASE) assert verdict.regressions == [f"base-os goes back: {PINNED_BASE} → {OLDER_BASE}"] def test_a_newer_base_os_is_no_regression(self): verdict = judge(_answer(closed=[CLOSED]), "13.7", "13.7", base_os=(OLDER_BASE, PINNED_BASE)) assert verdict.improved and verdict.regressions == [] def test_the_same_base_os_is_not_recorded(self): verdict = judge(_answer(closed=[CLOSED]), "13.7", "13.7", base_os=(PINNED_BASE,) * 2) assert verdict.improved and verdict.base_os is None @pytest.mark.parametrize( "current,candidate,back", [ ("13-20261108.9", "13-20261108.10", False), ("13-20261108.2", "13-20261108.1", True), ("13-20261108.1", "12-20261201.1", True), ], ) def test_base_os_versions_compare_as_numbers(self, current, candidate, back): verdict = judge(_answer(closed=[CLOSED]), "13.7", "13.7", base_os=(current, candidate)) assert bool(verdict.regressions) is back @pytest.mark.parametrize("odd", ["dev", "", "13-2026110.1", "latest"]) def test_a_base_os_version_that_cannot_be_compared_is_refused(self, odd): """No pin moves on a guess: an unreadable base-os is no verdict.""" with pytest.raises(ValueError): judge(_answer(closed=[CLOSED]), "13.7", "13.7", base_os=(PINNED_BASE, odd)) def test_a_new_cve_with_a_fix_is_a_regression(self): """The CVE gate's rule: it can be closed, so the candidate is not better.""" verdict = judge(_answer(closed=[CLOSED], new=[ZLIB]), "13.7", "13.7") assert not verdict.improved assert verdict.new == [ZLIB] assert verdict.regressions == [ "CVE-2026-76844 (zlib, high) is new and has a fix: 1:1.3.dfsg+really1.3.1-1+deb13u1" ] def test_a_point_release_counts_only_forwards(self): verdict = judge(_answer(), "13.8", "13.7") assert not verdict.improved assert verdict.point_release is None assert verdict.regressions == ["Debian goes back: 13.8 → 13.7"] def test_a_debian_version_that_is_no_number_still_counts_as_a_point_release(self): verdict = judge(_answer(), "trixie/sid", "13.1") assert verdict.improved and verdict.point_release == ("trixie/sid", "13.1") def test_a_changed_component_is_an_improvement_on_its_own(self): """A new PostgreSQL minor moves the pin even when no CVE closed.""" change = "PostgreSQL 17.11-0+deb13u1 → 17.12-0+deb13u1" verdict = judge(_answer(), "13.7", "13.7", changes=[change]) assert verdict.improved assert verdict.changes == [change] def test_a_changed_component_on_an_older_base_os_is_no_improvement(self): """netork#2523: 17.12 on base-os 13-20261025.1 against a pin on 13-20261108.1.""" verdict = judge( _answer(new=[ZLIB]), "13.7", "13.7", base_os=(PINNED_BASE, OLDER_BASE), changes=["PostgreSQL 17.11-0+deb13u1 → 17.12-0+deb13u1"], ) assert not verdict.improved assert verdict.regressions[0] == f"base-os goes back: {PINNED_BASE} → {OLDER_BASE}" assert "CVE-2026-76844" in verdict.regressions[1] class TestVerify: def _deps( self, answer=None, error=None, versions=("13.7", "13.7"), failing=((), ()), bases=(PINNED_BASE, PINNED_BASE), ): calls = {"sbom": [], "match": []} def sbom(ref): calls["sbom"].append(ref) return {"ArtifactName": ref} def match(current, candidate): calls["match"].append((current, candidate)) if error: raise error return answer debian = dict(zip(("cur", "cand"), versions, strict=True)) cis = dict(zip(("cur", "cand"), map(set, failing), strict=True)) base = dict(zip(("cur", "cand"), bases, strict=True)) return calls, { "sbom": sbom, "match": match, "debian_version": lambda ref: debian["cur" if ref == "cur" else "cand"], "cis_failing": lambda ref: cis["cur" if ref == "cur" else "cand"], "base_os_version": lambda ref: base["cur" if ref == "cur" else "cand"], } def test_both_go_to_the_matcher_in_one_call(self): calls, seams = self._deps(answer=_answer(closed=[CLOSED])) verdict = verify("cur", "cand", **seams) assert calls["sbom"] == ["cur", "cand"] assert calls["match"] == [({"ArtifactName": "cur"}, {"ArtifactName": "cand"})] assert verdict.improved def test_a_matcher_that_cannot_be_asked_moves_nothing(self): _, seams = self._deps(error=OSError("connection refused")) verdict = verify("cur", "cand", **seams) assert not verdict.improved assert "connection refused" in verdict.error def test_even_a_point_release_waits_for_the_matcher(self): _, seams = self._deps(error=OSError("down"), versions=("13.7", "13.8")) assert not verify("cur", "cand", **seams).improved def test_a_cis_control_the_pin_fails_and_the_candidate_passes_is_an_improvement(self): _, seams = self._deps( answer=_answer(), failing=({"docker:4.1", "docker:4.8"}, {"docker:4.8"}) ) verdict = verify("cur", "cand", **seams) assert verdict.improved assert verdict.cis_closed == ["docker:4.1"] def test_a_cis_control_the_candidate_newly_fails_is_no_improvement(self): """The gate in check_image.sh stops such a build before; here it closes nothing.""" _, seams = self._deps(answer=_answer(), failing=((), {"docker:4.8"})) assert not verify("cur", "cand", **seams).improved def test_a_cis_measurement_that_cannot_be_taken_moves_nothing(self): _, seams = self._deps(answer=_answer(closed=[CLOSED])) def broken(ref): raise subprocess.CalledProcessError(1, "docker") seams["cis_failing"] = broken assert not verify("cur", "cand", **seams).improved def test_the_2523_candidate_is_regressed_not_improved(self): """A new component goes through the verdict, which sees the older base-os.""" _, seams = self._deps(answer=_answer(new=[ZLIB]), bases=(PINNED_BASE, OLDER_BASE)) verdict = verify("cur", "cand", changes=["PostgreSQL 17.11 → 17.12"], **seams) assert not verdict.improved and verdict.error is None assert verdict.base_os == (PINNED_BASE, OLDER_BASE) assert verdict.changes == ["PostgreSQL 17.11 → 17.12"] def test_a_changed_component_on_the_same_base_is_an_improvement(self): _, seams = self._deps(answer=_answer()) assert verify("cur", "cand", changes=["PostgreSQL 17.11 → 17.12"], **seams).improved @pytest.mark.parametrize( "broken", [ subprocess.CalledProcessError(1, "docker"), ValueError("no VERSION in /etc/netork/base-os"), ], ) def test_a_base_os_that_cannot_be_read_moves_nothing(self, broken): _, seams = self._deps(answer=_answer(closed=[CLOSED])) def read(ref): raise broken seams["base_os_version"] = read verdict = verify("cur", "cand", changes=["x 1 → 2"], **seams) assert not verdict.improved and verdict.error def test_the_report_names_what_closed_and_what_is_new(): verdict = judge(_answer(closed=[CLOSED], new=[NEW]), "13.7", "13.8", ["docker:4.1"]) text = render(verdict, "cur", "cand") assert "CVE-2026-1111" in text and "openssl" in text and "3.5.1-1+deb13u1" in text assert "CVE-2026-2222" in text and "nginx" in text assert "13.7 → 13.8" in text assert "docker:4.1" in text assert "improved" in text.lower() def test_the_verdict_is_json_for_the_workflow(): verdict = judge(_answer(closed=[CLOSED]), "13.7", "13.7") data = json.loads(verdict.to_json()) assert data["improved"] is True assert data["closed"][0]["cve"] == "CVE-2026-1111" def test_the_report_leads_with_the_regression(): verdict = judge( _answer(closed=[CLOSED], new=[ZLIB]), "13.7", "13.7", base_os=(PINNED_BASE, OLDER_BASE), changes=["PostgreSQL 17.11 → 17.12"], ) text = render(verdict, "cur", "cand") first, rest = text.split("\n", 1) assert first.startswith("**Regressed** (the pin stays):") assert "improved" not in first.lower() regressions = rest.index("### Regressions") assert regressions < rest.index("### Closed critical/high") assert f"- base-os goes back: {PINNED_BASE} → {OLDER_BASE}" in text assert "- CVE-2026-76844 (zlib, high) is new and has a fix" in text assert "- PostgreSQL 17.11 → 17.12" in text def test_the_report_names_the_change_and_the_base_os_move(): verdict = judge( _answer(), "13.7", "13.7", base_os=(OLDER_BASE, PINNED_BASE), changes=["valkey 8.1.9 → 8.1.10"], ) text = render(verdict, "cur", "cand") assert text.startswith("**Improved**") assert "- valkey 8.1.9 → 8.1.10" in text assert f"base-os: {OLDER_BASE} → {PINNED_BASE}" in text def test_the_regressions_are_json_for_the_workflow(): data = json.loads(judge(_answer(), "13.8", "13.7").to_json()) assert data["improved"] is False assert data["regressions"] == ["Debian goes back: 13.8 → 13.7"] def test_the_change_lines_come_from_a_file_without_blanks_or_bullets(tmp_path): path = tmp_path / "changes.txt" path.write_text("SIGNAL_CLI 0.14.8 → 0.14.9\n\n- REST_API 0.95 → 0.96\n \n") assert read_changes(path) == ["SIGNAL_CLI 0.14.8 → 0.14.9", "REST_API 0.95 → 0.96"] @pytest.mark.parametrize( "record,version", [ ("VERSION=13-20261108.1\nSNAPSHOT=20261108T040000Z\n", "13-20261108.1"), ("SNAPSHOT=20261108T040000Z\nVERSION=13-20261025.1", "13-20261025.1"), ], ) def test_the_base_os_version_is_read_from_its_record(record, version): assert base_os_version(record) == version def test_a_record_without_a_version_is_refused(): with pytest.raises(ValueError): base_os_version("SNAPSHOT=20261108T040000Z\n") def test_a_verdict_has_no_surprise_fields(): assert set(Verdict.__dataclass_fields__) == { "improved", "closed", "new", "point_release", "cis_closed", "counts", "error", "base_os", "changes", "regressions", }