Skip to content

Commit

Permalink
Add checking for bytes URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
bcail committed Nov 22, 2024
1 parent 23e23d9 commit f75c6a5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Lib/test/test_urlparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
9 changes: 7 additions & 2 deletions Lib/urllib/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit f75c6a5

Please sign in to comment.