You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by rz...@apache.org on 2026/09/20 19:26:52 UTC
(geronimo-mail) 02/04: GERONIMO-6908, GERONIMO-6909 - InternetHeaders rejects legally repeated headers
This is an automated email from the ASF dual-hosted git repository.
rzo1 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/geronimo-mail.git
commit 7b37292e45ed57ecc7db83ddedaf88db1b412c9d
Author: Richard Zowalla <rz...@apache.org>
AuthorDate: Sun Sep 20 21:22:44 2026 +0200
GERONIMO-6908, GERONIMO-6909 - InternetHeaders rejects legally repeated headers
The single-value policy added in GERONIMO-6870 stops us from emitting a
message with duplicated headers, which some servers reject. It was applied
too widely in two ways.
GERONIMO-6908: it also ran while parsing, through load() -> addHeaderLine(),
so an ordinary inbound message could not be read at all. Any Gmail-delivered
mail carries two Authentication-Results headers and threw an unchecked
IllegalStateException out of the MimeMessage constructor. Parsing now never
rejects: a loading flag set for the duration of load() suppresses the check.
GERONIMO-6909: it treated every header except Received and Return-Path as
single-valued, so addHeader refused legal repetitions such as two X- headers
or two Comments headers. The policy now covers only the fields the specs
restrict to one occurrence - RFC 5322 section 3.6 and the RFC 2045 entity
headers plus Content-Disposition - and matches case-insensitively. Received
and Return-Path need no special case any more, being absent from that set.
Both changes are covered by new tests in InternetHeadersTest.
---
.../jakarta/mail/internet/InternetHeaders.java | 71 ++++++++++++++++----
.../jakarta/mail/internet/InternetHeadersTest.java | 78 +++++++++++++++++++++-
2 files changed, 133 insertions(+), 16 deletions(-)
diff --git a/geronimo-mail_2.1_spec/src/main/java/jakarta/mail/internet/InternetHeaders.java b/geronimo-mail_2.1_spec/src/main/java/jakarta/mail/internet/InternetHeaders.java
index c2a9f8c..5755a06 100644
--- a/geronimo-mail_2.1_spec/src/main/java/jakarta/mail/internet/InternetHeaders.java
+++ b/geronimo-mail_2.1_spec/src/main/java/jakarta/mail/internet/InternetHeaders.java
@@ -26,7 +26,10 @@ import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
+import java.util.HashSet;
import java.util.List;
+import java.util.Locale;
+import java.util.Set;
import jakarta.mail.Address;
import jakarta.mail.Header;
@@ -38,25 +41,59 @@ import jakarta.mail.MessagingException;
* @version $Rev$ $Date$
*/
public class InternetHeaders {
- // the list of headers (to preserve order);
- // From RFC822, outside Received and Return-Path, there should be no duplicate header otherwise, it's probably a
- // bug on our side. CC and BCC could theoretically be present multiple times, even though it's more common to
- // have one with multiple address similar to.
+ // Headers the specifications allow to occur at most once per message or entity. Emitting two of any
+ // of these produces a message some servers - Gmail among them - reject, so we refuse to build one
+ // (GERONIMO-6870). Every other header may legitimately repeat and is simply appended (GERONIMO-6909):
+ // Comments and Keywords are "unlimited" per RFC 5322 section 3.6.5, Received and Return-Path per
+ // section 3.6.7, the Resent-* set occurs once per resending per section 3.6.6, and List-* (RFC 2369),
+ // Authentication-Results (RFC 8601), the ARC-* set (RFC 8617), DKIM-Signature (RFC 6376) and
+ // application-defined X- headers all repeat in practice.
+ private static final Set<String> SINGLE_VALUE_HEADERS;
+ static {
+ final Set<String> singletons = new HashSet<String>();
+ // RFC 5322 section 3.6
+ singletons.add("date");
+ singletons.add("from");
+ singletons.add("sender");
+ singletons.add("reply-to");
+ singletons.add("to");
+ singletons.add("cc");
+ singletons.add("bcc");
+ singletons.add("message-id");
+ singletons.add("in-reply-to");
+ singletons.add("references");
+ singletons.add("subject");
+ // RFC 2045 entity headers, plus Content-Disposition from RFC 2183
+ singletons.add("mime-version");
+ singletons.add("content-type");
+ singletons.add("content-transfer-encoding");
+ singletons.add("content-id");
+ singletons.add("content-description");
+ singletons.add("content-disposition");
+ SINGLE_VALUE_HEADERS = Collections.unmodifiableSet(singletons);
+ }
+
+ // the list of headers (to preserve order)
protected List<InternetHeader> headers = new ArrayList<InternetHeader>() {
@Override
public boolean add(final InternetHeader o) {
- if ("Received".equals(o.getName()) || "Return-Path".equals(o.getName())) {
- return super.add(o);
- }
assertNoDuplicates(o);
return super.add(o);
}
private void assertNoDuplicates(final InternetHeader o) {
+ // headers read from a stream are never rejected - see the loading flag below
+ if (loading) {
+ return;
+ }
+ final String name = o.getName();
+ if (name == null || !SINGLE_VALUE_HEADERS.contains(name.toLowerCase(Locale.ENGLISH))) {
+ return;
+ }
for (InternetHeader header : this) {
- if (header.getName() != null && header.getName().equalsIgnoreCase(o.getName())) {
+ if (header.getName() != null && header.getName().equalsIgnoreCase(name)) {
if (header.getValue() != null && !header.getValue().isEmpty()) {
- throw new IllegalStateException("InternetHeaders cannot contain more than one value for header: " + o.getName());
+ throw new IllegalStateException("InternetHeaders cannot contain more than one value for header: " + name);
}
break;
}
@@ -65,15 +102,19 @@ public class InternetHeaders {
@Override
public void add(final int index, final InternetHeader o) {
- if ("Received".equals(o.getName()) || "Return-Path".equals(o.getName())) {
- super.add(o);
- return;
- }
assertNoDuplicates(o);
super.add(index, o);
}
};
+ // Set while headers are being parsed from a stream. The single-value policy above exists to stop us
+ // from *emitting* a message with duplicated singleton headers; it must not be applied to messages we
+ // read, because repeated headers are legal and common in the wild - Authentication-Results (RFC 8601),
+ // the ARC-* set (RFC 8617), DKIM-Signature, Comments, Resent-* and List-* all occur more than once on
+ // ordinary mail, and InternetHeaders.getHeader(String) returns a String[] precisely because of that.
+ // See GERONIMO-6908.
+ private boolean loading;
+
/**
* Create an empty InternetHeaders
*/
@@ -154,6 +195,8 @@ public class InternetHeaders {
* @since JavaMail 1.6
*/
public void load(InputStream is, boolean allowUtf8) throws MessagingException {
+ final boolean wasLoading = loading;
+ loading = true;
try {
final StringBuffer buffer = new StringBuffer(128);
String line;
@@ -189,6 +232,8 @@ public class InternetHeaders {
}
} catch (final IOException e) {
throw new MessagingException("Error loading headers", e);
+ } finally {
+ loading = wasLoading;
}
}
diff --git a/geronimo-mail_2.1_spec/src/test/java/jakarta/mail/internet/InternetHeadersTest.java b/geronimo-mail_2.1_spec/src/test/java/jakarta/mail/internet/InternetHeadersTest.java
index 653bee6..58092e9 100644
--- a/geronimo-mail_2.1_spec/src/test/java/jakarta/mail/internet/InternetHeadersTest.java
+++ b/geronimo-mail_2.1_spec/src/test/java/jakarta/mail/internet/InternetHeadersTest.java
@@ -25,6 +25,7 @@ import jakarta.mail.MessagingException;
import org.junit.jupiter.api.BeforeEach;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;
@@ -71,12 +72,83 @@ public class InternetHeadersTest {
@Test
public void testActuallyDuplicatedHeader() throws MessagingException {
- headers.addHeader("Other", "first");
+ headers.addHeader("Subject", "first");
+ try {
+ headers.addHeader("Subject", "second");
+ fail("No exception thrown");
+ } catch (IllegalStateException e) {
+ assertEquals("InternetHeaders cannot contain more than one value for header: Subject", e.getMessage());
+ }
+ }
+
+ /**
+ * The single-value policy covers the fields RFC 5322 section 3.6 and RFC 2045 restrict to one
+ * occurrence, and it is case-insensitive. See GERONIMO-6909.
+ */
+ @Test
+ public void testDuplicateSingletonHeaderIsRejectedRegardlessOfCase() throws MessagingException {
+ headers.addHeader("Content-Type", "text/plain");
+ try {
+ headers.addHeader("content-type", "text/html");
+ fail("No exception thrown");
+ } catch (IllegalStateException e) {
+ assertEquals("InternetHeaders cannot contain more than one value for header: content-type", e.getMessage());
+ }
+ }
+
+ /**
+ * Headers that are not singletons may be added more than once - RFC 5322 section 3.6.5 marks
+ * Comments and Keywords as unlimited, and application-defined headers are unconstrained.
+ * See GERONIMO-6909.
+ */
+ @Test
+ public void testRepeatableHeadersMayBeAddedMoreThanOnce() throws MessagingException {
+ headers.addHeader("X-Trace-Id", "abc");
+ headers.addHeader("X-Trace-Id", "def");
+ headers.addHeader("Comments", "first remark");
+ headers.addHeader("Comments", "second remark");
+
+ assertArrayEquals(new String[]{"abc", "def"}, headers.getHeader("X-Trace-Id"));
+ assertArrayEquals(new String[]{"first remark", "second remark"}, headers.getHeader("Comments"));
+ }
+
+ /**
+ * The single-value policy guards what we emit; it must never reject a message we are reading.
+ * Repeated headers such as Authentication-Results (RFC 8601), the ARC-* set (RFC 8617) and
+ * DKIM-Signature (RFC 6376) are legal and routine on inbound mail. See GERONIMO-6908.
+ */
+ @Test
+ public void testLoadKeepsLegallyRepeatedHeaders() throws MessagingException {
+ final String stream =
+ "Authentication-Results: mx.google.com; dkim=pass\r\n" +
+ "Authentication-Results: mx.example.com; spf=pass\r\n" +
+ "ARC-Seal: i=1; a=rsa-sha256\r\n" +
+ "ARC-Seal: i=2; a=rsa-sha256\r\n" +
+ "Subject: a subject\r\n" +
+ "\r\n";
+ headers.load(new ByteArrayInputStream(stream.getBytes()));
+
+ assertArrayEquals(new String[]{"mx.google.com; dkim=pass", "mx.example.com; spf=pass"},
+ headers.getHeader("Authentication-Results"));
+ assertArrayEquals(new String[]{"i=1; a=rsa-sha256", "i=2; a=rsa-sha256"},
+ headers.getHeader("ARC-Seal"));
+ assertArrayEquals(new String[]{"a subject"}, headers.getHeader("Subject"));
+ }
+
+ /**
+ * Loading must not leave the single-value policy switched off for headers added afterwards.
+ */
+ @Test
+ public void testPolicyStillAppliesAfterLoad() throws MessagingException {
+ final String stream = "Authentication-Results: a\r\nAuthentication-Results: b\r\n\r\n";
+ headers.load(new ByteArrayInputStream(stream.getBytes()));
+
+ headers.addHeader("Subject", "first");
try {
- headers.addHeader("Other", "second");
+ headers.addHeader("Subject", "second");
fail("No exception thrown");
} catch (IllegalStateException e) {
- assertEquals("InternetHeaders cannot contain more than one value for header: Other", e.getMessage());
+ assertEquals("InternetHeaders cannot contain more than one value for header: Subject", e.getMessage());
}
}
}