Skip to content

Commit 6e5d467

Browse files
committed
Various fixes to the link hash parser
1 parent 81f6a9f commit 6e5d467

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

news/11936.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix and improve the parsing of hashes embedded in URL fragments.

src/pip/_internal/models/link.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ class LinkHash:
6161
# against Hashes when hash-checking is needed. This is easier to debug than
6262
# proactively discarding an invalid hex digest, as we handle incorrect hashes
6363
# and malformed hashes in the same place.
64-
r"({choices})=(.*)".format(
64+
r"[#&]({choices})=([^&]+)".format(
6565
choices="|".join(re.escape(hash_name) for hash_name in _SUPPORTED_HASHES)
6666
),
6767
)
6868

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

7272
@classmethod
7373
@functools.lru_cache(maxsize=None)

tests/unit/test_collector.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,18 @@ def expand_path(path: str) -> str:
10511051
"https://pypi.org/pip-18.0.tar.gz#sha256=aa113592bbe",
10521052
LinkHash("sha256", "aa113592bbe"),
10531053
),
1054+
(
1055+
"https://pypi.org/pip-18.0.tar.gz#sha256=aa113592bbe&subdirectory=setup",
1056+
LinkHash("sha256", "aa113592bbe"),
1057+
),
1058+
(
1059+
"https://pypi.org/pip-18.0.tar.gz#subdirectory=setup&sha256=aa113592bbe",
1060+
LinkHash("sha256", "aa113592bbe"),
1061+
),
1062+
# "xsha256" is not a valid algorithm, so we discard it.
1063+
("https://pypi.org/pip-18.0.tar.gz#xsha256=aa113592bbe", None),
1064+
# Discard empty hash.
1065+
("https://pypi.org/pip-18.0.tar.gz#sha256=", None),
10541066
(
10551067
"https://pypi.org/pip-18.0.tar.gz#md5=aa113592bbe",
10561068
LinkHash("md5", "aa113592bbe"),

0 commit comments

Comments
 (0)