You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2017/11/30 02:39:22 UTC

[6/9] james-project git commit: JAMES-2239 Bounce email now properly decode header fields

JAMES-2239 Bounce email now properly decode header fields


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/d6d0e679
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/d6d0e679
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/d6d0e679

Branch: refs/heads/master
Commit: d6d0e679c64f1af30101b82c64b18eedb1462482
Parents: a585dc2
Author: Luc DUZAN <ld...@linagora.com>
Authored: Tue Nov 28 16:09:12 2017 +0100
Committer: benwa <bt...@linagora.com>
Committed: Thu Nov 30 09:36:59 2017 +0700

----------------------------------------------------------------------
 .../mailets/redirect/NotifyMailetsMessage.java  | 18 +++-
 .../redirect/NotifyMailetsMessageTest.java      | 98 ++++++++++++++++++++
 2 files changed, 114 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/d6d0e679/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/NotifyMailetsMessage.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/NotifyMailetsMessage.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/NotifyMailetsMessage.java
index b7511e8..1a7e1ee 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/NotifyMailetsMessage.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/NotifyMailetsMessage.java
@@ -19,11 +19,13 @@
 
 package org.apache.james.transport.mailets.redirect;
 
+import java.io.UnsupportedEncodingException;
 import java.util.List;
 import java.util.Optional;
 
 import javax.mail.MessagingException;
 import javax.mail.internet.MimeMessage;
+import javax.mail.internet.MimeUtility;
 
 import org.apache.james.core.MailAddress;
 import org.apache.james.transport.util.SizeUtils;
