Blame SOURCES/00320-CVE-2019-9636-and-CVE-2019-10160.patch

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