Skip to content

Commit 6204ab9

Browse files
committed
gh-105704: Disallow square brackets ( and ) in domain names for parsed URLs
1 parent d23f570 commit 6204ab9

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

Lib/test/test_urlparse.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,6 +1412,20 @@ def test_invalid_bracketed_hosts(self):
14121412
self.assertRaises(ValueError, urllib.parse.urlsplit, 'Scheme://user@[0439:23af::2309::fae7:1234]/Path?Query')
14131413
self.assertRaises(ValueError, urllib.parse.urlsplit, 'Scheme://user@[0439:23af:2309::fae7:1234:2342:438e:192.0.2.146]/Path?Query')
14141414
self.assertRaises(ValueError, urllib.parse.urlsplit, 'Scheme://user@]v6a.ip[/Path')
1415+
self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://prefix.[v6a.ip]')
1416+
self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://[v6a.ip].suffix')
1417+
self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://prefix.[v6a.ip]/')
1418+
self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://[v6a.ip].suffix/')
1419+
self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://prefix.[v6a.ip]?')
1420+
self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://[v6a.ip].suffix?')
1421+
self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://prefix.[::1]')
1422+
self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://[::1].suffix')
1423+
self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://prefix.[::1]/')
1424+
self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://[::1].suffix/')
1425+
self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://prefix.[::1]?')
1426+
self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://[::1].suffix?')
1427+
self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://user@prefix.[v6a.ip]')
1428+
self.assertRaises(ValueError, urllib.parse.urlsplit, 'scheme://user@[v6a.ip].suffix')
14151429

14161430
def test_splitting_bracketed_hosts(self):
14171431
p1 = urllib.parse.urlsplit('scheme://user@[v6a.ip]/path?query')

Lib/urllib/parse.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,23 @@ def _checknetloc(netloc):
439439
raise ValueError("netloc '" + netloc + "' contains invalid " +
440440
"characters under NFKC normalization")
441441

442+
def _check_bracketed_netloc(netloc):
443+
# Note that this function must mirror the splitting
444+
# done in NetlocResultMixins._hostinfo().
445+
hostname_and_port = netloc.rpartition('@')[2]
446+
before_bracket, have_open_br, bracketed = hostname_and_port.partition('[')
447+
if have_open_br:
448+
# No data is allowed before a bracket.
449+
if before_bracket:
450+
raise ValueError("Invalid IPv6 URL")
451+
hostname, _, port = bracketed.partition(']')
452+
# No data is allowed after the bracket but before the port delimiter.
453+
if port and not port.startswith(":"):
454+
raise ValueError("Invalid IPv6 URL")
455+
else:
456+
hostname, _, port = hostname_and_port.partition(':')
457+
_check_bracketed_host(hostname)
458+
442459
# Valid bracketed hosts are defined in
443460
# https://www.rfc-editor.org/rfc/rfc3986#page-49 and https://url.spec.whatwg.org/
444461
def _check_bracketed_host(hostname):
@@ -505,8 +522,7 @@ def _urlsplit(url, scheme=None, allow_fragments=True):
505522
(']' in netloc and '[' not in netloc)):
506523
raise ValueError("Invalid IPv6 URL")
507524
if '[' in netloc and ']' in netloc:
508-
bracketed_host = netloc.partition('[')[2].partition(']')[0]
509-
_check_bracketed_host(bracketed_host)
525+
_check_bracketed_netloc(netloc)
510526
if allow_fragments and '#' in url:
511527
url, fragment = url.split('#', 1)
512528
if '?' in url:
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
When using ``urllib.parse.urlsplit()`` and ``urlparse()`` host parsing would
2+
not reject domain names containing square brackets (``[`` and ``]``). Square
3+
brackets are only valid for IPv6 and IPvFuture hosts according to `RFC 3986
4+
Section 3.2.2 <https://www.rfc-editor.org/rfc/rfc3986#section-3.2.2>`__.

0 commit comments

Comments
 (0)