Blame SOURCES/00377-CVE-2022-0391.patch

91f5cf
From 6c472d3a1d334d4eeb4a25eba7bf3b01611bf667 Mon Sep 17 00:00:00 2001
91f5cf
From: "Miss Islington (bot)"
91f5cf
 <31488909+miss-islington@users.noreply.github.com>
91f5cf
Date: Thu, 6 May 2021 09:56:01 -0700
91f5cf
Subject: [PATCH] [3.6] bpo-43882 - urllib.parse should sanitize urls
91f5cf
 containing ASCII newline and tabs (GH-25924)
91f5cf
91f5cf
Co-authored-by: Gregory P. Smith <greg@krypto.org>
91f5cf
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
91f5cf
(cherry picked from commit 76cd81d60310d65d01f9d7b48a8985d8ab89c8b4)
91f5cf
Co-authored-by: Senthil Kumaran <senthil@uthcode.com>
91f5cf
(cherry picked from commit 515a7bc4e13645d0945b46a8e1d9102b918cd407)
91f5cf
91f5cf
Co-authored-by: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
91f5cf
---
91f5cf
 Doc/library/urllib.parse.rst                  | 13 +++++
91f5cf
 Lib/test/test_urlparse.py                     | 48 +++++++++++++++++++
91f5cf
 Lib/urllib/parse.py                           | 10 ++++
91f5cf
 .../2021-04-25-07-46-37.bpo-43882.Jpwx85.rst  |  6 +++
91f5cf
 4 files changed, 77 insertions(+)
91f5cf
 create mode 100644 Misc/NEWS.d/next/Security/2021-04-25-07-46-37.bpo-43882.Jpwx85.rst
91f5cf
91f5cf
diff --git a/Doc/library/urllib.parse.rst b/Doc/library/urllib.parse.rst
91f5cf
index 3c2e37ef2093a..b717d7cc05b2e 100644
91f5cf
--- a/Doc/library/urllib.parse.rst
91f5cf
+++ b/Doc/library/urllib.parse.rst
91f5cf
@@ -288,6 +288,9 @@ or on combining URL components into a URL string.
91f5cf
    ``#``, ``@``, or ``:`` will raise a :exc:`ValueError`. If the URL is
91f5cf
    decomposed before parsing, no error will be raised.
91f5cf
 
91f5cf
+   Following the `WHATWG spec`_ that updates RFC 3986, ASCII newline
91f5cf
+   ``\n``, ``\r`` and tab ``\t`` characters are stripped from the URL.
91f5cf
+
91f5cf
    .. versionchanged:: 3.6
91f5cf
       Out-of-range port numbers now raise :exc:`ValueError`, instead of
91f5cf
       returning :const:`None`.
91f5cf
@@ -296,6 +299,10 @@ or on combining URL components into a URL string.
91f5cf
       Characters that affect netloc parsing under NFKC normalization will
91f5cf
       now raise :exc:`ValueError`.
91f5cf
 
91f5cf
+   .. versionchanged:: 3.6.14
91f5cf
+      ASCII newline and tab characters are stripped from the URL.
91f5cf
+
91f5cf
+.. _WHATWG spec: https://url.spec.whatwg.org/#concept-basic-url-parser
91f5cf
 
91f5cf
 .. function:: urlunsplit(parts)
91f5cf
 
91f5cf
@@ -633,6 +640,10 @@ task isn't already covered by the URL parsing functions above.
91f5cf
 
91f5cf
 .. seealso::
91f5cf
 
91f5cf
+   `WHATWG`_ -  URL Living standard
91f5cf
+      Working Group for the URL Standard that defines URLs, domains, IP addresses, the
91f5cf
+      application/x-www-form-urlencoded format, and their API.
91f5cf
+
91f5cf
    :rfc:`3986` - Uniform Resource Identifiers
91f5cf
       This is the current standard (STD66). Any changes to urllib.parse module
91f5cf
       should conform to this. Certain deviations could be observed, which are
91f5cf
@@ -656,3 +667,5 @@ task isn't already covered by the URL parsing functions above.
91f5cf
 
