mirror of
https://github.com/python/cpython.git
synced 2025-08-31 14:07:50 +00:00
gh-96035: Make urllib.parse.urlparse reject non-numeric ports (#98273)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
parent
4ec9ed8fde
commit
6f15ca8c7a
3 changed files with 18 additions and 12 deletions
|
@ -653,13 +653,16 @@ class UrlParseTestCase(unittest.TestCase):
|
||||||
"""Check handling of invalid ports."""
|
"""Check handling of invalid ports."""
|
||||||
for bytes in (False, True):
|
for bytes in (False, True):
|
||||||
for parse in (urllib.parse.urlsplit, urllib.parse.urlparse):
|
for parse in (urllib.parse.urlsplit, urllib.parse.urlparse):
|
||||||
for port in ("foo", "1.5", "-1", "0x10"):
|
for port in ("foo", "1.5", "-1", "0x10", "-0", "1_1", " 1", "1 ", "६"):
|
||||||
with self.subTest(bytes=bytes, parse=parse, port=port):
|
with self.subTest(bytes=bytes, parse=parse, port=port):
|
||||||
netloc = "www.example.net:" + port
|
netloc = "www.example.net:" + port
|
||||||
url = "http://" + netloc
|
url = "http://" + netloc
|
||||||
if bytes:
|
if bytes:
|
||||||
netloc = netloc.encode("ascii")
|
if netloc.isascii() and port.isascii():
|
||||||
url = url.encode("ascii")
|
netloc = netloc.encode("ascii")
|
||||||
|
url = url.encode("ascii")
|
||||||
|
else:
|
||||||
|
continue
|
||||||
p = parse(url)
|
p = parse(url)
|
||||||
self.assertEqual(p.netloc, netloc)
|
self.assertEqual(p.netloc, netloc)
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
|
@ -1199,6 +1202,7 @@ class Utility_Tests(unittest.TestCase):
|
||||||
self.assertEqual(splitnport('127.0.0.1', 55), ('127.0.0.1', 55))
|
self.assertEqual(splitnport('127.0.0.1', 55), ('127.0.0.1', 55))
|
||||||
self.assertEqual(splitnport('parrot:cheese'), ('parrot', None))
|
self.assertEqual(splitnport('parrot:cheese'), ('parrot', None))
|
||||||
self.assertEqual(splitnport('parrot:cheese', 55), ('parrot', None))
|
self.assertEqual(splitnport('parrot:cheese', 55), ('parrot', None))
|
||||||
|
self.assertEqual(splitnport('parrot: +1_0 '), ('parrot', None))
|
||||||
|
|
||||||
def test_splitquery(self):
|
def test_splitquery(self):
|
||||||
# Normal cases are exercised by other tests; ensure that we also
|
# Normal cases are exercised by other tests; ensure that we also
|
||||||
|
|
|
@ -167,12 +167,11 @@ class _NetlocResultMixinBase(object):
|
||||||
def port(self):
|
def port(self):
|
||||||
port = self._hostinfo[1]
|
port = self._hostinfo[1]
|
||||||
if port is not None:
|
if port is not None:
|
||||||
try:
|
if port.isdigit() and port.isascii():
|
||||||
port = int(port, 10)
|
port = int(port)
|
||||||
except ValueError:
|
else:
|
||||||
message = f'Port could not be cast to integer value as {port!r}'
|
raise ValueError(f"Port could not be cast to integer value as {port!r}")
|
||||||
raise ValueError(message) from None
|
if not (0 <= port <= 65535):
|
||||||
if not ( 0 <= port <= 65535):
|
|
||||||
raise ValueError("Port out of range 0-65535")
|
raise ValueError("Port out of range 0-65535")
|
||||||
return port
|
return port
|
||||||
|
|
||||||
|
@ -1132,15 +1131,15 @@ def splitnport(host, defport=-1):
|
||||||
def _splitnport(host, defport=-1):
|
def _splitnport(host, defport=-1):
|
||||||
"""Split host and port, returning numeric port.
|
"""Split host and port, returning numeric port.
|
||||||
Return given default port if no ':' found; defaults to -1.
|
Return given default port if no ':' found; defaults to -1.
|
||||||
Return numerical port if a valid number are found after ':'.
|
Return numerical port if a valid number is found after ':'.
|
||||||
Return None if ':' but not a valid number."""
|
Return None if ':' but not a valid number."""
|
||||||
host, delim, port = host.rpartition(':')
|
host, delim, port = host.rpartition(':')
|
||||||
if not delim:
|
if not delim:
|
||||||
host = port
|
host = port
|
||||||
elif port:
|
elif port:
|
||||||
try:
|
if port.isdigit() and port.isascii():
|
||||||
nport = int(port)
|
nport = int(port)
|
||||||
except ValueError:
|
else:
|
||||||
nport = None
|
nport = None
|
||||||
return host, nport
|
return host, nport
|
||||||
return host, defport
|
return host, defport
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
Fix bug in :func:`urllib.parse.urlparse` that causes certain port numbers
|
||||||
|
containing whitespace, underscores, plus and minus signs, or non-ASCII digits to be
|
||||||
|
incorrectly accepted.
|
Loading…
Add table
Add a link
Reference in a new issue