Blame SOURCES/00354-cve-2020-26116-http-request-method-crlf-injection-in-httplib.patch

05b462
diff --git a/Lib/httplib.py b/Lib/httplib.py
05b462
index fcc4152..a636774 100644
05b462
--- a/Lib/httplib.py
05b462
+++ b/Lib/httplib.py
05b462
@@ -257,6 +257,10 @@ _contains_disallowed_url_pchar_re = re.compile('[\x00-\x20\x7f-\xff]')
05b462
 #  _is_allowed_url_pchars_re = re.compile(r"^[/!$&'()*+,;=:@%a-zA-Z0-9._~-]+$")
05b462
 # We are more lenient for assumed real world compatibility purposes.
05b462
 
05b462
+# These characters are not allowed within HTTP method names
05b462
+# to prevent http header injection.
05b462
+_contains_disallowed_method_pchar_re = re.compile('[\x00-\x1f]')
05b462
+
05b462
 # We always set the Content-Length header for these methods because some
05b462
 # servers will otherwise respond with a 411
05b462
 _METHODS_EXPECTING_BODY = {'PATCH', 'POST', 'PUT'}
05b462
@@ -935,6 +939,8 @@ class HTTPConnection:
05b462
         else:
05b462
             raise CannotSendRequest()
05b462
 
05b462
+        self._validate_method(method)
05b462
+
05b462
         # Save the method for use later in the response phase
05b462
         self._method = method
05b462
 
05b462
@@ -1020,6 +1026,16 @@ class HTTPConnection:
05b462
         # On Python 2, request is already encoded (default)
05b462
         return request
05b462
 
05b462
+    def _validate_method(self, method):
05b462
+        """Validate a method name for putrequest."""
05b462
+        # prevent http header injection
05b462
+        match = _contains_disallowed_method_pchar_re.search(method)
05b462
+        if match:
05b462
+            raise ValueError(
05b462
+                    "method can't contain control characters. %r "
05b462
+                    "(found at least %r)"
05b462
+                    % (method, match.group()))
05b462
+
05b462
     def _validate_path(self, url):
05b462
         """Validate a url for putrequest."""
05b462
         # Prevent CVE-2019-9740.
05b462
diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py
05b462
index d8a57f7..96a61dd 100644
05b462
--- a/Lib/test/test_httplib.py
05b462
+++ b/Lib/test/test_httplib.py
05b462
@@ -385,6 +385,29 @@ class HeaderTests(TestCase):
05b462
                 conn.putheader(name, value)
05b462
 
05b462
 
05b462
+class HttpMethodTests(TestCase):
05b462
+    def test_invalid_method_names(self):
05b462
+        methods = (
05b462
+            'GET\r',
05b462
+            'POST\n',
05b462
+            'PUT\n\r',
05b462
+            'POST\nValue',
05b462
+            'POST\nHOST:abc',
05b462
+            'GET\nrHost:abc\n',
05b462
+            'POST\rRemainder:\r',
05b462
+            'GET\rHOST:\n',
05b462
+            '\nPUT'
05b462
+        )
05b462
+
05b462
+        for method in methods:
05b462
+            with self.assertRaisesRegexp(
05b462
+                    ValueError, "method can't contain control characters"):
05b462
+                conn = httplib.HTTPConnection('example.com')
05b462
+                conn.sock = FakeSocket(None)
05b462
+                conn.request(method=method, url="/")
05b462
+
05b462
+
05b462
+
05b462
 class BasicTest(TestCase):
05b462
     def test_status_lines(self):
05b462
         # Test HTTP status lines
05b462
@@ -1009,9 +1032,9 @@ class TunnelTests(TestCase):
05b462
 
05b462
 @test_support.reap_threads
05b462
 def test_main(verbose=None):
05b462
-    test_support.run_unittest(HeaderTests, OfflineTest, BasicTest, TimeoutTest,
05b462
-                              HTTPTest, HTTPSTest, SourceAddressTest,
05b462
-                              TunnelTests)
05b462
+    test_support.run_unittest(HeaderTests, OfflineTest, HttpMethodTests,
05b462
+                              BasicTest, TimeoutTest, HTTPTest, HTTPSTest,
05b462
+                              SourceAddressTest, TunnelTests)
05b462
 
05b462
 if __name__ == '__main__':
05b462
     test_main()