From 3af21b24c14af53b5d0a6026921361e53e2c41e2 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Sat, 28 Nov 2026 01:49:43 +0000 Subject: [PATCH 1/3] fix(sys-ctl-hlth-runtime): rebuild a cached SBOM that does not parse MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A deploy's lock phase stops hlth-runtime with SIGTERM, which can cut syft off mid-write and leave an empty or partial SBOM in the cache. The next run took any existing file as cached and crashed in _read_json with JSONDecodeError (main run 46038122549, OpenProject#1 swarm arm64). A cached SBOM now counts only when it parses; otherwise syft builds it again. 🎯 Fix: 92 % | 💥 Break: 3 % | 💸 Tokens: – | ⏱️ Velocity: – | 🛡️ Security: 100 % | 🏆 Score: 89 | 🚦 Gate: ✅ 90/5, tokens and velocity <= 100, security 100 % Created-with: https://s.veen.world/skills Co-Authored-By: Claude Opus 5.5 --- roles/sys-ctl-hlth-runtime/files/python/script.py | 14 +++++++++++++- .../sys-ctl-hlth-runtime/files/test_script.py | 10 ++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/roles/sys-ctl-hlth-runtime/files/python/script.py b/roles/sys-ctl-hlth-runtime/files/python/script.py index 76149e5cbd..7e6994e470 100755 --- a/roles/sys-ctl-hlth-runtime/files/python/script.py +++ b/roles/sys-ctl-hlth-runtime/files/python/script.py @@ -1930,6 +1930,18 @@ def _read_json(path: Path) -> dict: return json.loads(path.read_text(encoding="utf-8")) +def _readable_json(path: Path) -> bool: + """ + Returns: + whether ``path`` holds a whole JSON document + """ + try: + _read_json(path) + except (OSError, ValueError): + return False + return True + + def missing_databases(engine: str, databases: Path) -> list[str]: """Name every file of a provided database that is not there.""" return [ @@ -2030,7 +2042,7 @@ def scan( target for target in targets if target.kind == "host" - or not all(path.is_file() for path in sbom_paths(layout, target)) + or not all(_readable_json(path) for path in sbom_paths(layout, target)) ] results = run_lanes( [[sbom_step(layout, images[SBOM_BUILDER], target)] for target in pending], diff --git a/tests/unit/python/roles/sys-ctl-hlth-runtime/files/test_script.py b/tests/unit/python/roles/sys-ctl-hlth-runtime/files/test_script.py index 7b12aa9089..f0b756312a 100644 --- a/tests/unit/python/roles/sys-ctl-hlth-runtime/files/test_script.py +++ b/tests/unit/python/roles/sys-ctl-hlth-runtime/files/test_script.py @@ -1428,6 +1428,16 @@ class TestScan(TestCase): (["syft container quay.io/centos/centos:stream10: exited 2: boom"], False), ) + def test_an_sbom_cut_off_mid_write_is_built_again(self): + self.targets = [CENTOS_TARGET] + syft, cyclonedx = SCRIPT.sbom_paths(self.layout, CENTOS_TARGET) + syft.write_text("", encoding="utf-8") + cyclonedx.write_text(json.dumps(CENTOS_10), encoding="utf-8") + with contextlib.redirect_stdout(io.StringIO()): + _findings, errors, runner = self._scan(_config(targets=["container"]), {}) + self.assertEqual(errors, []) + self.assertTrue(any("docker:sha256:abc" in call for call in runner.calls)) + def test_a_failed_sbom_skips_the_sbom_readers_of_that_target(self): _findings, errors, runner = self._scan( _config(targets=["host"], engines=["syft", "grype"]), From ec660108b703dcb3aad08594272c0c30f1a8e559 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Sat, 28 Nov 2026 02:54:01 +0000 Subject: [PATCH 2/3] fix(sys-ctl-hlth-runtime): leave OS packages to Trivy when the cached CycloneDX does not parse MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With Syft disabled no SBOM is rebuilt and every target counts as SBOM-ready, so an empty or cut-off cached CycloneDX still reached trivy_os_args and crashed the scan in _read_json. An unreadable CycloneDX now counts as missing there, and Trivy keeps scanning the OS packages. Addresses the CodeRabbit review on PR #3090. 🎯 Fix: 93 % | 💥 Break: 2 % | 💸 Tokens: – | ⏱️ Velocity: – | 🛡️ Security: 100 % | 🏆 Score: 91 | 🚦 Gate: ✅ 90/5, tokens and velocity <= 100, security 100 % Created-with: https://s.veen.world/skills Co-Authored-By: Claude Opus 5.5 --- .../sys-ctl-hlth-runtime/files/python/script.py | 2 +- .../sys-ctl-hlth-runtime/files/test_script.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/roles/sys-ctl-hlth-runtime/files/python/script.py b/roles/sys-ctl-hlth-runtime/files/python/script.py index 7e6994e470..76e17d3df6 100755 --- a/roles/sys-ctl-hlth-runtime/files/python/script.py +++ b/roles/sys-ctl-hlth-runtime/files/python/script.py @@ -1528,7 +1528,7 @@ def trivy_os_args( ``until`` has not passed, else ``[]``. """ cyclonedx = sbom_paths(layout, target)[1] - if "grype" not in scanners or not cyclonedx.is_file(): + if "grype" not in scanners or not _readable_json(cyclonedx): return [] release = sbom_os(_read_json(cyclonedx)) for entry in config.trivy_unmatched_os: diff --git a/tests/unit/python/roles/sys-ctl-hlth-runtime/files/test_script.py b/tests/unit/python/roles/sys-ctl-hlth-runtime/files/test_script.py index f0b756312a..789101021f 100644 --- a/tests/unit/python/roles/sys-ctl-hlth-runtime/files/test_script.py +++ b/tests/unit/python/roles/sys-ctl-hlth-runtime/files/test_script.py @@ -1438,6 +1438,22 @@ class TestScan(TestCase): self.assertEqual(errors, []) self.assertTrue(any("docker:sha256:abc" in call for call in runner.calls)) + def test_without_syft_an_unreadable_sbom_keeps_the_os_packages_in_trivy(self): + self.targets = [CENTOS_TARGET] + SCRIPT.sbom_paths(self.layout, CENTOS_TARGET)[1].write_text( + "", encoding="utf-8" + ) + _findings, errors, runner = self._scan( + _config( + targets=["container"], + engines=["trivy", "grype"], + trivy_unmatched_os=UNMATCHED_OS, + ), + {}, + ) + (trivy,) = [call for call in runner.calls if "--image-src" in call] + self.assertEqual((errors, "--pkg-types" in trivy), ([], False)) + def test_a_failed_sbom_skips_the_sbom_readers_of_that_target(self): _findings, errors, runner = self._scan( _config(targets=["host"], engines=["syft", "grype"]), From 301f3c519e974081ff7cc75956f30c96b9a8b341 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Sat, 28 Nov 2026 10:43:29 +0000 Subject: [PATCH 3/3] fix(sys-ctl-hlth-runtime): treat a too-deep SBOM as unreadable and read the CycloneDX once MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CodeRabbit round 2 on #3090: UNREADABLE_JSON now also catches RecursionError, so a deeply nested SBOM counts as unreadable instead of crashing the scan, and trivy_os_args reads the CycloneDX SBOM once instead of checking it and then reading it again. The unreadable-SBOM test covers an empty and a too-deep document. 🎯 Fix: 94 % 💥 Break: 2 % 💸 Tokens: – ⏱️ Velocity: – 🛡️ Security: 100 % 🏆 Score: 92 🚦 Gate: ✅ 90/5, tokens and velocity <= 100, security 100 % Created-with: https://s.veen.world/skills Co-Authored-By: Claude Opus 5.5 --- .../files/python/script.py | 14 +++++++--- .../sys-ctl-hlth-runtime/files/test_script.py | 28 ++++++++++--------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/roles/sys-ctl-hlth-runtime/files/python/script.py b/roles/sys-ctl-hlth-runtime/files/python/script.py index 76e17d3df6..7b3c827841 100755 --- a/roles/sys-ctl-hlth-runtime/files/python/script.py +++ b/roles/sys-ctl-hlth-runtime/files/python/script.py @@ -1527,10 +1527,13 @@ def trivy_os_args( names an OS release of :attr:`ScanConfig.trivy_unmatched_os` whose ``until`` has not passed, else ``[]``. """ - cyclonedx = sbom_paths(layout, target)[1] - if "grype" not in scanners or not _readable_json(cyclonedx): + if "grype" not in scanners: return [] - release = sbom_os(_read_json(cyclonedx)) + try: + cyclonedx = _read_json(sbom_paths(layout, target)[1]) + except UNREADABLE_JSON: + return [] + release = sbom_os(cyclonedx) for entry in config.trivy_unmatched_os: if (entry.name, entry.version) == release and today <= entry.until: print( @@ -1930,6 +1933,9 @@ def _read_json(path: Path) -> dict: return json.loads(path.read_text(encoding="utf-8")) +UNREADABLE_JSON = (OSError, ValueError, RecursionError) + + def _readable_json(path: Path) -> bool: """ Returns: @@ -1937,7 +1943,7 @@ def _readable_json(path: Path) -> bool: """ try: _read_json(path) - except (OSError, ValueError): + except UNREADABLE_JSON: return False return True diff --git a/tests/unit/python/roles/sys-ctl-hlth-runtime/files/test_script.py b/tests/unit/python/roles/sys-ctl-hlth-runtime/files/test_script.py index 789101021f..d366c9ab0e 100644 --- a/tests/unit/python/roles/sys-ctl-hlth-runtime/files/test_script.py +++ b/tests/unit/python/roles/sys-ctl-hlth-runtime/files/test_script.py @@ -1440,19 +1440,21 @@ class TestScan(TestCase): def test_without_syft_an_unreadable_sbom_keeps_the_os_packages_in_trivy(self): self.targets = [CENTOS_TARGET] - SCRIPT.sbom_paths(self.layout, CENTOS_TARGET)[1].write_text( - "", encoding="utf-8" - ) - _findings, errors, runner = self._scan( - _config( - targets=["container"], - engines=["trivy", "grype"], - trivy_unmatched_os=UNMATCHED_OS, - ), - {}, - ) - (trivy,) = [call for call in runner.calls if "--image-src" in call] - self.assertEqual((errors, "--pkg-types" in trivy), ([], False)) + for name, content in (("empty", ""), ("too deep", "[" * 100_000)): + with self.subTest(name): + SCRIPT.sbom_paths(self.layout, CENTOS_TARGET)[1].write_text( + content, encoding="utf-8" + ) + _findings, errors, runner = self._scan( + _config( + targets=["container"], + engines=["trivy", "grype"], + trivy_unmatched_os=UNMATCHED_OS, + ), + {}, + ) + (trivy,) = [call for call in runner.calls if "--image-src" in call] + self.assertEqual((errors, "--pkg-types" in trivy), ([], False)) def test_a_failed_sbom_skips_the_sbom_readers_of_that_target(self): _findings, errors, runner = self._scan(