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

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