This is an automated email from the ASF dual-hosted git repository.

coheigea pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/main by this push:
     new ac38635cde1 Strip line breaks from decoded Content-ID and Content-Type 
headers (#3750)
ac38635cde1 is described below

commit ac38635cde1fd04595f708955b757e5a0ddd41d2
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)
---
 .../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 cd0eb4c194f..c5e5837b45d 100644
--- a/core/src/main/java/org/apache/cxf/attachment/AttachmentSerializer.java
+++ b/core/src/main/java/org/apache/cxf/attachment/AttachmentSerializer.java
@@ -209,7 +209,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));
 
         // Content-Transfer-Encoding is deprecated (see please 
https://www.rfc-editor.org/rfc/rfc7578#page-6)
         if (!MULTIPART_FORM_DATA_TYPE.equalsIgnoreCase(multipartType)) {
@@ -229,9 +229,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:
@@ -249,9 +250,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;
     }

Reply via email to