Skip to content

Commit d4d195a

Browse files
committed
Refactor link hash checking
1 parent 84d3591 commit d4d195a

File tree

7 files changed

+22
-26
lines changed

7 files changed

+22
-26
lines changed

src/pip/_internal/cli/req_command.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ def get_requirements(
416416
use_pep517=options.use_pep517,
417417
user_supplied=True,
418418
config_settings=getattr(options, "config_settings", None),
419+
verify_link_hash=True,
419420
)
420421
requirements.append(req_to_add)
421422

src/pip/_internal/index/package_finder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,7 @@ def find_requirement(
904904
Returns a InstallationCandidate if found,
905905
Raises DistributionNotFound or BestVersionAlreadyInstalled otherwise
906906
"""
907-
hashes = req.hashes(trust_internet=False)
907+
hashes = req.hashes()
908908
best_candidate_result = self.find_best_candidate(
909909
req.name,
910910
specifier=req.specifier,

src/pip/_internal/operations/prepare.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ def _get_linked_req_hashes(self, req: InstallRequirement) -> Hashes:
332332
# (For example, we can raise VcsHashUnsupported for a VCS URL
333333
# rather than HashMissing.)
334334
if not self.require_hashes:
335-
return req.hashes(trust_internet=True)
335+
return req.hashes()
336336

337337
# We could check these first 2 conditions inside unpack_url
338338
# and save repetition of conditions, but then we would
@@ -355,7 +355,7 @@ def _get_linked_req_hashes(self, req: InstallRequirement) -> Hashes:
355355
# shim it with a facade object that will provoke hash
356356
# computation and then raise a HashMissing exception
357357
# showing the user what the hash should be.
358-
return req.hashes(trust_internet=False) or MissingHashes()
358+
return req.hashes() or MissingHashes()
359359

360360
def _fetch_metadata_only(
361361
self,

src/pip/_internal/req/constructors.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
InstallRequirement.
99
"""
1010

11+
import copy
1112
import logging
1213
import os
1314
import re
@@ -385,15 +386,25 @@ def install_req_from_line(
385386
line_source: Optional[str] = None,
386387
user_supplied: bool = False,
387388
config_settings: Optional[Dict[str, Union[str, List[str]]]] = None,
389+
verify_link_hash: bool = False,
388390
) -> InstallRequirement:
389391
"""Creates an InstallRequirement from a name, which might be a
390392
requirement, directory containing 'setup.py', filename, or URL.
391393
392394
:param line_source: An optional string describing where the line is from,
393395
for logging purposes in case of an error.
396+
:param verify_link_hash: If True, consider the downloaded resource has to be
397+
hash checked against the hash that was provided in the URL, in addition to
398+
the hashes provided via hash_options.
394399
"""
395400
parts = parse_req_from_line(name, line_source)
396401

402+
#
403+
if parts.link and parts.link.hash and verify_link_hash:
404+
assert parts.link.hash_name
405+
hash_options = copy.deepcopy(hash_options) or {}
406+
hash_options.setdefault(parts.link.hash_name, []).append(parts.link.hash)
407+
397408
return InstallRequirement(
398409
parts.requirement,
399410
comes_from,
@@ -483,6 +494,7 @@ def install_req_from_parsed_requirement(
483494
constraint=parsed_req.constraint,
484495
line_source=parsed_req.line_source,
485496
user_supplied=user_supplied,
497+
verify_link_hash=True,
486498
)
487499
return req
488500

src/pip/_internal/req/req_install.py

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -271,26 +271,9 @@ def has_hash_options(self) -> bool:
271271
"""
272272
return bool(self.hash_options)
273273

274-
def hashes(self, trust_internet: bool = True) -> Hashes:
275-
"""Return a hash-comparer that considers my option- and URL-based
276-
hashes to be known-good.
277-
278-
Hashes in URLs--ones embedded in the requirements file, not ones
279-
downloaded from an index server--are almost peers with ones from
280-
flags. They satisfy --require-hashes (whether it was implicitly or
281-
explicitly activated) but do not activate it. md5 and sha224 are not
282-
allowed in flags, which should nudge people toward good algos. We
283-
always OR all hashes together, even ones from URLs.
284-
285-
:param trust_internet: Whether to trust URL-based (#md5=...) hashes
286-
downloaded from the internet, as by populate_link()
287-
288-
"""
289-
good_hashes = self.hash_options.copy()
290-
link = self.link if trust_internet else self.original_link
291-
if link and link.hash:
292-
good_hashes.setdefault(link.hash_name, []).append(link.hash)
293-
return Hashes(good_hashes)
274+
def hashes(self) -> Hashes:
275+
"""Return a hash-comparer that considers my option-hashes to be known-good."""
276+
return Hashes(self.hash_options)
294277

295278
def from_path(self) -> Optional[str]:
296279
"""Format a nice indicator to show where this "comes from" """

src/pip/_internal/resolution/resolvelib/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def empty(cls) -> "Constraint":
3434
@classmethod
3535
def from_ireq(cls, ireq: InstallRequirement) -> "Constraint":
3636
links = frozenset([ireq.link]) if ireq.link else frozenset()
37-
return Constraint(ireq.specifier, ireq.hashes(trust_internet=False), links)
37+
return Constraint(ireq.specifier, ireq.hashes(), links)
3838

3939
def __bool__(self) -> bool:
4040
return bool(self.specifier) or bool(self.hashes) or bool(self.links)
@@ -43,7 +43,7 @@ def __and__(self, other: InstallRequirement) -> "Constraint":
4343
if not isinstance(other, InstallRequirement):
4444
return NotImplemented
4545
specifier = self.specifier & other.specifier
46-
hashes = self.hashes & other.hashes(trust_internet=False)
46+
hashes = self.hashes & other.hashes()
4747
links = self.links
4848
if other.link:
4949
links = links.union([other.link])

src/pip/_internal/resolution/resolvelib/factory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def _iter_found_candidates(
248248
for ireq in ireqs:
249249
assert ireq.req, "Candidates found on index must be PEP 508"
250250
specifier &= ireq.req.specifier
251-
hashes &= ireq.hashes(trust_internet=False)
251+
hashes &= ireq.hashes()
252252
extras |= frozenset(ireq.extras)
253253

254254
def _get_installed_candidate() -> Optional[Candidate]:

0 commit comments

Comments
 (0)