Skip to content

Commit 7774d78

Browse files
authored
bpo-38216, bpo-36274: Allow subclasses to separately override validation and encoding behavior (GH-16448)
* bpo-38216: Allow bypassing input validation * bpo-36274: Also allow the URL encoding to be overridden. * bpo-38216, bpo-36274: Add tests demonstrating a hook for overriding validation, test demonstrating override encoding, and a test to capture expectation of the interface for the URL. * Call with skip_host to avoid tripping on the host checking in the URL. * Remove obsolete comment. * Make _prepare_path_encoding its own attr. This makes overriding just that simpler. Also, don't use the := operator to make backporting easier. * Add a news entry. * _prepare_path_encoding -> _encode_prepared_path() * Once again separate the path validation and request encoding, drastically simplifying the behavior. Drop the guarantee that all processing happens in _prepare_path.
1 parent 441b10c commit 7774d78

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

Lib/http/client.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,18 +1085,15 @@ def putrequest(self, method, url, skip_host=False,
10851085
else:
10861086
raise CannotSendRequest(self.__state)
10871087

1088-
# Save the method we use, we need it later in the response phase
1088+
# Save the method for use later in the response phase
10891089
self._method = method
1090-
if not url:
1091-
url = '/'
1092-
# Prevent CVE-2019-9740.
1093-
if match := _contains_disallowed_url_pchar_re.search(url):
1094-
raise InvalidURL(f"URL can't contain control characters. {url!r} "
1095-
f"(found at least {match.group()!r})")
1090+
1091+
url = url or '/'
1092+
self._validate_path(url)
1093+
10961094
request = '%s %s %s' % (method, url, self._http_vsn_str)
10971095

1098-
# Non-ASCII characters should have been eliminated earlier
1099-
self._output(request.encode('ascii'))
1096+
self._output(self._encode_request(request))
11001097

11011098
if self._http_vsn == 11:
11021099
# Issue some standard headers for better HTTP/1.1 compliance
@@ -1174,6 +1171,18 @@ def putrequest(self, method, url, skip_host=False,
11741171
# For HTTP/1.0, the server will assume "not chunked"
11751172
pass
11761173

1174+
def _encode_request(self, request):
1175+
# ASCII also helps prevent CVE-2019-9740.
1176+
return request.encode('ascii')
1177+
1178+
def _validate_path(self, url):
1179+
"""Validate a url for putrequest."""
1180+
# Prevent CVE-2019-9740.
1181+
match = _contains_disallowed_url_pchar_re.search(url)
1182+
if match:
1183+
raise InvalidURL(f"URL can't contain control characters. {url!r} "
1184+
f"(found at least {match.group()!r})")
1185+
11771186
def putheader(self, header, *values):
11781187
"""Send a request header line to the server.
11791188

Lib/test/test_httplib.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,34 @@ def run_server():
11551155
thread.join()
11561156
self.assertEqual(result, b"proxied data\n")
11571157

1158+
def test_putrequest_override_validation(self):
1159+
"""
1160+
It should be possible to override the default validation
1161+
behavior in putrequest (bpo-38216).
1162+
"""
1163+
class UnsafeHTTPConnection(client.HTTPConnection):
1164+
def _validate_path(self, url):
1165+
pass
1166+
1167+
conn = UnsafeHTTPConnection('example.com')
1168+
conn.sock = FakeSocket('')
1169+
conn.putrequest('GET', '/\x00')
1170+
1171+
def test_putrequest_override_encoding(self):
1172+
"""
1173+
It should be possible to override the default encoding
1174+
to transmit bytes in another encoding even if invalid
1175+
(bpo-36274).
1176+
"""
1177+
class UnsafeHTTPConnection(client.HTTPConnection):
1178+
def _encode_request(self, str_url):
1179+
return str_url.encode('utf-8')
1180+
1181+
conn = UnsafeHTTPConnection('example.com')
1182+
conn.sock = FakeSocket('')
1183+
conn.putrequest('GET', '/☃')
1184+
1185+
11581186
class ExtendedReadTest(TestCase):
11591187
"""
11601188
Test peek(), read1(), readline()
@@ -1279,6 +1307,7 @@ def test_peek_0(self):
12791307
p = self.resp.peek(0)
12801308
self.assertLessEqual(0, len(p))
12811309

1310+
12821311
class ExtendedReadTestChunked(ExtendedReadTest):
12831312
"""
12841313
Test peek(), read1(), readline() in chunked mode
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Allow the rare code that wants to send invalid http requests from the
2+
`http.client` library a way to do so. The fixes for bpo-30458 led to
3+
breakage for some projects that were relying on this ability to test their
4+
own behavior in the face of bad requests.

0 commit comments

Comments
 (0)