Skip to content
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
6 changes: 5 additions & 1 deletion courlan/urlutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ def extract_domain(
def _parse(url: str | SplitResult) -> SplitResult:
"Parse a string or use urllib.parse object directly."
if isinstance(url, str):
parsed_url = urlsplit(unescape(url))
try:
parsed_url = urlsplit(unescape(url))
except ValueError:
# malformed URL (e.g. bad IPv6 literal): degrade to empty parts like a hostless URL.
parsed_url = SplitResult("", "", "", "", "")
elif isinstance(url, SplitResult):
parsed_url = url
else:
Expand Down
16 changes: 16 additions & 0 deletions tests/unit_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ def test_baseurls():
assert get_base_url("example.org") == ""


def test_malformed_url_degrades():
# A malformed URL (bad IPv6 literal) made urlsplit raise a raw ValueError that
# leaked out of get_base_url/get_hostinfo; it now degrades like a hostless URL.
assert get_base_url("HTTP://[::1]&") == ""
assert get_hostinfo("HTTP://[::1]&") == (None, "")
# get_host_and_path still raises its own documented incomplete-URL error.
with pytest.raises(ValueError, match="incomplete URL"):
get_host_and_path("HTTP://[::1]&")
# valid and hostless URLs are unaffected.
assert get_base_url("https://example.org/") == "https://example.org"
assert get_hostinfo("https://example.org/path") == (
"example.org",
"https://example.org",
)


def test_fix_relative():
assert (
fix_relative_urls("https://example.org", "page.html")
Expand Down
Loading