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