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

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