Skip to content

Commit

Permalink
ZBUG-1035:Content-Transfer-Encoding 8bit causes breakage of S/MIME si…
Browse files Browse the repository at this point in the history
…gnature
  • Loading branch information
sneha-patil-synacor committed Jun 18, 2020
1 parent 54cab9a commit 75b10b9
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions store/src/java/com/zimbra/cs/service/mail/ParseMimeMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
*/
package com.zimbra.cs.service.mail;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -31,10 +34,12 @@
import javax.mail.Part;
import javax.mail.SendFailedException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.InternetHeaders;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimePart;
import javax.mail.internet.MimeUtility;
import javax.mail.util.SharedByteArrayInputStream;

import com.google.common.base.Charsets;
Expand Down Expand Up @@ -567,6 +572,13 @@ private static void setContent(MimeMessage mm, MimeMultipart mmp, Element elem,
// or a multipart/alternative created just above....either way we are safe to stick
// the client's nice and simple body right here
String text = elem.getAttribute(MailConstants.E_CONTENT, "");
boolean isAscii = StringUtil.isAsciiString(text);
if (!isAscii) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream encodedOut = MimeUtility.encode(baos, MimeConstants.ET_QUOTED_PRINTABLE);
encodedOut.write(text.getBytes());
text = baos.toString();
}
byte[] raw = text.getBytes(Charsets.UTF_8);
if (raw.length > 0 || !LC.mime_exclude_empty_content.booleanValue() || ctype.getPrimaryType().equals("text")) {
ctxt.incrementSize("message body", raw.length);
Expand All @@ -578,9 +590,18 @@ private static void setContent(MimeMessage mm, MimeMultipart mmp, Element elem,
Object content = ctype.getContentType().equals(ContentType.MESSAGE_RFC822) ?
new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(raw)) : text;
if (mmp != null) {
MimeBodyPart mbp = new ZMimeBodyPart();
mbp.setContent(content, ctype.toString());
mmp.addBodyPart(mbp);
if (!isAscii) {
String mbpHeaders = "Content-Type: " + ctype.toString()
+ "\r\nContent-Transfer-Encoding: " + MimeConstants.ET_QUOTED_PRINTABLE
+ "\r\n";
mmp.addBodyPart(new MimeBodyPart(
new InternetHeaders(new ByteArrayInputStream(mbpHeaders.getBytes())),
content.toString().getBytes()));
} else {
MimeBodyPart mbp = new ZMimeBodyPart();
mbp.setContent(content, ctype.toString());
mmp.addBodyPart(mbp);
}
} else {
mm.setContent(content, ctype.toString());
}
Expand Down

0 comments on commit 75b10b9

Please sign in to comment.