From f75c6a5a6af9fbdca7fa67f4e5799047a3f0ea80 Mon Sep 17 00:00:00 2001 From: Ben Cail Date: Tue, 24 Oct 2023 11:56:13 -0400 Subject: [PATCH] Add checking for `bytes` URLs --- Lib/test/test_urlparse.py | 5 +++++ Lib/urllib/parse.py | 9 +++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_urlparse.py b/Lib/test/test_urlparse.py index f7b5a3cf47bfd3..4fe78a129de275 100644 --- a/Lib/test/test_urlparse.py +++ b/Lib/test/test_urlparse.py @@ -1425,6 +1425,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 8c4f6b4196cc86..6822f25b3ce7bb 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: