8000 bpo-34226: fix cgi.parse_multipart without content_length (GH-8530) · python/cpython@c72b7f7 · GitHub
[go: up one dir, main page]

Skip to content

Commit c72b7f7

Browse files
bpo-34226: fix cgi.parse_multipart without content_length (GH-8530)
In Python 3.7 the behavior of parse_multipart changed requiring CONTENT-LENGTH header, this fix remove this header as required and fix FieldStorage read_lines_to_outerboundary, by not using limit when it's negative, since by default it's -1 if not content-length and keeps substracting what was read from the file object. Also added a test case for this problem. (cherry picked from commit d8cf351) Co-authored-by: roger <rogerduran@gmail.com>
1 parent 811e040 commit c72b7f7

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

Lib/cgi.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,10 @@ def parse_multipart(fp, pdict, encoding="utf-8", errors="replace"):
200200
ctype = "multipart/form-data; boundary={}".format(boundary)
201201
headers = Message()
202202
headers.set_type(ctype)
203-
headers['Content-Length'] = pdict['CONTENT-LENGTH']
203+
try:
204+
headers['Content-Length'] = pdict['CONTENT-LENGTH']
205+
except KeyError:
206+
pass
204207
fs = FieldStorage(fp, headers=headers, encoding=encoding, errors=errors,
205208
en 8000 viron={'REQUEST_METHOD': 'POST'})
206209
return {k: fs.getlist(k) for k in fs}
@@ -736,7 +739,8 @@ def read_lines_to_outerboundary(self):
736739
last_line_lfend = True
737740
_read = 0
738741
while 1:
739-
if self.limit is not None and _read >= self.limit:
742+
743+
if self.limit is not None and 0 <= self.limit <= _read:
740744
break
741745
line = self.fp.readline(1<<16) # bytes
742746
self.bytes_read += len(line)

Lib/test/test_cgi.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,20 @@ def test_parse_multipart(self):
128128
'file': [b'Testing 123.\n'], 'title': ['']}
129129
self.assertEqual(result, expected)
130130

131+
def test_parse_multipart_without_content_length(self):
132+
POSTDATA = '''--JfISa01
133+
Content-Disposition: form-data; name="submit-name"
134+
135+
just a string
136+
137+
--JfISa01--
138+
'''
139+
fp = BytesIO(POSTDATA.encode('latin1'))
140+
env = {'boundary': 'JfISa01'.encode('latin1')}
141+
result = cgi.parse_multipart(fp, env)
142+
expected = {'submit-name': ['just a string\n']}
143+
self.assertEqual(result, expected)
144+
131145
def test_parse_multipart_invalid_encoding(self):
132146
BOUNDARY = "JfISa01"
133147
POSTDATA = """--JfISa01
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix `cgi.parse_multipart` without content_length. Patch by Roger Duran

0 commit comments

Comments
 (0)
0