]> khamidou — GitHub mirrors - flanker.git/commitdiff
Fix handling for broken .eml messages.
authorJeremy Schlatter <jeremy.schlatter@gmail.com>
Thu, 4 Dec 2014 00:54:06 +0000 (16:54 -0800)
committerJeremy Schlatter <jeremy.schlatter@gmail.com>
Mon, 27 Jul 2015 17:59:18 +0000 (10:59 -0700)
I didn't test this well enough yesterday. It turns out that the
attachment was getting created, but its ContentType was overwritten to
message/rfc822. This caused problems elsewhere because the the
attachment we created did not have the structure expected for
message/rfc822.

The fix: Don't let Body override our ContentType declaration.

flanker/mime/create.py
flanker/mime/message/part.py
tests/__init__.py
tests/fixtures/messages/false-multipart.eml [new file with mode: 0644]
tests/mime/message/create_test.py

index 7b8cefaa8a61ba132435d70e7ebf22b20f354403..679044df5c9ec5145ddd8ea1f7b4cdd1f77df6e1 100644 (file)
@@ -37,10 +37,11 @@ def text(subtype, body, charset=None, disposition=None, filename=None):
 
 
 def binary(maintype, subtype, body, filename=None,
-           disposition=None, charset=None):
+           disposition=None, charset=None, trust_ctype=False):
     return MimePart(
         container=Body(
             content_type=ContentType(maintype, subtype),
+            trust_ctype=trust_ctype,
             body=body,
             charset=charset,
             disposition=disposition,
@@ -75,7 +76,7 @@ def attachment(content_type, body, filename=None,
         content_type.sub,
         body, filename,
         disposition,
-        charset)
+        charset, True)
 
 
 def from_string(string):
index 391d945ff1c95c73b4045e0d4a1d61ac09c678f4..4e8c417d941ed9d05386e3b0d4b4bf4bb9309436 100644 (file)
@@ -137,7 +137,7 @@ def _guess_type(filename):
 
 class Body(object):
     def __init__(
-        self, content_type, body, charset=None, disposition=None, filename=None):
+        self, content_type, body, charset=None, disposition=None, filename=None, trust_ctype=False):
         self.headers = headers.MimeHeaders()
         self.body = body
         self.disposition = disposition or ('attachment' if filename else None)
@@ -147,7 +147,8 @@ class Body(object):
         if self.filename:
             self.filename = path.basename(self.filename)
 
-        content_type = adjust_content_type(content_type, body, filename)
+        if not trust_ctype:
+            content_type = adjust_content_type(content_type, body, filename)
 
         if content_type.main == 'text':
             # the text should have a charset
index 40db5c72c26fd11052517050686bbc3d7e37f4d2..dfe3bb3e8e53cb6557c37c94652c4ffaf41833cd 100644 (file)
@@ -52,6 +52,8 @@ ENCLOSED_BROKEN_BODY = open(
     fixture_file("messages/enclosed-broken-body.eml")).read()
 ENCLOSED_BROKEN_ENCODING = open(
     fixture_file("messages/enclosed-bad-encoding.eml")).read()
+FALSE_MULTIPART = open(
+    fixture_file("messages/false-multipart.eml")).read()
 ENCODED_HEADER = open(
     fixture_file("messages/encoded-header.eml")).read()
 MESSAGE_EXTERNAL_BODY= open(
diff --git a/tests/fixtures/messages/false-multipart.eml b/tests/fixtures/messages/false-multipart.eml
new file mode 100644 (file)
index 0000000..f208efc
--- /dev/null
@@ -0,0 +1,11 @@
+Content-Type: multipart/alternative;
+ boundary="9c1a8544d562472896bc53f5da3057c0"
+Mime-Version: 1.0
+Subject: Broken MIME
+From: Mime Breaker <broken.mime@notreallymultipart.com>
+To: example.smith@example.com
+Message-Id: <12345@example.com>
+
+\r
+
+
index 4c9b4c6be3cfa826b40ae88515f279769e628b75..79e2eaa81da0f609c4f26c5f105800e68e5a6ae9 100644 (file)
@@ -261,6 +261,13 @@ def attaching_emails_test():
     eq_("message/rfc822", attachment.content_type)
 
 
+def attaching_broken_emails_test():
+    attachment = create.attachment(
+        "application/octet-stream", FALSE_MULTIPART, "message.eml", "attachment")
+    ok_(attachment.is_attachment())
+    eq_("application/octet-stream", attachment.content_type)
+
+
 def attaching_images_test():
     attachment = create.attachment(
         "application/octet-stream", MAILGUN_PNG, "/home/alex/mailgun.png")