From 015c6e18cbc0a4fa8fa984ddd495941d6bb9665b Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Sun, 15 Nov 2026 22:43:55 +0100 Subject: [PATCH] fix(verdict): a rebuild that regresses is never an improvement A candidate on an older base-os than the pin, with Debian's point release going back, or with a critical or high CVE the pin does not have and that has a fix (the CVE gate's rule) is now "regressed": the pin stays, whatever else it closes, and the report names each regression first. The verdict action takes the components a nightly says moved (changes); each is an improvement on its own, but only through the verdict, so a new PostgreSQL minor on an older base-os is judged instead of waved through (NetOrk/netork#2523). verdict.json carries base_os, changes and regressions. A base-os version that cannot be read is no verdict. Closes #3 --- CHANGELOG.md | 12 +++ README.md | 3 +- actions/verdict/action.yml | 15 ++- image_tools/verify.py | 171 ++++++++++++++++++++++++++---- tests/test_actions.py | 12 +++ tests/test_verify.py | 212 ++++++++++++++++++++++++++++++++++++- 6 files changed, 397 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0272c3f..d9ac994 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,18 @@ version, so a section names the commit it describes once it is merged. regression. Without a verdict, an empty `NETORK_LICENSE_KEY` included, the step fails. - `trivy_sbom` reads one platform of a multi-platform index (`--platform`). +### Fixed +- A rebuild on an older base-os than the pin was judged an improvement as soon as it closed + something else, and the nightlies that call a new component an improvement on its own + did not ask the verdict at all (#3, NetOrk/netork#2523: PostgreSQL 17.12 on base-os + 13-20261025.1 brought back two zlib CVEs the pin had closed). The verdict now calls a + candidate **regressed**, and never improved, when it sits on an older base-os, when + Debian's point release goes back, or when it brings a critical or high CVE the pin does + not have and that has a fix; the report names each regression first. The verdict action + takes the moved components (`changes`), which count as an improvement only without a + regression, and `verdict.json` carries `base_os`, `changes` and `regressions`. A base-os + version that cannot be read is no verdict. + ### Changed - The matcher's 401 and 403 now say what they mean: a licence key (`nlk_…`) where a kb API key (`nak_kb_…`) belongs, an unknown key, or a plan without `feature_match`. An empty diff --git a/README.md b/README.md index 33f8cca..a31b5a0 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ with its CIS result and SBOM attached (NetOrk/netork#1390). | **SBOM** (`image_tools/sbom.py`) | The image's CycloneDX SBOM, as Trivy reads it. | | **Signature** (`bin/sign.sh`) | A cosign signature on the reference and on every platform image behind it, with the CIS result and the SBOM as in-toto attestations. Nothing goes to a public transparency log. | | **Verification** (`bin/verify-base.sh`, `bin/verify-signature.sh`) | Every `FROM` image of a Dockerfile is pinned by digest and signed with the key in `cosign.pub`. | -| **Verdict** (`image_tools/verify.py`) | Whether a rebuild improves on the pinned image, by netOrk's matcher (kb-server), for a base's nightly. | +| **Verdict** (`image_tools/verify.py`) | Whether a rebuild improves on the pinned image, by netOrk's matcher (kb-server), for a base's nightly. A rebuild that is worse anywhere is **regressed** and never an improvement, whatever else it closes: built on an older base-os than the pin (`/etc/netork/base-os`), a Debian point release backwards, or a critical or high CVE the pin does not have and that has a fix (the CVE gate's rule). A component the nightly says moved (`changes`, a new PostgreSQL minor) is an improvement on its own, but only through the verdict: no nightly skips it (#3). | | **CVE gate** (`image_tools/cve_gate.py`) | Whether a candidate brings a critical or high CVE that the last release of the same image does not have **and** that has a fix, by the same matcher in one call. Only that blocks; a new one without a fix is listed. No verdict (an empty key, a matcher that cannot be asked) never passes. | | **Bump** (`image_tools/bump.py`) | One pull request in every repository of a `dependents.toml` that moves only the pin, and that repository's workflow started. | @@ -49,6 +49,7 @@ with its CIS result and SBOM attached (NetOrk/netork#1390). with: current: registry.netork.io/netork/base-os:pinned candidate: registry.netork.io/netork/base-os:13-20261022.3-amd64 + changes: changes.txt # optional: one moved component per line, e.g. "PostgreSQL 17.11 → 17.12" # a release's image, before the push: nothing new and fixable against the last release - name: No new fixable critical or high CVE diff --git a/actions/verdict/action.yml b/actions/verdict/action.yml index 3b2942a..5071eb1 100644 --- a/actions/verdict/action.yml +++ b/actions/verdict/action.yml @@ -1,8 +1,10 @@ name: Judge a rebuild against the pin description: >- Whether a candidate improves on the pinned image, by netOrk's matcher in - kb-server (image_tools/verify.py). Writes verdict.json and report.md, prints - the report, and fails when no verdict could be reached. The caller passes + kb-server (image_tools/verify.py). A candidate that is worse than the pin + anywhere (an older base-os, a new fixable critical or high CVE) is never an + improvement. Writes verdict.json and report.md, prints the report, and fails + when no verdict could be reached. The caller passes NETORK_LICENSE_KEY, and TRIVY_USERNAME/TRIVY_PASSWORD for the registry, as env. inputs: current: @@ -14,6 +16,12 @@ inputs: packages: description: The packages the image may hold, for the CIS part of the verdict default: packages.allow + changes: + description: >- + A file naming the components that moved, one per line (a new PostgreSQL + minor). Each is an improvement on its own, unless the candidate regressed. + A nightly passes it here instead of skipping the verdict (image-tools#3). + default: '' outputs: improved: description: '"true" when the candidate improves on the pin' @@ -27,10 +35,11 @@ runs: CURRENT: ${{ inputs.current }} CANDIDATE: ${{ inputs.candidate }} PACKAGES: ${{ inputs.packages }} + CHANGES: ${{ inputs.changes }} PYTHONPATH: ${{ github.action_path }}/../.. run: | python3 -m image_tools.verify "$CURRENT" "$CANDIDATE" --packages "$PACKAGES" \ - --json verdict.json --report report.md + --changes "$CHANGES" --json verdict.json --report report.md # Into the log: Gitea shows no step summary. cat report.md cat report.md >> "$GITHUB_STEP_SUMMARY" diff --git a/image_tools/verify.py b/image_tools/verify.py index 57783ad..0608417 100644 --- a/image_tools/verify.py +++ b/image_tools/verify.py @@ -3,17 +3,21 @@ Trivy lists both images' packages; netOrk's matcher in kb-server judges both in one call, so against one state of its data (kb-server#155). The verdict: -- **improved** when a critical or high CVE that has a fixed version is closed, - Debian made a point release (``/etc/debian_version`` differs), or the - candidate passes a CIS control the pin fails (``image_tools/cis.py``, both measured - in the same run); -- **same** otherwise, and whenever the matcher cannot be asked: no pin moves on - a guess. +- **regressed** when the candidate is worse than the pin anywhere: built on an + older base-os (``/etc/netork/base-os``), Debian's point release going back, or + a critical or high CVE the pin does not have and that has a fixed version (the + CVE gate's rule). Nothing else outweighs it (image-tools#3, netork#2523); +- **improved** otherwise when a critical or high CVE that has a fixed version is + closed, Debian made a point release, the candidate passes a CIS control the + pin fails (``image_tools/cis.py``, both measured in the same run), or the + caller names a component that moved (``--changes``: a new PostgreSQL minor); +- **same** otherwise, and whenever the matcher cannot be asked or a base-os + version cannot be read: no pin moves on a guess. -New critical or high CVEs in the candidate never block; the report lists them. +New critical or high CVEs without a fix never block; the report lists them. python -m image_tools.verify [--packages packages.allow] \ - --json verdict.json --report report.md + [--changes changes.txt] --json verdict.json --report report.md Standard library only; Trivy runs as a pinned container. """ @@ -23,6 +27,7 @@ from __future__ import annotations import argparse import json import os +import re import subprocess import urllib.error import urllib.request @@ -37,6 +42,8 @@ TRIVY_IMAGE = ( SEVERE = frozenset({"critical", "high"}) KB_URL = os.environ.get("KB_URL", "https://kb.netork.io") TIMEOUT = 300 +#: base-os versions: ``-.`` (base-os ``tools/version.py``). +_BASE_OS = re.compile(r"^(\d+)-(\d{8})\.(\d+)$") @dataclass @@ -48,6 +55,12 @@ class Verdict: cis_closed: list[str] = field(default_factory=list) counts: dict = field(default_factory=dict) error: str | None = None + #: (pinned, candidate) base-os versions, when they differ. + base_os: tuple[str, str] | None = None + #: The components the caller says moved, one line each. + changes: list[str] = field(default_factory=list) + #: Why the candidate is worse than the pin; any of them keeps the pin. + regressions: list[str] = field(default_factory=list) def to_json(self) -> str: return json.dumps(asdict(self), indent=2) @@ -57,42 +70,112 @@ def _patched(finding: dict) -> bool: return (finding.get("severity") or "").lower() in SEVERE and bool(finding.get("fixed")) +def _base_os_key(version: str) -> tuple[int, int, int]: + match = _BASE_OS.match(version.strip()) + if not match: + raise ValueError(f"not a base-os version: {version!r}") + return int(match[1]), int(match[2]), int(match[3]) + + +def _debian_key(version: str) -> tuple[int, ...] | None: + """``13.7`` as numbers; None for a Debian version that is no number (``trixie/sid``).""" + parts = version.split(".") + return tuple(map(int, parts)) if all(p.isdigit() for p in parts) else None + + +def _base_os_move(base_os: tuple[str, str] | None) -> tuple[tuple[str, str] | None, list[str]]: + """The base-os move between pin and candidate, and the regression it is, if any.""" + if base_os is None: + return None, [] + current, candidate = (v.strip() for v in base_os) + if _base_os_key(candidate) < _base_os_key(current): + return (current, candidate), [f"base-os goes back: {current} → {candidate}"] + return ((current, candidate) if current != candidate else None), [] + + +def _debian_move(current: str, candidate: str) -> tuple[tuple[str, str] | None, list[str]]: + """A point release forwards, or the regression of one backwards.""" + current, candidate = current.strip(), candidate.strip() + if current == candidate: + return None, [] + old, new = _debian_key(current), _debian_key(candidate) + if old is not None and new is not None and new < old: + return None, [f"Debian goes back: {current} → {candidate}"] + return (current, candidate), [] + + +def _fixable_regression(finding: dict) -> str: + return ( + f"{finding.get('cve')} ({finding.get('package')}, {finding.get('severity')}) " + f"is new and has a fix: {finding.get('fixed')}" + ) + + def judge( answer: dict, current_debian: str, candidate_debian: str, cis_closed: Iterable[str] = (), + *, + base_os: tuple[str, str] | None = None, + changes: Iterable[str] = (), ) -> Verdict: - """The verdict on the matcher's *answer*, both images' Debian versions and - the CIS controls the candidate passes and the pin fails.""" + """The verdict on the matcher's *answer*, both images' Debian versions, the CIS + controls the candidate passes and the pin fails, both images' *base_os* versions + (pinned, candidate) and the components the caller says moved (*changes*). + + A ValueError when a base-os version is no base-os version.""" closed = [f for f in answer.get("closed") or [] if _patched(f)] new = [f for f in answer.get("new") or [] if (f.get("severity") or "").lower() in SEVERE] - point = None - if current_debian.strip() != candidate_debian.strip(): - point = (current_debian.strip(), candidate_debian.strip()) + base_move, regressions = _base_os_move(base_os) + point, debian_back = _debian_move(current_debian, candidate_debian) + regressions += debian_back + [_fixable_regression(f) for f in new if f.get("fixed")] counts = {"current": answer.get("current", {}), "candidate": answer.get("candidate", {})} cis = sorted(cis_closed) - return Verdict(bool(closed) or point is not None or bool(cis), closed, new, point, cis, counts) + changed = [c.strip() for c in changes if c.strip()] + better = bool(closed) or point is not None or bool(cis) or bool(changed) + return Verdict( + better and not regressions, + closed, + new, + point, + cis, + counts, + base_os=base_move, + changes=changed, + regressions=regressions, + ) def verify( current: str, candidate: str, *, + changes: Iterable[str] = (), sbom: Callable[[str], dict] | None = None, match: Callable[[dict, dict], dict] | None = None, debian_version: Callable[[str], str] | None = None, cis_failing: Callable[[str], set[str]] | None = None, + base_os_version: Callable[[str], str] | None = None, ) -> Verdict: - """Judge *candidate* against *current*; never raises.""" + """Judge *candidate* against *current*, with the components the caller says + moved (*changes*); never raises.""" sbom = sbom or trivy_sbom match = match or kb_match debian_version = debian_version or debian_version_of cis_failing = cis_failing or cis_failing_of + base_os_version = base_os_version or base_os_version_of try: answer = match(sbom(current), sbom(candidate)) cis_closed = cis_failing(current) - cis_failing(candidate) - return judge(answer, debian_version(current), debian_version(candidate), cis_closed) + return judge( + answer, + debian_version(current), + debian_version(candidate), + cis_closed, + base_os=(base_os_version(current), base_os_version(candidate)), + changes=changes, + ) except (OSError, ValueError, subprocess.SubprocessError) as exc: return Verdict(False, error=f"{type(exc).__name__}: {exc}") @@ -102,12 +185,24 @@ def _row(f: dict) -> str: return f"| {f.get('cve')} | {f.get('package')} | {f.get('severity')} | {f.get('installed')} | {fixed} |" +def _headline(verdict: Verdict) -> str: + if verdict.regressions: + return "**Regressed** (the pin stays)" + return "**Improved**" if verdict.improved else "**Same** (the pin stays)" + + def render(verdict: Verdict, current: str, candidate: str) -> str: - """The pull request body: what closed, what is new, against what.""" - head = "**Improved**" if verdict.improved else "**Same** (the pin stays)" - lines = [f"{head}: `{candidate}` against the pinned `{current}`.", ""] + """The pull request body: what got worse, what moved, what closed, what is new.""" + lines = [f"{_headline(verdict)}: `{candidate}` against the pinned `{current}`.", ""] if verdict.error: lines += [f"The matcher could not be asked: {verdict.error}", ""] + if verdict.regressions: + lines += ["### Regressions", "", *(f"- {r}" for r in verdict.regressions), ""] + if verdict.changes: + lines += ["Changed components:", "", *(f"- {c}" for c in verdict.changes), ""] + if verdict.base_os and not verdict.regressions: + old, new = verdict.base_os + lines += [f"base-os: {old} → {new}", ""] if verdict.point_release: old, new = verdict.point_release lines += [f"Debian point release: {old} → {new}", ""] @@ -128,6 +223,24 @@ def render(verdict: Verdict, current: str, candidate: str) -> str: return "\n".join(lines).rstrip() + "\n" +def read_changes(path: str | Path) -> list[str]: + """The change lines a nightly hands over, one component each; blanks and a + leading ``- `` dropped.""" + lines = ( + line.strip().removeprefix("- ").strip() for line in Path(path).read_text().splitlines() + ) + return [line for line in lines if line] + + +def base_os_version(record: str) -> str: + """The ``VERSION=`` of a base-os record (``/etc/netork/base-os``); a ValueError without one.""" + for line in record.splitlines(): + key, _, value = line.partition("=") + if key.strip() == "VERSION" and value.strip(): + return value.strip() + raise ValueError("no VERSION in /etc/netork/base-os") + + def trivy_sbom(ref: str, platform: str | None = None) -> dict: # pragma: no cover - container seam """Trivy's package inventory of *ref* (no vulnerability DB); of the *platform* variant (``linux/arm64``) when *ref* is an index of several.""" @@ -225,6 +338,18 @@ def debian_version_of(ref: str) -> str: # pragma: no cover - container seam return proc.stdout.strip() +def base_os_version_of(ref: str) -> str: # pragma: no cover - container seam + """The base-os *ref* was built on, from the record every image on base-os inherits.""" + proc = subprocess.run( + ["docker", "run", "--rm", "--entrypoint", "cat", ref, "/etc/netork/base-os"], + capture_output=True, + text=True, + timeout=120, + check=True, + ) + return base_os_version(proc.stdout) + + def cis_failing_of(ref: str, packages: str = "packages.allow") -> set[str]: # pragma: no cover """The CIS controls *ref* fails, measured as image_tools/cis.py measures a build; *packages* is the image repository's allow list.""" @@ -241,17 +366,23 @@ def main() -> int: # pragma: no cover - CLI seam parser.add_argument("--json", required=True) parser.add_argument("--report", required=True) parser.add_argument("--packages", default="packages.allow") + parser.add_argument( + "--changes", + default="", + help="a file of the components that moved, one per line (an improvement on its own)", + ) args = parser.parse_args() verdict = verify( args.current, args.candidate, + changes=read_changes(args.changes) if args.changes else (), cis_failing=lambda ref: cis_failing_of(ref, args.packages), ) with open(args.json, "w") as fh: fh.write(verdict.to_json()) with open(args.report, "w") as fh: fh.write(render(verdict, args.current, args.candidate)) - print("improved" if verdict.improved else "same") + print("regressed" if verdict.regressions else "improved" if verdict.improved else "same") return 0 diff --git a/tests/test_actions.py b/tests/test_actions.py index 480ee2f..0d7a8a7 100644 --- a/tests/test_actions.py +++ b/tests/test_actions.py @@ -50,6 +50,18 @@ def test_a_verdict_that_could_not_be_reached_fails_the_step(): assert "cat report.md" in step["run"].splitlines() +def test_a_changed_component_reaches_the_verdict_instead_of_skipping_it(): + """netork#2523: a nightly that called a new package an improvement on its own never + asked the matcher, so a candidate on an older base-os went out. The change lines go + through the verdict now, which still judges the regression (image-tools#3).""" + spec = yaml.safe_load((ROOT / "actions/verdict/action.yml").read_text()) + [step] = spec["runs"]["steps"] + + assert spec["inputs"]["changes"]["default"] == "" + assert step["env"]["CHANGES"] == "${{ inputs.changes }}" + assert '--changes "$CHANGES"' in step["run"] + + def test_the_cve_gate_fails_the_step_when_it_blocks_or_cannot_judge(): """The gate's exit code is the step's: 1 for a new fixable CVE, 2 for no verdict (an empty NETORK_LICENSE_KEY included). The report reaches the log either way.""" diff --git a/tests/test_verify.py b/tests/test_verify.py index 66bf050..2c006c1 100644 --- a/tests/test_verify.py +++ b/tests/test_verify.py @@ -2,8 +2,13 @@ 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, or -Debian made a point release. Everything else is "same": the pin stays. +"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. """ @@ -15,7 +20,7 @@ import subprocess import pytest -from image_tools.verify import Verdict, judge, render, verify +from image_tools.verify import Verdict, base_os_version, judge, read_changes, render, verify CLOSED = { "cve": "CVE-2026-1111", @@ -25,6 +30,15 @@ CLOSED = { "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=()): @@ -75,9 +89,96 @@ class TestJudge: 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=((), ())): + def _deps( + self, + answer=None, + error=None, + versions=("13.7", "13.7"), + failing=((), ()), + bases=(PINNED_BASE, PINNED_BASE), + ): calls = {"sbom": [], "match": []} def sbom(ref): @@ -92,11 +193,13 @@ class TestVerify: 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): @@ -146,6 +249,39 @@ class TestVerify: 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"]) @@ -167,6 +303,71 @@ def test_the_verdict_is_json_for_the_workflow(): 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", @@ -176,4 +377,7 @@ def test_a_verdict_has_no_surprise_fields(): "cis_closed", "counts", "error", + "base_os", + "changes", + "regressions", }