Skip to content

Commit 62d3654

Browse files
zoobalarryhastings
authored andcommitted
bpo-36216: Add check for characters in netloc that normalize to separators (GH-12201) (#12224)
1 parent 0d9d810 commit 62d3654

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

‎Doc/library/urllib.parse.rst‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ or on combining URL components into a URL string.
118118
See section :ref:`urlparse-result-object` for more information on the result
119119
object.
120120

121+
Characters in the :attr:`netloc` attribute that decompose under NFKC
122+
normalization (as used by the IDNA encoding) into any of ``/``, ``?``,
123+
``#``, ``@``, or ``:`` will raise a :exc:`ValueError`. If the URL is
124+
decomposed before parsing, no error will be raised.
125+
121126
.. versionchanged:: 3.2
122127
Added IPv6 URL parsing capabilities.
123128

@@ -126,6 +131,10 @@ or on combining URL components into a URL string.
126131
false), in accordance with :rfc:`3986`. Previously, a whitelist of
127132
schemes that support fragments existed.
128133

134+
.. versionchanged:: 3.4.10
135+
Characters that affect netloc parsing under NFKC normalization will
136+
now raise :exc:`ValueError`.
137+
129138

130139
.. function:: parse_qs(qs, keep_blank_values=False, strict_parsing=False, encoding='utf-8', errors='replace')
131140

@@ -231,6 +240,15 @@ or on combining URL components into a URL string.
231240
See section :ref:`urlparse-result-object` for more information on the result
232241
object.
233242

243+
Characters in the :attr:`netloc` attribute that decompose under NFKC
244+
normalization (as used by the IDNA encoding) into any of ``/``, ``?``,
245+
``#``, ``@``, or ``:`` will raise a :exc:`ValueError`. If the URL is
246+
decomposed before parsing, no error will be raised.
247+
248+
.. versionchanged:: 3.4.10
249+
Characters that affect netloc parsing under NFKC normalization will
250+
now raise :exc:`ValueError`.
251+
234252

235253
.. function:: urlunsplit(parts)
236254

‎Lib/test/test_urlparse.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import sys
2+
import unicodedata
13
import unittest
24
import urllib.parse
35

@@ -868,6 +870,27 @@ def test_Quoter_repr(self):
868870
quoter = urllib.parse.Quoter(urllib.parse._ALWAYS_SAFE)
869871
self.assertIn('Quoter', repr(quoter))
870872

873+
def test_urlsplit_normalization(self):
874+
# Certain characters should never occur in the netloc,
875+
# including under normalization.
876+
# Ensure that ALL of them are detected and cause an error
877+
illegal_chars = '/:#?@'
878+
hex_chars = {'{:04X}'.format(ord(c)) for c in illegal_chars}
879+
denorm_chars = [
880+
c for c in map(chr, range(128, sys.maxunicode))
881+
if (hex_chars & set(unicodedata.decomposition(c).split()))
882+
and c not in illegal_chars
883+
]
884+
# Sanity check that we found at least one such character
885+
self.assertIn('\u2100', denorm_chars)
886+
self.assertIn('\uFF03', denorm_chars)
887+
888+
for scheme in ["http", "https", "ftp"]:
889+
for c in denorm_chars:
890+
url = "{}://netloc{}false.netloc/path".format(scheme, c)
891+
with self.subTest(url=url, char='{:04X}'.format(ord(c))):
892+
with self.assertRaises(ValueError):
893+
urllib.parse.urlsplit(url)
871894

872895
class Utility_Tests(unittest.TestCase):
873896
"""Testcase to test the various utility functions in the urllib."""

‎Lib/urllib/parse.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,21 @@ def _splitnetloc(url, start=0):
316316
delim = min(delim, wdelim) # use earliest delim position
317317
return url[start:delim], url[delim:] # return (domain, rest)
318318

319+
def _checknetloc(netloc):
320+
if not netloc or not any(ord(c) > 127 for c in netloc):
321+
return
322+
# looking for characters like \u2100 that expand to 'a/c'
323+
# IDNA uses NFKC equivalence, so normalize for this check
324+
import unicodedata
325+
netloc2 = unicodedata.normalize('NFKC', netloc)
326+
if netloc == netloc2:
327+
return
328+
_, _, netloc = netloc.rpartition('@') # anything to the left of '@' is okay
329+
for c in '/?#@:':
330+
if c in netloc2:
331+
raise ValueError("netloc '" + netloc2 + "' contains invalid " +
332+
"characters under NFKC normalization")
333+
319334
def urlsplit(url, scheme='', allow_fragments=True):
320335
"""Parse a URL into 5 components:
321336
<scheme>://<netloc>/<path>?<query>#<fragment>
@@ -345,6 +360,7 @@ def urlsplit(url, scheme='', allow_fragments=True):
345360
url, fragment = url.split('#', 1)
346361
if '?' in url:
347362
url, query = url.split('?', 1)
363+
_checknetloc(netloc)
348364
v = SplitResult(scheme, netloc, url, query, fragment)
349365
_parse_cache[key] = v
350366
return _coerce_result(v)
@@ -368,6 +384,7 @@ def urlsplit(url, scheme='', allow_fragments=True):
368384
url, fragment = url.split('#', 1)
369385
if '?' in url:
370386
url, query = url.split('?', 1)
387+
_checknetloc(netloc)
371388
v = SplitResult(scheme, netloc, url, query, fragment)
372389
_parse_cache[key] = v
373390
return _coerce_result(v)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Changes urlsplit() to raise ValueError when the URL contains characters that
2+
decompose under IDNA encoding (NFKC-normalization) into characters that
3+
affect how the URL is parsed.

0 commit comments

Comments
 (0)