-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
invalid content-transfer-encoding in encoded-word causes KeyError #82513
Comments
The following will cause a KeyError on email.message.get() import email
import email.policy
text = "Subject: =?us-ascii?X?somevalue?="
eml = email.message_from_string(text, policy=email.policy.default)
eml.get('Subject') This is caused by the fact that the code in _encoded_words.py assumes the content-transfer-encoding of an encoded-word is always 'q' or 'b' (after lowercasing): cpython/Lib/email/_encoded_words.py Line 178 in aca8c40
I realise it's probably a silly edge case and I haven't (yet) encountered something like this in the wild, but it does seem contrary to the spirit of the email library to raise an exception like this that can propagate all the way to email.message.get(). |
Hello, I am not a email expert, but according to RFC 1342 the enconding can be either "B" or "Q". So, I think is reasonable that when a not correct enconding is set, should be raise an exception I think that we can improve the message raising a more specific Exception |
I agree with you that according to the RFC, the cte can of course only be "B" or "Q". My point is that, in my example, if you try to do that you get a KeyError propagating all the way down to email.message.get(), which I believe is incorrect. Consider an encoded word which is syntactically incorrect in a different way, like if for instance it's missing the terminating '?=': '=?UTF-8?Q?somevalue' Currently, this case will cause _encoded_words.py to throw a ValueError on this line: _, charset, cte, cte_string, _ = ew.split('?') Which is then caught by _header_value_parser.get_encoded_word() and handled appropriately. To me this is the same kind of thing. I agree that an exception should be thrown, I just don't think it should propagate all the way back to the caller of email.message.get(). On a separate note, I agree with you that perhaps _encoded_words.decode() should throw more specific exceptions instead of ValueError and KeyError but that's a separate thing. I can fix that if you prefer. |
Hi Andrei sorry for my last message. Now I understand perfectly your idea and your PR. IMO this is a correct patch. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: