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 <[email protected]> Authored: Tue Nov 28 16:09:12 2017 +0100 Committer: benwa <[email protected]> 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: [email protected]?=\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: [email protected]?=\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: [email protected]?=\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: [email protected] For additional commands, e-mail: [email protected]
