[issue27777] cgi.FieldStorage can't parse simple body with Content-Length and no Content-Disposition

rr- report at bugs.python.org
Tue Aug 16 09:55:46 EDT 2016


New submission from rr-:

Sending requests with Content-Length but without Content-Disposition headers causes following error:

Traceback (most recent call last):
  File "./test", line 19, in <module>
    form = cgi.FieldStorage(fp=env['wsgi.input'], environ=env)
  File "/usr/lib/python3.5/cgi.py", line 561, in __init__
    self.read_single()
  File "/usr/lib/python3.5/cgi.py", line 740, in read_single
    self.read_binary()
  File "/usr/lib/python3.5/cgi.py", line 762, in read_binary
    self.file.write(data)
TypeError: write() argument must be str, not bytes

I've attached a test file that reproduces the issue.

The issue is because read_single decides whether to read the content as binary or text depending on content-length - if it's > 0, it uses read_binary which assumes binary input, and rewrites this input to self.file, assuming self.file is opened in binary mode.

At the same, self.file is opened in text mode, because self._binary_file is set to False, which in turn is because there's no Content-Disposition header.

At very least, the decision whether to use binary or text should be consistent in both places (self.length >= 0 vs self._binary_file).

Related: https://bugs.python.org/issue27308
Note that unlike https://bugs.python.org/issue24764 this issue does NOT concern multipart requests.

----------
components: Library (Lib)
files: test
messages: 272856
nosy: rr-
priority: normal
severity: normal
status: open
title: cgi.FieldStorage can't parse simple body with Content-Length and no Content-Disposition
versions: Python 3.5
Added file: http://bugs.python.org/file44124/test

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue27777>
_______________________________________


More information about the Python-bugs-list mailing list