Skip to content

Commit

Permalink
Merge pull request #11779 from sbidoul/fix-direct_url-invalid-hash-sbi
Browse files Browse the repository at this point in the history
Do not crash in presence of misformatted hash field in ``direct_url.json``
  • Loading branch information
pradyunsg committed Feb 17, 2023
1 parent 368c7b4 commit 0138bd5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions news/11773.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Do not crash in presence of misformatted hash field in ``direct_url.json``.
7 changes: 6 additions & 1 deletion src/pip/_internal/models/direct_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,12 @@ def __init__(
if hash is not None:
# Auto-populate the hashes key to upgrade to the new format automatically.
# We don't back-populate the legacy hash key.
hash_name, hash_value = hash.split("=", 1)
try:
hash_name, hash_value = hash.split("=", 1)
except ValueError:
raise DirectUrlValidationError(
f"invalid archive_info.hash format: {hash!r}"
)
if hashes is None:
hashes = {hash_name: hash_value}
elif hash_name not in hash:
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/test_direct_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ def test_parsing_validation() -> None:
match="more than one of archive_info, dir_info, vcs_info",
):
DirectUrl.from_dict({"url": "http://...", "dir_info": {}, "archive_info": {}})
with pytest.raises(
DirectUrlValidationError,
match="invalid archive_info.hash format",
):
DirectUrl.from_dict(
{"url": "http://...", "archive_info": {"hash": "sha256:aaa"}}
)


def test_redact_url() -> None:
Expand Down

0 comments on commit 0138bd5

Please sign in to comment.