From 2b5ab82d00fb7ebe746f0df6ef66b014fd4e50f6 Mon Sep 17 00:00:00 2001 From: Folker Bernitt Date: Thu, 9 Apr 2015 11:38:07 +0200 Subject: Added fallback to us ascii for invalid Content-Type values. - Issue #347 --- service/pixelated/adapter/model/mail.py | 13 ++++++++++--- service/test/unit/adapter/test_mail.py | 9 +++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) (limited to 'service') diff --git a/service/pixelated/adapter/model/mail.py b/service/pixelated/adapter/model/mail.py index 618b980a..7e0d5ad1 100644 --- a/service/pixelated/adapter/model/mail.py +++ b/service/pixelated/adapter/model/mail.py @@ -240,21 +240,28 @@ class PixelatedMail(Mail): try: decoding_map = { - 'quoted-printable': lambda content, content_type: unicode(content.decode('quopri'), content_type), + 'quoted-printable': lambda content, content_type: content.decode('quopri').decode(content_type), 'base64': lambda content, content_type: content.decode('base64').decode('utf-8'), '7bit': lambda content, content_type: content.encode(content_type), '8bit': lambda content, content_type: content.encode(content_type) } if encoding: - return decoding_map[encoding](part['content'], content_type) + return self._decode_content_with_fallback(part['content'], decoding_map[encoding], content_type) else: - return part['content'] + return self._decode_content_with_fallback(part['content'], lambda content, content_type: content.encode(content_type), 'ascii') except Exception: logger.error('Failed to decode mail part with:') logger.error('Content-Transfer-Encoding: %s' % encoding) logger.error('Content-Type: %s' % part['headers'].get('Content-Type')) raise + def _decode_content_with_fallback(self, content, decode_func, content_type): + try: + return decode_func(content, content_type) + # return content.encode(content_type) + except ValueError: + return content.encode('ascii', 'ignore') + @property def alternatives(self): return self.parts.get('alternatives') diff --git a/service/test/unit/adapter/test_mail.py b/service/test/unit/adapter/test_mail.py index d77816cd..dbad72b3 100644 --- a/service/test/unit/adapter/test_mail.py +++ b/service/test/unit/adapter/test_mail.py @@ -232,6 +232,15 @@ class TestPixelatedMail(unittest.TestCase): self.assertEquals(u'H\xe4llo', mail.text_plain_body) self.assertEquals(u'

H\xe4llo

', mail.html_body) + def test_broken_content_type_defaults_to_usascii(self): + plain_headers = {'Content-Type': 'I lie to you', 'Content-Transfer-Encoding': 'quoted-printable'} + html_headers = {'Content-Type': 'text/html;\ncharset=utf-8', 'Content-Transfer-Encoding': 'quoted-printable'} + parts = {'alternatives': [{'content': 'H=E4llo', 'headers': plain_headers}, {'content': '

H=C3=A4llo

', 'headers': html_headers}]} + + mail = PixelatedMail.from_soledad(None, None, self._create_bdoc(raw='some raw body'), parts=parts, soledad_querier=None) + + self.assertEquals(u'H=E4llo', mail.text_plain_body) + def test_clean_line_breaks_on_address_headers(self): many_recipients = 'One ,\nTwo , Normal ,\nalone@mail.com' headers = {'Cc': many_recipients, -- cgit v1.2.3