Skip to content
Draft
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
30 changes: 22 additions & 8 deletions api/src/main/java/jakarta/mail/internet/InternetHeaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>Received</code> and
* <code>Return-Path</code> headers). If no existing header
* (or before in the case of <code>Received</code>,
* <code>Delivered-To</code> and <code>Return-Path</code> headers).
* If no existing header
* or placeholder for the header is found, new headers are
* added after the special placeholder with the name ":".
*
Expand Down Expand Up @@ -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 <code>Received</code> headers should be
* inserted in reverse order (newest before oldest), and that they
* addition, it knows that <code>Received</code> and
* <code>Delivered-To</code> (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 <code>Return-Path</code> header. <p>
*
Expand All @@ -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 {
Expand All @@ -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
*
Expand Down
121 changes: 121 additions & 0 deletions api/src/test/java/jakarta/mail/internet/InternetHeadersTest.java
Original file line number Diff line number Diff line change
@@ -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", "<author@com.example>");
h.addHeader("Delivered-To", "list@org.example");
assertEquals(List.of(
"Return-Path: <author@com.example>",
"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<String> lines(InternetHeaders h) {
List<String> result = new ArrayList<>();
Enumeration<Header> e = h.getAllHeaders();
while (e.hasMoreElements()) {
Header hdr = e.nextElement();
result.add(hdr.getName() + ": " + hdr.getValue());
}
return result;
}
}