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

chibenwa pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-mime4j.git

commit c984b2e1e3bc05874acf1ad8957b24edbecccbb0
Author: Benoit TELLIER <[email protected]>
AuthorDate: Tue Nov 10 12:14:31 2026 +0100

    [FIX] RawField#getBody: a fold right after the colon adds no leading space
    
    getBody skipped one whitespace after the colon only when it was the byte
    right after it. For "Subject:" CRLF SP "word" that byte is the CR, so
    nothing was skipped, unfolding deleted the line break and kept the SP, and
    the body read " word". MimeUtil#fold writes exactly that for a subject of
    one word of 68 to 77 characters, which EncoderUtil does not encode: a
    subject set by a JMAP user or a mailet came back with a leading space once
    the message was written and parsed again, and so did the received fields
    of the mailers that fold that way.
    
    The line break bytes right after the colon are now skipped before the one
    whitespace, so "Name:" CRLF SP "value" reads as "Name: value" does.
---
 .../java/org/apache/james/mime4j/stream/RawField.java  |  5 +++++
 .../org/apache/james/mime4j/stream/RawFieldTest.java   | 18 ++++++++++++++++++
 .../james/mime4j/message/DefaultMessageWriterTest.java | 15 +++++++++++++++
 3 files changed, 38 insertions(+)

diff --git a/core/src/main/java/org/apache/james/mime4j/stream/RawField.java 
b/core/src/main/java/org/apache/james/mime4j/stream/RawField.java
index a508f69f..f00fcd08 100644
--- a/core/src/main/java/org/apache/james/mime4j/stream/RawField.java
+++ b/core/src/main/java/org/apache/james/mime4j/stream/RawField.java
@@ -159,6 +159,11 @@ public final class RawField implements Field {
         if (raw != null) {
             int len = raw.length();
             int off = delimiterIdx + 1;
+            // A fold right after the colon ("Subject:" CRLF SP "word") 
separates the name from
+            // the body as a single whitespace does: its line break is deleted 
by unfolding anyway
+            while (len > off && (raw.byteAt(off) == '\r' || raw.byteAt(off) == 
'\n')) {
+                off++;
+            }
             if (len > off && (CharsetUtil.isWhitespace((char) (raw.byteAt(off) 
& 0xff)))) {
                 off++;
             }
diff --git 
a/core/src/test/java/org/apache/james/mime4j/stream/RawFieldTest.java 
b/core/src/test/java/org/apache/james/mime4j/stream/RawFieldTest.java
index 86a4dffb..1d15f778 100644
--- a/core/src/test/java/org/apache/james/mime4j/stream/RawFieldTest.java
+++ b/core/src/test/java/org/apache/james/mime4j/stream/RawFieldTest.java
@@ -177,4 +177,22 @@ public class RawFieldTest {
         assertThatCode(() -> new RawField("X-Custom_1.2!#$%&'*+/=?^`{|}~", 
"value")).doesNotThrowAnyException();
         assertThatCode(() -> new RawField(" Subject ", 
"value")).doesNotThrowAnyException();
     }
+
+    @Test
+    public void getBodyShouldSkipTheWhitespaceOfAFoldRightAfterTheColon() 
throws Exception {
+        RawField field = 
RawFieldParser.DEFAULT.parseField(ContentUtil.encode("Subject:\r\n abc"));
+        Assert.assertEquals("abc", field.getBody());
+
+        field = 
RawFieldParser.DEFAULT.parseField(ContentUtil.encode("Subject:\n\tabc"));
+        Assert.assertEquals("abc", field.getBody());
+    }
+
+    @Test
+    public void 
getBodyShouldKeepTheWhitespaceAfterTheOneThatSeparatesTheName() throws 
Exception {
+        RawField field = 
RawFieldParser.DEFAULT.parseField(ContentUtil.encode("Subject:\r\n  abc"));
+        Assert.assertEquals(" abc", field.getBody());
+
+        field = RawFieldParser.DEFAULT.parseField(ContentUtil.encode("Subject: 
 abc"));
+        Assert.assertEquals(" abc", field.getBody());
+    }
 }
diff --git 
a/dom/src/test/java/org/apache/james/mime4j/message/DefaultMessageWriterTest.java
 
b/dom/src/test/java/org/apache/james/mime4j/message/DefaultMessageWriterTest.java
index 1ae6731e..206653be 100644
--- 
a/dom/src/test/java/org/apache/james/mime4j/message/DefaultMessageWriterTest.java
+++ 
b/dom/src/test/java/org/apache/james/mime4j/message/DefaultMessageWriterTest.java
@@ -461,4 +461,19 @@ public class DefaultMessageWriterTest {
         assertOnePartKept("x\r\n--b=\r\n inline\r\nContent-Type: 
text/html\r\n\r\ny",
             "x\r\n--b inline\r\nContent-Type: text/html\r\n\r\ny");
     }
+
+    @Test
+    public void aOneWordSubjectShouldBeReadBackUnchanged() throws Exception {
+        for (int length = 60; length <= 80; length++) {
+            String subject = new String(new char[length]).replace('\0', 'a');
+            Message message = Message.Builder.of()
+                .setSubject(subject)
+                .setBody("x", StandardCharsets.UTF_8)
+                .build();
+
+            Message read = reparse(DefaultMessageWriter.asBytes(message));
+
+            assertThat(read.getSubject()).isEqualTo(subject);
+        }
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to