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

bf3aa8
diff --git a/Doc/library/urlparse.rst b/Doc/library/urlparse.rst
b54164
index 22249da..0989c88 100644
bf3aa8
--- a/Doc/library/urlparse.rst
bf3aa8
+++ b/Doc/library/urlparse.rst
b54164
@@ -119,12 +119,22 @@ The :mod:`urlparse` module defines the following functions:
bf3aa8
    See section :ref:`urlparse-result-object` for more information on the result
bf3aa8
    object.
bf3aa8
 
bf3aa8
+   Characters in the :attr:`netloc` attribute that decompose under NFKC
bf3aa8
+   normalization (as used by the IDNA encoding) into any of ``/``, ``?``,
bf3aa8
+   ``#``, ``@``, or ``:`` will raise a :exc:`ValueError`. If the URL is
bf3aa8
+   decomposed before parsing, or is not a Unicode string, no error will be
bf3aa8
+   raised.
bf3aa8
+
bf3aa8
    .. versionchanged:: 2.5
bf3aa8
       Added attributes to return value.
bf3aa8
 
b54164
    .. versionchanged:: 2.7
bf3aa8
       Added IPv6 URL parsing capabilities.
bf3aa8
 
bf3aa8
+   .. versionchanged:: 2.7.17
bf3aa8
+      Characters that affect netloc parsing under NFKC normalization will
bf3aa8
+      now raise :exc:`ValueError`.
bf3aa8
+
bf3aa8
 
b54164
 .. function:: parse_qs(qs[, keep_blank_values[, strict_parsing[, max_num_fields]]])
b54164
 
b54164
@@ -232,11 +242,21 @@ The :mod:`urlparse` module defines the following functions:
bf3aa8
    See section :ref:`urlparse-result-object` for more information on the result
bf3aa8
    object.
bf3aa8
 
bf3aa8
+   Characters in the :attr:`netloc` attribute that decompose under NFKC
bf3aa8
+   normalization (as used by the IDNA encoding) into any of ``/``, ``?``,
bf3aa8
+   ``#``, ``@``, or ``:`` will raise a :exc:`ValueError`. If the URL is
bf3aa8
+   decomposed before parsing, or is not a Unicode string, no error will be
bf3aa8
+   raised.
bf3aa8
+
bf3aa8
    .. versionadded:: 2.2
bf3aa8
 
bf3aa8
    .. versionchanged:: 2.5
bf3aa8
       Added attributes to return value.
bf3aa8
 
bf3aa8
+   .. versionchanged:: 2.7.17
bf3aa8
+      Characters that affect netloc parsing under NFKC normalization will
bf3aa8
+      now raise :exc:`ValueError`.
bf3aa8
+
bf3aa8
 
bf3aa8
 .. function:: urlunsplit(parts)
bf3aa8
 
bf3aa8
diff --git a/Lib/test/test_urlparse.py b/Lib/test/test_urlparse.py
b54164
index 4e1ded7..6fd1071 100644
bf3aa8
--- a/Lib/test/test_urlparse.py
bf3aa8
+++ b/Lib/test/test_urlparse.py
bf3aa8
@@ -1,4 +1,6 @@
bf3aa8
 from test import test_support
bf3aa8
+import sys
bf3aa8
+import unicodedata
bf3aa8
 import unittest
bf3aa8
 import urlparse
bf3aa8
 
b54164
@@ -624,6 +626,35 @@ class UrlParseTestCase(unittest.TestCase):
bf3aa8
         self.assertEqual(urlparse.urlparse("http://www.python.org:80"),
bf3aa8
                 ('http','www.python.org:80','','','',''))
bf3aa8
 
bf3aa8
+    def test_urlsplit_normalization(self):
bf3aa8
+        # Certain characters should never occur in the netloc,
bf3aa8
+        # including under normalization.
bf3aa8
+        # Ensure that ALL of them are detected and cause an error
bf3aa8
+        illegal_chars = u'/:#?@'
bf3aa8
+        hex_chars = {'{:04X}'.format(ord(c)) for c in illegal_chars}
bf3aa8
+        denorm_chars = [
bf3aa8
+            c for c in map(unichr, range(128, sys.maxunicode))
bf3aa8
+            if (hex_chars & set(unicodedata.decomposition(c).split()))
bf3aa8
+            and c not in illegal_chars
bf3aa8
+        ]
bf3aa8
+        # Sanity check that we found at least one such character
bf3aa8
+        self.assertIn(u'\u2100', denorm_chars)
bf3aa8
+        self.assertIn(u'\uFF03', denorm_chars)
bf3aa8
+
b54164
+        # bpo-36742: Verify port separators are ignored when they
b54164
+        # existed prior to decomposition
b54164
+        urlparse.urlsplit(u'http://\u30d5\u309a:80')
b54164
+        with self.assertRaises(ValueError):
b54164
+            urlparse.urlsplit(u'http://\u30d5\u309a\ufe1380')
b54164
+
bf3aa8
+        for scheme in [u"http", u"https", u"ftp"]:
bf3aa8
+            for c in denorm_chars:
bf3aa8
+                url = u"{}://netloc{}false.netloc/path".format(scheme, c)
bf3aa8
+                if test_support.verbose:
bf3aa8
+                    print "Checking %r" % url
bf3aa8
+                with self.assertRaises(ValueError):
bf3aa8
+                    urlparse.urlsplit(url)
bf3aa8
+
bf3aa8
 def test_main():
bf3aa8
     test_support.run_unittest(UrlParseTestCase)
bf3aa8
 
bf3aa8
diff --git a/Lib/urlparse.py b/Lib/urlparse.py
b54164
index f7c2b03..f08e0fe 100644
bf3aa8
--- a/Lib/urlparse.py
bf3aa8
+++ b/Lib/urlparse.py
b54164
@@ -165,6 +165,24 @@ def _splitnetloc(url, start=0):
bf3aa8
             delim = min(delim, wdelim)     # use earliest delim position
bf3aa8
     return url[start:delim], url[delim:]   # return (domain, rest)
bf3aa8
 
bf3aa8
+def _checknetloc(netloc):
bf3aa8
+    if not netloc or not isinstance(netloc, unicode):
bf3aa8
+        return
bf3aa8
+    # looking for characters like \u2100 that expand to 'a/c'
bf3aa8
+    # IDNA uses NFKC equivalence, so normalize for this check
bf3aa8
+    import unicodedata
b54164
+    n = netloc.rpartition('@')[2] # ignore anything to the left of '@'
b54164
+    n = n.replace(':', '')        # ignore characters already included
b54164
+    n = n.replace('#', '')        # but not the surrounding text
b54164
+    n = n.replace('?', '')
b54164
+    netloc2 = unicodedata.normalize('NFKC', n)
b54164
+    if n == netloc2:
bf3aa8
+        return
bf3aa8
+    for c in '/?#@:':
bf3aa8
+        if c in netloc2:
b54164
+            raise ValueError("netloc '" + netloc + "' contains invalid " +
bf3aa8
+                             "characters under NFKC normalization")
bf3aa8
+
bf3aa8
 def urlsplit(url, scheme='', allow_fragments=True):
bf3aa8
     """Parse a URL into 5 components:
bf3aa8
     <scheme>://<netloc>/<path>?<query>#<fragment>
b54164
@@ -193,6 +211,7 @@ def urlsplit(url, scheme='', allow_fragments=True):
bf3aa8
                 url, fragment = url.split('#', 1)
bf3aa8
             if '?' in url:
bf3aa8
                 url, query = url.split('?', 1)
bf3aa8
+            _checknetloc(netloc)
bf3aa8
             v = SplitResult(scheme, netloc, url, query, fragment)
bf3aa8
             _parse_cache[key] = v
bf3aa8
             return v
b54164
@@ -216,6 +235,7 @@ def urlsplit(url, scheme='', allow_fragments=True):
bf3aa8
         url, fragment = url.split('#', 1)
bf3aa8
     if '?' in url:
bf3aa8
         url, query = url.split('?', 1)
bf3aa8
+    _checknetloc(netloc)
bf3aa8
     v = SplitResult(scheme, netloc, url, query, fragment)
bf3aa8
     _parse_cache[key] = v
bf3aa8
     return v