Files
kevinveenbirkenbachandClaude Opus 5.5 9a61a1b3c2 fix(registry): accept only well-formed sha256 and sha512 digests
platform_digest and manifest_digest passed the digest a registry returned straight to their callers, which name files and scratch directories after it. A digest such as ../../etc or one with a trailing newline therefore reached the filesystem (CodeQL path traversal). valid_digest now admits only sha256: or sha512: followed by lowercase hex of the matching length; platform_digest falls through to the next manifest entry on a malformed one, and manifest_digest reports it as indeterminate.

Tests: TestValidDigest covers well-formed sha256 and sha512 and rejects traversal, uppercase, a trailing newline, md5, empty, None and 42; a malformed Docker-Content-Digest is indeterminate; a malformed platform entry falls through to a valid one. The fixtures now use well-formed digests. Negative control: the new tests fail on the old code.

Security score 0 % comes from 13 MEDUSA findings on unchanged lines of the existing registry HTTP client (WEB-SSRF, MEDUSA-ACP-SCAN-028, B310); committed on the operator word, the findings get their own review task.

🎯 Fix: 92 %
💥 Break: 2 %
💸 Cost: –
⏱️ Time: –
🛡️ Security: 0 %
🏆 Score: 0
🚦 Gate: ⚠️ security 0 % < 100: ask the operator before the commit

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 16:56:23 +00:00

164 lines
6.5 KiB
Python

