jakartaee / jakartaee/mail-api

Headers order for InternetHeaders read from stream are not always correct

Open
#98 4 comments 0 reactions 1 assignee Claimed by @bshannon View on GitHub
Component: internet normal Priority: P5
Dominant language
Java
Stars
285
Forks
109
Avg merge
15h 19m
Merged PRs (30d)
1

Description

(Originally submitted to Sun bug system, transferred to Oracle bug system,
and finally here for resolution.)

A DESCRIPTION OF THE PROBLEM :
InternetHeaders created from stream do not initialize the internal array with
placeholders for default headers.

public InternetHeaders(InputStream is) throws MessagingException {
headers = new ArrayList(40);
load(is);
}

should be better written as:

public InternetHeaders(InputStream is) throws MessagingException {
this();
load(is);
}

This way adding further headers to an InternetHeaders created from stream
will keep the correct order.

Furthermore if the stream that we used to create the internet headers have a
Return-Path in the wrong position (not as first line) any further call to
addHeader/setHeader for Return-Path will put the header in the wrong place.

REPRODUCIBILITY :
This bug can be reproduced always.

CUSTOMER SUBMITTED WORKAROUND :
Override the methods in a custom InternetHeaders extension:

public class MyInternetHeaders extends InternetHeaders {

/**
* Constructor that takes an InputStream containing the contents
* of the set of mail headers.
*
* @param in the InputStream containing the header data
*
* @throws MessagingException if the super class cannot be properly instantiated
* based on the stream
*/
public MyInternetHeaders(InputStream in) throws MessagingException {
super();
load(in);
}

/**
* If the new header is a Return-Path we get sure that we add it to the top
* Javamail, at least until 1.4.0 does the wrong thing if it loaded a stream with
* a return-path in the middle.
*
* @see javax.mail.internet.InternetHeaders#addHeader(java.lang.String, java.lang.String)
*/
public void addHeader(String arg0, String arg1) {
if (RFC2822Headers.RETURN_PATH.equalsIgnoreCase(arg0)) {
headers.add(0, new InternetHeader(arg0, arg1));
} else {
super.addHeader(arg0, arg1);
}
}

/**
* If the new header is a Return-Path we get sure that we add it to the top
* Javamail, at least until 1.4.0 does the wrong thing if it loaded a stream with
* a return-path in the middle.
*
* @see javax.mail.internet.InternetHeaders#setHeader(java.lang.String, java.lang.String)
*/
public void setHeader(String arg0, String arg1) {
if (RFC2822Headers.RETURN_PATH.equalsIgnoreCase(arg0)) {
super.removeHeader(arg0);
}
super.setHeader(arg0, arg1);
}

....
#### Affected Versions
1.4.3
#### Environment
All
All

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.