Skip to content

cgi: TypeError when no argument string is found #76210

Closed

Description

@srittau
mannequin
BPO 32029
Nosy @srittau, @berkerpeksag
Superseder
  • bpo-27777: cgi.FieldStorage can't parse simple body with Content-Length and no Content-Disposition
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2017-11-15.01:53:55.106>
    created_at = <Date 2017-11-15.00:31:53.576>
    labels = ['type-bug', 'library']
    title = 'cgi: TypeError when no argument string is found'
    updated_at = <Date 2017-11-15.01:53:55.097>
    user = 'https://github.com/srittau'

    bugs.python.org fields:

    activity = <Date 2017-11-15.01:53:55.097>
    actor = 'berker.peksag'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-11-15.01:53:55.106>
    closer = 'berker.peksag'
    components = ['Library (Lib)']
    creation = <Date 2017-11-15.00:31:53.576>
    creator = 'srittau'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 32029
    keywords = []
    message_count = 2.0
    messages = ['306244', '306248']
    nosy_count = 2.0
    nosy_names = ['srittau', 'berker.peksag']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '27777'
    type = 'behavior'
    url = 'https://bugs.python.org/issue32029'
    versions = ['Python 3.6']

    Activity

    1. srittau commented on Nov 15, 2017

      srittaumannequin
      MannequinAuthor

      Consider the following code:

          import cgi
          from io import BytesIO
      cgi.FieldStorage(BytesIO(b"{}"), environ={
          "REQUEST_METHOD": "POST",
          "CONTENT_TYPE": "application/json",
          "CONTENT_LENGTH": "14",
      })
      

      This will throw the following exception:

      Traceback (most recent call last):
        File "foo.py", line 7, in <module>
          "CONTENT_LENGTH": "14",
        File "/usr/lib/python3.6/cgi.py", line 561, in __init__
          self.read_single()
        File "/usr/lib/python3.6/cgi.py", line 740, in read_single
          self.read_binary()
        File "/usr/lib/python3.6/cgi.py", line 762, in read_binary
          self.file.write(data)
      TypeError: write() argument must be str, not bytes

      Of course, the input does not contain any argument string to be parsed by FieldStorage. Nevertheless, a TypeError in a totally unrelated part of the code seems like a bug. In my opinion, FieldStorage should just remain empty in this case, or - at best - throw a ValueError with a sensible error message.

    2. added
      stdlib
      on Nov 15, 2017
    3. changed the title [-]cgi: TypeError[/-] [+]cgi: TypeError when no argument string is found[/+] on Nov 15, 2017
    4. berkerpeksag commented on Nov 15, 2017

      @berkerpeksag
      Member

      Thank you for your report. This looks like a duplicate of bpo-27777. You might workaround this by passing headers={"Content-Disposition": "inline"} to cgi.FieldStorage() (untested)

    5. added
      type-bug
      on Nov 15, 2017
    6. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      stdlibtype-bug

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions