File CVE-2020-26116-httplib-header-injection.patch of Package python3-doc.16830
Index: Python-3.4.10/Lib/http/client.py
===================================================================
--- Python-3.4.10.orig/Lib/http/client.py
+++ Python-3.4.10/Lib/http/client.py
@@ -266,6 +266,10 @@ _contains_disallowed_url_pchar_re = re.c
# _is_allowed_url_pchars_re = re.compile(r"^[/!$&'()*+,;=:@%a-zA-Z0-9._~-]+$")
# We are more lenient for assumed real world compatibility purposes.
+# These characters are not allowed within HTTP method names
+# to prevent http header injection.
+_contains_disallowed_method_pchar_re = re.compile('[\x00-\x1f]')
+
# We always set the Content-Length header for these methods because some
# servers will otherwise respond with a 411
_METHODS_EXPECTING_BODY = {'PATCH', 'POST', 'PUT'}
@@ -1026,6 +1030,8 @@ class HTTPConnection:
else:
raise CannotSendRequest(self.__state)
+ self._validate_method(method)
+
# Save the method we use, we need it later in the response phase
self._method = method
if not url:
@@ -1124,6 +1130,15 @@ class HTTPConnection:
raise InvalidURL("URL can't contain control characters. {!r} ".format(host) +
"(found at least {!r})".format(match.group()))
+ def _validate_method(self, method):
+ """Validate a method name for putrequest."""
+ # prevent http header injection
+ match = _contains_disallowed_method_pchar_re.search(method)
+ if match:
+ raise ValueError(
+ "method can't contain control characters. %r (found at "
+ "least %r)" % (method, match.group()))
+
def putheader(self, header, *values):
"""Send a request header line to the server.
Index: Python-3.4.10/Lib/test/test_httplib.py
===================================================================
--- Python-3.4.10.orig/Lib/test/test_httplib.py
+++ Python-3.4.10/Lib/test/test_httplib.py
@@ -1223,12 +1223,33 @@ class TunnelTests(TestCase):
self.assertIn(b'Host: destination.com', self.conn.sock.data)
+class HttpMethodTests(TestCase):
+ def test_invalid_method_names(self):
+ methods = (
+ 'GET\r',
+ 'POST\n',
+ 'PUT\n\r',
+ 'POST\nValue',
+ 'POST\nHOST:abc',
+ 'GET\nrHost:abc\n',
+ 'POST\rRemainder:\r',
+ 'GET\rHOST:\n',
+ '\nPUT'
+ )
+
+ for method in methods:
+ with self.assertRaisesRegex(
+ ValueError, "method can't contain control characters"):
+ conn = client.HTTPConnection('example.com')
+ conn.sock = FakeSocket(None)
+ conn.request(method=method, url="/")
+
@support.reap_threads
def test_main(verbose=None):
support.run_unittest(HeaderTests, OfflineTest, BasicTest, TimeoutTest,
HTTPSTest, RequestBodyTest, SourceAddressTest,
- HTTPResponseTest, TunnelTests)
+ HTTPResponseTest, TunnelTests, HttpMethodTests)
if __name__ == '__main__':
test_main()