From 0fa52645c2605be8afd3e63f92ccfb068e106131 Mon Sep 17 00:00:00 2001 From: jedi Date: Sat, 18 Jan 2025 22:15:04 +0100 Subject: [PATCH] handle plain base64 as transfer-encoding in incoming mails --- core/mail/protocol.py | 8 ++++++-- core/mail/tests/v2/test_mails.py | 31 ++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/core/mail/protocol.py b/core/mail/protocol.py index 7fe6942..00872f0 100644 --- a/core/mail/protocol.py +++ b/core/mail/protocol.py @@ -128,10 +128,14 @@ def decode_email_segment(segment, charset, transfer_encoding): decode_as = 'cp1251' elif charset == 'iso-8859-1': decode_as = 'latin1' - segment = unescape_and_decode_quoted_printable(segment) - segment = unescape_and_decode_base64(segment) if transfer_encoding == 'quoted-printable': segment = unescape_simplified_quoted_printable(segment, decode_as) + elif transfer_encoding == 'base64': + import base64 + segment = base64.b64decode(segment).decode('utf-8') + else: + segment = unescape_and_decode_quoted_printable(segment) + segment = unescape_and_decode_base64(segment) return segment diff --git a/core/mail/tests/v2/test_mails.py b/core/mail/tests/v2/test_mails.py index 455faf1..d2f33fe 100644 --- a/core/mail/tests/v2/test_mails.py +++ b/core/mail/tests/v2/test_mails.py @@ -165,7 +165,7 @@ class LMTPHandlerTestCase(TestCase): # TODO replace with less hacky test self.assertEqual('Text mit Quoted-Printable-Kodierung: äöüß', Email.objects.all()[0].body) self.assertTrue(Email.objects.all()[0].raw_file.path) - def test_handle_base64(self): + def test_handle_base64_inline(self): from aiosmtpd.smtp import Envelope from asgiref.sync import async_to_sync import aiosmtplib @@ -186,6 +186,35 @@ class LMTPHandlerTestCase(TestCase): # TODO replace with less hacky test self.assertEqual('Text mit Base64-Kodierung: äöüß', Email.objects.all()[0].body) self.assertTrue(Email.objects.all()[0].raw_file.path) + def test_handle_base64_transfer_encoding(self): + from aiosmtpd.smtp import Envelope + from asgiref.sync import async_to_sync + import aiosmtplib + aiosmtplib.send = make_mocked_coro() + handler = LMTPHandler() + server = mock.Mock() + session = mock.Mock() + envelope = Envelope() + envelope.mail_from = 'test1@test' + envelope.rcpt_tos = ['test2@test'] + envelope.content = b'''Subject: test +From: test3@test +To: test4@test +Message-ID: <1@test> +Content-Type: text/plain; charset=utf-8 +Content-Transfer-Encoding: base64 + +VGVzdCBtaXQgQmFzZTY0LUtvZGllcnVuZzogw6TDtsO8w58=''' + + result = async_to_sync(handler.handle_DATA)(server, session, envelope) + self.assertEqual(result, '250 Message accepted for delivery') + self.assertEqual(len(Email.objects.all()), 2) + self.assertEqual(len(IssueThread.objects.all()), 1) + aiosmtplib.send.assert_called_once() + self.assertEqual('test', Email.objects.all()[0].subject) + self.assertEqual('Test mit Base64-Kodierung: äöüß', Email.objects.all()[0].body) + self.assertTrue(Email.objects.all()[0].raw_file.path) + def test_handle_client_reply(self): issue_thread = IssueThread.objects.create( name="test",