From edaf6f0603718e92a19dee9711e2c3ea854ccf21 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 15 May 2021 22:18:07 +0200 Subject: 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. --- alot/mail/envelope.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) 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): -- cgit v1.2.3