Skip to content

Commit

Permalink
convert hashes to lowercase prior to comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
dtrodrigues committed Jun 21, 2024
1 parent 66f4a5d commit 6a9ea31
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions news/12680.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Perform hash comparisons in a case-insensitive manner.
2 changes: 1 addition & 1 deletion src/pip/_internal/utils/hashes.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(self, hashes: Optional[Dict[str, List[str]]] = None) -> None:
if hashes is not None:
for alg, keys in hashes.items():
# Make sure values are always sorted (to ease equality checks)
allowed[alg] = sorted(keys)
allowed[alg] = [k.lower() for k in sorted(keys)]
self._allowed = allowed

def __and__(self, other: "Hashes") -> "Hashes":
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -1401,7 +1401,7 @@ def test_incorrect_metadata_hash(
)
assert result.returncode != 0
expected_msg = f"""\
Expected sha256 WRONG-HASH
Expected sha256 wrong-hash
Got {real_hash}"""
assert expected_msg in result.stderr

Expand Down
12 changes: 12 additions & 0 deletions tests/functional/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,18 @@ def test_hashed_install_failure(script: PipTestEnvironment, tmpdir: Path) -> Non
assert len(result.files_created) == 0


def test_case_insensitive_hashed_install_success(
script: PipTestEnvironment, tmpdir: Path
) -> None:
"""Test that hashes that differ only by case don't halt installation."""
with requirements_file(
"simple2==1.0 --hash=sha256:9336AF72CA661E6336EB87BC7DE3E8844D853E"
"3848C2B9BBD2E8BF01DB88C2C7\n",
tmpdir,
) as reqs_file:
script.pip_install_local("-r", reqs_file.resolve())


def test_link_hash_pass_require_hashes(
script: PipTestEnvironment, shared_data: TestData
) -> None:
Expand Down

0 comments on commit 6a9ea31

Please sign in to comment.