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