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

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