Skip to content

Commit 6778ce9

Browse files
committed
Respond 400 when first header starts with space
Prevent unhandled exception in header parsing that results in 500 responses when the first received header begins with whitespace.
1 parent 1ff20b1 commit 6778ce9

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

cheroot/server.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ def __call__(self, rfile, hdict=None): # noqa: C901 # FIXME
197197
if hdict is None:
198198
hdict = {}
199199

200+
k = None
200201
while True:
201202
line = rfile.readline()
202203
if not line:
@@ -215,6 +216,8 @@ def __call__(self, rfile, hdict=None): # noqa: C901 # FIXME
215216
# NOTE: `BytesWarning('Comparison between bytes and int')`
216217
# NOTE: The latter is equivalent and does not.
217218
# It's a continuation line.
219+
if k is None:
220+
raise ValueError('Illegal continuation line.')
218221
v = line.strip()
219222
else:
220223
try:

cheroot/test/test_core.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,21 @@ def test_parse_uri_invalid_uri(test_client):
189189
c.close()
190190

191191

192+
def test_parse_invalid_line_fold(test_client):
193+
"""Check that server responds with Bad Request to invalid GET queries.
194+
195+
Invalid field line test case: the first should not begin with whitespoace
196+
"""
197+
c = test_client.get_connection()
198+
c._output(u'GET / HTTP/1.1\r\n I-am-misfolded!\r\n\r\n'.encode('utf-8'))
199+
c._send_output()
200+
response = _get_http_response(c, method='GET')
201+
response.begin()
202+
assert response.status == HTTP_BAD_REQUEST
203+
assert response.read(26) == b'Illegal continuation line.'
204+
c.close()
205+
206+
192207
@pytest.mark.parametrize(
193208
'uri',
194209
(
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
The server has been updated to respond 400 to requests in
2+
which the first header field line begins with whitespace,
3+
instead of 500.
4+
-- by :user:`kenballus`

0 commit comments

Comments
 (0)