|
|
31dc771 |
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
|
31dc771 |
From: AMIR <31338382+amiremohamadi@users.noreply.github.com>
|
|
|
31dc771 |
Date: Sun, 19 Jul 2020 00:46:10 +0430
|
|
|
31dc771 |
Subject: [PATCH]
|
|
|
31dc771 |
00354-cve-2020-26116-http-request-method-crlf-injection-in-httplib.patch
|
|
|
31dc771 |
|
|
|
31dc771 |
00354 #
|
|
|
31dc771 |
Reject control chars in HTTP method in httplib.putrequest to prevent
|
|
|
31dc771 |
HTTP header injection
|
|
|
31dc771 |
|
|
|
31dc771 |
Backported from Python 3.5-3.10 (and adjusted for py2's single-module httplib):
|
|
|
31dc771 |
- https://bugs.python.org/issue39603
|
|
|
31dc771 |
- https://github.com/python/cpython/pull/18485 (3.10)
|
|
|
31dc771 |
- https://github.com/python/cpython/pull/21946 (3.5)
|
|
|
31dc771 |
|
|
|
31dc771 |
Co-authored-by: AMIR <31338382+amiremohamadi@users.noreply.github.com>
|
|
|
31dc771 |
---
|
|
|
31dc771 |
Lib/httplib.py | 16 +++++++++++++
|
|
|
31dc771 |
Lib/test/test_httplib.py | 23 +++++++++++++++++++
|
|
|
31dc771 |
.../2020-02-12-14-17-39.bpo-39603.Gt3RSg.rst | 2 ++
|
|
|
31dc771 |
3 files changed, 41 insertions(+)
|
|
|
31dc771 |
create mode 100644 Misc/NEWS.d/next/Security/2020-02-12-14-17-39.bpo-39603.Gt3RSg.rst
|
|
|
31dc771 |
|
|
|
31dc771 |
diff --git a/Lib/httplib.py b/Lib/httplib.py
|
|
|
31dc771 |
index fcc4152aaf2..a63677477d5 100644
|
|
|
31dc771 |
--- a/Lib/httplib.py
|
|
|
31dc771 |
+++ b/Lib/httplib.py
|
|
|
31dc771 |
@@ -257,6 +257,10 @@ _contains_disallowed_url_pchar_re = re.compile('[\x00-\x20\x7f-\xff]')
|
|
|
31dc771 |
# _is_allowed_url_pchars_re = re.compile(r"^[/!$&'()*+,;=:@%a-zA-Z0-9._~-]+$")
|
|
|
31dc771 |
# We are more lenient for assumed real world compatibility purposes.
|
|
|
31dc771 |
|
|
|
31dc771 |
+# These characters are not allowed within HTTP method names
|
|
|
31dc771 |
+# to prevent http header injection.
|
|
|
31dc771 |
+_contains_disallowed_method_pchar_re = re.compile('[\x00-\x1f]')
|
|
|
31dc771 |
+
|
|
|
31dc771 |
# We always set the Content-Length header for these methods because some
|
|
|
31dc771 |
# servers will otherwise respond with a 411
|
|
|
31dc771 |
_METHODS_EXPECTING_BODY = {'PATCH', 'POST', 'PUT'}
|
|
|
31dc771 |
@@ -935,6 +939,8 @@ class HTTPConnection:
|
|
|
31dc771 |
else:
|
|
|
31dc771 |
raise CannotSendRequest()
|
|
|
31dc771 |
|
|
|
31dc771 |
+ self._validate_method(method)
|
|
|
31dc771 |
+
|
|
|
31dc771 |
# Save the method for use later in the response phase
|
|
|
31dc771 |
self._method = method
|
|
|
31dc771 |
|
|
|
31dc771 |
@@ -1020,6 +1026,16 @@ class HTTPConnection:
|
|
|
31dc771 |
# On Python 2, request is already encoded (default)
|
|
|
31dc771 |
return request
|
|
|
31dc771 |
|
|
|
31dc771 |
+ def _validate_method(self, method):
|
|
|
31dc771 |
+ """Validate a method name for putrequest."""
|
|
|
31dc771 |
+ # prevent http header injection
|
|
|
31dc771 |
+ match = _contains_disallowed_method_pchar_re.search(method)
|
|
|
31dc771 |
+ if match:
|
|
|
31dc771 |
+ raise ValueError(
|
|
|
31dc771 |
+ "method can't contain control characters. %r "
|
|
|
31dc771 |
+ "(found at least %r)"
|
|
|
31dc771 |
+ % (method, match.group()))
|
|
|
31dc771 |
+
|
|
|
31dc771 |
def _validate_path(self, url):
|
|
|
31dc771 |
"""Validate a url for putrequest."""
|
|
|
31dc771 |
# Prevent CVE-2019-9740.
|
|
|
31dc771 |
diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py
|
|
|
31dc771 |
index d8a57f73530..e295bb796ec 100644
|
|
|
31dc771 |
--- a/Lib/test/test_httplib.py
|
|
|
31dc771 |
+++ b/Lib/test/test_httplib.py
|
|
|
31dc771 |
@@ -385,6 +385,28 @@ class HeaderTests(TestCase):
|
|
|
31dc771 |
conn.putheader(name, value)
|
|
|
31dc771 |
|
|
|
31dc771 |
|
|
|
31dc771 |
+class HttpMethodTests(TestCase):
|
|
|
31dc771 |
+ def test_invalid_method_names(self):
|
|
|
31dc771 |
+ methods = (
|
|
|
31dc771 |
+ 'GET\r',
|
|
|
31dc771 |
+ 'POST\n',
|
|
|
31dc771 |
+ 'PUT\n\r',
|
|
|
31dc771 |
+ 'POST\nValue',
|
|
|
31dc771 |
+ 'POST\nHOST:abc',
|
|
|
31dc771 |
+ 'GET\nrHost:abc\n',
|
|
|
31dc771 |
+ 'POST\rRemainder:\r',
|
|
|
31dc771 |
+ 'GET\rHOST:\n',
|
|
|
31dc771 |
+ '\nPUT'
|
|
|
31dc771 |
+ )
|
|
|
31dc771 |
+
|
|
|
31dc771 |
+ for method in methods:
|
|
|
31dc771 |
+ with self.assertRaisesRegexp(
|
|
|
31dc771 |
+ ValueError, "method can't contain control characters"):
|
|
|
31dc771 |
+ conn = httplib.HTTPConnection('example.com')
|
|
|
31dc771 |
+ conn.sock = FakeSocket(None)
|
|
|
31dc771 |
+ conn.request(method=method, url="/")
|
|
|
31dc771 |
+
|
|
|
31dc771 |
+
|
|
|
31dc771 |
class BasicTest(TestCase):
|
|
|
31dc771 |
def test_status_lines(self):
|
|
|
31dc771 |
# Test HTTP status lines
|
|
|
31dc771 |
@@ -1010,6 +1032,7 @@ class TunnelTests(TestCase):
|
|
|
31dc771 |
@test_support.reap_threads
|
|
|
31dc771 |
def test_main(verbose=None):
|
|
|
31dc771 |
test_support.run_unittest(HeaderTests, OfflineTest, BasicTest, TimeoutTest,
|
|
|
31dc771 |
+ HttpMethodTests,
|
|
|
31dc771 |
HTTPTest, HTTPSTest, SourceAddressTest,
|
|
|
31dc771 |
TunnelTests)
|
|
|
31dc771 |
|
|
|
31dc771 |
diff --git a/Misc/NEWS.d/next/Security/2020-02-12-14-17-39.bpo-39603.Gt3RSg.rst b/Misc/NEWS.d/next/Security/2020-02-12-14-17-39.bpo-39603.Gt3RSg.rst
|
|
|
31dc771 |
new file mode 100644
|
|
|
31dc771 |
index 00000000000..990affc3edd
|
|
|
31dc771 |
--- /dev/null
|
|
|
31dc771 |
+++ b/Misc/NEWS.d/next/Security/2020-02-12-14-17-39.bpo-39603.Gt3RSg.rst
|
|
|
31dc771 |
@@ -0,0 +1,2 @@
|
|
|
31dc771 |
+Prevent http header injection by rejecting control characters in
|
|
|
31dc771 |
+http.client.putrequest(...).
|