From e395ba64456cdceb04c79bf1201aaedfb7f92fc5 Mon Sep 17 00:00:00 2001 From: Jitka Halova Date: Mon, 30 Nov 2026 21:09:36 +0100 Subject: [PATCH] Move the pull-through token into the link path pip adds ".metadata" to the end of the whole link, query included, so a "?pt=" link gave the package and its metadata the same path. The cache and the repository lookup then served one in place of the other. fixes #1428 fixes #1453 Assisted By: Claude Opus 5.5 --- CHANGES/1428.bugfix | 1 + CHANGES/1453.bugfix | 1 + pulp_python/app/models.py | 14 +-- pulp_python/app/utils.py | 8 +- .../tests/functional/api/test_full_mirror.py | 98 +++++++++++++++++-- pulp_python/tests/unit/test_pull_through.py | 3 +- 6 files changed, 99 insertions(+), 26 deletions(-) create mode 100644 CHANGES/1428.bugfix create mode 100644 CHANGES/1453.bugfix diff --git a/CHANGES/1428.bugfix b/CHANGES/1428.bugfix new file mode 100644 index 00000000..e9df6c82 --- /dev/null +++ b/CHANGES/1428.bugfix @@ -0,0 +1 @@ +Fixed pull-through links serving a package in place of its metadata, or the other way around. Pull-through links have a new format, refresh cached simple index pages to replace old links. diff --git a/CHANGES/1453.bugfix b/CHANGES/1453.bugfix new file mode 100644 index 00000000..e9df6c82 --- /dev/null +++ b/CHANGES/1453.bugfix @@ -0,0 +1 @@ +Fixed pull-through links serving a package in place of its metadata, or the other way around. Pull-through links have a new format, refresh cached simple index pages to replace old links. diff --git a/pulp_python/app/models.py b/pulp_python/app/models.py index 79077c98..6cf6982c 100644 --- a/pulp_python/app/models.py +++ b/pulp_python/app/models.py @@ -35,7 +35,6 @@ from .provenance import Provenance from .utils import ( - PULL_THROUGH_PARAM, PYPI_LAST_SERIAL, PYPI_SERIAL_CONSTANT, artifact_to_metadata_artifact, @@ -364,15 +363,10 @@ def get_remote_artifact_url(self, relative_path=None, request=None): """Get url for remote_artifact, content app requests need a signed pull-through token.""" if request is None: return super().get_remote_artifact_url(relative_path) - if not (token := request.query.get(PULL_THROUGH_PARAM)): - return None + # Pull-through links are /, relative_path is also the package's + # metadata file when pulpcore saves the downloaded package + token = PurePath(request.path).parent.name filename = PurePath(relative_path or "").name - # pip adds ".metadata" to the end of the url (the token) instead of the url path - if token.endswith(".metadata"): - token = token.removesuffix(".metadata") - if not filename.endswith(".metadata"): - filename = f"{filename}.metadata" - setattr(self, "_real_relative_path", filename) if resolved := unsign_pull_through_token(self, token, filename): url, sha256 = resolved if sha256: @@ -382,8 +376,6 @@ def get_remote_artifact_url(self, relative_path=None, request=None): def get_remote_artifact_content_type(self, relative_path=None): """Return PythonPackageContent, except for metadata artifacts.""" - if hasattr(self, "_real_relative_path"): - relative_path = getattr(self, "_real_relative_path") if relative_path and relative_path.endswith(".metadata"): return None return PythonPackageContent diff --git a/pulp_python/app/utils.py b/pulp_python/app/utils.py index 3fac71d7..59328ff2 100644 --- a/pulp_python/app/utils.py +++ b/pulp_python/app/utils.py @@ -37,7 +37,6 @@ SIMPLE_API_VERSION = "1.1" -PULL_THROUGH_PARAM = "pt" PULL_THROUGH_SALT = "pulp_python.pull_through" PYPI_SIMPLE_V1_HTML = "application/vnd.pypi.simple.v1+html" @@ -789,8 +788,11 @@ def sign_pull_through_path(remote, filename, url, sha256=None, metadata_sha256=N data["s"] = sha256 if metadata_sha256: data["m"] = metadata_sha256 - token = _pull_through_signer(remote).sign_object(data, compress=True) - return f"{filename}?{PULL_THROUGH_PARAM}={token}" + # Uncompressed tokens never start with ".", which would make a hidden path segment + token = _pull_through_signer(remote).sign_object(data) + # The token goes in the path, so ".metadata" added by clients (pip adds it to the end of the + # whole url) gives the metadata its own path, apart from the package + return f"{token}/{filename}" def unsign_pull_through_token(remote, token, filename): diff --git a/pulp_python/tests/functional/api/test_full_mirror.py b/pulp_python/tests/functional/api/test_full_mirror.py index c7a06a64..6509e2a4 100644 --- a/pulp_python/tests/functional/api/test_full_mirror.py +++ b/pulp_python/tests/functional/api/test_full_mirror.py @@ -16,6 +16,14 @@ ) +def pull_through_token(distro, url): + """Returns the signed token of a pull-through link, None for a link to local content.""" + parent = urlsplit(url).path.rpartition("/")[0] + if parent.endswith(f"/{distro.base_path}"): + return None + return parent.rpartition("/")[2] + + def test_pull_through_install( python_bindings, python_remote_factory, python_distribution_factory, delete_orphans_pre ): @@ -54,8 +62,10 @@ def test_pull_through_simple(python_remote_factory, python_distribution_factory, assert len(project_page.packages) == 2 for package in project_page.packages: assert package.filename in PYTHON_XS_FIXTURE_CHECKSUMS - relative_path = f"{distro.base_path}/{package.filename}?pt=" - assert urljoin(pulp_content_url, relative_path) in package.url + token = pull_through_token(distro, package.url) + assert token + relative_path = f"{distro.base_path}/{token}/{package.filename}" + assert urljoin(pulp_content_url, relative_path) == package.url assert PYTHON_XS_FIXTURE_CHECKSUMS[package.filename] == package.digests["sha256"] @@ -134,7 +144,7 @@ def test_pull_through_with_repo( url = urljoin(distro.base_url, "simple/aiohttp/") project_page = ProjectPage.from_response(requests.get(url), "aiohttp") for package in sample(project_page.packages, 3): - assert "?pt=" in package.url + assert pull_through_token(distro, package.url) r = requests.get(package.url) assert r.status_code == 200 @@ -172,12 +182,14 @@ def test_pull_through_local_only( url = f"{distro.base_url}simple/pulpcore/" r = requests.get(url) assert r.status_code == 200 - assert "?pt=" not in r.text + packages = ProjectPage.from_response(r, "pulpcore").packages + assert not any(pull_through_token(distro, p.url) for p in packages) url = f"{distro.base_url}simple/shelf-reader/" r = requests.get(url) assert r.status_code == 200 - assert "?pt=" in r.text + packages = ProjectPage.from_response(r, "shelf-reader").packages + assert all(pull_through_token(distro, p.url) for p in packages) url = f"{distro.base_url}simple/pulp_python/" r = requests.get(url) @@ -239,6 +251,70 @@ def test_pull_through_metadata(python_remote_factory, python_distribution_factor assert sha256(r.content).hexdigest() == package2.metadata_digests["sha256"] +@pytest.mark.parallel +def test_pull_through_metadata_cached_package(python_remote_factory, python_distribution_factory): + """ + Tests that a package and its metadata never get served for each other (#1428). + + Only reproduces #1428 with CACHE_ENABLED. + """ + remote = python_remote_factory(includes=["pytz"]) + distro = python_distribution_factory(remote=remote.pulp_href) + + url = f"{distro.base_url}simple/pytz/" + project_page = ProjectPage.from_response(requests.get(url), "pytz") + filename1 = "pytz-2023.2-py2.py3-none-any.whl" + filename2 = "pytz-2023.3-py2.py3-none-any.whl" + package1 = next(p for p in project_page.packages if p.filename == filename1) + package2 = next(p for p in project_page.packages if p.filename == filename2) + + # The package first, then its metadata the way pip asks for it + r = requests.get(package1.url) + assert sha256(r.content).hexdigest() == package1.digests["sha256"] + r = requests.get(f"{package1.url}.metadata") + assert sha256(r.content).hexdigest() == package1.metadata_digests["sha256"] + + # The metadata first, then the package + r = requests.get(f"{package2.url}.metadata") + assert sha256(r.content).hexdigest() == package2.metadata_digests["sha256"] + r = requests.get(package2.url) + assert sha256(r.content).hexdigest() == package2.digests["sha256"] + + +@pytest.mark.parallel +def test_pull_through_metadata_after_package_added( + python_bindings, + pulpcore_bindings, + python_repo_factory, + python_remote_factory, + python_distribution_factory, + monitor_task, +): + """Tests that a pull-through link serves metadata after its package is added (#1453).""" + remote = python_remote_factory(url=PYPI_URL, includes=["pip"]) + repo = python_repo_factory() + distro = python_distribution_factory(repository=repo.pulp_href, remote=remote.pulp_href) + + url = f"{distro.base_url}simple/pip/" + project_page = ProjectPage.from_response(requests.get(url), "pip") + filename = "pip-26.0.1-py3-none-any.whl" + package = next(p for p in project_page.packages if p.filename == filename) + assert package.has_metadata + + # Another client downloads the package, which adds it to the repository + r = requests.get(package.url) + assert r.status_code == 200 + for task in pulpcore_bindings.TasksApi.list(reserved_resources=repo.prn).results: + monitor_task(task.pulp_href) + repo = python_bindings.RepositoriesPythonApi.read(repo.pulp_href) + assert repo.latest_version_href[-2] == "1" + + # The first client asks for the metadata through its link, the way pip does + r = requests.get(f"{package.url}.metadata") + assert r.status_code == 200 + assert sha256(r.content).hexdigest() == package.metadata_digests["sha256"] + + @pytest.mark.parallel def test_pull_through_rejects_bad_links(python_remote_factory, python_distribution_factory): """Tests that pull-through only downloads files from links signed for the distro's remote.""" @@ -249,7 +325,9 @@ def test_pull_through_rejects_bad_links(python_remote_factory, python_distributi url = f"{distro.base_url}simple/shelf-reader/" package1, package2 = ProjectPage.from_response(requests.get(url), "shelf-reader").packages - token1 = package1.url.split("?pt=")[1] + token1 = pull_through_token(distro, package1.url) + token2 = pull_through_token(distro, package2.url) + assert token1 and token2 # Tampered token tampered = token1[:-1] + ("A" if token1[-1] != "A" else "B") @@ -257,7 +335,7 @@ def test_pull_through_rejects_bad_links(python_remote_factory, python_distributi assert r.status_code == 404 # Token used for another file - r = requests.get(package2.url.replace(package2.url.split("?pt=")[1], token1)) + r = requests.get(package2.url.replace(token2, token1)) assert r.status_code == 404 # Token used on a distribution with another remote @@ -320,7 +398,7 @@ def test_pull_through_metadata_with_repo( filename = "pip-26.0.1-py3-none-any.whl" package = next(p for p in project_page.packages if p.filename == filename) assert package.has_metadata - assert "?pt=" in package.url + assert pull_through_token(distro, package.url) # Retrieve the metadata and assert the content was not saved to the repository parts = urlsplit(package.url) @@ -331,7 +409,7 @@ def test_pull_through_metadata_with_repo( project_page = ProjectPage.from_response(requests.get(pip_url), "pip") package = next(p for p in project_page.packages if p.filename == filename) assert package.has_metadata - assert "?pt=" in package.url + assert pull_through_token(distro, package.url) # Now retrieve the package and assert the content was saved with metadata r = requests.get(package.url) @@ -344,7 +422,7 @@ def test_pull_through_metadata_with_repo( # Check the simple page is updated to point to the local repository project_page = ProjectPage.from_response(requests.get(pip_url), "pip") package = next(p for p in project_page.packages if p.filename == filename) - assert "?pt=" not in package.url + assert not pull_through_token(distro, package.url) r = requests.get(package.metadata_url) assert r.status_code == 200 assert sha256(r.content).hexdigest() == package.metadata_digests["sha256"] diff --git a/pulp_python/tests/unit/test_pull_through.py b/pulp_python/tests/unit/test_pull_through.py index c69d89f5..03f5af53 100644 --- a/pulp_python/tests/unit/test_pull_through.py +++ b/pulp_python/tests/unit/test_pull_through.py @@ -4,7 +4,6 @@ import pytest from pulp_python.app.utils import ( - PULL_THROUGH_PARAM, sign_pull_through_path, unsign_pull_through_token, ) @@ -23,7 +22,7 @@ def _remote(**kwargs): def _token(remote, **kwargs): path = sign_pull_through_path(remote, FILENAME, URL, **kwargs) - filename, token = path.split(f"?{PULL_THROUGH_PARAM}=") + token, filename = path.split("/") assert filename == FILENAME return token