Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not crash in presence of misformatted hash field in direct_url.json #11779

Merged
merged 1 commit into from
Feb 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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