]> khamidou — GitHub mirrors - flanker.git/commitdiff
fix exception in serialization of messages with malformed headers
authorJeremy Schlatter <jeremy.schlatter@gmail.com>
Thu, 11 Jun 2015 18:21:12 +0000 (11:21 -0700)
committerJeremy Schlatter <jeremy.schlatter@gmail.com>
Thu, 11 Jun 2015 18:21:12 +0000 (11:21 -0700)
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.

flanker/mime/message/part.py
tests/__init__.py
tests/fixtures/messages/outlook-express.eml [new file with mode: 0644]
tests/mime/message/part_test.py

index ba079f2c5693be3e5a92dd821b5fe4a57189793f..391d945ff1c95c73b4045e0d4a1d61ac09c678f4 100644 (file)
@@ -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:
index a666e428b5cc6f4406dbd43cd30c17abad71a7c5..40db5c72c26fd11052517050686bbc3d7e37f4d2 100644 (file)
@@ -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 (file)
index 0000000..5366b1d
--- /dev/null
@@ -0,0 +1,28 @@
+X-Envelope-From: <bob@example.com>
+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: <EFOOJNZYKZFPXKXDZPRSCFMBVCHB.bob@example.com>
+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: <bob@example.com>
+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 <bob@example.com>
+Subject: Sup?
+To: "Ally" <ally@example.com>
+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=
index ff86c8e3881a3fddba97bedf1947316a2c2fbc38..abdce342fbd5180d357846c42ca63dfaf9bb8fc5 100644 (file)
@@ -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