Conversation
LinkHash.find_hash_url_fragment() searched the whole URL for [#&](algo)=value, so a query-string parameter named after a hash algorithm was treated as a pinned hash. _clean_link() already reads only the fragment, so the same URL counted as hashed for pinning and as unhashed for link equivalence. Search urlsplit(url).fragment instead, anchoring the algorithm name to the start of the fragment or a preceding &. Every documented form keeps working, including #subdirectory=setup&sha256=... Refs pypa#14315
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Fixes #14315.
LinkHash.find_hash_url_fragment()regex-searched the whole URL for[#&](algo)=value, so a query-string parameter named after a hash algorithm wasread as a pinned hash:
This only triggers when the parameter is not the first one (
?sha256=...alonedoes not match, since the separator has to be
#or&).Two things make this a divergence rather than a feature:
_clean_link(), used to decide whether two links are equivalent, reads onlythe fragment (
urllib.parse.parse_qs(parsed.fragment)). The same URLtherefore counted as hashed for pinning and as unhashed for equivalence.
_hash_url_fragment_re;news/11936.bugfix.rstdescribesthe change that introduced
[#&]as "Fix and improve the parsing of hashesembedded in URL fragments"; and every case in
test_link_hash_parsinguses#. The&alternative covers&-separated parameters inside the fragment(
#subdirectory=setup&sha256=...), not a query.For a direct, user-supplied requirement,
InstallRequirement.hashes(trust_internet=False)includes the link's hash, so aquery value counts towards
--require-hashes. Reproduced against a locallybuilt wheel (no network):
How
LinkHash._hash_url_fragment_reanchors the algorithm name to the start of thefragment or a preceding
&((?:^|&)) instead of[#&].find_hash_url_fragment()searchesurlsplit(url).fragmentinstead of thewhole URL.
Hex/length validation is deliberately not added: the existing comment
explains why malformed digests are handled by
Hashes, and that is unchanged.This is not a change of intent
The scope was already the fragment; matching a query parameter was a side effect
of running the regex over the whole URL. See the issue for the history.
Behaviour
...#sha256=<digest>...#sha256=<digest>&subdirectory=x...#subdirectory=x&sha256=<digest>...?token=x&sha256=<digest>...?sha256=<digest>Tests
Two tests added to
tests/unit/test_link.py:test_hash_is_read_only_from_fragment— a query parameter (second and firstposition) and a path segment are not hashes. Fails on
mainbefore the change.test_hash_in_fragment_after_other_params— the fragment form still worksafter other fragment parameters.
test_link_hash_parsingintests/unit/test_collector.pyis unaffected andpasses with the patch applied.
PR Checklist:
Assisted-by: Claude