Skip to content

get_package_data: keep the index URL's path - #255

Open
koenvincent wants to merge 1 commit into
peterbe:masterfrom
koenvincent:keep-index-url-path
Open

koenvincent wants to merge 1 commit into
peterbe:masterfrom
koenvincent:keep-index-url-path

Conversation

@koenvincent

Copy link
Copy Markdown

urljoin(index_url, "/pypi/%s/json") replaces the whole path of index_url. That is harmless for https://pypi.org/, but it breaks any index served under a path, such as an Artifactory or Nexus PyPI proxy at https://host/artifactory/api/pypi/<repo>/. The request then goes to https://host/pypi/<pkg>/json and returns 404, while https://host/artifactory/api/pypi/<repo>/pypi/<pkg>/json answers with the JSON and sha256 digests.

 def get_package_data(package, index_url, verbose=False):
-    path = "/pypi/%s/json" % package
-    url = urljoin(index_url, path)
+    base = index_url if index_url.endswith("/") else index_url + "/"
+    url = urljoin(base, "pypi/%s/json" % package)

https://pypi.org/ and https://pypi.org both still give https://pypi.org/pypi/<pkg>/json.

This is what breaks Dependabot's hash lookup for a private Python registry under a path: its helper passes the registry URL to hashin.get_package_hashes.

Tested: pytest tests (73 passed) with a new parametrized test_get_package_data_keeps_the_index_path covering both pypi.org forms and a path-prefixed index with and without a trailing slash; black --check and flake8 clean.

urljoin(index_url, "/pypi/%s/json") replaces the whole path of index_url.
That is harmless for https://pypi.org/, but an index served under a path,
such as an Artifactory or Nexus PyPI proxy at
https://host/artifactory/api/pypi/<repo>/, loses it and the request 404s.
Join a relative path onto the index URL with a trailing slash instead.

@hartwork hartwork left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@koenvincent I am happy to help but checking for a trailing slash seems like black magic and prone to error to me. Can you please come up with an alternative that is robust and self-explaining?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants