Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Lib/test/test_urlparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,19 @@ def test_urlsplit_scoped_IPv6(self):
self.assertEqual(p.hostname, b"fe80::822a:a8ff:fe49:470c%tESt")
self.assertEqual(p.netloc, b'[FE80::822a:a8ff:fe49:470c%tESt]:1234')

def test_urlsplit_prevents_hostname_injection(self):
cases = [
# bpo-36338: Ensure that [ is at position 0 of hostname
'http://good.com[bad.com]',
b'http://good.com[bad.com]',
]
for case in cases:
with self.assertRaises(ValueError, msg=case):
urllib.parse.urlsplit(case)

with self.assertRaises(ValueError, msg=case):
urllib.parse.urlparse(case)

def test_urlsplit_attributes(self):
url = "HTTP://WWW.PYTHON.ORG/doc/#frag"
p = urllib.parse.urlsplit(url)
Expand Down
3 changes: 2 additions & 1 deletion Lib/urllib/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,8 @@ def urlsplit(url, scheme='', allow_fragments=True):
if url[:2] == '//':
netloc, url = _splitnetloc(url, 2)
if (('[' in netloc and ']' not in netloc) or
(']' in netloc and '[' not in netloc)):
(']' in netloc and '[' not in netloc) or
('[' in netloc and netloc.index('[') != 0)):
raise ValueError("Invalid IPv6 URL")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_splitnetloc() is called 2 times and the same code to validate the IPv6 address is duplicated, whereas you only fix one place. IMHO it would be better to move the check into _splitnetloc().

if allow_fragments and '#' in url:
url, fragment = url.split('#', 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Raise ValueError when parsing hostname with ``[`` in position > 0, e.g. ``good.com[malicious.com]``, that would otherwise return a hostname of ``malicious.com``.