Title: EmailMessage.add_attachment(filename="long or spécial") crashes or produces invalid output
Type: behavior Stage: resolved
Components: email Versions: Python 3.7, Python 3.6
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: barry, calimeroteknik, r.david.murray
Priority: normal Keywords:

Created on 2017-10-20 22:22 by calimeroteknik, last changed 2017-12-04 00:51 by r.david.murray. This issue is now closed.

Messages (8)
msg304684 - (view) Author: calimeroteknik (calimeroteknik) Date: 2017-10-20 22:22
The following code excerpt demonstrates a crash:

import email.message
mail = email.message.EmailMessage()
   maintype = "text",
   subtype  = "plain",
   filename = "I thought I could put a few words in the filename but apparently it does not go so well.txt" 

Output on python 3.7.0a1:
Output on python 3.6.2:

Additionally, a behavioral issue is demonstrated by replacing in the above:
filename = "What happens if we try French in here? touché!.txt"

Which results in the following output (headers):

Content-Type: text/plain
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
MIME-Version: 1.0

Instead of, for example, this correct output (by Mozilla Thunderbird here):

Content-Type: text/plain; charset=UTF-8;
Content-Transfer-Encoding: base64
Content-Disposition: attachment;

Issues to note here:
-the "filename" parameter is not indented, mail clients ignore it
-the "filename" parameter is not split according to RFC 2231

The relevant standard is exemplified in section 4.1 of

Python 3.4.6 and 3.5.4 simply do not wrap anything, which works with  but is not conformant to standards.

Solving all of the above would imply correctly splitting any header.
Function "set_param" in /usr/lib/python*/email/ looked like a place to look.

Unfortunately I do not understand what's going on there very well.

As yet an additional misbehaviour to note, try to repeat the above print statement twice.
The result is not identical, and the second time you get the following output:

Content-Type: text/plain
Content-Transfer-Encoding: base64
Content-Disposition: attachment;*=utf-8''What%20happens%20if%20we%20try%20French%20in%20here%3F%20touch%C3%A9%21.txt
MIME-Version: 1.0

It would appear that "filename" has disappeared.
The issue does not reveal itself with simple values for the 'filename' argument, e.g. "test.txt".

PS: The above output also illustrates this (way more minor) issue:
msg304685 - (view) Author: calimeroteknik (calimeroteknik) Date: 2017-10-20 22:27
Erratum: the output generated by python 3.5 and 3.4 causes line wraps in the SMTP delivery chain, which cause exactly the same breakage as ulterior versions: the crucially needed indendation of one space ends up being absent.
msg304691 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-10-21 00:12
Does the patch in gh-3488 fix this?  I think it should, or if it doesn't that's a bug in the PR patch.
msg304705 - (view) Author: calimeroteknik (calimeroteknik) Date: 2017-10-21 13:49
I confirm that as for the crash, the patch in gh-3488 fixes it.
The first code excerpt in my initial report now outputs the following, valid headers:

Content-Type: text/plain
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
MIME-Version: 1.0

However, when Unicode is added and the filename is short, things don't look right, this code:

import email.message
mail = email.message.EmailMessage()
mail.add_attachment(b"test", maintype="text", subtype="plain", filename="é.txt")

Results in these headers:

Content-Type: text/plain
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="é.txt"
MIME-Version: 1.0

To begin with, it is easy to deduce that there is no way to know that this 'é' character is UTF-8.
And it's two 8-bit values at east one of which is detectably outside of 7-bit US-ASCII.

>a lightweight encoding mechanism is needed to accommodate 8-bit information in parameter values.

The 8-bit encoding goes straight through instead of undergoing the encoding process, which seems required in my interpretation of RFC2231.
msg304706 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-10-21 13:55
You are correct, that is a bug.  Presumably I forgot to check for non-ascii when the parameter value doesn't need to be folded.  I'm not sure when I'll have time to look at this, unfortunately :(.  If you can see how to fix it, you could submit a PR against my PR branch, I think.
msg304726 - (view) Author: calimeroteknik (calimeroteknik) Date: 2017-10-22 00:48
Eventually there is no bug, I was just confused at the output of print() on the EmailMessage.

I noticed that in email/ policy.utf8 was True.
The reason is found in email/ line 970 (class MIMEPart):

    def __str__(self):
       	return self.as_string(policy=self.policy.clone(utf8=True)

print() will use __str__() and this is why it happens.

I didn't dig out the exact reason since there are so many delegated calls.
In any case, the flattened message in smtplib.SMTP does contain what as_string() returns, which means that the policy.utf8 is only forced when using print().

Sorry for the false alert.
I can guess that the intention in forcing policy.utf8=True in __str__() was that SMTPUTF8 output is visually prettier than any ASCII-armored text.

After additional fuzzing, checking the output with EmailMessage.as_string(), everything seems OK.

That's a +1 for gh-3488, which fixes this bug.
msg304756 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-10-22 16:27
Great, thank you for that research.  And yes, that's exactly why __str__ uses utf8=True, the "picture" of the message is much more readable.  I will commit that PR soon.
msg307532 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-12-04 00:51
The PR has been committed.
Date User Action Args
2017-12-04 00:51:27r.david.murraysetstatus: open -> closed
versions: - Python 3.4, Python 3.5
type: crash -> behavior
messages: + msg307532

resolution: fixed
stage: resolved
2017-10-22 16:27:10r.david.murraysetmessages: + msg304756
2017-10-22 00:48:12calimerotekniksetmessages: + msg304726
2017-10-21 13:55:36r.david.murraysetmessages: + msg304706
2017-10-21 13:49:54calimerotekniksetmessages: + msg304705
2017-10-21 00:12:30r.david.murraysetmessages: + msg304691
2017-10-20 22:27:45calimerotekniksetmessages: + msg304685
versions: + Python 3.4, Python 3.5
2017-10-20 22:22:56calimeroteknikcreate