91f5cf
    :rfc:`1738` - Uniform Resource Locators (URL)
91f5cf
       This specifies the formal syntax and semantics of absolute URLs.
91f5cf
+
91f5cf
+.. _WHATWG: https://url.spec.whatwg.org/
91f5cf
diff --git a/Lib/test/test_urlparse.py b/Lib/test/test_urlparse.py
91f5cf
index e3088b2f39bd7..3509278a01694 100644
91f5cf
--- a/Lib/test/test_urlparse.py
91f5cf
+++ b/Lib/test/test_urlparse.py
91f5cf
@@ -612,6 +612,54 @@ def test_urlsplit_attributes(self):
91f5cf
         with self.assertRaisesRegex(ValueError, "out of range"):
91f5cf
             p.port
91f5cf
 
91f5cf
+    def test_urlsplit_remove_unsafe_bytes(self):
91f5cf
+        # Remove ASCII tabs and newlines from input, for http common case scenario.
91f5cf
+        url = "h\nttp://www.python\n.org\t/java\nscript:\talert('msg\r\n')/?query\n=\tsomething#frag\nment"
91f5cf
+        p = urllib.parse.urlsplit(url)
91f5cf
+        self.assertEqual(p.scheme, "http")
91f5cf
+        self.assertEqual(p.netloc, "www.python.org")
91f5cf
+        self.assertEqual(p.path, "/javascript:alert('msg')/")
91f5cf
+        self.assertEqual(p.query, "query=something")
91f5cf
+        self.assertEqual(p.fragment, "fragment")
91f5cf
+        self.assertEqual(p.username, None)
91f5cf
+        self.assertEqual(p.password, None)
91f5cf
+        self.assertEqual(p.hostname, "www.python.org")
91f5cf
+        self.assertEqual(p.port, None)
91f5cf
+        self.assertEqual(p.geturl(), "http://www.python.org/javascript:alert('msg')/?query=something#fragment")
91f5cf
+
91f5cf
+        # Remove ASCII tabs and newlines from input as bytes, for http common case scenario.
91f5cf
+        url = b"h\nttp://www.python\n.org\t/java\nscript:\talert('msg\r\n')/?query\n=\tsomething#frag\nment"
91f5cf
+        p = urllib.parse.urlsplit(url)
91f5cf
+        self.assertEqual(p.scheme, b"http")
91f5cf
+        self.assertEqual(p.netloc, b"www.python.org")
91f5cf
+        self.assertEqual(p.path, b"/javascript:alert('msg')/")
91f5cf
+        self.assertEqual(p.query, b"query=something")
91f5cf
+        self.assertEqual(p.fragment, b"fragment")
91f5cf
+        self.assertEqual(p.username, None)
91f5cf
+        self.assertEqual(p.password, None)
91f5cf
+        self.assertEqual(p.hostname, b"www.python.org")
91f5cf
+        self.assertEqual(p.port, None)
91f5cf
+        self.assertEqual(p.geturl(), b"http://www.python.org/javascript:alert('msg')/?query=something#fragment")
91f5cf
+
91f5cf
+        # any scheme
91f5cf
+        url = "x-new-scheme\t://www.python\n.org\t/java\nscript:\talert('msg\r\n')/?query\n=\tsomething#frag\nment"
91f5cf
+        p = urllib.parse.urlsplit(url)
91f5cf
+        self.assertEqual(p.geturl(), "x-new-scheme://www.python.org/javascript:alert('msg')/?query=something#fragment")
91f5cf
+
91f5cf
+        # Remove ASCII tabs and newlines from input as bytes, any scheme.
91f5cf
+        url = b"x-new-scheme\t://www.python\n.org\t/java\nscript:\talert('msg\r\n')/?query\n=\tsomething#frag\nment"
91f5cf
+        p = urllib.parse.urlsplit(url)
91f5cf
+        self.assertEqual(p.geturl(), b"x-new-scheme://www.python.org/javascript:alert('msg')/?query=something#fragment")
91f5cf
+
91f5cf
+        # Unsafe bytes is not returned from urlparse cache.
91f5cf
+        # scheme is stored after parsing, sending an scheme with unsafe bytes *will not* return an unsafe scheme
91f5cf
+        url = "https://www.python\n.org\t/java\nscript:\talert('msg\r\n')/?query\n=\tsomething#frag\nment"
91f5cf
+        scheme = "htt\nps"
91f5cf
+        for _ in range(2):
91f5cf
+            p = urllib.parse.urlsplit(url, scheme=scheme)
91f5cf
+            self.assertEqual(p.scheme, "https")
91f5cf
+            self.assertEqual(p.geturl(), "https://www.python.org/javascript:alert('msg')/?query=something#fragment")
91f5cf
+
91f5cf
     def test_attributes_bad_port(self):
