10000 bpo-39603: Prevent header injection in http methods (GH-18485) · python/cpython@668d321 · GitHub
[go: up one dir, main page]

Skip to content

Commit 668d321

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 <31338382+amiremohamadi@users.noreply.github.com>
1 parent 7734738 commit 668d321

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 10000 )
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
@@ -364,6 +364,28 @@ def test_headers_debuglevel(self):
364364
self.assertEqual(lines[3], "header: Second: val2")
365365

366366

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

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