Skip to content

Commit

Permalink
Remove URL validation from requirement parsing
Browse files Browse the repository at this point in the history
The scheme and path validation logic limits how users of the library can
provide URL support. This limitation is lifted, and dependants now need
to implement their own URL validation logic they see fit.
  • Loading branch information
uranusjr committed Mar 30, 2023
1 parent 5b34465 commit 72eb9b8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 32 deletions.
14 changes: 1 addition & 13 deletions src/packaging/requirements.py
Expand Up @@ -2,7 +2,6 @@
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
# for complete details.

import urllib.parse
from typing import Any, List, Optional, Set

from ._parser import parse_requirement as _parse_requirement
Expand Down Expand Up @@ -37,18 +36,7 @@ def __init__(self, requirement_string: str) -> None:
raise InvalidRequirement(str(e)) from e

self.name: str = parsed.name
if parsed.url:
parsed_url = urllib.parse.urlparse(parsed.url)
if parsed_url.scheme == "file":
if urllib.parse.urlunparse(parsed_url) != parsed.url:
raise InvalidRequirement("Invalid URL given")
elif not (parsed_url.scheme and parsed_url.netloc) or (
not parsed_url.scheme and not parsed_url.netloc
):
raise InvalidRequirement(f"Invalid URL: {parsed.url}")
self.url: Optional[str] = parsed.url
else:
self.url = None
self.url: Optional[str] = parsed.url or None
self.extras: Set[str] = set(parsed.extras if parsed.extras else [])
self.specifier: SpecifierSet = SpecifierSet(parsed.specifier)
self.marker: Optional[Marker] = None
Expand Down
22 changes: 3 additions & 19 deletions tests/test_requirements.py
Expand Up @@ -164,6 +164,9 @@ def test_valid_marker(self, marker: str) -> None:
[
"file:///absolute/path",
"file://.",
"gopher:/foo/com",
"file:.",
"file:/.",
],
)
def test_file_url(self, url: str) -> None:
Expand Down Expand Up @@ -503,25 +506,6 @@ def test_error_invalid_marker_with_invalid_op(self) -> None:
" ^"
)

@pytest.mark.parametrize(
"url",
[
"gopher:/foo/com",
"file:.",
"file:/.",
],
)
def test_error_on_invalid_url(self, url: str) -> None:
# GIVEN
to_parse = f"name @ {url}"

# WHEN
with pytest.raises(InvalidRequirement) as ctx:
Requirement(to_parse)

# THEN
assert "Invalid URL" in ctx.exconly()

def test_error_on_legacy_version_outside_triple_equals(self) -> None:
# GIVEN
to_parse = "name==1.0.org1"
Expand Down

0 comments on commit 72eb9b8

Please sign in to comment.