Skip to content

Commit d90f8d1

Browse files
committed
Closes #23801 - Ignore entire preamble to multipart in cgi.FieldStorage
1 parent 1058cda commit d90f8d1

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

Lib/cgi.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -693,8 +693,13 @@ def read_multi(self, environ, keep_blank_values, strict_parsing):
693693
raise ValueError("%s should return bytes, got %s" \
694694
% (self.fp, type(first_line).__name__))
695695
self.bytes_read += len(first_line)
696-
# first line holds boundary ; ignore it, or check that
697-
# b"--" + ib == first_line.strip() ?
696+
697+
# Ensure that we consume the file until we've hit our inner boundary
698+
while (first_line.strip() != (b"--" + self.innerboundary) and
699+
first_line):
700+
first_line = self.fp.readline()
701+
self.bytes_read += len(first_line)
702+
698703
while True:
699704
parser = FeedParser()
700705
hdr_text = b""

Lib/test/test_cgi.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,25 @@ def test_fieldstorage_multipart(self):
248248
got = getattr(fs.list[x], k)
249249
self.assertEqual(got, exp)
250250

251+
def test_fieldstorage_multipart_leading_whitespace(self):
252+
env = {
253+
'REQUEST_METHOD': 'POST',
254+
'CONTENT_TYPE': 'multipart/form-data; boundary={}'.format(BOUNDARY),
255+
'CONTENT_LENGTH': '560'}
256+
# Add some leading whitespace to our post data that will cause the
257+
# first line to not be the innerboundary.
258+
fp = BytesIO(b"\r\n" + POSTDATA.encode('latin-1'))
259+
fs = cgi.FieldStorage(fp, environ=env, encoding="latin-1")
260+
self.assertEqual(len(fs.list), 4)
261+
expect = [{'name':'id', 'filename':None, 'value':'1234'},
262+
{'name':'title', 'filename':None, 'value':''},
263+
{'name':'file', 'filename':'test.txt', 'value':b'Testing 123.\n'},
264+
{'name':'submit', 'filename':None, 'value':' Add '}]
265+
for x in range(len(fs.list)):
266+
for k, exp in expect[x].items():
267+
got = getattr(fs.list[x], k)
268+
self.assertEqual(got, exp)
269+
251270
def test_fieldstorage_multipart_non_ascii(self):
252271
#Test basic FieldStorage multipart parsing
253272
env = {'REQUEST_METHOD':'POST',

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ Library
124124

125125
- Issue #23361: Fix possible overflow in Windows subprocess creation code.
126126

127+
- Issue #23801: Fix issue where cgi.FieldStorage did not always ignore the
128+
entire preamble to a multipart body.
129+
127130
Tests
128131
-----
129132

0 commit comments

Comments
 (0)