91f5cf
         """Check handling of invalid ports."""
91f5cf
         for bytes in (False, True):
91f5cf
diff --git a/Lib/urllib/parse.py b/Lib/urllib/parse.py
91f5cf
index 66056bf589bf6..ac6e7a9cee0b9 100644
91f5cf
--- a/Lib/urllib/parse.py
91f5cf
+++ b/Lib/urllib/parse.py
91f5cf
@@ -76,6 +76,9 @@
91f5cf
                 '0123456789'
91f5cf
                 '+-.')
91f5cf
 
91f5cf
+# Unsafe bytes to be removed per WHATWG spec
91f5cf
+_UNSAFE_URL_BYTES_TO_REMOVE = ['\t', '\r', '\n']
91f5cf
+
91f5cf
 # XXX: Consider replacing with functools.lru_cache
91f5cf
 MAX_CACHE_SIZE = 20
91f5cf
 _parse_cache = {}
91f5cf
@@ -409,6 +412,11 @@ def _checknetloc(netloc):
91f5cf
             raise ValueError("netloc '" + netloc + "' contains invalid " +
91f5cf
                              "characters under NFKC normalization")
91f5cf
 
91f5cf
+def _remove_unsafe_bytes_from_url(url):
91f5cf
+    for b in _UNSAFE_URL_BYTES_TO_REMOVE:
91f5cf
+        url = url.replace(b, "")
91f5cf
+    return url
91f5cf
+
91f5cf
 def urlsplit(url, scheme='', allow_fragments=True):
91f5cf
     """Parse a URL into 5 components:
91f5cf
     <scheme>://<netloc>/<path>?<query>#<fragment>
91f5cf
@@ -416,6 +424,8 @@ def urlsplit(url, scheme='', allow_fragments=True):
91f5cf
     Note that we don't break the components up in smaller bits
91f5cf
     (e.g. netloc is a single string) and we don't expand % escapes."""
91f5cf
     url, scheme, _coerce_result = _coerce_args(url, scheme)
91f5cf
+    url = _remove_unsafe_bytes_from_url(url)
91f5cf
+    scheme = _remove_unsafe_bytes_from_url(scheme)
91f5cf
     allow_fragments = bool(allow_fragments)
91f5cf
     key = url, scheme, allow_fragments, type(url), type(scheme)
91f5cf
     cached = _parse_cache.get(key, None)
91f5cf
diff --git a/Misc/NEWS.d/next/Security/2021-04-25-07-46-37.bpo-43882.Jpwx85.rst b/Misc/NEWS.d/next/Security/2021-04-25-07-46-37.bpo-43882.Jpwx85.rst
91f5cf
new file mode 100644
91f5cf
index 0000000000000..a326d079dff4a
91f5cf
--- /dev/null
91f5cf
+++ b/Misc/NEWS.d/next/Security/2021-04-25-07-46-37.bpo-43882.Jpwx85.rst
91f5cf
@@ -0,0 +1,6 @@
91f5cf
+The presence of newline or tab characters in parts of a URL could allow
91f5cf
+some forms of attacks.
91f5cf
+
91f5cf
+Following the controlling specification for URLs defined by WHATWG
91f5cf
+:func:`urllib.parse` now removes ASCII newlines and tabs from URLs,
91f5cf
+preventing such attacks.