Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c7bfe0e

Browse files
Issue #18167: cgi.FieldStorage no more fails to handle multipart/form-data
when \r\n appears at end of 65535 bytes without other newlines.
1 parent 8b56292 commit c7bfe0e

3 files changed

Lines changed: 35 additions & 0 deletions

File tree

Lib/cgi.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,9 @@ def read_lines_to_outerboundary(self):
786786
if not line:
787787
self.done = -1
788788
break
789+
if delim == b"\r":
790+
line = delim + line
791+
delim = b""
789792
if line.startswith(b"--") and last_line_lfend:
790793
strippedline = line.rstrip()
791794
if strippedline == next_boundary:
@@ -802,6 +805,12 @@ def read_lines_to_outerboundary(self):
802805
delim = b"\n"
803806
line = line[:-1]
804807
last_line_lfend = True
808+
elif line.endswith(b"\r"):
809+
# We may interrupt \r\n sequences if they span the 2**16
810+
# byte boundary
811+
delim = b"\r"
812+
line = line[:-1]
813+
last_line_lfend = False
805814
else:
806815
delim = b""
807816
last_line_lfend = False

Lib/test/test_cgi.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,29 @@ def test_fieldstorage_multipart_non_ascii(self):
256256
got = getattr(fs.list[x], k)
257257
self.assertEqual(got, exp)
258258

259+
def test_fieldstorage_multipart_maxline(self):
260+
# Issue #18167
261+
maxline = 1 << 16
262+
self.maxDiff = None
263+
def check(content):
264+
data = """---123
265+
Content-Disposition: form-data; name="upload"; filename="fake.txt"
266+
Content-Type: text/plain
267+
268+
%s
269+
---123--
270+
""".replace('\n', '\r\n') % content
271+
environ = {
272+
'CONTENT_LENGTH': str(len(data)),
273+
'CONTENT_TYPE': 'multipart/form-data; boundary=-123',
274+
'REQUEST_METHOD': 'POST',
275+
}
276+
self.assertEqual(gen_result(data, environ),
277+
{'upload': content.encode('latin1')})
278+
check('x' * (maxline - 1))
279+
check('x' * (maxline - 1) + '\r')
280+
check('x' * (maxline - 1) + '\r' + 'y' * (maxline - 1))
281+
259282
_qs_result = {
260283
'key1': 'value1',
261284
'key2': ['value2x', 'value2y'],

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ Core and Builtins
3232
Library
3333
-------
3434

35+
- Issue #18167: cgi.FieldStorage no more fails to handle multipart/form-data
36+
when \r\n appears at end of 65535 bytes without other newlines.
37+
3538
- subprocess: Prevent a possible double close of parent pipe fds when the
3639
subprocess exec runs into an error. Prevent a regular multi-close of the
3740
/dev/null fd when any of stdin, stdout and stderr was set to DEVNULL.

0 commit comments

Comments
 (0)