diff --git a/Lib/test/test_urlparse.py b/Lib/test/test_urlparse.py index d90131be8dcccfe..bfbff544e4d46f3 100644 --- a/Lib/test/test_urlparse.py +++ b/Lib/test/test_urlparse.py @@ -1345,6 +1345,11 @@ def test_invalid_bracketed_hosts(self): urllib.parse.urlsplit(case).hostname with self.assertRaises(ValueError): urllib.parse.urlparse(case).hostname + bytes_case = case.encode('utf8') + with self.assertRaises(ValueError): + urllib.parse.urlsplit(bytes_case).hostname + with self.assertRaises(ValueError): + urllib.parse.urlparse(bytes_case).hostname def test_splitting_bracketed_hosts(self): p1 = urllib.parse.urlsplit('scheme://user@[v6a.ip]/path?query') diff --git a/Lib/urllib/parse.py b/Lib/urllib/parse.py index 003feab396a2e42..ece30fa338aa33c 100644 --- a/Lib/urllib/parse.py +++ b/Lib/urllib/parse.py @@ -241,10 +241,15 @@ def _userinfo(self): def _hostinfo(self): netloc = self.netloc _, _, hostinfo = netloc.rpartition(b'@') - _, have_open_br, bracketed = hostinfo.partition(b'[') + bracket_prefix, have_open_br, bracketed = hostinfo.partition(b'[') if have_open_br: + if bracket_prefix: + raise ValueError('Invalid IPv6 URL') hostname, _, port = bracketed.partition(b']') - _, _, port = port.partition(b':') + _check_bracketed_host(hostname.decode(_implicit_encoding, _implicit_errors)) + bracket_suffix, _, port = port.partition(b':') + if bracket_suffix: + raise ValueError('Invalid IPv6 URL') else: hostname, _, port = hostinfo.partition(b':') if not port: