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

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