pviktori / rpms / python2

Forked from rpms/python2 6 years ago
Clone
2c50f13
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2c50f13
From: AMIR <31338382+amiremohamadi@users.noreply.github.com>
2c50f13
Date: Sun, 19 Jul 2020 00:46:10 +0430
2c50f13
Subject: [PATCH] 
2c50f13
 00354-cve-2020-26116-http-request-method-crlf-injection-in-httplib.patch
2c50f13
2c50f13
00354 #
2c50f13
Reject control chars in HTTP method in httplib.putrequest to prevent
2c50f13
HTTP header injection
2c50f13
2c50f13
Backported from Python 3.5-3.10 (and adjusted for py2's single-module httplib):
2c50f13
- https://bugs.python.org/issue39603
2c50f13
- https://github.com/python/cpython/pull/18485 (3.10)
2c50f13
- https://github.com/python/cpython/pull/21946 (3.5)
2c50f13
2c50f13
Co-authored-by: AMIR <31338382+amiremohamadi@users.noreply.github.com>
2c50f13
---
2c50f13
 Lib/httplib.py                                | 16 +++++++++++++
2c50f13
 Lib/test/test_httplib.py                      | 23 +++++++++++++++++++
2c50f13
 .../2020-02-12-14-17-39.bpo-39603.Gt3RSg.rst  |  2 ++
2c50f13
 3 files changed, 41 insertions(+)
2c50f13
 create mode 100644 Misc/NEWS.d/next/Security/2020-02-12-14-17-39.bpo-39603.Gt3RSg.rst
2c50f13
2c50f13
diff --git a/Lib/httplib.py b/Lib/httplib.py
2c50f13
index fcc4152aaf2..a63677477d5 100644
2c50f13
--- a/Lib/httplib.py
2c50f13
+++ b/Lib/httplib.py
2c50f13
@@ -257,6 +257,10 @@ _contains_disallowed_url_pchar_re = re.compile('[\x00-\x20\x7f-\xff]')
2c50f13
 #  _is_allowed_url_pchars_re = re.compile(r"^[/!$&'()*+,;=:@%a-zA-Z0-9._~-]+$")
2c50f13
 # We are more lenient for assumed real world compatibility purposes.
2c50f13
 
2c50f13
+# These characters are not allowed within HTTP method names
2c50f13
+# to prevent http header injection.
2c50f13
+_contains_disallowed_method_pchar_re = re.compile('[\x00-\x1f]')
2c50f13
+
2c50f13
 # We always set the Content-Length header for these methods because some
2c50f13
 # servers will otherwise respond with a 411
2c50f13
 _METHODS_EXPECTING_BODY = {'PATCH', 'POST', 'PUT'}
2c50f13
@@ -935,6 +939,8 @@ class HTTPConnection:
2c50f13
         else:
2c50f13
             raise CannotSendRequest()
2c50f13
 
2c50f13
+        self._validate_method(method)
2c50f13
+
2c50f13
         # Save the method for use later in the response phase
2c50f13
         self._method = method
2c50f13
 
2c50f13
@@ -1020,6 +1026,16 @@ class HTTPConnection:
2c50f13
         # On Python 2, request is already encoded (default)
2c50f13
         return request
2c50f13
 
2c50f13
+    def _validate_method(self, method):
2c50f13
+        """Validate a method name for putrequest."""
2c50f13
+        # prevent http header injection
2c50f13
+        match = _contains_disallowed_method_pchar_re.search(method)
2c50f13
+        if match:
2c50f13
+            raise ValueError(
2c50f13
+                    "method can't contain control characters. %r "
2c50f13
+                    "(found at least %r)"
2c50f13
+                    % (method, match.group()))
2c50f13
+
2c50f13
     def _validate_path(self, url):
2c50f13
         """Validate a url for putrequest."""
2c50f13
         # Prevent CVE-2019-9740.
2c50f13
diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py
2c50f13
index d8a57f73530..e295bb796ec 100644
2c50f13
--- a/Lib/test/test_httplib.py
2c50f13
+++ b/Lib/test/test_httplib.py
2c50f13
@@ -385,6 +385,28 @@ class HeaderTests(TestCase):
2c50f13
                 conn.putheader(name, value)
2c50f13
 
2c50f13
 
2c50f13
+class HttpMethodTests(TestCase):
2c50f13
+    def test_invalid_method_names(self):
2c50f13
+        methods = (
2c50f13
+            'GET\r',
2c50f13
+            'POST\n',
2c50f13
+            'PUT\n\r',
2c50f13
+            'POST\nValue',
2c50f13
+            'POST\nHOST:abc',
2c50f13
+            'GET\nrHost:abc\n',
2c50f13
+            'POST\rRemainder:\r',
2c50f13
+            'GET\rHOST:\n',
2c50f13
+            '\nPUT'
2c50f13
+        )
2c50f13
+
2c50f13
+        for method in methods:
2c50f13
+            with self.assertRaisesRegexp(
2c50f13
+                    ValueError, "method can't contain control characters"):
2c50f13
+                conn = httplib.HTTPConnection('example.com')
2c50f13
+                conn.sock = FakeSocket(None)
2c50f13
+                conn.request(method=method, url="/")
2c50f13
+
2c50f13
+
2c50f13
 class BasicTest(TestCase):
2c50f13
     def test_status_lines(self):
2c50f13
         # Test HTTP status lines
2c50f13
@@ -1010,6 +1032,7 @@ class TunnelTests(TestCase):
2c50f13
 @test_support.reap_threads
2c50f13
 def test_main(verbose=None):
2c50f13
     test_support.run_unittest(HeaderTests, OfflineTest, BasicTest, TimeoutTest,
2c50f13
+                              HttpMethodTests,
2c50f13
                               HTTPTest, HTTPSTest, SourceAddressTest,
2c50f13
                               TunnelTests)
2c50f13
 
2c50f13
diff --git a/Misc/NEWS.d/next/Security/2020-02-12-14-17-39.bpo-39603.Gt3RSg.rst b/Misc/NEWS.d/next/Security/2020-02-12-14-17-39.bpo-39603.Gt3RSg.rst
2c50f13
new file mode 100644
2c50f13
index 00000000000..990affc3edd
2c50f13
--- /dev/null
2c50f13
+++ b/Misc/NEWS.d/next/Security/2020-02-12-14-17-39.bpo-39603.Gt3RSg.rst
2c50f13
@@ -0,0 +1,2 @@
2c50f13
+Prevent http header injection by rejecting control characters in
2c50f13
+http.client.putrequest(...).