|
|
036b9c |
diff --git a/Doc/library/urllib.parse.rst b/Doc/library/urllib.parse.rst
|
|
|
036b9c |
index d991254..647af61 100644
|
|
|
036b9c |
--- a/Doc/library/urllib.parse.rst
|
|
|
036b9c |
+++ b/Doc/library/urllib.parse.rst
|
|
|
036b9c |
@@ -121,6 +121,11 @@ or on combining URL components into a URL string.
|
|
|
036b9c |
Unmatched square brackets in the :attr:`netloc` attribute will raise a
|
|
|
036b9c |
:exc:`ValueError`.
|
|
|
036b9c |
|
|
|
036b9c |
+ Characters in the :attr:`netloc` attribute that decompose under NFKC
|
|
|
036b9c |
+ normalization (as used by the IDNA encoding) into any of ``/``, ``?``,
|
|
|
036b9c |
+ ``#``, ``@``, or ``:`` will raise a :exc:`ValueError`. If the URL is
|
|
|
036b9c |
+ decomposed before parsing, no error will be raised.
|
|
|
036b9c |
+
|
|
|
036b9c |
.. versionchanged:: 3.2
|
|
|
036b9c |
Added IPv6 URL parsing capabilities.
|
|
|
036b9c |
|
|
|
036b9c |
@@ -133,6 +138,10 @@ or on combining URL components into a URL string.
|
|
|
036b9c |
Out-of-range port numbers now raise :exc:`ValueError`, instead of
|
|
|
036b9c |
returning :const:`None`.
|
|
|
036b9c |
|
|
|
036b9c |
+ .. versionchanged:: 3.6.9
|
|
|
036b9c |
+ Characters that affect netloc parsing under NFKC normalization will
|
|
|
036b9c |
+ now raise :exc:`ValueError`.
|
|
|
036b9c |
+
|
|
|
036b9c |
|
|
|
036b9c |
.. function:: parse_qs(qs, keep_blank_values=False, strict_parsing=False, encoding='utf-8', errors='replace', max_num_fields=None)
|
|
|
036b9c |
|
|
|
036b9c |
@@ -256,10 +265,19 @@ or on combining URL components into a URL string.
|
|
|
036b9c |
Unmatched square brackets in the :attr:`netloc` attribute will raise a
|
|
|
036b9c |
:exc:`ValueError`.
|
|
|
036b9c |
|
|
|
036b9c |
+ Characters in the :attr:`netloc` attribute that decompose under NFKC
|
|
|
036b9c |
+ normalization (as used by the IDNA encoding) into any of ``/``, ``?``,
|
|
|
036b9c |
+ ``#``, ``@``, or ``:`` will raise a :exc:`ValueError`. If the URL is
|
|
|
036b9c |
+ decomposed before parsing, no error will be raised.
|
|
|
036b9c |
+
|
|
|
036b9c |
.. versionchanged:: 3.6
|
|
|
036b9c |
Out-of-range port numbers now raise :exc:`ValueError`, instead of
|
|
|
036b9c |
returning :const:`None`.
|
|
|
036b9c |
|
|
|
036b9c |
+ .. versionchanged:: 3.6.9
|
|
|
036b9c |
+ Characters that affect netloc parsing under NFKC normalization will
|
|
|
036b9c |
+ now raise :exc:`ValueError`.
|
|
|
036b9c |
+
|
|
|
036b9c |
|
|
|
036b9c |
.. function:: urlunsplit(parts)
|
|
|
036b9c |
|
|
|
036b9c |
diff --git a/Lib/test/test_urlparse.py b/Lib/test/test_urlparse.py
|
|
|
036b9c |
index be50b47..68f633c 100644
|
|
|
036b9c |
--- a/Lib/test/test_urlparse.py
|
|
|
036b9c |
+++ b/Lib/test/test_urlparse.py
|
|
|
036b9c |
@@ -1,3 +1,5 @@
|
|
|
036b9c |
+import sys
|
|
|
036b9c |
+import unicodedata
|
|
|
036b9c |
import unittest
|
|
|
036b9c |
import urllib.parse
|
|
|
036b9c |
|
|
|
036b9c |
@@ -984,6 +986,34 @@ class UrlParseTestCase(unittest.TestCase):
|
|
|
036b9c |
expected.append(name)
|
|
|
036b9c |
self.assertCountEqual(urllib.parse.__all__, expected)
|
|
|
036b9c |
|
|
|
036b9c |
+ def test_urlsplit_normalization(self):
|
|
|
036b9c |
+ # Certain characters should never occur in the netloc,
|
|
|
036b9c |
+ # including under normalization.
|
|
|
036b9c |
+ # Ensure that ALL of them are detected and cause an error
|
|
|
036b9c |
+ illegal_chars = '/:#?@'
|
|
|
036b9c |
+ hex_chars = {'{:04X}'.format(ord(c)) for c in illegal_chars}
|
|
|
036b9c |
+ denorm_chars = [
|
|
|
036b9c |
+ c for c in map(chr, range(128, sys.maxunicode))
|
|
|
036b9c |
+ if (hex_chars & set(unicodedata.decomposition(c).split()))
|
|
|
036b9c |
+ and c not in illegal_chars
|
|
|
036b9c |
+ ]
|
|
|
036b9c |
+ # Sanity check that we found at least one such character
|
|
|
036b9c |
+ self.assertIn('\u2100', denorm_chars)
|
|
|
036b9c |
+ self.assertIn('\uFF03', denorm_chars)
|
|
|
036b9c |
+
|
|
|
036b9c |
+ # bpo-36742: Verify port separators are ignored when they
|
|
|
036b9c |
+ # existed prior to decomposition
|
|
|
036b9c |
+ urllib.parse.urlsplit('http://\u30d5\u309a:80')
|
|
|
036b9c |
+ with self.assertRaises(ValueError):
|
|
|
036b9c |
+ urllib.parse.urlsplit('http://\u30d5\u309a\ufe1380')
|
|
|
036b9c |
+
|
|
|
036b9c |
+ for scheme in ["http", "https", "ftp"]:
|
|
|
036b9c |
+ for netloc in ["netloc{}false.netloc", "n{}user@netloc"]:
|
|
|
036b9c |
+ for c in denorm_chars:
|
|
|
036b9c |
+ url = "{}://{}/path".format(scheme, netloc.format(c))
|
|
|
036b9c |
+ with self.subTest(url=url, char='{:04X}'.format(ord(c))):
|
|
|
036b9c |
+ with self.assertRaises(ValueError):
|
|
|
036b9c |
+ urllib.parse.urlsplit(url)
|
|
|
036b9c |
|
|
|
036b9c |
class Utility_Tests(unittest.TestCase):
|
|
|
036b9c |
"""Testcase to test the various utility functions in the urllib."""
|
|
|
036b9c |
diff --git a/Lib/urllib/parse.py b/Lib/urllib/parse.py
|
|
|
036b9c |
index 85e68c8..fa8827a 100644
|
|
|
036b9c |
--- a/Lib/urllib/parse.py
|
|
|
036b9c |
+++ b/Lib/urllib/parse.py
|
|
|
036b9c |
@@ -391,6 +391,24 @@ def _splitnetloc(url, start=0):
|
|
|
036b9c |
delim = min(delim, wdelim) # use earliest delim position
|
|
|
036b9c |
return url[start:delim], url[delim:] # return (domain, rest)
|
|
|
036b9c |
|
|
|
036b9c |
+def _checknetloc(netloc):
|
|
|
036b9c |
+ if not netloc or not any(ord(c) > 127 for c in netloc):
|
|
|
036b9c |
+ return
|
|
|
036b9c |
+ # looking for characters like \u2100 that expand to 'a/c'
|
|
|
036b9c |
+ # IDNA uses NFKC equivalence, so normalize for this check
|
|
|
036b9c |
+ import unicodedata
|
|
|
036b9c |
+ n = netloc.replace('@', '') # ignore characters already included
|
|
|
036b9c |
+ n = n.replace(':', '') # but not the surrounding text
|
|
|
036b9c |
+ n = n.replace('#', '')
|
|
|
036b9c |
+ n = n.replace('?', '')
|
|
|
036b9c |
+ netloc2 = unicodedata.normalize('NFKC', n)
|
|
|
036b9c |
+ if n == netloc2:
|
|
|
036b9c |
+ return
|
|
|
036b9c |
+ for c in '/?#@:':
|
|
|
036b9c |
+ if c in netloc2:
|
|
|
036b9c |
+ raise ValueError("netloc '" + netloc + "' contains invalid " +
|
|
|
036b9c |
+ "characters under NFKC normalization")
|
|
|
036b9c |
+
|
|
|
036b9c |
def urlsplit(url, scheme='', allow_fragments=True):
|
|
|
036b9c |
"""Parse a URL into 5 components:
|
|
|
036b9c |
<scheme>://<netloc>/<path>?<query>#<fragment>
|
|
|
036b9c |
@@ -420,6 +438,7 @@ def urlsplit(url, scheme='', allow_fragments=True):
|
|
|
036b9c |
url, fragment = url.split('#', 1)
|
|
|
036b9c |
if '?' in url:
|
|
|
036b9c |
url, query = url.split('?', 1)
|
|
|
036b9c |
+ _checknetloc(netloc)
|
|
|
036b9c |
v = SplitResult(scheme, netloc, url, query, fragment)
|
|
|
036b9c |
_parse_cache[key] = v
|
|
|
036b9c |
return _coerce_result(v)
|
|
|
036b9c |
@@ -443,6 +462,7 @@ def urlsplit(url, scheme='', allow_fragments=True):
|
|
|
036b9c |
url, fragment = url.split('#', 1)
|
|
|
036b9c |
if '?' in url:
|
|
|
036b9c |
url, query = url.split('?', 1)
|
|
|
036b9c |
+ _checknetloc(netloc)
|
|
|
036b9c |
v = SplitResult(scheme, netloc, url, query, fragment)
|
|
|
036b9c |
_parse_cache[key] = v
|
|
|
036b9c |
return _coerce_result(v)
|