From 5bc7ac3901ef7a05bbb8fe9f33e6672a578f18d3 Mon Sep 17 00:00:00 2001 From: cketti Date: Sat, 10 Oct 2020 22:05:08 +0200 Subject: [PATCH] When constructing new messages check header values --- .../AutocryptTransferMessageCreator.kt | 6 +++--- .../com/fsck/k9/message/MessageBuilder.java | 12 ++++++------ .../fsck/k9/message/PgpMessageBuilder.java | 6 +++--- .../fsck/k9/mail/internet/MimeBodyPart.java | 19 +++++++++++++++++++ .../fsck/k9/mail/internet/MimeMessage.java | 12 ++++++++++++ 5 files changed, 43 insertions(+), 12 deletions(-) diff --git a/app/core/src/main/java/com/fsck/k9/autocrypt/AutocryptTransferMessageCreator.kt b/app/core/src/main/java/com/fsck/k9/autocrypt/AutocryptTransferMessageCreator.kt index 1d5fb11a0..2228ab251 100644 --- a/app/core/src/main/java/com/fsck/k9/autocrypt/AutocryptTransferMessageCreator.kt +++ b/app/core/src/main/java/com/fsck/k9/autocrypt/AutocryptTransferMessageCreator.kt @@ -20,8 +20,8 @@ class AutocryptTransferMessageCreator(private val stringProvider: AutocryptStrin val subjectText = stringProvider.transferMessageSubject() val messageText = stringProvider.transferMessageBody() - val textBodyPart = MimeBodyPart(TextBody(messageText)) - val dataBodyPart = MimeBodyPart(BinaryMemoryBody(data, "7bit")) + val textBodyPart = MimeBodyPart.create(TextBody(messageText)) + val dataBodyPart = MimeBodyPart.create(BinaryMemoryBody(data, "7bit")) dataBodyPart.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "application/autocrypt-setup") dataBodyPart.setHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, "attachment; filename=\"autocrypt-setup-message\"") @@ -29,7 +29,7 @@ class AutocryptTransferMessageCreator(private val stringProvider: AutocryptStrin messageBody.addBodyPart(textBodyPart) messageBody.addBodyPart(dataBodyPart) - val message = MimeMessage() + val message = MimeMessage.create() MimeMessageHelper.setBody(message, messageBody) val nowDate = Date() diff --git a/app/core/src/main/java/com/fsck/k9/message/MessageBuilder.java b/app/core/src/main/java/com/fsck/k9/message/MessageBuilder.java index d0727e590..ca8869963 100644 --- a/app/core/src/main/java/com/fsck/k9/message/MessageBuilder.java +++ b/app/core/src/main/java/com/fsck/k9/message/MessageBuilder.java @@ -84,7 +84,7 @@ public abstract class MessageBuilder { protected MimeMessage build() throws MessagingException { //FIXME: check arguments - MimeMessage message = new MimeMessage(); + MimeMessage message = MimeMessage.create(); buildHeader(message); buildBody(message); @@ -168,8 +168,8 @@ public abstract class MessageBuilder { composedMimeMessage.setSubType("alternative"); // Let the receiver select either the text or the HTML part. bodyPlain = buildText(isDraft, SimpleMessageFormat.TEXT); - composedMimeMessage.addBodyPart(new MimeBodyPart(bodyPlain, "text/plain")); - composedMimeMessage.addBodyPart(new MimeBodyPart(body, "text/html")); + composedMimeMessage.addBodyPart(MimeBodyPart.create(bodyPlain, "text/plain")); + composedMimeMessage.addBodyPart(MimeBodyPart.create(body, "text/html")); if (hasAttachments) { // If we're HTML and have attachments, we have a MimeMultipart container to hold the @@ -177,7 +177,7 @@ public abstract class MessageBuilder { // (composedMimeMessage) with the user's composed messages, and subsequent parts for // the attachments. MimeMultipart mp = createMimeMultipart(); - mp.addBodyPart(new MimeBodyPart(composedMimeMessage)); + mp.addBodyPart(MimeBodyPart.create(composedMimeMessage)); addAttachmentsToMessage(mp); MimeMessageHelper.setBody(message, mp); } else { @@ -188,7 +188,7 @@ public abstract class MessageBuilder { // Text-only message. if (hasAttachments) { MimeMultipart mp = createMimeMultipart(); - mp.addBodyPart(new MimeBodyPart(body, "text/plain")); + mp.addBodyPart(MimeBodyPart.create(body, "text/plain")); addAttachmentsToMessage(mp); MimeMessageHelper.setBody(message, mp); } else { @@ -233,7 +233,7 @@ public abstract class MessageBuilder { } Body body = new TempFileBody(attachment.getFileName()); - MimeBodyPart bp = new MimeBodyPart(body); + MimeBodyPart bp = MimeBodyPart.create(body); addContentType(bp, attachment.getContentType(), attachment.getName()); addContentDisposition(bp, attachment.getName(), attachment.getSize()); diff --git a/app/core/src/main/java/com/fsck/k9/message/PgpMessageBuilder.java b/app/core/src/main/java/com/fsck/k9/message/PgpMessageBuilder.java index e55319853..61c963836 100644 --- a/app/core/src/main/java/com/fsck/k9/message/PgpMessageBuilder.java +++ b/app/core/src/main/java/com/fsck/k9/message/PgpMessageBuilder.java @@ -395,7 +395,7 @@ public class PgpMessageBuilder extends MessageBuilder { multipartSigned.setSubType("signed"); multipartSigned.addBodyPart(signedBodyPart); multipartSigned.addBodyPart( - new MimeBodyPart(new BinaryMemoryBody(signedData, MimeUtil.ENC_7BIT), + MimeBodyPart.create(new BinaryMemoryBody(signedData, MimeUtil.ENC_7BIT), "application/pgp-signature; name=\"signature.asc\"")); MimeMessageHelper.setBody(currentProcessedMimeMessage, multipartSigned); @@ -414,8 +414,8 @@ public class PgpMessageBuilder extends MessageBuilder { private void mimeBuildEncryptedMessage(@NonNull Body encryptedBodyPart) throws MessagingException { MimeMultipart multipartEncrypted = createMimeMultipart(); multipartEncrypted.setSubType("encrypted"); - multipartEncrypted.addBodyPart(new MimeBodyPart(new TextBody("Version: 1"), "application/pgp-encrypted")); - MimeBodyPart encryptedPart = new MimeBodyPart(encryptedBodyPart, "application/octet-stream; name=\"encrypted.asc\""); + multipartEncrypted.addBodyPart(MimeBodyPart.create(new TextBody("Version: 1"), "application/pgp-encrypted")); + MimeBodyPart encryptedPart = MimeBodyPart.create(encryptedBodyPart, "application/octet-stream; name=\"encrypted.asc\""); encryptedPart.addHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, "inline; filename=\"encrypted.asc\""); multipartEncrypted.addBodyPart(encryptedPart); MimeMessageHelper.setBody(currentProcessedMimeMessage, multipartEncrypted); diff --git a/mail/common/src/main/java/com/fsck/k9/mail/internet/MimeBodyPart.java b/mail/common/src/main/java/com/fsck/k9/mail/internet/MimeBodyPart.java index ffb0aac2e..1c86dec2d 100644 --- a/mail/common/src/main/java/com/fsck/k9/mail/internet/MimeBodyPart.java +++ b/mail/common/src/main/java/com/fsck/k9/mail/internet/MimeBodyPart.java @@ -27,6 +27,20 @@ public class MimeBodyPart extends BodyPart { private final MimeHeader mHeader; private Body mBody; + /** + * Creates an instance that will check the header field syntax when adding headers. + */ + public static MimeBodyPart create(Body body) throws MessagingException { + return new MimeBodyPart(body, null, true); + } + + /** + * Creates an instance that will check the header field syntax when adding headers. + */ + public static MimeBodyPart create(Body body, String contentType) throws MessagingException { + return new MimeBodyPart(body, contentType, true); + } + public MimeBodyPart() throws MessagingException { this(null); } @@ -36,7 +50,12 @@ public class MimeBodyPart extends BodyPart { } public MimeBodyPart(Body body, String contentType) throws MessagingException { + this(body, contentType, false); + } + + private MimeBodyPart(Body body, String contentType, boolean checkHeaders) throws MessagingException { mHeader = new MimeHeader(); + mHeader.setCheckHeaders(checkHeaders); if (contentType != null) { addHeader(MimeHeader.HEADER_CONTENT_TYPE, contentType); } diff --git a/mail/common/src/main/java/com/fsck/k9/mail/internet/MimeMessage.java b/mail/common/src/main/java/com/fsck/k9/mail/internet/MimeMessage.java index 640ab8924..b91c0316b 100644 --- a/mail/common/src/main/java/com/fsck/k9/mail/internet/MimeMessage.java +++ b/mail/common/src/main/java/com/fsck/k9/mail/internet/MimeMessage.java @@ -75,7 +75,19 @@ public class MimeMessage extends Message { return mimeMessage; } + /** + * Creates an instance that will check the header field syntax when adding headers. + */ + public static MimeMessage create() { + return new MimeMessage(true); + } + public MimeMessage() { + this(false); + } + + private MimeMessage(boolean checkHeaders) { + mHeader.setCheckHeaders(checkHeaders); } /**