Skip to content

Commit bf9f5ba

Browse files
MaelPicbdraco
andauthored
🐛 Raises meaningful exception when IPv6 URL is malformed (#1512)
Co-authored-by: J. Nick Koston <[email protected]>
1 parent dcc3b15 commit bf9f5ba

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

CHANGES/1512.bugfix.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Started raising a :exc:`ValueError` exception raised for corrupted
2+
IPv6 URL values.
3+
4+
These fixes the issue where exception :exc:`IndexError` was
5+
leaking from the internal code because of not being handled and
6+
transformed into a user-facing error. The problem was happening
7+
under the following conditions: empty IPv6 URL, brackets in
8+
reverse order.
9+
10+
-- by :user:`MaelPic`.

tests/test_url.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,26 @@ def test_ipv6_missing_right_bracket() -> None:
338338
URL("http://[1dec:0:0:0::1/")
339339

340340

341-
def test_ipv4_brackets_not_allowed() -> None:
342-
with pytest.raises(ValueError, match="An IPv4 address cannot be in brackets"):
343-
URL("http://[127.0.0.1]/")
341+
@pytest.mark.parametrize(
342+
"url",
343+
(
344+
"http://[]/",
345+
"http://[1]/",
346+
"http://[127.0.0.1]/",
347+
"http://]1dec:0:0:0::1[/",
348+
),
349+
ids=(
350+
"empty-IPv6-like-URL",
351+
"no-colons-in-IPv6",
352+
"IPv4-inside-brackets",
353+
"brackets-in-reversed-order",
354+
),
355+
)
356+
def test_ipv6_invalid_url(url: str) -> None:
357+
with pytest.raises(
358+
ValueError, match="The IPv6 content between brackets is not valid"
359+
):
360+
URL(url)
344361

345362

346363
def test_ipfuture_brackets_not_allowed() -> None:

yarl/_parse.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ def split_url(url: str) -> SplitURLType:
6969
# Valid bracketed hosts are defined in
7070
# https://www.rfc-editor.org/rfc/rfc3986#page-49
7171
# https://url.spec.whatwg.org/
72-
if bracketed_host[0] == "v":
72+
if bracketed_host and bracketed_host[0] == "v":
7373
if not re.match(r"\Av[a-fA-F0-9]+\..+\Z", bracketed_host):
7474
raise ValueError("IPvFuture address is invalid")
7575
elif ":" not in bracketed_host:
76-
raise ValueError("An IPv4 address cannot be in brackets")
76+
raise ValueError("The IPv6 content between brackets is not valid")
7777
if has_hash:
7878
url, _, fragment = url.partition("#")
7979
if has_question_mark:

0 commit comments

Comments
 (0)