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 3f642e4d4985be43c2e28d904978ce1436b9f687 Author: Benoit TELLIER <[email protected]> AuthorDate: Tue Nov 10 12:16:56 2026 +0100 [FIX] RawFieldParser#skipComment: a backslash before a line break escapes what follows it In a comment, a backslash escaped whatever byte came next, a bare CR or LF included. RawField#getBody deletes that line break (as MimeUtil#unfold), so the strict parsers read "(\" CR "(x)" as the comment "(\(x)", while skipComment let the backslash escape the CR, opened a nested comment at the "(" and never closed it: "; filename=a.exe" was swallowed. With one byte, any sender made a part's filename, parameters or Content-Language differ between the strict and the lenient parsers. As copyQuotedContent already does, a line break after a backslash in a comment is skipped and the quoted pair applies to the character after it. --- .../org/apache/james/mime4j/stream/RawFieldParser.java | 5 +++++ .../mime4j/field/BareLineBreakStrictLenientTest.java | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java b/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java index 9c72c6e9..2d0f689e 100644 --- a/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java +++ b/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java @@ -311,6 +311,11 @@ public class RawFieldParser { for (int i = indexFrom; i < indexTo; i++, pos++) { current = (char) (buf.byteAt(i) & 0xff); if (escaped) { + if (isLineBreak(current)) { + // Deleted, as by MimeUtil#unfold (a fold or a bare CR / LF): the quoted pair + // applies to the character after it, as in copyQuotedContent + continue; + } escaped = false; } else { if (current == '\\') { diff --git a/dom/src/test/java/org/apache/james/mime4j/field/BareLineBreakStrictLenientTest.java b/dom/src/test/java/org/apache/james/mime4j/field/BareLineBreakStrictLenientTest.java index b88cfb3c..8c798e07 100644 --- a/dom/src/test/java/org/apache/james/mime4j/field/BareLineBreakStrictLenientTest.java +++ b/dom/src/test/java/org/apache/james/mime4j/field/BareLineBreakStrictLenientTest.java @@ -117,4 +117,21 @@ public class BareLineBreakStrictLenientTest { Assert.assertEquals(expected, lenient.getFilename()); Assert.assertEquals(expected, strict.getFilename()); } + + @Test + public void bareCarriageReturnAfterABackslashInACommentShouldBeReadTheSameWayByBothParsers() throws Exception { + for (String lineBreak : new String[] {"\r", "\n", "\r\n"}) { + String header = "Content-Disposition: attachment (\\" + lineBreak + "(x); filename=a.exe"; + ContentDispositionField strict = (ContentDispositionField) DefaultFieldParser.parse(header); + ContentDispositionField lenient = (ContentDispositionField) LenientFieldParser.parse(header); + + Assert.assertEquals("a.exe", strict.getFilename()); + Assert.assertEquals(strict.getFilename(), lenient.getFilename()); + + String language = "Content-Language: (\\" + lineBreak + "(x) en"; + Assert.assertEquals( + ((ContentLanguageField) DefaultFieldParser.parse(language)).getLanguages(), + ((ContentLanguageField) LenientFieldParser.parse(language)).getLanguages()); + } + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
