Skip to content

Commit c7f3ced

Browse files
fix(registry): refuse an upload a registry sends to another origin before its credentials go
Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 12305b4 commit c7f3ced

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

‎src/agent_env/store/image_store/registry_api.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,10 @@ def upload(self, digest: str, path: Path) -> None:
300300
self._raise_for(opened)
301301
raise ValueError(f"{self._what}: {self._host} opened no upload for {digest} (HTTP {opened.status_code})")
302302
location = httpx.URL(f"{self._base}/").join(opened.headers["location"])
303+
base = httpx.URL(self._base)
304+
if (location.scheme, location.netloc) != (base.scheme, base.netloc):
305+
raise ValueError(f"{self._what}: {self._host} sent the upload of {digest} to {location.scheme}://"
306+
f"{location.netloc.decode()}, which its credentials don't go to")
303307
response = self._send("PUT", str(location.copy_merge_params({"digest": digest})), None, stream=False,
304308
content=path, content_type="application/octet-stream")
305309
if response.status_code != 201:

‎tst/unit/store/registry_api_test.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,3 +408,27 @@ def registries(request):
408408

409409
assert pushed == ["/v2/org/computers/blobs/uploads/u1", f"/v2/org/computers/manifests/{_sha(image)}",
410410
"/v2/org/computers/manifests/k"]
411+
412+
413+
@pytest.mark.parametrize("location", ["https://elsewhere.example/v2/org/computers/blobs/uploads/u1",
414+
"http://ghcr.io/v2/org/computers/blobs/uploads/u1"],
415+
ids=["another-host", "plain-http"])
416+
def test_an_upload_the_registry_sends_elsewhere_is_refused_before_its_credentials_go(fake_registry, location):
417+
config = b'{"architecture": "amd64"}'
418+
image = json.dumps({"mediaType": OCI_IMAGE, "config": {"digest": _sha(config)}, "layers": []}).encode()
419+
420+
def registries(request):
421+
if request.url.host == "localhost":
422+
if "/blobs/" in request.url.path:
423+
return httpx.Response(200, content=config)
424+
return httpx.Response(200, content=image, headers={"Content-Type": OCI_IMAGE})
425+
assert request.url.host == "ghcr.io" and request.url.scheme == "https", request.url
426+
if request.method == "HEAD":
427+
return httpx.Response(404)
428+
return httpx.Response(202, headers={"Location": location})
429+
430+
fake_registry(registries)
431+
432+
with pytest.raises(ValueError, match="which its credentials don't go to"):
433+
registry_api.copy_image(f"localhost:5000/agentenv-computers:k@{_sha(image)}", "ghcr.io/org/computers:k", None,
434+
RegistryAuth("ghcr.io", "bot", "t"))

0 commit comments

Comments
 (0)