Skip to content

Commit 1a58a6a

Browse files
authored
[3.15] gh-161987: Revert port validation at parse time (gh-161619) (#161989)
Revert "[3.15] gh-88037 - Move port validation logic to parsing time (GH-159122) (#161619)" This reverts commit 1a530ac.
1 parent 33f71f8 commit 1a58a6a

6 files changed

Lines changed: 35 additions & 82 deletions

File tree

‎Lib/test/test_urllib.py‎

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import email.message
88
import io
99
import unittest
10-
import unittest.mock
1110
from test import support
1211
from test.support import os_helper
1312
from test.support import socket_helper
@@ -401,17 +400,11 @@ def test_url_host_with_newline_header_injection_rejected(self):
401400
host = "localhost\r\nX-injected: header\r\n"
402401
schemeless_url = "//" + host + ":8080/test/?test=a"
403402
try:
404-
# Once \r\n are stripped from the URL, the ':' in
405-
# "X-injected:" is mistaken for the port separator, and
406-
# urlsplit()/urlparse() now reject the resulting
407-
# non-numeric "port" at parse time, before a Request is
408-
# even built -- rejecting the injection earlier than
409-
# http.client's own InvalidURL check ever gets a chance to.
403+
InvalidURL = http.client.InvalidURL
410404
with self.assertRaisesRegex(
411-
ValueError, "Port could not be cast to integer value"):
405+
InvalidURL, r"contain control.*\\r"):
412406
urllib.request.urlopen(f"http:{schemeless_url}")
413-
with self.assertRaisesRegex(
414-
ValueError, "Port could not be cast to integer value"):
407+
with self.assertRaisesRegex(InvalidURL, r"contain control.*\\n"):
415408
urllib.request.urlopen(f"https:{schemeless_url}")
416409
finally:
417410
self.unfakehttp()
@@ -1679,17 +1672,6 @@ def test_url2pathname_resolve_host(self):
16791672
self.assertEqual(fn(f'//{socket.gethostname()}/foo/bar'), f'{sep}foo{sep}bar')
16801673
self.assertEqual(fn(f'//{socket.gethostname()}/foo/bar', resolve_host=True), f'{sep}foo{sep}bar')
16811674

1682-
def test_url2pathname_win_netloc_not_validated_as_port(self):
1683-
# The Windows branch of url2pathname() stuffs things like drive
1684-
# letters into the netloc (e.g. '//C:\\' -> authority 'C:\\'),
1685-
# which is not a real host[:port] pair. url2pathname() must not
1686-
# reject it as an invalid port; run the Windows branch directly
1687-
# so this is exercised on every platform, not just win32.
1688-
with unittest.mock.patch('os.name', 'nt'):
1689-
fn = urllib.request.url2pathname
1690-
self.assertEqual(fn('//C:\\'), 'C:\\')
1691-
self.assertEqual(fn('//C:80/'), 'C:80\\')
1692-
16931675
@unittest.skipUnless(sys.platform == 'win32',
16941676
'test specific to Windows pathnames.')
16951677
def test_url2pathname_win(self):

‎Lib/test/test_urllib2.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -920,9 +920,9 @@ def test_file(self):
920920
parsed._replace(netloc='localhost:80').geturl(),
921921
"file:///file_does_not_exist.txt",
922922
"file://not-a-local-host.com//dir/file.txt",
923-
"file://%s:80/%s/%s" % (socket.gethostbyname('localhost'),
923+
"file://%s:80%s/%s" % (socket.gethostbyname('localhost'),
924924
os.getcwd(), TESTFN),
925-
"file://somerandomhost.ontheinternet.com/%s/%s" %
925+
"file://somerandomhost.ontheinternet.com%s/%s" %
926926
(os.getcwd(), TESTFN),
927927
]:
928928
try:

‎Lib/test/test_urlparse.py‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -917,8 +917,9 @@ def test_urlsplit_attributes(self):
917917

918918
# Verify an illegal port raises ValueError
919919
url = b"HTTP://WWW.PYTHON.ORG:65536/doc/#frag"
920+
p = urllib.parse.urlsplit(url)
920921
with self.assertRaisesRegex(ValueError, "out of range"):
921-
urllib.parse.urlsplit(url)
922+
p.port
922923

923924
def test_urlsplit_remove_unsafe_bytes(self):
924925
# Remove ASCII tabs and newlines from input
@@ -1028,8 +1029,10 @@ def test_attributes_bad_port(self, bytes, parse, port):
10281029
self.skipTest('non-ASCII bytes')
10291030
netloc = str_encode(netloc)
10301031
url = str_encode(url)
1032+
p = parse(url)
1033+
self.assertEqual(p.netloc, netloc)
10311034
with self.assertRaises(ValueError):
1032-
parse(url)
1035+
p.port
10331036

10341037
@support.subTests('bytes', (False, True))
10351038
@support.subTests('parse', (urllib.parse.urlsplit, urllib.parse.urlparse))
@@ -1667,11 +1670,13 @@ def test_splitting_bracketed_hosts(self):
16671670

16681671
def test_port_casting_failure_message(self):
16691672
message = "Port could not be cast to integer value as 'oracle'"
1673+
p1 = urllib.parse.urlparse('http://Server=sde; Service=sde:oracle')
16701674
with self.assertRaisesRegex(ValueError, message):
1671-
urllib.parse.urlparse('http://Server=sde; Service=sde:oracle')
1675+
p1.port
16721676

1677+
p2 = urllib.parse.urlsplit('http://Server=sde; Service=sde:oracle')
16731678
with self.assertRaisesRegex(ValueError, message):
1674-
urllib.parse.urlsplit('http://Server=sde; Service=sde:oracle')
1679+
p2.port
16751680

