Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8ca8a2e

Browse files
bpo-39603: Prevent header injection in http methods (GH-18485)
reject control chars in http method in http.client.putrequest to prevent http header injection
1 parent 9b01c59 commit 8ca8a2e

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

Lib/http/client.py

+15
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@
147147
# _is_allowed_url_pchars_re = re.compile(r"^[/!$&'()*+,;=:@%a-zA-Z0-9._~-]+$")
148148
# We are more lenient for assumed real world compatibility purposes.
149149

150+
# These characters are not allowed within HTTP method names
151+
# to prevent http header injection.
152+
_contains_disallowed_method_pchar_re = re.compile('[\x00-\x1f]')
153+
150154
# We always set the Content-Length header for these methods because some
151155
# servers will otherwise respond with a 411
152156
_METHODS_EXPECTING_BODY = {'PATCH', 'POST', 'PUT'}
@@ -1102,6 +1106,8 @@ def putrequest(self, method, url, skip_host=False,
11021106
else:
11031107
raise CannotSendRequest(self.__state)
11041108

1109+
self._validate_method(method)
1110+
11051111
# Save the method for use later in the response phase
11061112
self._method = method
11071113

@@ -1192,6 +1198,15 @@ def _encode_request(self, request):
11921198
# ASCII also helps prevent CVE-2019-9740.
11931199
return request.encode('ascii')
11941200

1201+
def _validate_method(self, method):
1202+
"""Validate a method name for putrequest."""
1203+
# prevent http header injection
1204+
match = _contains_disallowed_method_pchar_re.search(method)
1205+
if match:
1206+
raise ValueError(
1207+
f"method can't contain control characters. {method!r} "
1208+
f"(found at least {match.group()!r})")
1209+
11951210
def _validate_path(self, url):
11961211
"""Validate a url for putrequest."""
11971212
# Prevent CVE-2019-9740.

Lib/test/test_httplib.py

+22
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,28 @@ def test_headers_debuglevel(self):
368368
self.assertEqual(lines[3], "header: Second: val2")
369369

370370

371+
class HttpMethodTests(TestCase):
372+
def test_invalid_method_names(self):
373+
methods = (
374+
'GET\r',
375+
'POST\n',
376+
'PUT\n\r',
377+
'POST\nValue',
378+
'POST\nHOST:abc',
379+
'GET\nrHost:abc\n',
380+
'POST\rRemainder:\r',
381+
'GET\rHOST:\n',
382+
'\nPUT'
383+
)
384+
385+
for method in methods:
386+
with self.assertRaisesRegex(
387+
ValueError, "method can't contain control characters"):
388+
conn = client.HTTPConnection('example.com')
389+
conn.sock = FakeSocket(None)
390+
conn.request(method=method, url="/")
391+
392+
371393
class TransferEncodingTest(TestCase):
372394
expected_body = b"It's just a flesh wound"
373395

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Prevent http header injection by rejecting control characters in
2+
http.client.putrequest(...).

0 commit comments

Comments
 (0)