MAILET-149 Decode file names in StripAttachment
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/229c4d38 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/229c4d38 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/229c4d38 Branch: refs/heads/master Commit: 229c4d382db5f6884d1f2c136ab6feb5a3071dd7 Parents: f195524 Author: Benoit Tellier <btell...@linagora.com> Authored: Fri Jan 20 14:36:48 2017 +0700 Committer: Antoine Duprat <adup...@linagora.com> Committed: Mon Jan 23 16:19:25 2017 +0100 ---------------------------------------------------------------------- .../mailet/base/test/MimeMessageBuilder.java | 4 + .../transport/mailets/StripAttachment.java | 9 +- .../transport/mailets/StripAttachmentTest.java | 36 +++++ .../standard/src/test/resources/mime/gmail.mime | 153 +++++++++++++++++++ 4 files changed, 199 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/229c4d38/mailet/base/src/test/java/org/apache/mailet/base/test/MimeMessageBuilder.java ---------------------------------------------------------------------- diff --git a/mailet/base/src/test/java/org/apache/mailet/base/test/MimeMessageBuilder.java b/mailet/base/src/test/java/org/apache/mailet/base/test/MimeMessageBuilder.java index 40a7e18..c75136d 100644 --- a/mailet/base/src/test/java/org/apache/mailet/base/test/MimeMessageBuilder.java +++ b/mailet/base/src/test/java/org/apache/mailet/base/test/MimeMessageBuilder.java @@ -186,6 +186,10 @@ public class MimeMessageBuilder { return new MimeMessage(Session.getDefaultInstance(new Properties())); } + public static MimeMessage mimeMessageFromStream(InputStream inputStream) throws MessagingException { + return new MimeMessage(Session.getDefaultInstance(new Properties()), inputStream); + } + public static MimeMessageBuilder mimeMessageBuilder() { return new MimeMessageBuilder(); } http://git-wip-us.apache.org/repos/asf/james-project/blob/229c4d38/mailet/standard/src/main/java/org/apache/james/transport/mailets/StripAttachment.java ---------------------------------------------------------------------- diff --git a/mailet/standard/src/main/java/org/apache/james/transport/mailets/StripAttachment.java b/mailet/standard/src/main/java/org/apache/james/transport/mailets/StripAttachment.java index 20ff946..4bf7a5c 100644 --- a/mailet/standard/src/main/java/org/apache/james/transport/mailets/StripAttachment.java +++ b/mailet/standard/src/main/java/org/apache/james/transport/mailets/StripAttachment.java @@ -43,6 +43,8 @@ import javax.mail.internet.MimeUtility; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; +import org.apache.james.mime4j.codec.DecodeMonitor; +import org.apache.james.mime4j.codec.DecoderUtil; import org.apache.mailet.Mail; import org.apache.mailet.MailetException; import org.apache.mailet.base.GenericMailet; @@ -313,9 +315,10 @@ public class StripAttachment extends GenericMailet { } boolean shouldRemove = removeAttachments.equals(REMOVE_ALL); - if (isMatching(bodyPart, fileName)) { - storeBodyPartAsFile(bodyPart, mail, fileName); - storeBodyPartAsMailAttribute(bodyPart, mail, fileName); + String decodedName = DecoderUtil.decodeEncodedWords(fileName, DecodeMonitor.SILENT); + if (isMatching(bodyPart, decodedName)) { + storeBodyPartAsFile(bodyPart, mail, decodedName); + storeBodyPartAsMailAttribute(bodyPart, mail, decodedName); if (removeAttachments.equals(REMOVE_MATCHED)) { shouldRemove = true; } http://git-wip-us.apache.org/repos/asf/james-project/blob/229c4d38/mailet/standard/src/test/java/org/apache/james/transport/mailets/StripAttachmentTest.java ---------------------------------------------------------------------- diff --git a/mailet/standard/src/test/java/org/apache/james/transport/mailets/StripAttachmentTest.java b/mailet/standard/src/test/java/org/apache/james/transport/mailets/StripAttachmentTest.java index d888c44..0c481c9 100644 --- a/mailet/standard/src/test/java/org/apache/james/transport/mailets/StripAttachmentTest.java +++ b/mailet/standard/src/test/java/org/apache/james/transport/mailets/StripAttachmentTest.java @@ -310,6 +310,42 @@ public class StripAttachmentTest { } @Test + public void serviceShouldDecodeHeaderFilenames() throws MessagingException, IOException { + StripAttachment mailet = new StripAttachment(); + + String customAttribute = "my.custom.attribute"; + FakeMailetConfig mci = FakeMailetConfig.builder() + .mailetName("Test") + .setProperty("pattern", ".*\\.tmp") + .setProperty("attribute", customAttribute) + .build(); + mailet.init(mci); + + String expectedKey = "invite.tmp"; + MimeMessage message = MimeMessageBuilder.mimeMessageBuilder() + .setMultipartWithBodyParts( + MimeMessageBuilder.bodyPartBuilder() + .data("simple text") + .build(), + createAttachmentBodyPart(EXPECTED_ATTACHMENT_CONTENT, "=?US-ASCII?Q?" + expectedKey + "?=", TEXT_HEADERS)) + .build(); + + Mail mail = FakeMail.builder() + .mimeMessage(message) + .build(); + + mailet.service(mail); + + @SuppressWarnings("unchecked") + Map<String, byte[]> saved = (Map<String, byte[]>) mail.getAttribute(customAttribute); + assertThat(saved).hasSize(1); + assertThat(saved).containsKey(expectedKey); + MimeBodyPart savedBodyPart = new MimeBodyPart(new ByteArrayInputStream(saved.get(expectedKey))); + String content = IOUtils.toString(savedBodyPart.getInputStream()); + assertThat(content).isEqualTo(EXPECTED_ATTACHMENT_CONTENT); + } + + @Test public void initShouldThrowWhenPatternAndNotPatternAndMimeTypeAreNull() throws MessagingException { Mailet mailet = new StripAttachment(); http://git-wip-us.apache.org/repos/asf/james-project/blob/229c4d38/mailet/standard/src/test/resources/mime/gmail.mime ---------------------------------------------------------------------- diff --git a/mailet/standard/src/test/resources/mime/gmail.mime b/mailet/standard/src/test/resources/mime/gmail.mime new file mode 100644 index 0000000..9ef251f --- /dev/null +++ b/mailet/standard/src/test/resources/mime/gmail.mime @@ -0,0 +1,153 @@ +Return-Path: <adup...@linagora.com> +Subject: Fwd: Invitation: (Aucun objet) - ven. 20 janv. 2017 14:00 - 15:00 + (CET) (adup...@linagora.com) +To: =?UTF-8?Q?Beno=c3=aet_TELLIER?= <tell...@linagora.com> +From: aduprat <dup...@linagora.com> +X-Forwarded-Message-Id: <001a114380a6a260d10546776...@google.com> +Message-ID: <f18fa52e-2e9d-1125-327d-2100b23f8...@linagora.com> +Date: Thu, 19 Jan 2017 20:36:37 +0100 +MIME-Version: 1.0 +In-Reply-To: <001a114380a6a260d10546776...@google.com> +Content-Type: multipart/mixed; + boundary="------------17D96D411CBD55D8239A8C1F" + +This is a multi-part message in MIME format. +--------------17D96D411CBD55D8239A8C1F +Content-Type: multipart/alternative; + boundary="------------64D716A3DDAEC185D3E67448" + + +--------------64D716A3DDAEC185D3E67448 +Content-Type: text/plain; charset=utf-8; format=flowed +Content-Transfer-Encoding: 8bit + + + + +-------- Message transéré -------- +Sujet : Invitation: (Aucun objet) - ven. 20 janv. 2017 14:00 - 15:00 +(CET) (dup...@linagora.com) +Date : Thu, 19 Jan 2017 19:18:23 +0000 +De : Antoine Duprat <dup...@gmail.com> +Répondre à : antdup...@gmail.com +Pour : adup...@linagora.com + + + + +--------------64D716A3DDAEC185D3E67448 +Content-Type: text/html; charset=utf-8 +Content-Transfer-Encoding: 8bit + +<html> + <head> + + <meta http-equiv="content-type" content="text/html; charset=utf-8"> + </head> + <body bgcolor="#FFFFFF" text="#000000"> + <p><br> + </p> + <div class="moz-forward-container"><br> + <br> + -------- Message transéré -------- + <table class="moz-email-headers-table" border="0" cellpadding="0" + cellspacing="0"> + <tbody> + <tr> + <th align="RIGHT" valign="BASELINE" nowrap="nowrap">Sujetà : + </th> + <td>Invitation: (Aucun objet) - ven. 20 janv. 2017 14:00 - + 15:00 (CET) (<a class="moz-txt-link-abbreviated" href="mailto:adup...@linagora.com">adup...@linagora.com</a>)</td> + </tr> + <tr> + <th align="RIGHT" valign="BASELINE" nowrap="nowrap">Dateà : </th> + <td>Thu, 19 Jan 2017 19:18:23 +0000</td> + </tr> + <tr> + <th align="RIGHT" valign="BASELINE" nowrap="nowrap">Deà : </th> + <td>Antoine Duprat <a class="moz-txt-link-rfc2396E" href="mailto:antdup...@gmail.com"><antdup...@gmail.com></a></td> + </tr> + <tr> + <th align="RIGHT" valign="BASELINE" nowrap="nowrap">Répondre + à à : </th> + <td><a class="moz-txt-link-abbreviated" href="mailto:antdup...@gmail.com">antdup...@gmail.com</a></td> + </tr> + <tr> + <th align="RIGHT" valign="BASELINE" nowrap="nowrap">Pourà : </th> + <td><a class="moz-txt-link-abbreviated" href="mailto:adup...@linagora.com">adup...@linagora.com</a></td> + </tr> + </tbody> + </table> + <br> + <br> + </div> + </body> +</html> + +--------------64D716A3DDAEC185D3E67448-- + +--------------17D96D411CBD55D8239A8C1F +Content-Type: text/calendar; + name="Portion de message joint" +Content-Transfer-Encoding: 8bit +Content-Disposition: attachment; + filename="Portion de message joint" + +BEGIN:VCALENDAR +PRODID:-//Google Inc//Google Calendar 70.9054//EN +VERSION:2.0 +CALSCALE:GREGORIAN +METHOD:REQUEST +BEGIN:VEVENT +DTSTART:20170120T130000Z +DTEND:20170120T140000Z +DTSTAMP:20170119T191823Z +ORGANIZER;CN=Antoine Duprat:mailto:dup...@gmail.com +UID:ah86k5m342bmcrbe9khkkhl...@google.com +ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP= + TRUE;CN=adup...@linagora.com;X-NUM-GUESTS=0:mailto:dup...@linagora.com +ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=ACCEPTED;RSVP=TRUE + ;CN=Antoine Duprat;X-NUM-GUESTS=0:mailto:dup...@gmail.com +CREATED:20170119T191823Z +DESCRIPTION:Affichez votre evenement sur la page https://www.google.com/cal + endar/event?action=VIEW&eid=YWg4Nms1bTM0MmJtY3JiZTlraGtraGxuMDAgYWR1cHJhdEB + saW5hZ29yYS5jb20&tok=MTkjYW50ZHVwcmF0QGdtYWlsLmNvbTg1OTM5NWM4MGRlYmE1YTI4Nz + RjN2UyNjU0M2YyZmQ4NzRkNThhYTQ&ctz=Europe/Paris&hl=fr. +LAST-MODIFIED:20170119T191823Z +LOCATION: +SEQUENCE:0 +STATUS:CONFIRMED +SUMMARY: +TRANSP:OPAQUE +END:VEVENT +END:VCALENDAR + + +--------------17D96D411CBD55D8239A8C1F +Content-Type: application/ics; + name="invite.ics" +Content-Transfer-Encoding: base64 +Content-Disposition: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dU1EQWdZV1IxY0hKaGRFQg0KIHNhVzVoWjI5eVlTNWpiMjAmdG9rPU1Ua2pZVzUw +WkhWd2NtRjBRR2R0WVdsc0xtTnZiVGcxT1RNNU5XTTRNR1JsWW1FMVlUSTROeg0KIFJqTjJV +eU5qVTBNMll5Wm1RNE56UmtOVGhoWVRRJmN0ej1FdXJvcGUvUGFyaXMmaGw9ZnIuDQpMQVNU +LU1PRElGSUVEOjIwMTcwMTE5VDE5MTgyM1oNCkxPQ0FUSU9OOg0KU0VRVUVOQ0U6MA0KU1RB +VFVTOkNPTkZJUk1FRA0KU1VNTUFSWToNClRSQU5TUDpPUEFRVUUNCkVORDpWRVZFTlQNCkVO +RDpWQ0FMRU5EQVINCg== +--------------17D96D411CBD55D8239A8C1F-- --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org