Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d8cf351

Browse filesBrowse files
authored
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.
1 parent e2d47a0 commit d8cf351
Copy full SHA for d8cf351

File tree

3 files changed

+21
-2
lines changed
Filter options

3 files changed

+21
-2
lines changed

‎Lib/cgi.py

Copy file name to clipboardExpand all lines: Lib/cgi.py
+6-2Lines 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
environ={'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

Copy file name to clipboardExpand all lines: Lib/test/test_cgi.py
+14Lines 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
+1Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.