From 80f9be7d0ba90b3a43c6ec04ca11ee3d77b79658 Mon Sep 17 00:00:00 2001 From: Wynne Pirini Date: Fri, 16 Oct 2026 18:27:10 +1300 Subject: [PATCH 1/2] fix(ns8): normalise the repository argument, and test that it cannot drift Main has been red on every push because ns8-module.yml passes `ghcr.io/${{ github.repository_owner }}/ns8-calnode` and that exact string cannot be fetched, while the image it names is present and multi-arch. THE MECHANISM, measured rather than assumed. Three spellings resolve and one does not: calnode/ns8-calnode 200 Calnode/ns8-calnode 200 ghcr.io/calnode/ns8-calnode 200 ghcr.io/Calnode/ns8-calnode 404 <- what the workflow passes With no extra leading segment the repository is matched case-insensitively, so capital C is fine. With ONE extra leading segment that segment is tolerated whatever it is, and the segments after it are matched case-sensitively: /v2/ghcr.io/calnode/ns8-calnode -> 200 /v2/quay.io/calnode/ns8-calnode -> 200 /v2/zzz.example/calnode/ns8-calnode -> 200 /v2/ghcr.io/Calnode/ns8-calnode -> 404 /v2/quay.io/Calnode/ns8-calnode -> 404 /v2/zzz.example/Calnode/ns8-calnode -> 404 Those 404s come from GitHub's edge router, not from the registry. The body is `404 page not found` in text/plain, whereas a genuinely absent repository returns JSON with MANIFEST_UNKNOWN. So nothing about the image or the label is wrong; the failure is routing, and it only appears when the caller supplies the redundant host segment. That also corrects a conclusion I got wrong twice this week. d5cc8ee lowercased the repo for the token scope and I read it as half a fix for a case problem that did not exist. The manifest endpoint is case-insensitive. The actual fault is the redundant host prefix, and it only becomes fatal once the owner is capitalised, which is what github.repository_owner hands us verbatim. THE FIX. One normaliser applied in token(), get() and get_blob(): strip an optional leading ghcr.io, then lowercase. Applied at all three so they cannot drift apart, which is how the earlier half-fix happened. Only the host this script talks to is stripped. An arbitrary dotted first segment is left alone, because a dotted owner is a legal image name and `my.org/team/image` must survive intact; rewriting it to `team/image` would be a far worse failure than the 404 being prevented. That case is tested. Four spellings now produce one outcome: calnode/ns8-calnode exit=0 Calnode/ns8-calnode exit=0 ghcr.io/calnode/ns8-calnode exit=0 ghcr.io/Calnode/ns8-calnode exit=0 ENFORCED, NOT SPOT-CHECKED, as asked. packaging/ns8/test_assert_multiarc.py runs in the validate job on every push, so a regression fails immediately instead of waiting for a release. The four-way equivalence is asserted as one invariant over four inputs, alongside the dotted-owner case, idempotence, case-insensitive host matching and that all three fetch paths call the normaliser. The live registry round-trip tests are in the same file but opt-in behind NS8_REGISTRY_TESTS=1, so CI does not depend on ghcr being up to accept a source change. Locally they pass against the real registry: 9 tests, 11.3s, all four spellings resolving to the same digest. Re-verified, unchanged from #151: module-only exit 0, --pair --expect-app-tag 0.11.1 exit 0, dead --app-repo still rejected with exit 2, missing app image still fails bounded in 2.2s, mismatched pair still exit 1, gate logic still verify. --- .github/workflows/ns8-module.yml | 5 ++ 404 | 0 packaging/ns8/assert-multiarc.py | 59 +++++++++++-- packaging/ns8/test_assert_multiarc.py | 121 ++++++++++++++++++++++++++ 4 files changed, 180 insertions(+), 5 deletions(-) create mode 100644 404 create mode 100644 packaging/ns8/test_assert_multiarc.py diff --git a/.github/workflows/ns8-module.yml b/.github/workflows/ns8-module.yml index e5c5d4a8..b6c44de5 100644 --- a/.github/workflows/ns8-module.yml +++ b/.github/workflows/ns8-module.yml @@ -34,6 +34,11 @@ jobs: run: | python3 -m py_compile imageroot/actions/configure-module/20configure imageroot/actions/get-configuration/20read python3 -m py_compile assert-multiarc.py + python3 -m py_compile test_assert_multiarc.py + # Offline tests, so a broken invariant fails on every push rather than + # waiting for a release to notice. The registry round-trip tests in the + # same file are opt-in and skipped unless NS8_REGISTRY_TESTS=1. + python3 -m unittest discover -s . -p 'test_*.py' python3 -c "import json, glob; [json.load(open(f)) for f in glob.glob('imageroot/actions/*/*.json') + glob.glob('ui/public/*.json') + ['ui/public/i18n/en/translation.json']]" bash -n imageroot/actions/configure-module/80start_services bash -n build-images.sh diff --git a/404 b/404 new file mode 100644 index 00000000..e69de29b diff --git a/packaging/ns8/assert-multiarc.py b/packaging/ns8/assert-multiarc.py index 533e7f80..c9b3f744 100644 --- a/packaging/ns8/assert-multiarc.py +++ b/packaging/ns8/assert-multiarc.py @@ -72,12 +72,59 @@ class Fail(Exception): """A check failed in a way worth reporting verbatim.""" +def normalize_repo(repo): + """Reduce any spelling of a repository to the one form this script uses. + + Two independent normalisations, because two independent things go wrong. + + 1. A leading registry host. This script builds absolute URLs itself, + `https://ghcr.io/v2/{repo}/...`, so a caller that passes + `ghcr.io/calnode/ns8-calnode` is supplying a host that does not belong in + that path at all. ghcr happens to tolerate the redundant segment, which + is exactly why it went unnoticed, but it does not tolerate it combined + with a capital: see the routing note below. + + Only the host this script actually talks to is stripped. An arbitrary + dotted first segment is NOT stripped, because a dotted owner is a legal + image name and `my.org/team/image` must survive intact. + + 2. Case. ghcr's token endpoint answers HTTP 400 for an uppercase scope, and + GitHub reports the owner as `github.repository_owner`, verbatim `Calnode`. + + ROUTING NOTE, measured against ghcr rather than assumed. Given a path with no + extra leading segment, the repository is matched case-insensitively: + + /v2/calnode/ns8-calnode/manifests/latest -> 200 + /v2/Calnode/ns8-calnode/manifests/latest -> 200 + + Given ONE extra leading segment, that segment is tolerated whatever it is, + and the segments after it are then matched case-sensitively: + + /v2/ghcr.io/calnode/ns8-calnode/manifests/latest -> 200 + /v2/quay.io/calnode/ns8-calnode/manifests/latest -> 200 + /v2/ghcr.io/Calnode/ns8-calnode/manifests/latest -> 404 + /v2/quay.io/Calnode/ns8-calnode/manifests/latest -> 404 + + Those 404s are GitHub's edge router, not the registry: the body is + `404 page not found` in text/plain, whereas a genuinely absent repository + returns a JSON MANIFEST_UNKNOWN. So the failure is routing, and it only + manifests when a caller supplies the redundant host. Which is precisely the + input this function removes. + + ns8-module.yml passes `ghcr.io/${{ github.repository_owner }}/ns8-calnode`, + so prefix plus capital-C, the one combination that fails, and that made main + red on every push while the image itself was present and multi-arch. + """ + repo = repo.strip() + host = urllib.parse.urlsplit(REGISTRY).netloc + prefix = host + "/" + if repo.lower().startswith(prefix.lower()): + repo = repo[len(prefix):] + return repo.lower() + + def token(repo): - # ghcr's token endpoint requires the repository path LOWERCASE and answers - # HTTP 400 otherwise. The owner arrives from GitHub as `github.repository_owner` - # verbatim, which is `Calnode` for this repo, so normalise here rather than - # trusting every caller to remember: a case slip here fails a correct release. - repo = repo.lower() + repo = normalize_repo(repo) if repo not in _tokens: url = f"{REGISTRY}/token?scope=repository:{repo}:pull&service=ghcr.io" with urllib.request.urlopen(url, timeout=30) as f: @@ -87,6 +134,7 @@ def token(repo): def get(repo, reference): """GET a manifest, returning (body, mediaType).""" + repo = normalize_repo(repo) req = urllib.request.Request( f"{REGISTRY}/v2/{repo}/manifests/{reference}", headers={"Authorization": f"Bearer {token(repo)}", "Accept": ACCEPT}, @@ -96,6 +144,7 @@ def get(repo, reference): def get_blob(repo, digest): + repo = normalize_repo(repo) req = urllib.request.Request( f"{REGISTRY}/v2/{repo}/blobs/{digest}", headers={"Authorization": f"Bearer {token(repo)}"}, diff --git a/packaging/ns8/test_assert_multiarc.py b/packaging/ns8/test_assert_multiarc.py new file mode 100644 index 00000000..58b8dc2c --- /dev/null +++ b/packaging/ns8/test_assert_multiarc.py @@ -0,0 +1,121 @@ +"""Tests for the repository-argument handling in assert-multiarc.py. + +Run offline, so they are safe to enforce on every push: + + python3 -m unittest discover -s packaging/ns8 -p 'test_*.py' -v + +The registry round-trip tests are opt-in, because CI should not depend on +ghcr's availability to decide whether a source change is valid: + + NS8_REGISTRY_TESTS=1 python3 -m unittest discover -s packaging/ns8 -p 'test_*.py' -v + +The four spellings below are the ones that matter. Three resolve and one does +not, and the one that does not is the exact string ns8-module.yml passes, which +made main red while the image itself was present and correct. +""" + +import importlib.util +import os +import pathlib +import unittest + +_SPEC = importlib.util.spec_from_file_location( + "assert_multiarc", + pathlib.Path(__file__).resolve().parent / "assert-multiarc.py", +) +am = importlib.util.module_from_spec(_SPEC) +_SPEC.loader.exec_module(am) + + +class NormalizeRepo(unittest.TestCase): + def test_the_four_spellings_agree(self): + """Every spelling of one repository must reduce to one canonical form. + + This is the regression. `ghcr.io/Calnode/ns8-calnode` used to 404 while + the other three resolved, because a redundant leading host segment makes + GitHub's edge router match the segments after it case-sensitively. + """ + forms = [ + "calnode/ns8-calnode", + "Calnode/ns8-calnode", + "ghcr.io/calnode/ns8-calnode", + "ghcr.io/Calnode/ns8-calnode", + ] + canonical = {am.normalize_repo(f) for f in forms} + self.assertEqual(canonical, {"calnode/ns8-calnode"}) + + def test_host_prefix_is_case_insensitive(self): + for host in ("ghcr.io", "GHCR.IO", "Ghcr.io"): + self.assertEqual( + am.normalize_repo(f"{host}/Calnode/ns8-calnode"), + "calnode/ns8-calnode", + ) + + def test_dotted_owner_is_not_mistaken_for_a_host(self): + """A dotted first segment is a legal owner, not a registry host. + + Stripping it would silently rewrite `my.org/team/image` into a different + repository, which is far worse than the 404 this function prevents. + """ + self.assertEqual(am.normalize_repo("my.org/team/image"), "my.org/team/image") + self.assertEqual(am.normalize_repo("My.Org/Team/Image"), "my.org/team/image") + + def test_two_segment_names_are_untouched_apart_from_case(self): + self.assertEqual(am.normalize_repo("Calnode/Ns8-Calnode"), "calnode/ns8-calnode") + + def test_whitespace_is_trimmed(self): + self.assertEqual(am.normalize_repo(" calnode/ns8-calnode\n"), "calnode/ns8-calnode") + + def test_normalisation_is_idempotent(self): + once = am.normalize_repo("ghcr.io/Calnode/ns8-calnode") + self.assertEqual(am.normalize_repo(once), once) + + def test_every_fetch_path_normalises(self): + """get(), get_blob() and token() must all agree, not just one of them.""" + for name in ("get", "get_blob"): + src = getattr(am, name).__doc__ or "" + self.assertIn("normalize_repo", __import__("inspect").getsource(getattr(am, name))) + del src + self.assertIn("normalize_repo", __import__("inspect").getsource(am.token)) + + +class RegistryRoundTrip(unittest.TestCase): + """Opt-in: proves the four spellings produce identical live outcomes.""" + + REPO = "calnode/ns8-calnode" + TAG = os.environ.get("NS8_REGISTRY_TEST_TAG", "latest") + + def setUp(self): + if not os.environ.get("NS8_REGISTRY_TESTS"): + self.skipTest("set NS8_REGISTRY_TESTS=1 to run against ghcr.io") + + def test_all_four_spellings_resolve_to_the_same_index(self): + bodies = {} + for form in ( + self.REPO, + "Calnode/ns8-calnode", + f"ghcr.io/{self.REPO}", + f"ghcr.io/Calnode/ns8-calnode", + ): + body, _ = am.get(form, self.TAG) + bodies[form] = body + digests = {form: body["manifests"][0]["digest"] for form, body in bodies.items()} + self.assertEqual( + len(set(digests.values())), + 1, + f"the four spellings resolved to different images: {digests}", + ) + + def test_every_spelling_passes_the_real_check(self): + for form in ( + self.REPO, + "Calnode/ns8-calnode", + f"ghcr.io/{self.REPO}", + f"ghcr.io/Calnode/ns8-calnode", + ): + with self.subTest(repo=form): + am.check_tag(form, self.TAG, ["amd64", "arm64"], False, None) + + +if __name__ == "__main__": + unittest.main() \ No newline at end of file From 8340b1328c9361b2b06e4b0bf9cce902c06c77b7 Mon Sep 17 00:00:00 2001 From: Wynne Pirini Date: Fri, 16 Oct 2026 18:42:08 +1300 Subject: [PATCH 2/2] chore: remove a stray zero-byte file named 404 Committed by accident in 80f9be7. It does not exist on main and there is no reason a file of that name should ever exist in this repository. No .gitignore rule for it, deliberately. An ignore rule would hide the whole class of mistake, which is shell output or a redirect landing in the repository root under whatever name it happened to be given. A file called 404 appearing at the root is worth seeing, not worth muting. --- 404 | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 404 diff --git a/404 b/404 deleted file mode 100644 index e69de29b..00000000