Only pass encoded headers to MimeHeader

Remove code to encode header values from from MimeHeader.writeTo()
This commit is contained in:
cketti 2020-10-10 23:05:53 +02:00
parent 85b8cb96a3
commit fac346009a
5 changed files with 35 additions and 17 deletions

View file

@ -26,6 +26,7 @@ import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.internet.MessageIdGenerator; import com.fsck.k9.mail.internet.MessageIdGenerator;
import com.fsck.k9.mail.internet.MimeBodyPart; import com.fsck.k9.mail.internet.MimeBodyPart;
import com.fsck.k9.mail.internet.MimeHeader; import com.fsck.k9.mail.internet.MimeHeader;
import com.fsck.k9.mail.internet.MimeHeaderEncoder;
import com.fsck.k9.mail.internet.MimeMessage; import com.fsck.k9.mail.internet.MimeMessage;
import com.fsck.k9.mail.internet.MimeMessageHelper; import com.fsck.k9.mail.internet.MimeMessageHelper;
import com.fsck.k9.mail.internet.MimeMultipart; import com.fsck.k9.mail.internet.MimeMultipart;
@ -108,7 +109,8 @@ public abstract class MessageBuilder {
} }
if (!K9.isHideUserAgent()) { if (!K9.isHideUserAgent()) {
message.setHeader("User-Agent", resourceProvider.userAgent()); String encodedUserAgent = MimeHeaderEncoder.encode("User-Agent", resourceProvider.userAgent());
message.setHeader("User-Agent", encodedUserAgent);
} }
final String replyTo = identity.getReplyTo(); final String replyTo = identity.getReplyTo();

View file

@ -27,6 +27,7 @@ import com.fsck.k9.mail.Message.RecipientType;
import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.filter.EOLConvertingOutputStream; import com.fsck.k9.mail.filter.EOLConvertingOutputStream;
import com.fsck.k9.mail.internet.BinaryTempFileBody; import com.fsck.k9.mail.internet.BinaryTempFileBody;
import com.fsck.k9.mail.internet.Headers;
import com.fsck.k9.mail.internet.MessageIdGenerator; import com.fsck.k9.mail.internet.MessageIdGenerator;
import com.fsck.k9.mail.internet.MimeBodyPart; import com.fsck.k9.mail.internet.MimeBodyPart;
import com.fsck.k9.mail.internet.MimeHeader; import com.fsck.k9.mail.internet.MimeHeader;
@ -219,7 +220,7 @@ public class PgpMessageBuilder extends MessageBuilder {
messageContentBodyPart.setHeader(MimeHeader.HEADER_CONTENT_TYPE, messageContentBodyPart.setHeader(MimeHeader.HEADER_CONTENT_TYPE,
messageContentBodyPart.getContentType() + "; protected-headers=\"v1\""); messageContentBodyPart.getContentType() + "; protected-headers=\"v1\"");
messageContentBodyPart.setHeader(MimeHeader.SUBJECT, subjects[0]); messageContentBodyPart.setHeader(MimeHeader.SUBJECT, subjects[0]);
currentProcessedMimeMessage.setHeader(MimeHeader.SUBJECT, resourceProvider.encryptedSubject()); currentProcessedMimeMessage.setSubject(resourceProvider.encryptedSubject());
} }
} }

View file

@ -76,21 +76,9 @@ class MimeHeader {
} }
private fun Appendable.appendNameValueField(field: Field) { private fun Appendable.appendNameValueField(field: Field) {
val value = field.value
val encodedValue = if (hasToBeEncoded(value)) {
EncoderUtil.encodeEncodedWord(value)
} else {
value
}
append(field.name) append(field.name)
append(": ") append(": ")
append(encodedValue) append(field.value)
}
// encode non printable characters except LF/CR/TAB codes.
private fun hasToBeEncoded(text: String): Boolean {
return text.any { !it.isVChar() && !it.isWspOrCrlf() }
} }
companion object { companion object {

View file

@ -0,0 +1,26 @@
package com.fsck.k9.mail.internet
object MimeHeaderEncoder {
@JvmStatic
fun encode(name: String, value: String): String {
// TODO: Fold long text that provides enough opportunities for folding and doesn't contain any characters that
// need to be encoded.
return if (hasToBeEncoded(name, value)) {
EncoderUtil.encodeEncodedWord(value)
} else {
value
}
}
private fun hasToBeEncoded(name: String, value: String): Boolean {
return exceedsRecommendedLineLength(name, value) || charactersNeedEncoding(value)
}
private fun exceedsRecommendedLineLength(name: String, value: String): Boolean {
return name.length + 2 /* colon + space */ + value.length > RECOMMENDED_MAX_LINE_LENGTH
}
private fun charactersNeedEncoding(text: String): Boolean {
return text.any { !it.isVChar() && !it.isWspOrCrlf() }
}
}

View file

@ -261,12 +261,13 @@ public class MimeMessage extends Message {
*/ */
@Override @Override
public String getSubject() { public String getSubject() {
return MimeUtility.unfoldAndDecode(getFirstHeader("Subject"), this); return MimeUtility.unfoldAndDecode(getFirstHeader(MimeHeader.SUBJECT), this);
} }
@Override @Override
public void setSubject(String subject) { public void setSubject(String subject) {
setHeader("Subject", subject); String encodedSubject = MimeHeaderEncoder.encode(MimeHeader.SUBJECT, subject);
setHeader(MimeHeader.SUBJECT, encodedSubject);
} }
@Override @Override