diff --git a/tests/unit/python/utils/docker/test_registry.py b/tests/unit/python/utils/docker/test_registry.py index f5724fdd2a..16120d0c0b 100644 --- a/tests/unit/python/utils/docker/test_registry.py +++ b/tests/unit/python/utils/docker/test_registry.py @@ -162,6 +162,23 @@ class TestFetchRegistryTags(unittest.TestCase): with mock.patch.object(registry.urllib.request, "urlopen", side_effect=_fake): self.assertEqual(registry.fetch_registry_tags("ghcr.io/foo/bar"), ["2.0.0"]) + def test_a_realm_other_than_https_is_never_opened(self) -> None: + for realm in ("file:///tmp/token.json", "http://169.254.169.254/token"): + challenge = {"WWW-Authenticate": f'Bearer realm="{realm}",service="reg"'} + with ( + self.subTest(realm=realm), + mock.patch.object( + registry.urllib.request, + "urlopen", + side_effect=_http_error(401, challenge), + ) as opened, + ): + self.assertEqual(registry.fetch_registry_tags("ghcr.io/foo/bar"), []) + self.assertEqual( + [call.args[0].full_url for call in opened.call_args_list], + ["https://ghcr.io/v2/foo/bar/tags/list?n=1000"], + ) + def test_pagination_follows_link(self) -> None: page1 = _Resp( headers={"Link": '; rel="next"'}, diff --git a/tests/unit/python/utils/update/test_source.py b/tests/unit/python/utils/update/test_source.py index df905623d2..d000b304d6 100644 --- a/tests/unit/python/utils/update/test_source.py +++ b/tests/unit/python/utils/update/test_source.py @@ -1,8 +1,11 @@ from __future__ import annotations +import io import tempfile import unittest import unittest.mock as mock +import urllib.error +from email.message import Message from pathlib import Path from utils.roles.mapping import ROLE_DIR_META_ADDONS, ROLE_FILE_META_SERVICES @@ -150,6 +153,25 @@ class TestCandidates(unittest.TestCase): self.assertEqual(module.candidates(entry, root), ["2.1.0"]) +class TestRegistryV2Tags(unittest.TestCase): + def test_a_realm_other_than_https_is_never_read(self) -> None: + for realm in ("file:///tmp/token.json", "http://169.254.169.254/token"): + headers = Message() + headers["WWW-Authenticate"] = f'Bearer realm="{realm}",service="reg"' + denied = urllib.error.HTTPError( + "https://reg.example/v2", 401, "err", headers, io.BytesIO(b"") + ) + with ( + self.subTest(realm=realm), + mock.patch.object(module, "get", side_effect=denied) as get, + ): + self.assertEqual(module.registry_v2_tags("reg.example/foo/bar"), []) + self.assertEqual( + [call.args[0] for call in get.call_args_list], + ["https://reg.example/v2/foo/bar/tags/list"], + ) + + class TestOutdated(unittest.TestCase): def _entry(self, root: Path): return next(e for e in module.collect_entries(root) if e.key == "app_version") diff --git a/utils/docker/registry.py b/utils/docker/registry.py index 99a8e33a82..d13ad0a55e 100644 --- a/utils/docker/registry.py +++ b/utils/docker/registry.py @@ -66,7 +66,7 @@ def _resolve(image: str) -> tuple[str, str] | None: def _bearer_token(challenge: str, repo: str) -> str | None: params = dict(_CHALLENGE_PARAM_RE.findall(challenge or "")) realm = params.get("realm") - if not realm: + if not realm or urlparse(realm).scheme != "https": return None query = {"scope": params.get("scope") or f"repository:{repo}:pull"} if params.get("service"): diff --git a/utils/update/source.py b/utils/update/source.py index c39827c8be..ac0af3e91e 100644 --- a/utils/update/source.py +++ b/utils/update/source.py @@ -35,7 +35,7 @@ import subprocess import urllib.error from dataclasses import dataclass from typing import TYPE_CHECKING, Any -from urllib.parse import urlencode +from urllib.parse import urlencode, urlparse from utils.annotations.suppress import is_suppressed_at from utils.cache.files import read_text @@ -156,7 +156,7 @@ def registry_v2_tags(image: str, match: str = "") -> list[str]: return [] fields = dict(re.findall(r'(\w+)="([^"]*)"', challenge)) realm = fields.pop("realm", "") - if not realm: + if not realm or urlparse(realm).scheme != "https": return [] query = "&".join(f"{name}={value}" for name, value in fields.items()) try: