File tree Expand file tree Collapse file tree 2 files changed +26
-6
lines changed Expand file tree Collapse file tree 2 files changed +26
-6
lines changed Original file line number Diff line number Diff line change 23
23
24
24
from waitress .buffers import OverflowableBuffer
25
25
from waitress .receiver import ChunkedReceiver , FixedStreamReceiver
26
+ from waitress .rfc7230 import HEADER_FIELD_RE , ONLY_DIGIT_RE
26
27
from waitress .utilities import (
27
28
BadRequest ,
28
29
RequestEntityTooLarge ,
31
32
find_double_newline ,
32
33
)
33
34
34
- from .rfc7230 import HEADER_FIELD
35
-
36
35
37
36
def unquote_bytes_to_wsgi (bytestring ):
38
37
return unquote_to_bytes (bytestring ).decode ("latin-1" )
@@ -221,7 +220,7 @@ def parse_header(self, header_plus):
221
220
headers = self .headers
222
221
223
222
for line in lines :
224
- header = HEADER_FIELD .match (line )
223
+ header = HEADER_FIELD_RE .match (line )
225
224
226
225
if not header :
227
226
raise ParsingError ("Invalid header" )
@@ -317,11 +316,12 @@ def parse_header(self, header_plus):
317
316
self .connection_close = True
318
317
319
318
if not self .chunked :
320
- try :
321
- cl = int ( headers . get ( "CONTENT_LENGTH" , 0 ))
322
- except ValueError :
319
+ cl = headers . get ( "CONTENT_LENGTH" , "0" )
320
+
321
+ if not ONLY_DIGIT_RE . match ( cl . encode ( "latin-1" )) :
323
322
raise ParsingError ("Content-Length is invalid" )
324
323
324
+ cl = int (cl )
325
325
self .content_length = cl
326
326
327
327
if cl > 0 :
Original file line number Diff line number Diff line change @@ -193,6 +193,26 @@ def test_parse_header_bad_content_length(self):
193
193
else : # pragma: nocover
194
194
self .assertTrue (False )
195
195
196
+ def test_parse_header_bad_content_length_plus (self ):
197
+ data = b"GET /foobar HTTP/8.4\r \n content-length: +10\r \n "
198
+
199
+ try :
200
+ self .parser .parse_header (data )
201
+ except ParsingError as e :
202
+ self .assertIn ("Content-Length is invalid" , e .args [0 ])
203
+ else : # pragma: nocover
204
+ self .assertTrue (False )
205
+
206
+ def test_parse_header_bad_content_length_minus (self ):
207
+ data = b"GET /foobar HTTP/8.4\r \n content-length: -10\r \n "
208
+
209
+ try :
210
+ self .parser .parse_header (data )
211
+ except ParsingError as e :
212
+ self .assertIn ("Content-Length is invalid" , e .args [0 ])
213
+ else : # pragma: nocover
214
+ self .assertTrue (False )
215
+
196
216
def test_parse_header_multiple_content_length (self ):
197
217
data = b"GET /foobar HTTP/8.4\r \n content-length: 10\r \n content-length: 20\r \n "
198
218
You can’t perform that action at this time.
0 commit comments