From: Jeremy Schlatter Date: Thu, 11 Jun 2015 18:21:12 +0000 (-0700) Subject: fix exception in serialization of messages with malformed headers X-Git-Tag: v0.4.32~1 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=45e70dc61a8cb979fa16fba1c62fb535fa190670;p=flanker.git fix exception in serialization of messages with malformed headers flanker.mime.create.from_string does not fully parse message headers. Instead, headers are parsed lazily the first time they are accessed. So even though flanker.mime.create.from_string does not support malformed headers, this isn't a problem if they are never accessed. Commits 9c89b875dc0eafc01c7f9725d5db5aff8f798f70 and cd271929bf831999c40e616da3dfacd3694eae56 caused headers to be accessed every time a message is serialized. This made it impossible to parse and then re-serialize a message with broken headers, even though this used to be possible if the headers were never accessed after parsing. This commit brings back that option: it is again possible to parse and then re-serialize a message with broken headers. --- diff --git a/flanker/mime/message/part.py b/flanker/mime/message/part.py index ba079f2..391d945 100644 --- a/flanker/mime/message/part.py +++ b/flanker/mime/message/part.py @@ -80,6 +80,9 @@ class Stream(object): self._body = value self._body_changed = True + def _stream_prepended_headers(self, out): + if self._headers: + self._headers.to_stream(out, prepends_only=True) def headers_changed(self, ignore_prepends=False): return self._headers is not None and self._headers.have_changed(ignore_prepends) @@ -179,6 +182,9 @@ class Body(object): def body_changed(self): return True + def _stream_prepended_headers(self, out): + self.headers.to_stream(out, prepends_only=True) + class Part(object): @@ -199,6 +205,9 @@ class Part(object): def body_changed(self): return True + def _stream_prepended_headers(self, out): + self.headers.to_stream(out, prepends_only=True) + class RichPartMixin(object): @@ -469,7 +478,7 @@ class MimePart(RichPartMixin): # no copying, no alternation, yeah! if self.is_root() and not self.was_changed(ignore_prepends=True): with closing(StringIO()) as out: - self.headers.to_stream(out, prepends_only=True) + self._container._stream_prepended_headers(out) return out.getvalue() + self._container.string else: with closing(StringIO()) as out: @@ -481,7 +490,7 @@ class MimePart(RichPartMixin): Serializes the message using a file like object. """ if not self.was_changed(ignore_prepends=True): - self.headers.to_stream(out, prepends_only=True) + self._container._stream_prepended_headers(out) out.write(self._container.read_message()) else: try: diff --git a/tests/__init__.py b/tests/__init__.py index a666e42..40db5c7 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -65,6 +65,7 @@ QUOTED_PRINTABLE = open( TEXT_ONLY = open(fixture_file("messages/text-only.eml")).read() MAILGUN_PIC = open(fixture_file("messages/mailgun-pic.eml")).read() BZ2_ATTACHMENT = open(fixture_file("messages/bz2-attachment.eml")).read() +OUTLOOK_EXPRESS = open(fixture_file("messages/outlook-express.eml")).read() AOL_FBL = open(fixture_file("messages/complaints/aol.eml")).read() YAHOO_FBL = open(fixture_file("messages/complaints/yahoo.eml")).read() diff --git a/tests/fixtures/messages/outlook-express.eml b/tests/fixtures/messages/outlook-express.eml new file mode 100644 index 0000000..5366b1d --- /dev/null +++ b/tests/fixtures/messages/outlook-express.eml @@ -0,0 +1,28 @@ +X-Envelope-From: +Received: from hwmail1.mailset.cn (hwmail1.mailset.cn [106.186.124.194]) by + mxa.mailgun.org with ESMTP id 53c3560e.7fe658457ed8-in3; Mon, 14 Jul 2014 + 04:01:18 -0000 (UTC) +Received: (ce send program); Mon, 14 Jul 2014 12:01:03 +0800 +Received: from 121.14.7.230 (HELO etcsmtp.xinnetdns.com) (121.14.7.230) by + 106.186.124.194 with SMTP; Mon, 14 Jul 2014 12:01:03 +0800 +Received: (ce send program); Mon, 14 Jul 2014 11:49:09 +0800 +X-Ce-Autoforward: etcsmtp.xinnetdns.com +Message-Id: +Received: (ce send program); Mon, 14 Jul 2014 11:49:07 +0800 +Received: from 123.100.1.157 (HELO sink-gdmm.com) (123.100.1.157) by + 121.14.68.74 with SMTP; Mon, 14 Jul 2014 11:49:07 +0800 +Return-Path: +Received: from [110.230.143.217] by 121.14.68.114 with surfront esmtp id + 9885528886420; Mon, 14 Jul 2014 12:00:56 +0800 (CST) +From: Bobby +Subject: Sup? +To: "Ally" +Content-Type: application/octet-stream; + name="�½� �ı��ĵ�.txt" +Content-Transfer-Encoding: base64 +Mime-Version: 1.0 +Date: Tue, 14 Jul 2015 12:19:08 +0800 +X-Mailer: Microsoft Outlook Express 6.00.2800.1106 +X-Mailgun-Incoming: Yes + +0YLQtdC60YHRgtC+0LLQvtC1INCy0LvQvtC20LXQvdC40LU= diff --git a/tests/mime/message/part_test.py b/tests/mime/message/part_test.py index ff86c8e..abdce34 100644 --- a/tests/mime/message/part_test.py +++ b/tests/mime/message/part_test.py @@ -14,7 +14,7 @@ from tests import (BILINGUAL, BZ2_ATTACHMENT, ENCLOSED, TORTURE, TORTURE_PART, TEXT_ONLY, ENCLOSED_BROKEN_BODY, RUSSIAN_ATTACH_YAHOO, MAILGUN_PIC, MAILGUN_PNG, MULTIPART, IPHONE, SPAM_BROKEN_CTYPE, BOUNCE, NDN, NO_CTYPE, RELATIVE, - MULTI_RECEIVED_HEADERS) + MULTI_RECEIVED_HEADERS, OUTLOOK_EXPRESS) from tests.mime.message.scanner_test import TORTURE_PARTS, tree_to_string @@ -236,6 +236,16 @@ def preserve_formatting_with_new_headers_test(): eq_(MULTIPART, remaining_mime) +def parse_then_serialize_malformed_message_test(): + """ + We don't have to fully parse message headers if they are never accessed. + Thus we should be able to parse then serialize a message with malformed + headers without crashing, even though we would crash if we fully parsed it. + """ + serialized = scan(OUTLOOK_EXPRESS).to_string() + eq_(OUTLOOK_EXPRESS, serialized) + + def ascii_to_unicode_test(): """Make sure that ascii uprades to unicode whenever needed""" # contains unicode chars