diff options
author | Anton Khirnov <anton@khirnov.net> | 2021-05-15 22:18:07 +0200 |
---|---|---|
committer | Anton Khirnov <anton@khirnov.net> | 2021-05-15 22:18:07 +0200 |
commit | edaf6f0603718e92a19dee9711e2c3ea854ccf21 (patch) | |
tree | 152fc527099832c93eb579f421c22d8f975ee09a | |
parent | 116e94841e5de975c463eddf3df19e4ee9d4fdfc (diff) |
mail/envelope: only accept valid UTF-8 for text attachments
Also set the charset parameter to UTF-8.
While this restricts the kinds of files that may be attached, it ensures
we do not generate invalid files, as we do not do charset detection
currently. That can be implemented in the future, if necessary.
-rw-r--r-- | alot/mail/envelope.py | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/alot/mail/envelope.py b/alot/mail/envelope.py index a4985c7a..9af4a42b 100644 --- a/alot/mail/envelope.py +++ b/alot/mail/envelope.py @@ -221,11 +221,18 @@ class Envelope: ctype = helper.guess_mimetype(data) + # accept only valid utf-8 as text attachments + if ctype.partition('/')[0] == 'text': + try: + data.decode('utf-8') + except UnicodeDecodeError as e: + raise ValueError('Attachment is not valid UTF-8') from e + # Set the filename parameter if not filename: filename = os.path.basename(path) - attachment = Attachment(data, ctype, filename, ()) + attachment = Attachment(data, ctype, filename, (('charset', 'utf-8'))) self.attach(attachment) def attach(self, attachment): |