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

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