[issue40034] cgi.parse() does not work with multipart POST requests.

San report at bugs.python.org
Sat Mar 21 13:23:48 EDT 2020


New submission from San <san.gh at clockwatching.net>:

The cgi.parse stdlib function works in most cases but never works when given a multipart/form-data POST request because it does not set up pdict in a way cgi.parse_multipart() likes (boundary as bytes (not str) and including content length).




$ pwd
/tmp
$
$ /tmp/cpython/python --version
Python 3.9.0a4+
$
$ cat cgi-bin/example.cgi
#!/tmp/cpython/python
import sys, cgi
query_dict = cgi.parse()
write = sys.stdout.buffer.write
write("Content-Type: text/plain; charset=utf-8\r\n\r\n".encode("ascii"))
write(f"Worked, query dict is {query_dict}.\n".encode())
$
$ /tmp/cpython/python -m http.server --cgi & sleep 1
[1] 30201
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
$
$ # GET (url-encoded) requests work:
$ curl localhost:8000/cgi-bin/example.cgi?example_key=example_value
127.0.0.1 - - [20/Mar/2020 23:33:48] "GET /cgi-bin/example.cgi?example_key=example_value HTTP/1.1" 200 -
Worked, query dict is {'example_key': ['example_value']}.
$
$ # POST (multipart) requests do not:
$ curl localhost:8000/cgi-bin/example.cgi -F example_key=example_value
127.0.0.1 - - [20/Mar/2020 23:34:15] "POST /cgi-bin/example.cgi HTTP/1.1" 200 -
Traceback (most recent call last):
  File "/tmp/cgi-bin/example.cgi", line 3, in <module>
    query_dict = cgi.parse()
  File "/tmp/cpython/Lib/cgi.py", line 159, in parse
    return parse_multipart(fp, pdict)
  File "/tmp/cpython/Lib/cgi.py", line 201, in parse_multipart
    boundary = pdict['boundary'].decode('ascii')
AttributeError: 'str' object has no attribute 'decode'
127.0.0.1 - - [20/Mar/2020 23:34:16] CGI script exit status 0x100
$
$ $EDITOR /tmp/cpython/Lib/cgi.py
$
$ # After changing cgi.parse POST (multipart) requests work:
$ curl localhost:8000/cgi-bin/example.cgi -F example_key=example_value
127.0.0.1 - - [20/Mar/2020 23:35:10] "POST /cgi-bin/example.cgi HTTP/1.1" 200 -
Worked, query dict is {'example_key': ['example_value']}.
$

----------
components: Library (Lib)
messages: 364762
nosy: sangh
priority: normal
severity: normal
status: open
title: cgi.parse() does not work with multipart POST requests.
type: behavior
versions: Python 3.5, Python 3.6, Python 3.7, Python 3.8, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40034>
_______________________________________


More information about the Python-bugs-list mailing list