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

Skip to content

Commit aa83935

Browse files
[3.7] bpo-34226: fix cgi.parse_multipart without content_length (GH-8530) (GH-20892)
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> Automerge-Triggered-By: @ned-deily
1 parent e1ca0c5 commit aa83935

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
@@ -217,7 +217,10 @@ def parse_multipart(fp, pdict, encoding="utf-8", errors="replace"):
217217
ctype = "multipart/form-data; boundary={}".format(boundary)
218218
headers = Message()
219219
headers.set_type(ctype)
220-
headers['Content-Length'] = pdict['CONTENT-LENGTH']
220+
try:
221+
headers['Content-Length'] = pdict['CONTENT-LENGTH']
222+
except KeyError:
223+
pass
221224
fs = FieldStorage(fp, headers=headers, encoding=encoding, errors=errors,
222225
environ={'REQUEST_METHOD': 'POST'})
223226
return {k: fs.getlist(k) for k in fs}
@@ -753,7 +756,8 @@ def read_lines_to_outerboundary(self):
753756
last_line_lfend = True
754757
_read = 0
755758
while 1:
756-
if self.limit is not None and _read >= self.limit:
759+
760+
if self.limit is not None and 0 <= self.limit <= _read:
757761
break
758762
line = self.fp.readline(1<<16) # bytes
759763
self.bytes_read += len(line)

Lib/test/test_cgi.py

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

133+
def test_parse_multipart_without_content_length(self):
134+
POSTDATA = '''--JfISa01
135+
Content-Disposition: form-data; name="submit-name"
136+
137+
just a string
138+
139+
--JfISa01--
140+
'''
141+
fp = BytesIO(POSTDATA.encode('latin1'))
142+
env = {'boundary': 'JfISa01'.encode('latin1')}
143+
result = cgi.parse_multipart(fp, env)
144+
expected = {'submit-name': ['just a string\n']}
145+
self.assertEqual(result, expected)
146+
133147
def test_parse_multipart_invalid_encoding(self):
134148
BOUNDARY = "JfISa01"
135149
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