Skip to content

Commit

Permalink
Various fixes to the link hash parser
Browse files Browse the repository at this point in the history
  • Loading branch information
sbidoul committed Apr 7, 2023
1 parent 81f6a9f commit 6e5d467
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions news/11936.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix and improve the parsing of hashes embedded in URL fragments.
4 changes: 2 additions & 2 deletions src/pip/_internal/models/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ class LinkHash:
# against Hashes when hash-checking is needed. This is easier to debug than
# proactively discarding an invalid hex digest, as we handle incorrect hashes
# and malformed hashes in the same place.
r"({choices})=(.*)".format(
r"[#&]({choices})=([^&]+)".format(
choices="|".join(re.escape(hash_name) for hash_name in _SUPPORTED_HASHES)
),
)

def __post_init__(self) -> None:
assert self._hash_re.match(f"{self.name}={self.value}")
assert self._hash_re.match(f"#{self.name}={self.value}")

@classmethod
@functools.lru_cache(maxsize=None)
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/test_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,18 @@ def expand_path(path: str) -> str:
"https://pypi.org/pip-18.0.tar.gz#sha256=aa113592bbe",
LinkHash("sha256", "aa113592bbe"),
),
(
"https://pypi.org/pip-18.0.tar.gz#sha256=aa113592bbe&subdirectory=setup",
LinkHash("sha256", "aa113592bbe"),
),
(
"https://pypi.org/pip-18.0.tar.gz#subdirectory=setup&sha256=aa113592bbe",
LinkHash("sha256", "aa113592bbe"),
),
# "xsha256" is not a valid algorithm, so we discard it.
("https://pypi.org/pip-18.0.tar.gz#xsha256=aa113592bbe", None),
# Discard empty hash.
("https://pypi.org/pip-18.0.tar.gz#sha256=", None),
(
"https://pypi.org/pip-18.0.tar.gz#md5=aa113592bbe",
LinkHash("md5", "aa113592bbe"),
Expand Down

0 comments on commit 6e5d467

Please sign in to comment.