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

fdff70
diff --git a/Lib/httplib.py b/Lib/httplib.py
fdff70
index fc8e895..0f43e3a 100644
fdff70
--- a/Lib/httplib.py
fdff70
+++ b/Lib/httplib.py
fdff70
@@ -257,6 +257,10 @@ _contains_disallowed_url_pchar_re = re.compile('[\x00-\x20\x7f-\xff]')
fdff70
 #  _is_allowed_url_pchars_re = re.compile(r"^[/!$&'()*+,;=:@%a-zA-Z0-9._~-]+$")
fdff70
 # We are more lenient for assumed real world compatibility purposes.
fdff70
 
fdff70
+# These characters are not allowed within HTTP method names
fdff70
+# to prevent http header injection.
fdff70
+_contains_disallowed_method_pchar_re = re.compile('[\x00-\x1f]')
fdff70
+
fdff70
 class HTTPMessage(mimetools.Message):
fdff70
 
fdff70
     def addheader(self, key, value):
fdff70
@@ -931,6 +935,8 @@ class HTTPConnection:
fdff70
         else:
fdff70
             raise CannotSendRequest()
fdff70
 
fdff70
+        self._validate_method(method)
fdff70
+
fdff70
         # Save the method we use, we need it later in the response phase
fdff70
         self._method = method
fdff70
         if not url:
fdff70
@@ -1016,6 +1022,16 @@ class HTTPConnection:
fdff70
             # For HTTP/1.0, the server will assume "not chunked"
fdff70
             pass
fdff70
 
fdff70
+    def _validate_method(self, method):
fdff70
+        """Validate a method name for putrequest."""
fdff70
+        # prevent http header injection
fdff70
+        match = _contains_disallowed_method_pchar_re.search(method)
fdff70
+        if match:
fdff70
+            raise ValueError(
fdff70
+                    "method can't contain control characters. %r "
fdff70
+                    "(found at least %r)"
fdff70
+                    % (method, match.group()))
fdff70
+
fdff70
     def putheader(self, header, *values):
fdff70
         """Send a request header line to the server.
fdff70
 
fdff70
diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py
fdff70
index 9db30cc..6ad1427 100644
fdff70
--- a/Lib/test/test_httplib.py
fdff70
+++ b/Lib/test/test_httplib.py
fdff70
@@ -214,6 +214,29 @@ class HeaderTests(TestCase):
fdff70
                 conn.putheader(name, value)
fdff70
 
fdff70
 
fdff70
+class HttpMethodTests(TestCase):
fdff70
+    def test_invalid_method_names(self):
fdff70
+        methods = (
fdff70
+            'GET\r',
fdff70
+            'POST\n',
fdff70
+            'PUT\n\r',
fdff70
+            'POST\nValue',
fdff70
+            'POST\nHOST:abc',
fdff70
+            'GET\nrHost:abc\n',
fdff70
+            'POST\rRemainder:\r',
fdff70
+            'GET\rHOST:\n',
fdff70
+            '\nPUT'
fdff70
+        )
fdff70
+
fdff70
+        for method in methods:
fdff70
+            with self.assertRaisesRegexp(
fdff70
+                    ValueError, "method can't contain control characters"):
fdff70
+                conn = httplib.HTTPConnection('example.com')
fdff70
+                conn.sock = FakeSocket(None)
fdff70
+                conn.request(method=method, url="/")
fdff70
+
fdff70
+
fdff70
+
fdff70
 class BasicTest(TestCase):
fdff70
     def test_status_lines(self):
fdff70
         # Test HTTP status lines
fdff70
@@ -789,9 +812,9 @@ class TunnelTests(TestCase):
fdff70
 
fdff70
 @test_support.reap_threads
fdff70
 def test_main(verbose=None):
fdff70
-    test_support.run_unittest(HeaderTests, OfflineTest, BasicTest, TimeoutTest,
fdff70
-                              HTTPTest, HTTPSTest, SourceAddressTest,
fdff70
-                               TunnelTests)
fdff70
+    test_support.run_unittest(HeaderTests, OfflineTest, HttpMethodTests,
fdff70
+                              BasicTest, TimeoutTest, HTTPTest, HTTPSTest,
fdff70
+                              SourceAddressTest, TunnelTests)
fdff70
 
fdff70
 if __name__ == '__main__':
fdff70
     test_main()