-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
Email parser creates a message object that can't be flattened #71508
Comments
The attached file, bad_email, can be parsed via msg = email.message_from_binary_file(open('bad_email', 'rb')) but then msg.as_string() prodices the following: Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/email/message.py", line 159, in as_string
g.flatten(self, unixfrom=unixfrom)
File "/usr/lib/python3.5/email/generator.py", line 115, in flatten
self._write(msg)
File "/usr/lib/python3.5/email/generator.py", line 189, in _write
msg.replace_header('content-transfer-encoding', munge_cte[0])
File "/usr/lib/python3.5/email/message.py", line 559, in replace_header
raise KeyError(_name)
KeyError: 'content-transfer-encoding' |
One additional observation. The original message contained no Content-Transfer-Encoding header even though the message body was raw koi8-r characters. Adding Content-Transfer-Encoding: 8bit to the message headers avoids the issue, but that is not a practical solution as the message was Russian spam received by a Mailman list and the resultant KeyError caused problems in Mailman. We can work on defending against this in Mailman, but I suggest that the munge_cte code in generator._write() avoid the documented possible KeyError raised by replace_header() by using __delitem__() and __setitem__() instead as in the attached generator.patch. |
While I was reviewing https://gitlab.com/mailman/mailman/merge_requests/197/diffs I noticed the KeyError and it made me thing "hmm, I wonder if this should be turned into one of the email package errors"? |
Any updates on this? I'm having the same problem with some non-spam emails while trying to use some mail-handling tools written in Python. |
replace_header has a different semantic than del-and-set (replace_header leaves the header in the same location in the list, rather than appending it to the end...that's it's purpose). If replace_header is throwing a key error, then I guess we need a look-before-you-leap if statement. And a test :) Note that a correct fix would preserve the broken input email, but since we're obviously already doing munging I'm fine with just making this work for now. |
I considered look before you leap, but I decided since we're munging the headers anyway, preserving their order is not that critical, but the patch is easy enough. I'll work on that and a test. |
Fix: kyrias@a986a82 Can start a PR once my CLA signature goes through I guess. |
It looks like Johannes beat me to it. Thanks for that, but see my comments in the diff at kyrias@a986a82 |
Ah, didn't even see your comment before I did it! Fix to the comments are on the same branch, will be rebased before PR is up. |
maybe we could merge the PR, and I could propose a backport for 3.5 and 3.6. 2.7 is affected ? |
I'm going to try to review this this weekend. |
ok, I have tested on 3.6 and 3.5, just with the test. and in this case, we get the errors on both. +1 the backports for 3.5 and 3.6 is just a git cherry-picking. |
Ping? |
Note: I reviewed this a while ago but the review comments haven't been addressed. |
Requested a small additional change to the new tests, and then this will be ready to go in. |
Ping on an ETA for this fix? |
@r.david.murray, it appears that all your requested changes have been addressed on the PR. Please re-review this when you get a chance. Thanks! |
any chance of getting this merged? A work-around is not obvious |
I work around it with
This is easy for me because it's Mailman which already subclasses email.message.Message for other reasons. It is perhaps more difficult if you aren't already subclassing email.message.Message for other purposes. |
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: