churchyard / rpms / python3

Forked from rpms/python3 6 years ago
Clone

Blame 00320-CVE-2019-9636.patch

498b883
From daad2c482c91de32d8305abbccc76a5de8b3a8be Mon Sep 17 00:00:00 2001
498b883
From: Steve Dower <steve.dower@microsoft.com>
498b883
Date: Thu, 7 Mar 2019 09:08:18 -0800
498b883
Subject: [PATCH] bpo-36216: Add check for characters in netloc that normalize
498b883
 to separators (GH-12201)
498b883
498b883
---
498b883
 Doc/library/urllib.parse.rst                  | 18 +++++++++++++++
498b883
 Lib/test/test_urlparse.py                     | 23 +++++++++++++++++++
498b883
 Lib/urllib/parse.py                           | 17 ++++++++++++++
498b883
 .../2019-03-06-09-38-40.bpo-36216.6q1m4a.rst  |  3 +++
498b883
 4 files changed, 61 insertions(+)
498b883
 create mode 100644 Misc/NEWS.d/next/Security/2019-03-06-09-38-40.bpo-36216.6q1m4a.rst
498b883
498b883
diff --git a/Doc/library/urllib.parse.rst b/Doc/library/urllib.parse.rst
498b883
index 0c8f0f607314..b565e1edd321 100644
498b883
--- a/Doc/library/urllib.parse.rst
498b883
+++ b/Doc/library/urllib.parse.rst
498b883
@@ -124,6 +124,11 @@ or on combining URL components into a URL string.
498b883
    Unmatched square brackets in the :attr:`netloc` attribute will raise a
498b883
    :exc:`ValueError`.
498b883
 
498b883
+   Characters in the :attr:`netloc` attribute that decompose under NFKC
498b883
+   normalization (as used by the IDNA encoding) into any of ``/``, ``?``,
498b883
+   ``#``, ``@``, or ``:`` will raise a :exc:`ValueError`. If the URL is
498b883
+   decomposed before parsing, no error will be raised.
498b883
+
498b883
    .. versionchanged:: 3.2
498b883
       Added IPv6 URL parsing capabilities.
498b883
 
498b883
@@ -136,6 +141,10 @@ or on combining URL components into a URL string.
498b883
       Out-of-range port numbers now raise :exc:`ValueError`, instead of
498b883
       returning :const:`None`.
498b883
 
498b883
+   .. versionchanged:: 3.7.3
498b883
+      Characters that affect netloc parsing under NFKC normalization will
498b883
+      now raise :exc:`ValueError`.
498b883
+
498b883
 
498b883
 .. function:: parse_qs(qs, keep_blank_values=False, strict_parsing=False, encoding='utf-8', errors='replace', max_num_fields=None)
498b883
 
498b883
@@ -257,10 +266,19 @@ or on combining URL components into a URL string.
498b883
    Unmatched square brackets in the :attr:`netloc` attribute will raise a
498b883
    :exc:`ValueError`.
498b883
 
498b883
+   Characters in the :attr:`netloc` attribute that decompose under NFKC
498b883
+   normalization (as used by the IDNA encoding) into any of ``/``, ``?``,
498b883
+   ``#``, ``@``, or ``:`` will raise a :exc:`ValueError`. If the URL is
498b883
+   decomposed before parsing, no error will be raised.
498b883
+
498b883
    .. versionchanged:: 3.6
498b883
       Out-of-range port numbers now raise :exc:`ValueError`, instead of
498b883
       returning :const:`None`.
498b883
 
498b883
+   .. versionchanged:: 3.7.3
498b883
+      Characters that affect netloc parsing under NFKC normalization will
498b883
+      now raise :exc:`ValueError`.
498b883
+
498b883
 
498b883
 .. function:: urlunsplit(parts)
498b883
 
498b883
diff --git a/Lib/test/test_urlparse.py b/Lib/test/test_urlparse.py
498b883
index be50b47603aa..e6638aee2244 100644
498b883
--- a/Lib/test/test_urlparse.py
498b883
+++ b/Lib/test/test_urlparse.py
498b883
@@ -1,3 +1,5 @@
498b883
+import sys
498b883
+import unicodedata
498b883
 import unittest
498b883
 import urllib.parse
498b883
 
498b883
@@ -984,6 +986,27 @@ def test_all(self):
498b883
                 expected.append(name)
498b883
         self.assertCountEqual(urllib.parse.__all__, expected)
498b883
 
498b883
+    def test_urlsplit_normalization(self):
498b883
+        # Certain characters should never occur in the netloc,
498b883
+        # including under normalization.
498b883
+        # Ensure that ALL of them are detected and cause an error
498b883
+        illegal_chars = '/:#?@'
498b883
+        hex_chars = {'{:04X}'.format(ord(c)) for c in illegal_chars}
498b883
+        denorm_chars = [
498b883
+            c for c in map(chr, range(128, sys.maxunicode))
498b883
+            if (hex_chars & set(unicodedata.decomposition(c).split()))
498b883
+            and c not in illegal_chars
498b883
+        ]
498b883
+        # Sanity check that we found at least one such character
498b883
+        self.assertIn('\u2100', denorm_chars)
498b883
+        self.assertIn('\uFF03', denorm_chars)
498b883
+
498b883
+        for scheme in ["http", "https", "ftp"]:
498b883
+            for c in denorm_chars:
498b883
+                url = "{}://netloc{}false.netloc/path".format(scheme, c)
498b883
+                with self.subTest(url=url, char='{:04X}'.format(ord(c))):
498b883
+                    with self.assertRaises(ValueError):
498b883
+                        urllib.parse.urlsplit(url)
498b883
 
498b883
 class Utility_Tests(unittest.TestCase):
498b883
     """Testcase to test the various utility functions in the urllib."""
498b883
diff --git a/Lib/urllib/parse.py b/Lib/urllib/parse.py
498b883
index f691ab74f87f..39c5d6a80824 100644
498b883
--- a/Lib/urllib/parse.py
498b883
+++ b/Lib/urllib/parse.py
498b883
@@ -391,6 +391,21 @@ def _splitnetloc(url, start=0):
498b883
             delim = min(delim, wdelim)     # use earliest delim position
498b883
     return url[start:delim], url[delim:]   # return (domain, rest)
498b883
 
498b883
+def _checknetloc(netloc):
498b883
+    if not netloc or netloc.isascii():
498b883
+        return
498b883
+    # looking for characters like \u2100 that expand to 'a/c'
498b883
+    # IDNA uses NFKC equivalence, so normalize for this check
498b883
+    import unicodedata
498b883
+    netloc2 = unicodedata.normalize('NFKC', netloc)
498b883
+    if netloc == netloc2:
498b883
+        return
498b883
+    _, _, netloc = netloc.rpartition('@') # anything to the left of '@' is okay
498b883
+    for c in '/?#@:':
498b883
+        if c in netloc2:
498b883
+            raise ValueError("netloc '" + netloc2 + "' contains invalid " +
498b883
+                             "characters under NFKC normalization")
498b883
+
498b883
 def urlsplit(url, scheme='', allow_fragments=True):
498b883
     """Parse a URL into 5 components:
498b883
     <scheme>://<netloc>/<path>?<query>#<fragment>
498b883
@@ -419,6 +434,7 @@ def urlsplit(url, scheme='', allow_fragments=True):
498b883
                 url, fragment = url.split('#', 1)
498b883
             if '?' in url:
498b883
                 url, query = url.split('?', 1)
498b883
+            _checknetloc(netloc)
498b883
             v = SplitResult('http', netloc, url, query, fragment)
498b883
             _parse_cache[key] = v
498b883
             return _coerce_result(v)
498b883
@@ -442,6 +458,7 @@ def urlsplit(url, scheme='', allow_fragments=True):
498b883
         url, fragment = url.split('#', 1)
498b883
     if '?' in url:
498b883
         url, query = url.split('?', 1)
498b883
+    _checknetloc(netloc)
498b883
     v = SplitResult(scheme, netloc, url, query, fragment)
498b883
     _parse_cache[key] = v
498b883
     return _coerce_result(v)
498b883
diff --git a/Misc/NEWS.d/next/Security/2019-03-06-09-38-40.bpo-36216.6q1m4a.rst b/Misc/NEWS.d/next/Security/2019-03-06-09-38-40.bpo-36216.6q1m4a.rst
498b883
new file mode 100644
498b883
index 000000000000..5546394157f9
498b883
--- /dev/null
498b883
+++ b/Misc/NEWS.d/next/Security/2019-03-06-09-38-40.bpo-36216.6q1m4a.rst
498b883
@@ -0,0 +1,3 @@
498b883
+Changes urlsplit() to raise ValueError when the URL contains characters that
498b883
+decompose under IDNA encoding (NFKC-normalization) into characters that
498b883
+affect how the URL is parsed.