Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'tuple' object has no attribute 'encode' #78

Closed
lorenpike opened this issue Feb 7, 2024 · 8 comments
Closed

AttributeError: 'tuple' object has no attribute 'encode' #78

lorenpike opened this issue Feb 7, 2024 · 8 comments

Comments

@lorenpike
Copy link
Contributor

I get an error when sending a request from a C# client library stemming from the parse_options_header. This error only started cropping up in 0.0.7 and did not exist beforehand.

Here's some sample code to reproduce the error

With python-multipart==0.0.7:

from multipart.multipart import parse_options_header

ctype, options = parse_options_header(
    b'form-data; name=parameters; filename="C:\Users\name\path\to\file.png";'
    b" filename*=utf-8''C%3A%5CUsers%5Cname%5Cpath%5Cto3%5Cfile.png"
)

print(ctype, options)

Output:

Traceback (most recent call last):
  File ".\bug.py", line 3, in <module>
    parse_options_header(
  File "M:\Metrized\in-progress\metrized-cv\venv\lib\site-packages\multipart\multipart.py", line 118, in parse_options_header
    options[key.encode('latin-1')] = value.encode('latin-1')
AttributeError: 'tuple' object has no attribute 'encode'

With python-multipart==0.0.6, the output is:

b'form-data' {b'name': b'parameters', b'filename': b'path\to\x0cile.png', b'filename*': b"utf-8''C%3A%5CUsers%5Cname%5Cpath%5Cto3%5Cfile.png"}

I believe a fix in parse_options_header could be (multpart.py:111-112):

    for param in params:
        key, value = param
        if isinstance(value, tuple):          <---- These lines
            value = value[-1]                   <----
lorenpike pushed a commit to lorenpike/python-multipart that referenced this issue Feb 7, 2024
@bdenglish
Copy link

We're also experiencing the same issue since the 0.0.7 release. It only occurs with 1 client (a .net application) all of my other clients (python requests, curl, etc) do not create the second filename header which is a tuple

@jbvirt
Copy link

jbvirt commented Feb 7, 2024

Assuming this is a python-multipart issue, there should also probably be a unit test written to cover this edge case.

@RobbeSneyders
Copy link

We're having the same error with connexion. Seems to be introduced by this change.

@lorenpike
Copy link
Contributor Author

lorenpike commented Feb 8, 2024

To catch anyone up to speed on the issue, this is related to MIME parameters and how they get parsed using the email.message module in Python. When the parameter is specified to include a character set and language information like in the example below:

Content-Type: application/x-stuff; title*=us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A

reference

Python's email.message.Message.get_param returns a 3-tuple (CHARSET, LANGUAGE, VALUE) reference instead of a str. This case wasn't accounted for when the code was refactored in the latest update.

Here's a potential fix with a unit test for the case: #79

If anyone has any questions about the bug or the fix, let me know.

@h0rn3t
Copy link

h0rn3t commented Feb 8, 2024

1 similar comment
@olehliubchyk
Copy link

@christiankofler
Copy link

++

Kludex pushed a commit that referenced this issue Feb 9, 2024
…arse_options_header` (#79)

* Fixing issue #78

* Added test for parse_options_header

* Added a comment clarifying a condition in parse_options_header

* Fixed a typo

---------

Co-authored-by: lorenpike <noahe@metrized.com>
@Kludex
Copy link
Owner

Kludex commented Feb 9, 2024

@Kludex Kludex closed this as completed Feb 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants