Skip to content

Commit bd642f1

Browse files
committed
Respond 400 instead of 500 when first header field line starts with SP or HTAB.
1 parent 1ff20b1 commit bd642f1

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,17 @@ def test_parse_uri_invalid_uri(test_client):
189189
c.close()
190190

191191

192+
def test_parse_invalid_line_fold(test_client):
193+
c = test_client.get_connection()
194+
c._output(u'GET / HTTP/1.1\r\n I-am-misfolded!\r\n\r\n'.encode('utf-8'))
195+
c._send_output()
196+
response = _get_http_response(c, method='GET')
197+
response.begin()
198+
assert response.status == HTTP_BAD_REQUEST
199+
assert response.read(26) == b'Illegal continuation line.'
200+
c.close()
201+
202+
192203
@pytest.mark.parametrize(
193204
'uri',
194205
(
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)