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

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