16761681
def test_telurl_params(self):
16771682
p1 = urllib.parse.urlparse('tel:123-4;phone-context=+1-650-516')

‎Lib/urllib/parse.py‎

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -140,27 +140,6 @@ def _coerce_args(*args):
140140
return args + (_noop,)
141141
return _decode_args(args) + (_encode_result,)
142142

143-
def _parse_hostinfo(netloc):
144-
_, _, hostinfo = netloc.rpartition('@')
145-
_, have_open_br, bracketed = hostinfo.partition('[')
146-
if have_open_br:
147-
hostname, _, port = bracketed.partition(']')
148-
_, _, port = port.partition(':')
149-
else:
150-
hostname, _, port = hostinfo.partition(':')
151-
if not port:
152-
port = None
153-
return hostname, port
154-
155-
def _validate_port(port):
156-
if port.isdigit() and port.isascii():
157-
port = int(port)
158-
else:
159-
raise ValueError(f"Port could not be cast to integer value as {port!r}")
160-
if not (0 <= port <= 65535):
161-
raise ValueError("Port out of range 0-65535")
162-
return port
163-
164143
# Result objects are more helpful than simple tuples
165144
class _ResultMixinStr(object):
166145
"""Standard approach to encoding parsed results from str to bytes"""
@@ -219,7 +198,12 @@ def hostname(self):
219198
def port(self):
220199
port = self._hostinfo[1]
221200
if port is not None:
222-
port = _validate_port(port)
201+
if port.isdigit() and port.isascii():
202+
port = int(port)
203+
else:
204+
raise ValueError(f"Port could not be cast to integer value as {port!r}")
205+
if not (0 <= port <= 65535):
206+
raise ValueError("Port out of range 0-65535")
223207
return port
224208

225209
__class_getitem__ = classmethod(types.GenericAlias)
@@ -247,7 +231,16 @@ def _hostinfo(self):
247231
netloc = self.netloc
248232
if netloc is None:
249233
return None, None
250-
return _parse_hostinfo(netloc)
234+
_, _, hostinfo = netloc.rpartition('@')
235+
_, have_open_br, bracketed = hostinfo.partition('[')
236+
if have_open_br:
237+
hostname, _, port = bracketed.partition(']')
238+
_, _, port = port.partition(':')
239+
else:
240+
hostname, _, port = hostinfo.partition(':')
241+
if not port:
242+
port = None
243+
return hostname, port
251244

252245

253246
class _NetlocResultMixinBytes(_NetlocResultMixinBase, _ResultMixinBytes):
@@ -513,7 +506,9 @@ def _splitnetloc(url, start=0):
513506
delim = min(delim, wdelim) # use earliest delim position
514507
return url[start:delim], url[delim:] # return (domain, rest)
515508

516-
def _checknetloc_nfkc(netloc):
509+
def _checknetloc(netloc):
510+
if not netloc or netloc.isascii():
511+
return
517512
# looking for characters like \u2100 that expand to 'a/c'
518513
# IDNA uses NFKC equivalence, so normalize for this check
519514
import unicodedata
@@ -529,16 +524,6 @@ def _checknetloc_nfkc(netloc):
529524
raise ValueError("netloc '" + netloc + "' contains invalid " +
530525
"characters under NFKC normalization")
531526

532-
def _checknetloc(netloc):
533-
if not netloc:
534-
return
535-
if not netloc.isascii():
536-
_checknetloc_nfkc(netloc)
537-
538-
_, port = _parse_hostinfo(netloc)
539-
if port is not None:
540-
_validate_port(port)
541-
542527
def _check_bracketed_netloc(netloc):
543528
# Note that this function must mirror the splitting
544529
# done in NetlocResultMixins._hostinfo().

‎Lib/urllib/request.py‎

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@
101101
from urllib.parse import (
102102
urlparse, urlsplit, urljoin, unwrap, quote, unquote,
103103
_splittype, _splithost, _splitport, _splituser, _splitpasswd,
104-
_splitattr, _splitvalue, _splittag, _splitnetloc,
105-
_WHATWG_C0_CONTROL_OR_SPACE, _UNSAFE_URL_BYTES_TO_REMOVE,
104+
_splitattr, _splitvalue, _splittag,
106105
unquote_to_bytes, urlunparse)
107106
from urllib.response import addinfourl, addclosehook
108107

@@ -1754,22 +1753,7 @@ def url2pathname(url, *, require_scheme=False, resolve_host=False):
17541753
"""
17551754
if not require_scheme:
17561755
url = 'file:' + url
1757-
# Match urlsplit()'s own control-character handling up front, so the
1758-
# except branch below can reuse this same cleaned-up string.
1759-
url = url.lstrip(_WHATWG_C0_CONTROL_OR_SPACE)
1760-
for b in _UNSAFE_URL_BYTES_TO_REMOVE:
1761-
url = url.replace(b, '')
1762-
try:
1763-
scheme, authority, url = urlsplit(url)[:3] # Discard query and fragment.
1764-
except ValueError:
1765-
# The authority here is not a real host[:port] pair -- it may
1766-
# hold a Windows drive letter (e.g. 'C:\\') -- so urlsplit()'s
1767-
# eager port check doesn't apply; split without it instead.
1768-
scheme, _, url = url.partition(':')
1769-
scheme = scheme.lower()
1770-
authority = ''
1771-
if url[:2] == '//':
1772-
authority, url = _splitnetloc(url, 2)
1756+
scheme, authority, url = urlsplit(url)[:3] # Discard query and fragment.
17731757
if scheme != 'file':
17741758
raise URLError("URL is missing a 'file:' scheme")
17751759
if os.name == 'nt':

‎Misc/NEWS.d/next/Library/2026-10-11-13-08-07.gh-issue-88037.CzKcxh.rst‎

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)