Skip to content

Commit 684521f

Browse files
authored
Merge pull request #11779 from sbidoul/fix-direct_url-invalid-hash-sbi
Do not crash in presence of misformatted hash field in ``direct_url.json``
2 parents 6a416d2 + e5c8895 commit 684521f

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

news/11773.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Do not crash in presence of misformatted hash field in ``direct_url.json``.

src/pip/_internal/models/direct_url.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,12 @@ def __init__(
108108
if hash is not None:
109109
# Auto-populate the hashes key to upgrade to the new format automatically.
110110
# We don't back-populate the legacy hash key.
111-
hash_name, hash_value = hash.split("=", 1)
111+
try:
112+
hash_name, hash_value = hash.split("=", 1)
113+
except ValueError:
114+
raise DirectUrlValidationError(
115+
f"invalid archive_info.hash format: {hash!r}"
116+
)
112117
if hashes is None:
113118
hashes = {hash_name: hash_value}
114119
elif hash_name not in hash:

tests/unit/test_direct_url.py

+7
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ def test_parsing_validation() -> None:
102102
match="more than one of archive_info, dir_info, vcs_info",
103103
):
104104
DirectUrl.from_dict({"url": "http://...", "dir_info": {}, "archive_info": {}})
105+
with pytest.raises(
106+
DirectUrlValidationError,
107+
match="invalid archive_info.hash format",
108+
):
109+
DirectUrl.from_dict(
110+
{"url": "http://...", "archive_info": {"hash": "sha256:aaa"}}
111+
)
105112

106113

107114
def test_redact_url() -> None:

0 commit comments

Comments
 (0)