Skip to content

Commit cef7afa

Browse files
jbescoslukasj
authored andcommitted
Exception in Internet-headers when parsing malformed MIME headers #827
Signed-off-by: jbescos <jorge.bescos.gascon@oracle.com>
1 parent b90441f commit cef7afa

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

api/src/main/java/jakarta/mail/internet/InternetHeaders.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2025 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2026 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -660,8 +660,10 @@ public void addHeaderLine(String line) {
660660
try {
661661
char c = line.charAt(0);
662662
if (c == ' ' || c == '\t') {
663-
InternetHeader h = headers.get(headers.size() - 1);
664-
h.line += "\r\n" + line;
663+
if (!headers.isEmpty()) {
664+
InternetHeader h = headers.get(headers.size() - 1);
665+
h.line += "\r\n" + line;
666+
}
665667
} else
666668
headers.add(new InternetHeader(line));
667669
} catch (StringIndexOutOfBoundsException e) {

api/src/test/java/jakarta/mail/internet/MimeMessageTest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2025 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2025, 2026 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -19,6 +19,8 @@
1919

2020
import static org.junit.Assert.assertEquals;
2121

22+
import java.io.ByteArrayInputStream;
23+
import java.util.Base64;
2224
import java.util.Properties;
2325
import jakarta.mail.Session;
2426
import org.junit.Test;
@@ -83,4 +85,13 @@ public void testSystemPropertiesAndSessionWithNoProperties() throws Exception {
8385
System.clearProperty("mail.mime.allowutf8");
8486
}
8587
}
88+
89+
@Test
90+
public void testEmptyHeaders() throws Exception {
91+
String base64Input = "ICAgIAogICAKCg==";
92+
byte[] input = Base64.getDecoder().decode(base64Input);
93+
Session session = Session.getDefaultInstance(new Properties());
94+
ByteArrayInputStream is = new ByteArrayInputStream(input);
95+
new MimeMessage(session, is);
96+
}
8697
}

doc/release/CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ longer available.
2121

2222
CHANGES IN THE 2.2.0 RELEASE
2323
----------------------------
24+
E 827 Exception in Internet-headers when parsing malformed MIME headers
2425
E 714 Support minimum JDK of 11+ and minimum of Android API 33+
2526
E 720 finalize() in java.lang.Object has been deprecated
2627
E 744 Remove SecurityManager reference from API

0 commit comments

Comments
 (0)