This is an automated email from the ASF dual-hosted git repository.
coheigea pushed a commit to branch 4.1.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git
The following commit(s) were added to refs/heads/4.1.x-fixes by this push:
new 8042005cbc1 Strip line breaks from decoded Content-ID and Content-Type
headers (#3750)
8042005cbc1 is described below
commit 8042005cbc10ae26cd326224fdb47b7e7fd1de87
Author: Colm O hEigeartaigh <[email protected]>
AuthorDate: Fri Nov 20 12:26:48 2026 +0000
Strip line breaks from decoded Content-ID and Content-Type headers (#3750)
(cherry picked from commit ac38635cde1fd04595f708955b757e5a0ddd41d2)
---
.../cxf/attachment/AttachmentSerializer.java | 11 ++--
.../cxf/attachment/AttachmentSerializerTest.java | 69 ++++++++++++++++++++++
2 files changed, 75 insertions(+), 5 deletions(-)
diff --git
a/core/src/main/java/org/apache/cxf/attachment/AttachmentSerializer.java
b/core/src/main/java/org/apache/cxf/attachment/AttachmentSerializer.java
index 59e87c2bf98..41513059b97 100644
--- a/core/src/main/java/org/apache/cxf/attachment/AttachmentSerializer.java
+++ b/core/src/main/java/org/apache/cxf/attachment/AttachmentSerializer.java
@@ -207,7 +207,7 @@ public class AttachmentSerializer {
private void writeHeaders(String contentType, String attachmentId,
Map<String, List<String>> headers, Writer
writer) throws IOException {
writer.write("\r\nContent-Type: ");
- writer.write(contentType);
+ writer.write(stripLineBreaks(contentType));
writer.write("\r\nContent-Transfer-Encoding: " +
contentTransferEncoding + "\r\n");
if (attachmentId != null) {
@@ -221,9 +221,10 @@ public class AttachmentSerializer {
// character to their equivalent US-ASCII characters, and
enclosing the
// remaining parts with an angle bracket pair, "<" and ">".
//
+ // Line breaks are stripped after decoding, as %0D%0A would
otherwise decode into a raw CR/LF
if (attachmentId.startsWith("cid:")) {
- writer.write(decode(attachmentId.substring(4),
- StandardCharsets.UTF_8));
+ writer.write(stripLineBreaks(decode(attachmentId.substring(4),
+ StandardCharsets.UTF_8)));
} else {
//
// RFC-2392 (https://datatracker.ietf.org/doc/html/rfc2392)
says:
@@ -241,9 +242,9 @@ public class AttachmentSerializer {
final String decoded = tryDecode(address[1],
StandardCharsets.UTF_8);
// If the domain part is encoded, decode it
if (!decoded.equalsIgnoreCase(address[1])) {
- writer.write(address[0] + "@" + decoded);
+ writer.write(stripLineBreaks(address[0] + "@" +
decoded));
} else {
- writer.write(attachmentId);
+ writer.write(stripLineBreaks(attachmentId));
}
} else {
writer.write(URLEncoder.encode(attachmentId,
StandardCharsets.UTF_8.name()));
diff --git
a/core/src/test/java/org/apache/cxf/attachment/AttachmentSerializerTest.java
b/core/src/test/java/org/apache/cxf/attachment/AttachmentSerializerTest.java
index 166e085cfac..85084cd96ae 100644
--- a/core/src/test/java/org/apache/cxf/attachment/AttachmentSerializerTest.java
+++ b/core/src/test/java/org/apache/cxf/attachment/AttachmentSerializerTest.java
@@ -41,6 +41,7 @@ import org.apache.cxf.message.MessageImpl;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
public class AttachmentSerializerTest {
@@ -305,6 +306,74 @@ public class AttachmentSerializerTest {
assertEquals(null, part.getHeader("X-Injected"));
}
+ @Test
+ public void testDecodedCidLineBreaksAreStripped() throws Exception {
+ // %0D%0A only becomes a line break when the cid: URL is decoded
+ AttachmentImpl a =
newAttachment("cid:a%0D%0AX-Injected:%[email protected]",
+ "application/octet-stream");
+ MimeBodyPart part = writeAndParseAttachment(a);
+ assertEquals("<aX-Injected: [email protected]>",
part.getHeader("Content-ID")[0]);
+ assertNull(part.getHeader("X-Injected"));
+ }
+
+ @Test
+ public void testDecodedContentIdDomainLineBreaksAreStripped() throws
Exception {
+ AttachmentImpl a =
newAttachment("[email protected]%0D%0AX-Injected:%20yes",
+ "application/octet-stream");
+ MimeBodyPart part = writeAndParseAttachment(a);
+ assertEquals("<[email protected]: yes>",
part.getHeader("Content-ID")[0]);
+ assertNull(part.getHeader("X-Injected"));
+ }
+
+ @Test
+ public void testRawContentIdLineBreaksAreStripped() throws Exception {
+ AttachmentImpl a = newAttachment("[email protected]\r\nX-Injected:
yes",
+ "application/octet-stream");
+ MimeBodyPart part = writeAndParseAttachment(a);
+ assertNull(part.getHeader("X-Injected"));
+ }
+
+ @Test
+ public void testContentTypeLineBreaksAreStripped() throws Exception {
+ AttachmentImpl a = newAttachment("test.xml",
"application/octet-stream\r\nX-Injected: yes");
+ MimeBodyPart part = writeAndParseAttachment(a);
+ assertNull(part.getHeader("X-Injected"));
+ }
+
+ private AttachmentImpl newAttachment(String contentId, String contentType)
throws Exception {
+ AttachmentImpl a = new AttachmentImpl(contentId);
+ InputStream is = getClass().getResourceAsStream("my.wav");
+ a.setDataHandler(new DataHandler(new ByteArrayDataSource(is,
contentType)));
+ return a;
+ }
+
+ private static MimeBodyPart writeAndParseAttachment(AttachmentImpl a)
throws Exception {
+ MessageImpl msg = new MessageImpl();
+ Collection<Attachment> atts = new ArrayList<>();
+ atts.add(a);
+ msg.setAttachments(atts);
+ msg.put(Message.CONTENT_TYPE, "application/soap+xml");
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ msg.setContent(OutputStream.class, out);
+
+ AttachmentSerializer serializer = new AttachmentSerializer(msg);
+ serializer.writeProlog();
+ out.write("<soap:Body/>".getBytes());
+ serializer.writeAttachments();
+ out.flush();
+
+ String ct = (String) msg.get(Message.CONTENT_TYPE);
+ DataSource source = new ByteArrayDataSource(new
ByteArrayInputStream(out.toByteArray()), ct);
+ MimeMultipart mpart = new MimeMultipart(source);
+ Session session = Session.getDefaultInstance(new Properties());
+ MimeMessage inMsg = new MimeMessage(session);
+ inMsg.setContent(mpart);
+ inMsg.addHeaderLine("Content-Type: " + ct);
+ MimeMultipart multipart = (MimeMultipart) inMsg.getContent();
+ return (MimeBodyPart) multipart.getBodyPart(1);
+ }
+
private static String escapeQuotes(String s) {
return s.indexOf('"') != 0 ? s.replace("\"", "\\\"") : s;
}