diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py index 3858b03546061e..ab59727a8fd182 100644 --- a/Lib/test/test_urllib.py +++ b/Lib/test/test_urllib.py @@ -7,7 +7,6 @@ import email.message import io import unittest -import unittest.mock from test import support from test.support import os_helper from test.support import socket_helper @@ -401,17 +400,11 @@ def test_url_host_with_newline_header_injection_rejected(self): host = "localhost\r\nX-injected: header\r\n" schemeless_url = "//" + host + ":8080/test/?test=a" try: - # Once \r\n are stripped from the URL, the ':' in - # "X-injected:" is mistaken for the port separator, and - # urlsplit()/urlparse() now reject the resulting - # non-numeric "port" at parse time, before a Request is - # even built -- rejecting the injection earlier than - # http.client's own InvalidURL check ever gets a chance to. + InvalidURL = http.client.InvalidURL with self.assertRaisesRegex( - ValueError, "Port could not be cast to integer value"): + InvalidURL, r"contain control.*\\r"): urllib.request.urlopen(f"http:{schemeless_url}") - with self.assertRaisesRegex( - ValueError, "Port could not be cast to integer value"): + with self.assertRaisesRegex(InvalidURL, r"contain control.*\\n"): urllib.request.urlopen(f"https:{schemeless_url}") finally: self.unfakehttp() @@ -1679,17 +1672,6 @@ def test_url2pathname_resolve_host(self): self.assertEqual(fn(f'//{socket.gethostname()}/foo/bar'), f'{sep}foo{sep}bar') self.assertEqual(fn(f'//{socket.gethostname()}/foo/bar', resolve_host=True), f'{sep}foo{sep}bar') - def test_url2pathname_win_netloc_not_validated_as_port(self): - # The Windows branch of url2pathname() stuffs things like drive - # letters into the netloc (e.g. '//C:\\' -> authority 'C:\\'), - # which is not a real host[:port] pair. url2pathname() must not - # reject it as an invalid port; run the Windows branch directly - # so this is exercised on every platform, not just win32. - with unittest.mock.patch('os.name', 'nt'): - fn = urllib.request.url2pathname - self.assertEqual(fn('//C:\\'), 'C:\\') - self.assertEqual(fn('//C:80/'), 'C:80\\') - @unittest.skipUnless(sys.platform == 'win32', 'test specific to Windows pathnames.') def test_url2pathname_win(self): diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py index 67399ab9f885ca..2e3f02fbd699fe 100644 --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -903,9 +903,9 @@ def test_file(self): parsed._replace(netloc='localhost:80').geturl(), "file:///file_does_not_exist.txt", "file://not-a-local-host.com//dir/file.txt", - "file://%s:80/%s/%s" % (socket.gethostbyname('localhost'), + "file://%s:80%s/%s" % (socket.gethostbyname('localhost'), os.getcwd(), TESTFN), - "file://somerandomhost.ontheinternet.com/%s/%s" % + "file://somerandomhost.ontheinternet.com%s/%s" % (os.getcwd(), TESTFN), ]: try: diff --git a/Lib/test/test_urlparse.py b/Lib/test/test_urlparse.py index 864248b36e8ccd..98f0b190ee759b 100644 --- a/Lib/test/test_urlparse.py +++ b/Lib/test/test_urlparse.py @@ -874,8 +874,9 @@ def test_urlsplit_attributes(self): # Verify an illegal port raises ValueError url = b"HTTP://WWW.PYTHON.ORG:65536/doc/#frag" + p = urllib.parse.urlsplit(url) with self.assertRaisesRegex(ValueError, "out of range"): - urllib.parse.urlsplit(url) + p.port def test_urlsplit_remove_unsafe_bytes(self): # Remove ASCII tabs and newlines from input @@ -985,8 +986,10 @@ def test_attributes_bad_port(self, bytes, parse, port): self.skipTest('non-ASCII bytes') netloc = str_encode(netloc) url = str_encode(url) + p = parse(url) + self.assertEqual(p.netloc, netloc) with self.assertRaises(ValueError): - parse(url) + p.port @support.subTests('bytes', (False, True)) @support.subTests('parse', (urllib.parse.urlsplit, urllib.parse.urlparse)) @@ -1440,11 +1443,13 @@ def test_splitting_bracketed_hosts(self): def test_port_casting_failure_message(self): message = "Port could not be cast to integer value as 'oracle'" + p1 = urllib.parse.urlparse('http://Server=sde; Service=sde:oracle') with self.assertRaisesRegex(ValueError, message): - urllib.parse.urlparse('http://Server=sde; Service=sde:oracle') + p1.port + p2 = urllib.parse.urlsplit('http://Server=sde; Service=sde:oracle') with self.assertRaisesRegex(ValueError, message): - urllib.parse.urlsplit('http://Server=sde; Service=sde:oracle') + p2.port def test_telurl_params(self): p1 = urllib.parse.urlparse('tel:123-4;phone-context=+1-650-516') diff --git a/Lib/urllib/parse.py b/Lib/urllib/parse.py index fceec50ac3ffa7..c9f8a33e72c39d 100644 --- a/Lib/urllib/parse.py +++ b/Lib/urllib/parse.py @@ -132,27 +132,6 @@ def _coerce_args(*args): return args + (_noop,) return _decode_args(args) + (_encode_result,) -def _parse_hostinfo(netloc): - _, _, hostinfo = netloc.rpartition('@') - _, have_open_br, bracketed = hostinfo.partition('[') - if have_open_br: - hostname, _, port = bracketed.partition(']') - _, _, port = port.partition(':') - else: - hostname, _, port = hostinfo.partition(':') - if not port: - port = None - return hostname, port - -def _validate_port(port): - if port.isdigit() and port.isascii(): - port = int(port) - else: - raise ValueError(f"Port could not be cast to integer value as {port!r}") - if not (0 <= port <= 65535): - raise ValueError("Port out of range 0-65535") - return port - # Result objects are more helpful than simple tuples class _ResultMixinStr(object): """Standard approach to encoding parsed results from str to bytes""" @@ -197,7 +176,12 @@ def hostname(self): def port(self): port = self._hostinfo[1] if port is not None: - port = _validate_port(port) + if port.isdigit() and port.isascii(): + port = int(port) + else: + raise ValueError(f"Port could not be cast to integer value as {port!r}") + if not (0 <= port <= 65535): + raise ValueError("Port out of range 0-65535") return port __class_getitem__ = classmethod(types.GenericAlias) @@ -221,9 +205,16 @@ def _userinfo(self): @property def _hostinfo(self): netloc = self.netloc - if netloc is None: - return None, None - return _parse_hostinfo(netloc) + _, _, hostinfo = netloc.rpartition('@') + _, have_open_br, bracketed = hostinfo.partition('[') + if have_open_br: + hostname, _, port = bracketed.partition(']') + _, _, port = port.partition(':') + else: + hostname, _, port = hostinfo.partition(':') + if not port: + port = None + return hostname, port class _NetlocResultMixinBytes(_NetlocResultMixinBase, _ResultMixinBytes): @@ -432,7 +423,9 @@ def _splitnetloc(url, start=0): delim = min(delim, wdelim) # use earliest delim position return url[start:delim], url[delim:] # return (domain, rest) -def _checknetloc_nfkc(netloc): +def _checknetloc(netloc): + if not netloc or netloc.isascii(): + return # looking for characters like \u2100 that expand to 'a/c' # IDNA uses NFKC equivalence, so normalize for this check import unicodedata @@ -448,16 +441,6 @@ def _checknetloc_nfkc(netloc): raise ValueError("netloc '" + netloc + "' contains invalid " + "characters under NFKC normalization") -def _checknetloc(netloc): - if not netloc: - return - if not netloc.isascii(): - _checknetloc_nfkc(netloc) - - _, port = _parse_hostinfo(netloc) - if port is not None: - _validate_port(port) - def _check_bracketed_netloc(netloc): # Note that this function must mirror the splitting # done in NetlocResultMixins._hostinfo(). diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index 436fe15cc7f424..2e2f0631f9cc8c 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -101,8 +101,7 @@ from urllib.parse import ( urlparse, urlsplit, urljoin, unwrap, quote, unquote, _splittype, _splithost, _splitport, _splituser, _splitpasswd, - _splitattr, _splitvalue, _splittag, _splitnetloc, - _WHATWG_C0_CONTROL_OR_SPACE, _UNSAFE_URL_BYTES_TO_REMOVE, + _splitattr, _splitvalue, _splittag, unquote_to_bytes, urlunparse) from urllib.response import addinfourl, addclosehook @@ -1752,22 +1751,7 @@ def url2pathname(url, *, require_scheme=False, resolve_host=False): """ if not require_scheme: url = 'file:' + url - # Match urlsplit()'s own control-character handling up front, so the - # except branch below can reuse this same cleaned-up string. - url = url.lstrip(_WHATWG_C0_CONTROL_OR_SPACE) - for b in _UNSAFE_URL_BYTES_TO_REMOVE: - url = url.replace(b, '') - try: - scheme, authority, url = urlsplit(url)[:3] # Discard query and fragment. - except ValueError: - # The authority here is not a real host[:port] pair -- it may - # hold a Windows drive letter (e.g. 'C:\\') -- so urlsplit()'s - # eager port check doesn't apply; split without it instead. - scheme, _, url = url.partition(':') - scheme = scheme.lower() - authority = '' - if url[:2] == '//': - authority, url = _splitnetloc(url, 2) + scheme, authority, url = urlsplit(url)[:3] # Discard query and fragment. if scheme != 'file': raise URLError("URL is missing a 'file:' scheme") if os.name == 'nt': diff --git a/Misc/NEWS.d/next/Library/2026-10-11-13-08-07.gh-issue-88037.CzKcxh.rst b/Misc/NEWS.d/next/Library/2026-10-11-13-08-07.gh-issue-88037.CzKcxh.rst deleted file mode 100644 index 697b44ba2985a0..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-10-11-13-08-07.gh-issue-88037.CzKcxh.rst +++ /dev/null @@ -1,3 +0,0 @@ -:func:`urllib.parse.urlsplit` and :func:`urllib.parse.urlparse` now raise -:exc:`ValueError` for an invalid port when the URL is parsed, instead of -when the :attr:`!port` attribute is read. Patch by Miguel Brito and Senthil Kumaran.