Skip to content

Commit 27b8110

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 (cherry picked from commit 8ca8a2e) Co-authored-by: AMIR <[email protected]>
1 parent f925444 commit 27b8110

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

Lib/http/client.py

Lines changed: 15 additions & 0 deletions
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'}
@@ -1087,6 +1091,8 @@ def putrequest(self, method, url, skip_host=False,
10871091
else:
10881092
raise CannotSendRequest(self.__state)
10891093

1094+
self._validate_method(method)
1095+
10901096
# Save the method for use later in the response phase
10911097
self._method = method
10921098

@@ -1177,6 +1183,15 @@ def _encode_request(self, request):
11771183
# ASCII also helps prevent CVE-2019-9740.
11781184
return request.encode('ascii')
11791185

1186+
def _validate_method(self, method):
1187+
"""Validate a method name for putrequest."""
1188+
# prevent http header injection
1189+
match = _contains_disallowed_method_pchar_re.search(method)
1190+
if match:
1191+
raise ValueError(
1192+
f"method can't contain control characters. {method!r} "
1193+
f"(found at least {match.group()!r})")
1194+
11801195
def _validate_path(self, url):
11811196
"""Validate a url for putrequest."""
11821197
# Prevent CVE-2019-9740.

Lib/test/test_httplib.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,28 @@ def test_headers_debuglevel(self):
365365
self.assertEqual(lines[3], "header: Second: val2")
366366

367367

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

Lines changed: 2 additions & 0 deletions
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)