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

newline character in the attachment filename, due to a folded content-disposition header, prevents forwarding mail. #52

Open
hbog opened this issue May 26, 2024 · 0 comments

Comments

@hbog
Copy link
Contributor

hbog commented May 26, 2024

For example, forwarding a mail message containing an attachment with a folded content-disposition header:

...
Content-Disposition: attachment;
        filename="Foo Bar Technology - Data Centre Hardware Maintenance and
 Monitoring (December 2021).pdf"; size=599285;
        creation-date="Thu, 04 Aug 2022 14:17:36 GMT";
        modification-date="Thu, 04 Aug 2022 14:21:02 GMT"
Content-Transfer-Encoding: base64
...

Results in the following attachment file

$ ls /tmp/dodo-n0njogin
'Foo Bar Technology - Data Centre Hardware Maintenance and'$'\n'' Monitoring (December 2021).pdf'

and the following mail composition template;

From: example@example.org
To:recipient@foobar.org
Subject: FW: Hardware Maintenance manual
A: Foo Bar Technology - Data Centre Hardware Maintenance and
 Monitoring (December 2021).pdf

Upon sending this forwarded mail, the exception Header values may not contain linefeed or carriage return characters is thrown

The presence of newline characters in the filename of the attachments may also have (security) implications with certain file management tools and scripts.

The issue is due to a bug in the get_filename() method of the (legacy) email.message.Message class. This bug is not present in the EmailMessage class:

$ python
>>> import email
>>> import email.policy
>>> f = open('mail.txt','r')
>>> msg = email.message_from_file(f)
>>> for part in msg.walk():
...   print(part.get_filename())
...
None
None
None
None
Foo Bar Technology - Data Centre Hardware Maintenance and
 Monitoring (December 2021).pdf
>>> f = open('mail.txt','r')
>>> msg = email.message_from_file(f,policy=email.policy.default)
>>> for part in msg.walk():
...   print(part.get_filename())
...
None
None
None
None
Foo Bar Technology - Data Centre Hardware Maintenance and Monitoring (December 2021).pdf
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

1 participant