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

be5967
diff --git a/Lib/http/client.py b/Lib/http/client.py
be5967
index f0d2642..0a044e9 100644
be5967
--- a/Lib/http/client.py
be5967
+++ b/Lib/http/client.py
be5967
@@ -151,6 +151,10 @@ _contains_disallowed_url_pchar_re = re.compile('[\x00-\x20\x7f]')
be5967
 #  _is_allowed_url_pchars_re = re.compile(r"^[/!$&'()*+,;=:@%a-zA-Z0-9._~-]+$")
be5967
 # We are more lenient for assumed real world compatibility purposes.
be5967
 
be5967
+# These characters are not allowed within HTTP method names
be5967
+# to prevent http header injection.
be5967
+_contains_disallowed_method_pchar_re = re.compile('[\x00-\x1f]')
be5967
+
be5967
 # We always set the Content-Length header for these methods because some
be5967
 # servers will otherwise respond with a 411
be5967
 _METHODS_EXPECTING_BODY = {'PATCH', 'POST', 'PUT'}
be5967
@@ -1117,6 +1121,8 @@ class HTTPConnection:
be5967
         else:
be5967
             raise CannotSendRequest(self.__state)
be5967
 
be5967
+        self._validate_method(method)
be5967
+
be5967
         # Save the method we use, we need it later in the response phase
be5967
         self._method = method
be5967
         if not url:
be5967
@@ -1207,6 +1213,15 @@ class HTTPConnection:
be5967
             # For HTTP/1.0, the server will assume "not chunked"
be5967
             pass
be5967
 
be5967
+    def _validate_method(self, method):
be5967
+        """Validate a method name for putrequest."""
be5967
+        # prevent http header injection
be5967
+        match = _contains_disallowed_method_pchar_re.search(method)
be5967
+        if match:
be5967
+            raise ValueError(
be5967
+                    f"method can't contain control characters. {method!r} "
be5967
+                    f"(found at least {match.group()!r})")
be5967
+
be5967
     def putheader(self, header, *values):
be5967
         """Send a request header line to the server.
be5967
 
be5967
diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py
be5967
index 5795b7a..af0350f 100644
be5967
--- a/Lib/test/test_httplib.py
be5967
+++ b/Lib/test/test_httplib.py
be5967
@@ -359,6 +359,28 @@ class HeaderTests(TestCase):
be5967
         self.assertEqual(lines[2], "header: Second: val")
be5967
 
be5967
 
be5967
+class HttpMethodTests(TestCase):
be5967
+    def test_invalid_method_names(self):
be5967
+        methods = (
be5967
+            'GET\r',
be5967
+            'POST\n',
be5967
+            'PUT\n\r',
be5967
+            'POST\nValue',
be5967
+            'POST\nHOST:abc',
be5967
+            'GET\nrHost:abc\n',
be5967
+            'POST\rRemainder:\r',
be5967
+            'GET\rHOST:\n',
be5967
+            '\nPUT'
be5967
+        )
be5967
+
be5967
+        for method in methods:
be5967
+            with self.assertRaisesRegex(
be5967
+                    ValueError, "method can't contain control characters"):
be5967
+                conn = client.HTTPConnection('example.com')
be5967
+                conn.sock = FakeSocket(None)
be5967
+                conn.request(method=method, url="/")
be5967
+
be5967
+
be5967
 class TransferEncodingTest(TestCase):
be5967
     expected_body = b"It's just a flesh wound"
be5967