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

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