Skip to content

Commit dd954f2

Browse files
committed
Add checking for bytes URLs
1 parent c3bc170 commit dd954f2

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

Lib/test/test_urlparse.py

+5
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,11 @@ def test_invalid_bracketed_hosts(self):
11251125
urllib.parse.urlsplit(case).hostname
11261126
with self.assertRaises(ValueError):
11271127
urllib.parse.urlparse(case).hostname
1128+
bytes_case = case.encode('utf8')
1129+
with self.assertRaises(ValueError):
1130+
urllib.parse.urlsplit(bytes_case).hostname
1131+
with self.assertRaises(ValueError):
1132+
urllib.parse.urlparse(bytes_case).hostname
11281133

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

Lib/urllib/parse.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,15 @@ def _userinfo(self):
241241
def _hostinfo(self):
242242
netloc = self.netloc
243243
_, _, hostinfo = netloc.rpartition(b'@')
244-
_, have_open_br, bracketed = hostinfo.partition(b'[')
244+
bracket_prefix, have_open_br, bracketed = hostinfo.partition(b'[')
245245
if have_open_br:
246+
if bracket_prefix:
247+
raise ValueError('Invalid IPv6 URL')
246248
hostname, _, port = bracketed.partition(b']')
247-
_, _, port = port.partition(b':')
249+
_check_bracketed_host(hostname.decode(_implicit_encoding, _implicit_errors))
250+
bracket_suffix, _, port = port.partition(b':')
251+
if bracket_suffix:
252+
raise ValueError('Invalid IPv6 URL')
248253
else:
249254
hostname, _, port = hostinfo.partition(b':')
250255
if not port:

0 commit comments

Comments
 (0)