diff --git a/api/src/main/java/jakarta/mail/internet/InternetHeaders.java b/api/src/main/java/jakarta/mail/internet/InternetHeaders.java index 4536f1e6..1640bb00 100644 --- a/api/src/main/java/jakarta/mail/internet/InternetHeaders.java +++ b/api/src/main/java/jakarta/mail/internet/InternetHeaders.java @@ -286,8 +286,9 @@ public Header nextElement() { * order of headers. Headers are never actually removed from * the list, they're converted into placeholder entries. * New headers are added after existing headers of the same name - * (or before in the case of Received and - * Return-Path headers). If no existing header + * (or before in the case of Received, + * Delivered-To and Return-Path headers). + * If no existing header * or placeholder for the header is found, new headers are * added after the special placeholder with the name ":". * @@ -566,8 +567,10 @@ public void setHeader(String name, String value) { * * The current implementation knows about the preferred order of most * well-known headers and will insert headers in that order. In - * addition, it knows that Received headers should be - * inserted in reverse order (newest before oldest), and that they + * addition, it knows that Received and + * Delivered-To (RFC 9228) headers are trace headers + * that should be inserted in reverse order (newest before oldest), + * above any existing trace header of either name, and that they * should appear at the beginning of the headers, preceeded only by * a possible Return-Path header.

* @@ -578,14 +581,14 @@ public void setHeader(String name, String value) { */ public void addHeader(String name, String value) { int pos = headers.size(); - boolean addReverse = - name.equalsIgnoreCase("Received") || - name.equalsIgnoreCase("Return-Path"); + boolean trace = isTraceHeader(name); + boolean addReverse = trace || name.equalsIgnoreCase("Return-Path"); if (addReverse) pos = 0; for (int i = headers.size() - 1; i >= 0; i--) { InternetHeader h = headers.get(i); - if (name.equalsIgnoreCase(h.getName())) { + if (name.equalsIgnoreCase(h.getName()) || + (trace && isTraceHeader(h.getName()))) { if (addReverse) { pos = i; } else { @@ -600,6 +603,17 @@ public void addHeader(String name, String value) { headers.add(pos, new InternetHeader(name, value)); } + /** + * Is this a trace header that is prepended to the current top of + * the trace headers? The relative order of these headers records + * the handling sequence of the message, so they must never be + * reordered with respect to each other (RFC 5321, RFC 9228). + */ + private static boolean isTraceHeader(String name) { + return name.equalsIgnoreCase("Received") || + name.equalsIgnoreCase("Delivered-To"); + } + /** * Remove all header entries that match the given name * diff --git a/api/src/test/java/jakarta/mail/internet/InternetHeadersTest.java b/api/src/test/java/jakarta/mail/internet/InternetHeadersTest.java new file mode 100644 index 00000000..229dffde --- /dev/null +++ b/api/src/test/java/jakarta/mail/internet/InternetHeadersTest.java @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2026 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package jakarta.mail.internet; + +import jakarta.mail.Header; +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +/** + * Test the order in which InternetHeaders adds trace headers. + */ +public class InternetHeadersTest { + + /** + * Delivered-To headers are placed at the top of the headers, + * newest first, like Received headers (RFC 9228, section 4). + */ + @Test + public void testDeliveredToAddedAtTop() throws Exception { + InternetHeaders h = new InternetHeaders(); + h.addHeader("Subject", "test"); + h.addHeader("Delivered-To", "first@example.com"); + h.addHeader("Delivered-To", "second@example.com"); + assertEquals(List.of( + "Delivered-To: second@example.com", + "Delivered-To: first@example.com", + "Subject: test"), lines(h)); + } + + /** + * Received and Delivered-To headers are never reordered with + * respect to each other; each new one goes on top of both + * (the example in RFC 9228, section 5). + */ + @Test + public void testTraceHeadersKeepHandlingSequence() throws Exception { + InternetHeaders h = new InternetHeaders(); + h.addHeader("From", "author@com.example"); + h.addHeader("Received", "by mail.com.example"); + h.addHeader("Received", "by submit.org.example"); + h.addHeader("Delivered-To", "list@org.example"); + h.addHeader("Received", "from mail.org.example by relay.edu.example"); + h.addHeader("Delivered-To", "Recipient-alumn@edu.example"); + assertEquals(List.of( + "Delivered-To: Recipient-alumn@edu.example", + "Received: from mail.org.example by relay.edu.example", + "Delivered-To: list@org.example", + "Received: by submit.org.example", + "Received: by mail.com.example", + "From: author@com.example"), lines(h)); + } + + /** + * A Return-Path header stays above the trace headers. + */ + @Test + public void testReturnPathStaysFirst() throws Exception { + InternetHeaders h = new InternetHeaders(); + h.addHeader("Received", "by mail.com.example"); + h.addHeader("Return-Path", ""); + h.addHeader("Delivered-To", "list@org.example"); + assertEquals(List.of( + "Return-Path: ", + "Delivered-To: list@org.example", + "Received: by mail.com.example"), lines(h)); + } + + /** + * Headers loaded from a message keep their order, and a new + * Delivered-To header goes above the existing trace headers. + */ + @Test + public void testDeliveredToOnLoadedHeaders() throws Exception { + String msg = + "Delivered-To: list@org.example\r\n" + + "Received: by submit.org.example\r\n" + + "From: author@com.example\r\n" + + "\r\n"; + InternetHeaders h = new InternetHeaders( + new ByteArrayInputStream(msg.getBytes(StandardCharsets.US_ASCII))); + h.addHeader("Received", "from mail.org.example by relay.edu.example"); + h.addHeader("Delivered-To", "Recipient-alumn@edu.example"); + assertEquals(List.of( + "Delivered-To: Recipient-alumn@edu.example", + "Received: from mail.org.example by relay.edu.example", + "Delivered-To: list@org.example", + "Received: by submit.org.example", + "From: author@com.example"), lines(h)); + } + + private static List lines(InternetHeaders h) { + List result = new ArrayList<>(); + Enumeration

e = h.getAllHeaders(); + while (e.hasMoreElements()) { + Header hdr = e.nextElement(); + result.add(hdr.getName() + ": " + hdr.getValue()); + } + return result; + } +}