"""A config-blob read must separate "declares none" from "could not tell".
A sweep that reads an indeterminate answer as an absent probe reports every
image as a gap the moment the network hiccups.
"""
from __future__ import annotations
import json
import os
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path
from utils.docker import registry
from utils.docker.image import config
AMD = "sha256:" + "b" * 64
MANIFEST = {"config": {"digest": "sha256:cfg"}, "layers": [{"size": 1}]}
INDEX = {
"manifests": [{"platform": {"os": "linux", "architecture": "amd64"}, "digest": AMD}]
}
PROBE = {"Test": ["CMD-SHELL", "dig @127.0.0.1 || exit 1"]}
VOLUMES = {"/opt/ejabberd": {}}
class _Isolated:
def setUp(self) -> None:
super().setUp()
cache = tempfile.TemporaryDirectory()
self.addCleanup(cache.cleanup)
for patcher in (
mock.patch.object(registry, "_CACHE_ROOT", Path(cache.name)),
mock.patch.dict(os.environ, {"INFINITO_GHCR_MIRROR_PREFIX": ""}),
):
patcher.start()
self.addCleanup(patcher.stop)
def serve(self, manifest, blob):
"""Answer manifest requests with *manifest* and blob requests with *blob*."""
def _request(url, repo, method, accept):
if "/blobs/" in url:
return (200, {}, json.dumps(blob).encode()) if blob else None
return (200, {}, json.dumps(manifest).encode()) if manifest else None
return mock.patch.object(registry, "_request", side_effect=_request)
class TestImageHealthcheck(_Isolated, unittest.TestCase):
def test_a_declared_probe_comes_back(self) -> None:
with self.serve(MANIFEST, {"config": {"Healthcheck": PROBE}}):
self.assertEqual(config.image_healthcheck("redis", "alpine"), PROBE)
def test_an_image_without_one_answers_with_an_empty_mapping(self) -> None:
with self.serve(MANIFEST, {"config": {}}):
self.assertEqual(config.image_healthcheck("redis", "alpine"), {})
def test_an_unreachable_registry_is_indeterminate_not_absent(self) -> None:
with mock.patch.object(registry, "_request", return_value=None):
self.assertIsNone(config.image_healthcheck("redis", "alpine"))
def test_a_multi_platform_index_is_resolved_to_the_platform(self) -> None:
seen = []
def _request(url, repo, method, accept):
seen.append(url)
if "/blobs/" in url:
return (
200,
{},
json.dumps({"config": {"Healthcheck": PROBE}}).encode(),
)
body = MANIFEST if AMD.replace(":", "%3A") in url else INDEX
return (200, {}, json.dumps(body).encode())
with mock.patch.object(registry, "_request", side_effect=_request):
self.assertEqual(config.image_healthcheck("redis", "alpine"), PROBE)
self.assertTrue(any(AMD.replace(":", "%3A") in url for url in seen))
def test_an_index_without_the_platform_is_indeterminate(self) -> None:
with (
self.serve(INDEX, {"config": {}}),
mock.patch.object(config, "platform_digest", return_value=None),
):
self.assertIsNone(config.image_healthcheck("redis", "alpine"))
def test_a_determinate_answer_is_cached(self) -> None:
calls = []
def _request(url, repo, method, accept):
calls.append(url)
if "/blobs/" in url:
return (200, {}, json.dumps({"config": {}}).encode())
return (200, {}, json.dumps(MANIFEST).encode())
with mock.patch.object(registry, "_request", side_effect=_request):
config.image_healthcheck("redis", "alpine")
before = len(calls)
config.image_healthcheck("redis", "alpine")
self.assertEqual(len(calls), before, "the second read must come from cache")
class TestImageVolumes(_Isolated, unittest.TestCase):
def test_declared_paths_come_back(self) -> None:
with self.serve(MANIFEST, {"config": {"Volumes": VOLUMES}}):
self.assertEqual(config.image_volumes("ejabberd", "26.09"), VOLUMES)
def test_an_image_without_any_answers_with_an_empty_mapping(self) -> None:
with self.serve(MANIFEST, {"config": {}}):
self.assertEqual(config.image_volumes("ejabberd", "26.09"), {})
def test_an_unreachable_registry_is_indeterminate_not_absent(self) -> None:
with mock.patch.object(registry, "_request", return_value=None):
self.assertIsNone(config.image_volumes("ejabberd", "26.09"))
def test_the_two_sections_do_not_share_a_cache_entry(self) -> None:
with self.serve(MANIFEST, {"config": {"Volumes": VOLUMES}}):
self.assertEqual(config.image_volumes("ejabberd", "26.09"), VOLUMES)
self.assertEqual(config.image_healthcheck("ejabberd", "26.09"), {})
class TestProbeSource(_Isolated, unittest.TestCase):
def test_the_source_says_where_the_answer_came_from(self) -> None:
with self.serve(MANIFEST, {"config": {}}):
_found, source = config.image_healthcheck_probed("redis", "alpine")
self.assertEqual(source, "upstream")
def test_a_cached_answer_says_so(self) -> None:
with self.serve(MANIFEST, {"config": {}}):
config.image_healthcheck_probed("redis", "alpine")
_found, source = config.image_healthcheck_probed("redis", "alpine")
self.assertEqual(source, "cache")
def test_an_unreachable_registry_reports_none(self) -> None:
with mock.patch.object(registry, "_request", return_value=None):
found, source = config.image_healthcheck_probed("redis", "alpine")
self.assertIsNone(found)
self.assertEqual(source, "none")
class TestConfigBlob(_Isolated, unittest.TestCase):
def test_the_whole_blob_comes_back_with_its_history(self) -> None:
blob = {
"config": {"Env": ["PHP_VERSION=8.4.27"]},
"history": [{"created_by": "x"}],
}
with (
self.serve(INDEX, blob),
mock.patch.object(
registry, "fetch_manifest", side_effect=[INDEX, MANIFEST]
),
):
self.assertEqual(registry.config_blob("php", "8.4", "linux", "amd64"), blob)
def test_an_index_without_the_platform_is_indeterminate(self) -> None:
with self.serve(INDEX, {"config": {}}):
self.assertIsNone(registry.config_blob("php", "8.4", "linux", "riscv64"))
if __name__ == "__main__":
unittest.main()