Skip to content

Commit 575994c

Browse files
Upon receiving invalid Content-Length bail
Instead of attempting to continue processing the request, we instead raise a ParsingError and return a HTTP Bad Request to the client. This also catches the case where two Content-Length's are sent, and are folded together using HTTP header folding.
1 parent 804e313 commit 575994c

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

waitress/parser.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,8 @@ def parse_header(self, header_plus):
254254
try:
255255
cl = int(headers.get("CONTENT_LENGTH", 0))
256256
except ValueError:
257-
cl = 0
257+
raise ParsingError("Content-Length is invalid")
258+
258259
self.content_length = cl
259260
if cl > 0:
260261
buf = OverflowableBuffer(self.adj.inbuf_overflow)

waitress/tests/test_parser.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,28 @@ def test_parse_header_no_cr_in_headerplus(self):
167167
self.assertTrue(False)
168168

169169
def test_parse_header_bad_content_length(self):
170+
from waitress.parser import ParsingError
171+
170172
data = b"GET /foobar HTTP/8.4\r\ncontent-length: abc\r\n"
171-
self.parser.parse_header(data)
172-
self.assertEqual(self.parser.body_rcv, None)
173+
174+
try:
175+
self.parser.parse_header(data)
176+
except ParsingError as e:
177+
self.assertIn("Content-Length is invalid", e.args[0])
178+
else: # pragma: nocover
179+
self.assertTrue(False)
180+
181+
def test_parse_header_multiple_content_length(self):
182+
from waitress.parser import ParsingError
183+
184+
data = b"GET /foobar HTTP/8.4\r\ncontent-length: 10\r\ncontent-length: 20\r\n"
185+
186+
try:
187+
self.parser.parse_header(data)
188+
except ParsingError as e:
189+
self.assertIn("Content-Length is invalid", e.args[0])
190+
else: # pragma: nocover
191+
self.assertTrue(False)
173192

174193
def test_parse_header_11_te_chunked(self):
175194
# NB: test that capitalization of header value is unimportant

0 commit comments

Comments
 (0)