Blame SOURCES/00386-cve-2021-28861.patch

8c28fd
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
8c28fd
From: "Miss Islington (bot)"
8c28fd
 <31488909+miss-islington@users.noreply.github.com>
8c28fd
Date: Wed, 22 Jun 2022 15:05:00 -0700
8c28fd
Subject: [PATCH] 00386: CVE-2021-28861
8c28fd
8c28fd
Fix an open redirection vulnerability in the `http.server` module when
8c28fd
an URI path starts with `//` that could produce a 301 Location header
8c28fd
with a misleading target.  Vulnerability discovered, and logic fix
8c28fd
proposed, by Hamza Avvan (@hamzaavvan).
8c28fd
8c28fd
Test and comments authored by Gregory P. Smith [Google].
8c28fd
(cherry picked from commit 4abab6b603dd38bec1168e9a37c40a48ec89508e)
8c28fd
8c28fd
Upstream: https://github.com/python/cpython/pull/93879
8c28fd
Tracking bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2120642
8c28fd
8c28fd
Co-authored-by: Gregory P. Smith <greg@krypto.org>
8c28fd
---
8c28fd
 Lib/http/server.py                            |  7 +++
8c28fd
 Lib/test/test_httpservers.py                  | 53 ++++++++++++++++++-
8c28fd
 ...2-06-15-20-09-23.gh-issue-87389.QVaC3f.rst |  3 ++
8c28fd
 3 files changed, 61 insertions(+), 2 deletions(-)
8c28fd
 create mode 100644 Misc/NEWS.d/next/Security/2022-06-15-20-09-23.gh-issue-87389.QVaC3f.rst
8c28fd
8c28fd
diff --git a/Lib/http/server.py b/Lib/http/server.py
8c28fd
index 60a4dadf03..ce05be13d3 100644
8c28fd
--- a/Lib/http/server.py
8c28fd
+++ b/Lib/http/server.py
8c28fd
@@ -323,6 +323,13 @@ class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
8c28fd
             return False
8c28fd
         self.command, self.path, self.request_version = command, path, version
8c28fd
 
8c28fd
+        # gh-87389: The purpose of replacing '//' with '/' is to protect
8c28fd
+        # against open redirect attacks possibly triggered if the path starts
8c28fd
+        # with '//' because http clients treat //path as an absolute URI
8c28fd
+        # without scheme (similar to http://path) rather than a path.
8c28fd
+        if self.path.startswith('//'):
8c28fd
+            self.path = '/' + self.path.lstrip('/')  # Reduce to a single /
8c28fd
+
8c28fd
         # Examine the headers and look for a Connection directive.
8c28fd
         try:
