Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/io/vertx/ext/mail/impl/SMTPSendMail.java
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ private Future<MailResult> sendMailData(boolean includeData) {

private Future<Void> sendMailHeaders(MultiMap headers) {
StringBuilder sb = new StringBuilder();
headers.forEach(header -> sb.append(header.getKey()).append(": ").append(header.getValue()).append("\r\n"));
headers.forEach(header -> sb.append(header.getKey()).append(": ").append(Utils.normalizeSmtpLineBreaks(header.getValue())).append("\r\n"));
final String headerLines = sb.toString();
return connection.writeLineWithDrain(headerLines, written.getAndAdd(headerLines.length()) < 1000);
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/io/vertx/ext/mail/impl/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,18 @@ public static <T> List<T> asList(T element) {
return Collections.singletonList(element);
}

/**
* Normalizes embedded header line breaks to CRLF so folded headers are sent
* on the wire with RFC-compliant separators.
*
* @param value the header value
* @return the normalized header value
*/
public static String normalizeSmtpLineBreaks(String value) {
if (value == null || value.indexOf('\n') == -1 && value.indexOf('\r') == -1) {
return value;
}
return value.replace("\r\n", "\n").replace('\r', '\n').replace("\n", "\r\n");
}

}
9 changes: 6 additions & 3 deletions src/main/java/io/vertx/ext/mail/impl/dkim/DKIMSigner.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package io.vertx.ext.mail.impl.dkim;

import static io.vertx.ext.mail.impl.Utils.normalizeSmtpLineBreaks;

import io.vertx.codegen.annotations.Nullable;
import io.vertx.core.*;
import io.vertx.core.buffer.Buffer;
Expand Down Expand Up @@ -414,7 +416,7 @@ private String copiedHeaders(List<String> headers, EncodedPart encodedMessage) {
return headers.stream().map(h -> {
String hValue = encodedMessage.headers().get(h);
if (hValue != null) {
return h + ":" + dkimQuotedPrintableCopiedHeader(hValue);
return h + ":" + dkimQuotedPrintableCopiedHeader(normalizeSmtpLineBreaks(hValue));
}
throw new RuntimeException("Unknown email header: " + h + " in copied headers.");
}).collect(Collectors.joining("|"));
Expand Down Expand Up @@ -444,11 +446,12 @@ private String dkimQuotedPrintableCopiedHeader(String value) {
* @return the canonicalization email header in format of 'Name':'Value'.
*/
public String canonicHeader(String emailHeaderName, String emailHeaderValue) {
String normalizedHeaderValue = normalizeSmtpLineBreaks(emailHeaderValue);
if (this.dkimSignOptions.getHeaderCanonAlgo() == CanonicalizationAlgorithm.SIMPLE) {
return emailHeaderName + ": " + emailHeaderValue;
return emailHeaderName + ": " + normalizedHeaderValue;
}
String headerName = emailHeaderName.trim().toLowerCase();
return headerName + ":" + canonicalLine(emailHeaderValue, this.dkimSignOptions.getHeaderCanonAlgo());
return headerName + ":" + canonicalLine(normalizedHeaderValue, this.dkimSignOptions.getHeaderCanonAlgo());
}

public String dkimMailBody(String mailBody) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2011-2026 The original author or authors
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* The Apache License v2.0 is available at
* http://www.opensource.org/licenses/apache2.0.php
*
* You may elect to redistribute this code under either of these licenses.
*/

package io.vertx.tests.mail.client;

import io.vertx.ext.mail.MailClient;
import io.vertx.ext.mail.MailMessage;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(VertxUnitRunner.class)
public class MailHeaderLineBreakTest extends SMTPTestWiser {

@Test
public void testFoldedSubjectUsesCrLf(TestContext testContext) {
this.testContext = testContext;
MailClient mailClient = mailClientLogin();

MailMessage message = exampleMessage().setSubject("Unicode folding test ⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐");
testSuccess(mailClient, message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public void testDKIMHeaderTemplate() {
public void testSimpleHeader() {
DKIMSignOptions dkimOps = dkimOps().setHeaderCanonAlgo(CanonicalizationAlgorithm.SIMPLE);
DKIMSigner signer = new DKIMSigner(dkimOps, null);
// there will be possible to have \n in the header value, keep it as it is.
// folded headers are normalized to CRLF before signing.
String name = "from";
String value = "user@example.com";
String canonicHeaderLine = signer.canonicHeader(name, value);
Expand All @@ -191,14 +191,14 @@ public void testSimpleHeader() {
name = " from ";
value = " user@example.com \n ";
canonicHeaderLine = signer.canonicHeader(name, value);
assertEquals(" from : user@example.com \n ", canonicHeaderLine);
assertEquals(" from : user@example.com \r\n ", canonicHeaderLine);
}

@Test
public void testRelaxedHeader() {
DKIMSignOptions dkimOps = dkimOps().setHeaderCanonAlgo(CanonicalizationAlgorithm.RELAXED);
DKIMSigner signer = new DKIMSigner(dkimOps, null);
// there will be possible to have \n in the header value
// folded headers are normalized to CRLF before relaxed canonicalization.
String name = "From";
String value = "user@example.com";
String canonicHeaderLine = signer.canonicHeader(name, value);
Expand Down