@@ -59,7 +61,8 @@ public class NotifyMailetsMessage {
             .append(LINE_BREAK);
 
         if (message.getSubject() != null) {
-            builder.append("  Subject: " + message.getSubject())
+            builder.append("  Subject: ")
+                .append(safelyDecode(message.getSubject()))
                 .append(LINE_BREAK);
         }
         if (message.getSentDate() != null) {
@@ -110,12 +113,23 @@ public class NotifyMailetsMessage {
         }
     }
 
+    @VisibleForTesting static String safelyDecode(String text) {
+        try {
+            return MimeUtility.decodeText(text);
+        } catch (UnsupportedEncodingException e) {
+            LOGGER.error("Could not decode following value {}", text, e);
+
+            return text;
+        }
+    }
+
     private void appendAddresses(StringBuilder builder, String title, String[] addresses) {
         if (addresses != null) {
             builder.append("  " + title + ": ")
                 .append(LINE_BREAK);
             for (String address : flatten(addresses)) {
-                builder.append(address + " ")
+                builder.append(safelyDecode(address))
+                    .append(" ")
                     .append(LINE_BREAK);
             }
             builder.append(LINE_BREAK);

http://git-wip-us.apache.org/repos/asf/james-project/blob/d6d0e679/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/redirect/NotifyMailetsMessageTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/redirect/NotifyMailetsMessageTest.java b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/redirect/NotifyMailetsMessageTest.java
index ca6448c..0b4c993 100644
--- a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/redirect/NotifyMailetsMessageTest.java
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/redirect/NotifyMailetsMessageTest.java
@@ -33,6 +33,7 @@ import javax.mail.MessagingException;
 import javax.mail.Session;
 import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeMessage;
+import javax.mail.internet.MimeUtility;
 
 import org.apache.james.core.MailAddress;
 import org.apache.mailet.Mail;
@@ -232,4 +233,101 @@ public class NotifyMailetsMessageTest {
         assertThat(NotifyMailetsMessage.getMessageSizeEstimation(mail))
             .isEqualTo(Optional.of(size));
     }
+
+    @Test
+    public void generateMessageShouldDecodeEncodedSubject() throws Exception {
+        String content = "MIME-Version: 1.0\r\n" +
+            "Subject: =?UTF-8?Q?Cl=c3=b4ture_&_Paie_du_mois?=\r\n" +
+            "Content-Type: text/plain; charset=utf-8\r\n" +
+            "\r\n" +
+            "test\r\n";
+
+        MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()), new ByteArrayInputStream(content.getBytes()));
+        FakeMail mail = FakeMail.from(message);
+
+        String generateMessage = new NotifyMailetsMessage().generateMessage("my message", mail);
+
+        assertThat(generateMessage).contains("Subject: Clôture & Paie du mois");
+    }
+
+    @Test
+    public void generateMessageShouldDecodeEncodedFrom() throws Exception {
+        String content = "MIME-Version: 1.0\r\n" +
+            "From: =?UTF-8?Q?=F0=9F=90=83@linagora.com?=\r\n" +
+            "Content-Type: text/plain; charset=utf-8\r\n" +
+            "\r\n" +
+            "test\r\n";
+
+        MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()), new ByteArrayInputStream(content.getBytes()));
+        FakeMail mail = FakeMail.from(message);
+
+        String generateMessage = new NotifyMailetsMessage().generateMessage("my message", mail);
+
+        assertThat(generateMessage).contains("  From: \n" +
+            "🐃@linagora.com");
+    }
+
+    @Test
+    public void generateMessageShouldDecodeEncodedTo() throws Exception {
+        String content = "MIME-Version: 1.0\r\n" +
+            "To: =?UTF-8?Q?=F0=9F=9A=BE@linagora.com?=\r\n" +
+            "Content-Type: text/plain; charset=utf-8\r\n" +
+            "\r\n" +
+            "test\r\n";
+
+        MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()), new ByteArrayInputStream(content.getBytes()));
+        FakeMail mail = FakeMail.from(message);
+
+        String generateMessage = new NotifyMailetsMessage().generateMessage("my message", mail);
+
+        assertThat(generateMessage).contains("  To: \n" +
+            "🚾@linagora.com");
+    }
+
+    @Test
+    public void generateMessageShouldDecodeEncodedCc() throws Exception {
+        String content = "MIME-Version: 1.0\r\n" +
+            "Cc: =?UTF-8?Q?=F0=9F=9A=B2@linagora.com?=\r\n" +
+            "Content-Type: text/plain; charset=utf-8\r\n" +
+            "\r\n" +
+            "test\r\n";
+
+        MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()), new ByteArrayInputStream(content.getBytes()));
+        FakeMail mail = FakeMail.from(message);
+
+        String generateMessage = new NotifyMailetsMessage().generateMessage("my message", mail);
+
+        assertThat(generateMessage).contains("  CC: \n" +
+            "🚲@linagora.com");
+    }
+
+    @Test
+    public void safelyDecodeShouldReturnTextNotEncodedUnmodified() throws Exception {
+        String text = "Why not unicode for Llama";
+
+        assertThat(NotifyMailetsMessage.safelyDecode(text))
+            .isEqualTo(text);
+    }
+
+    @Test
+    public void safelyDecodeShouldCorrectlyDecodeQuotedPrintable() throws Exception {
+        assertThat(NotifyMailetsMessage.safelyDecode("=?UTF-8?Q?=E2=99=A5=F0=9F=9A=B2?="))
+            .isEqualTo("♥🚲");
+    }
+
+    @Test
+    public void safelyDecodeShouldReturnInvalidEncodedTextUnmodified() throws Exception {
+        String invalidEncodedText = "=?UTF-8?Q?=E2=99=A5=FX=9F=9A=B2?=";
+
+        assertThat(NotifyMailetsMessage.safelyDecode(invalidEncodedText))
+            .isEqualTo(invalidEncodedText);
+    }
+
+    @Test
+    public void safelyDecodeShouldReturnEncodedTextUnmodifiedWhenUnknownCharset() throws Exception {
+        String encodedTextWithUnknownCharset = "=?UTF-9?Q?=E2=99=A5=F0=9F=9A=B2?=";
+
+        assertThat(NotifyMailetsMessage.safelyDecode(encodedTextWithUnknownCharset))
+            .isEqualTo(encodedTextWithUnknownCharset);
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org