8c28fd
             self.headers = http.client.parse_headers(self.rfile,
8c28fd
diff --git a/Lib/test/test_httpservers.py b/Lib/test/test_httpservers.py
8c28fd
index 66e937e04b..5a0a7c3f74 100644
8c28fd
--- a/Lib/test/test_httpservers.py
8c28fd
+++ b/Lib/test/test_httpservers.py
8c28fd
@@ -324,7 +324,7 @@ class SimpleHTTPServerTestCase(BaseTestCase):
8c28fd
         pass
8c28fd
 
8c28fd
     def setUp(self):
8c28fd
-        BaseTestCase.setUp(self)
8c28fd
+        super().setUp()
8c28fd
         self.cwd = os.getcwd()
8c28fd
         basetempdir = tempfile.gettempdir()
8c28fd
         os.chdir(basetempdir)
8c28fd
@@ -343,7 +343,7 @@ class SimpleHTTPServerTestCase(BaseTestCase):
8c28fd
             except:
8c28fd
                 pass
8c28fd
         finally:
8c28fd
-            BaseTestCase.tearDown(self)
8c28fd
+            super().tearDown()
8c28fd
 
8c28fd
     def check_status_and_reason(self, response, status, data=None):
8c28fd
         def close_conn():
8c28fd
@@ -399,6 +399,55 @@ class SimpleHTTPServerTestCase(BaseTestCase):
8c28fd
         self.check_status_and_reason(response, HTTPStatus.OK,
8c28fd
                                      data=support.TESTFN_UNDECODABLE)
8c28fd
 
8c28fd
+    def test_get_dir_redirect_location_domain_injection_bug(self):
8c28fd
+        """Ensure //evil.co/..%2f../../X does not put //evil.co/ in Location.
8c28fd
+
8c28fd
+        //netloc/ in a Location header is a redirect to a new host.
8c28fd
+        https://github.com/python/cpython/issues/87389
8c28fd
+
8c28fd
+        This checks that a path resolving to a directory on our server cannot
8c28fd
+        resolve into a redirect to another server.
8c28fd
+        """
8c28fd
+        os.mkdir(os.path.join(self.tempdir, 'existing_directory'))
8c28fd
+        url = f'/python.org/..%2f..%2f..%2f..%2f..%2f../%0a%0d/../{self.tempdir_name}/existing_directory'
8c28fd
+        expected_location = f'{url}/'  # /python.org.../ single slash single prefix, trailing slash
8c28fd
+        # Canonicalizes to /tmp/tempdir_name/existing_directory which does
8c28fd
+        # exist and is a dir, triggering the 301 redirect logic.
8c28fd
+        response = self.request(url)
8c28fd
+        self.check_status_and_reason(response, HTTPStatus.MOVED_PERMANENTLY)
8c28fd
+        location = response.getheader('Location')
8c28fd
+        self.assertEqual(location, expected_location, msg='non-attack failed!')
8c28fd
+
8c28fd
+        # //python.org... multi-slash prefix, no trailing slash
8c28fd
+        attack_url = f'/{url}'
8c28fd
+        response = self.request(attack_url)
8c28fd
+        self.check_status_and_reason(response, HTTPStatus.MOVED_PERMANENTLY)
8c28fd
+        location = response.getheader('Location')
8c28fd
+        self.assertFalse(location.startswith('//'), msg=location)
8c28fd
+        self.assertEqual(location, expected_location,
8c28fd
+                msg='Expected Location header to start with a single / and '
8c28fd
+                'end with a / as this is a directory redirect.')
8c28fd
+
8c28fd
+        # ///python.org... triple-slash prefix, no trailing slash
8c28fd
+        attack3_url = f'//{url}'
8c28fd
+        response = self.request(attack3_url)
8c28fd
+        self.check_status_and_reason(response, HTTPStatus.MOVED_PERMANENTLY)
8c28fd
+        self.assertEqual(response.getheader('Location'), expected_location)
8c28fd
+
8c28fd
+        # If the second word in the http request (Request-URI for the http
8c28fd
+        # method) is a full URI, we don't worry about it, as that'll be parsed
8c28fd
+        # and reassembled as a full URI within BaseHTTPRequestHandler.send_head
8c28fd
+        # so no errant scheme-less //netloc//evil.co/ domain mixup can happen.
8c28fd
+        attack_scheme_netloc_2slash_url = f'https://pypi.org/{url}'
8c28fd
+        expected_scheme_netloc_location = f'{attack_scheme_netloc_2slash_url}/'
8c28fd
+        response = self.request(attack_scheme_netloc_2slash_url)
8c28fd
+        self.check_status_and_reason(response, HTTPStatus.MOVED_PERMANENTLY)
8c28fd
+        location = response.getheader('Location')
8c28fd
+        # We're just ensuring that the scheme and domain make it through, if
8c28fd
+        # there are or aren't multiple slashes at the start of the path that
8c28fd
+        # follows that isn't important in this Location: header.
8c28fd
+        self.assertTrue(location.startswith('https://pypi.org/'), msg=location)
8c28fd
+
8c28fd
     def test_get(self):
8c28fd
         #constructs the path relative to the root directory of the HTTPServer
8c28fd
         response = self.request(self.base_url + '/test')
8c28fd
diff --git a/Misc/NEWS.d/next/Security/2022-06-15-20-09-23.gh-issue-87389.QVaC3f.rst b/Misc/NEWS.d/next/Security/2022-06-15-20-09-23.gh-issue-87389.QVaC3f.rst
8c28fd
new file mode 100644
8c28fd
index 0000000000..029d437190
8c28fd
--- /dev/null
8c28fd
+++ b/Misc/NEWS.d/next/Security/2022-06-15-20-09-23.gh-issue-87389.QVaC3f.rst
8c28fd
@@ -0,0 +1,3 @@
8c28fd
+:mod:`http.server`: Fix an open redirection vulnerability in the HTTP server
8c28fd
+when an URI path starts with ``//``.  Vulnerability discovered, and initial
8c28fd
+fix proposed, by Hamza Avvan.