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 ccbf177e532edf722f133526b8f6b227b6d5aa18 Author: Benoit TELLIER <[email protected]> AuthorDate: Tue Nov 10 11:50:58 2026 +0100 [FIX] QuotedPrintableOutputStream: a dash never starts an output line The encoder escaped only "=" and "." among the printable characters, and wrote the byte that follows its own soft break as is. So a "-" could start an output line, after a line break of the content or after a soft break, and the encoded body could hold "--<boundary>". DefaultMessageWriter writes the parts of a multipart between delimiters of the original boundary: a quoted-printable part whose text had "--b" at column 76, or whose decoded text held a delimiter line ("=2D-b", or a soft break inside "--b=" CRLF " inline"), was written with that delimiter, and the re-parsed message had parts the first parse never saw, with headers the sender chose. Any sender could trigger it, and James re-serialises the DOM in many places. A "-" that would be the first byte of an output line, including the line opened by a soft break, is now written as "=2D" (RFC 2045 6.7 rule 1), so the quoted-printable output never holds a delimiter, whatever the boundary (RFC 2046 5.1.1). The expected outputs of six example messages change accordingly: they decode to the same content. --- .../mime4j/codec/QuotedPrintableOutputStream.java | 20 ++++++++++++ .../codec/QuotedPrintableOutputStreamTest.java | 36 +++++++++++++++++++++ .../test/resources/mimetools-testmsgs/ak-0696.out | 2 +- .../mimetools-testmsgs/empty-preamble.out | 2 +- .../resources/mimetools-testmsgs/german-qp.out | 6 ++-- .../test/resources/mimetools-testmsgs/german.out | 2 +- .../test/resources/mimetools-testmsgs/jt-0498.out | 2 +- .../resources/mimetools-testmsgs/mp-msg-rfc822.out | 2 +- .../mime4j/message/DefaultMessageWriterTest.java | 37 ++++++++++++++++++++++ 9 files changed, 101 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/org/apache/james/mime4j/codec/QuotedPrintableOutputStream.java b/core/src/main/java/org/apache/james/mime4j/codec/QuotedPrintableOutputStream.java index e07f3e3e..89bfbc7b 100644 --- a/core/src/main/java/org/apache/james/mime4j/codec/QuotedPrintableOutputStream.java +++ b/core/src/main/java/org/apache/james/mime4j/codec/QuotedPrintableOutputStream.java @@ -30,6 +30,10 @@ import java.io.OutputStream; * by default because this is a workaround for some "filter"/"antivirus" * "old mua" having issues with dots at the beginning or the end of a * qp encode line (maybe a bad dot-destuffing algo). + * + * A dash "-" that would start an output line is encoded too, so that the + * encoded data never holds a multipart delimiter, whatever the boundary + * (RFC 2046 section 5.1.1). */ public class QuotedPrintableOutputStream extends FilterOutputStream { @@ -39,6 +43,7 @@ public class QuotedPrintableOutputStream extends FilterOutputStream { private static final byte SP = 0x20; private static final byte EQ = 0x3D; private static final byte DOT = 0x2E; + private static final byte DASH = 0x2D; private static final byte CR = 0x0D; private static final byte LF = 0x0A; private static final byte QUOTED_PRINTABLE_LAST_PLAIN = 0x7E; @@ -55,6 +60,8 @@ public class QuotedPrintableOutputStream extends FilterOutputStream { private boolean pendingCR; private int nextSoftBreak; private int outputIndex; + // nothing written yet on the current output line + private boolean lineStart = true; private boolean closed = false; @@ -168,10 +175,21 @@ public class QuotedPrintableOutputStream extends FilterOutputStream { } private void plain(byte next) throws IOException { + // A line starting with "--" may be read as a multipart delimiter (RFC 2046 5.1.1): a dash + // never starts an output line, whether the line follows a line break or a soft break + if (next == DASH && lineStart) { + escape(next); + return; + } if (--nextSoftBreak <= 1) { softBreak(); + if (next == DASH) { + escape(next); + return; + } } write(next); + lineStart = false; } private void escape(byte next) throws IOException { @@ -186,6 +204,7 @@ public class QuotedPrintableOutputStream extends FilterOutputStream { write(HEX_DIGITS[nextUnsigned >> 4]); --nextSoftBreak; write(HEX_DIGITS[nextUnsigned % 0x10]); + lineStart = false; } private void write(byte next) throws IOException { @@ -204,6 +223,7 @@ public class QuotedPrintableOutputStream extends FilterOutputStream { write(CR); write(LF); nextSoftBreak = QUOTED_PRINTABLE_MAX_LINE_LENGTH; + lineStart = true; } void flushOutput() throws IOException { diff --git a/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableOutputStreamTest.java b/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableOutputStreamTest.java index 77a8ebe3..52a9cfb8 100644 --- a/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableOutputStreamTest.java +++ b/core/src/test/java/org/apache/james/mime4j/codec/QuotedPrintableOutputStreamTest.java @@ -112,4 +112,40 @@ public class QuotedPrintableOutputStreamTest { return null; } } + + @Test + public void encodeShouldNeverStartALineWithADash() throws Exception { + String a75 = new String(new char[75]).replace('\0', 'a'); + String[] inputs = {"--b\r\n" + a75 + "--b", "x\r\n--b\r\n-", a75 + "a-b", "-"}; + for (boolean binary : new boolean[] {false, true}) { + for (String input : inputs) { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + QuotedPrintableOutputStream qp = new QuotedPrintableOutputStream(out, binary); + qp.write(input.getBytes("US-ASCII")); + qp.close(); + String encoded = out.toString("US-ASCII"); + for (String line : encoded.split("\r\n")) { + Assert.assertFalse(encoded, line.startsWith("-")); + Assert.assertTrue(encoded, line.length() <= 76); + } + ByteArrayOutputStream decoded = new ByteArrayOutputStream(); + QuotedPrintableInputStream in = new QuotedPrintableInputStream( + new java.io.ByteArrayInputStream(out.toByteArray())); + int b; + while ((b = in.read()) != -1) { + decoded.write(b); + } + Assert.assertEquals(input, decoded.toString("US-ASCII")); + } + } + } + + @Test + public void encodeShouldKeepADashInsideALine() throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + QuotedPrintableOutputStream qp = new QuotedPrintableOutputStream(out, false); + qp.write("a--b\r\n-x".getBytes("US-ASCII")); + qp.close(); + Assert.assertEquals("a--b\r\n=2Dx", out.toString("US-ASCII")); + } } diff --git a/core/src/test/resources/mimetools-testmsgs/ak-0696.out b/core/src/test/resources/mimetools-testmsgs/ak-0696.out index b422e49a..c988f49a 100644 --- a/core/src/test/resources/mimetools-testmsgs/ak-0696.out +++ b/core/src/test/resources/mimetools-testmsgs/ak-0696.out @@ -99,7 +99,7 @@ Dipl=2E-Ing=2E Guenter D E U S C H L E Deutsche Telekom AG Niederlassung 3 Hannover GrossKundenManagement - Techn=2E Vertriebs-Support: Team-Leiter Internet Online-Dienste ---------------------------------------------------- +=2D-------------------------------------------------- GrKM-TVS-IOD Tel: +49-511-333-2772 Vahrenwalder-Str=2E 245 FAX: +49-511-333-2751 30179 Hannover eMail: deuschle@mnh=2Etelekom=2Ede=20 diff --git a/core/src/test/resources/mimetools-testmsgs/empty-preamble.out b/core/src/test/resources/mimetools-testmsgs/empty-preamble.out index d218cf60..cc46bc8c 100644 --- a/core/src/test/resources/mimetools-testmsgs/empty-preamble.out +++ b/core/src/test/resources/mimetools-testmsgs/empty-preamble.out @@ -8,7 +8,7 @@ Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Das ist ein Test=2E ---=20 +=2D-=20 sub i($){print$_[0]}*j=3D*ENV;sub w($){sleep$_[0]}sub _($){i"$p:$c> ",w+01 ,$_=3D$_[0],tr;i-za-h,;a-hi-z ;,i$_,w+01,i"\n"}$|=3D1;$f=3D'HO';($c=3D$j{P= WD})=3D~ diff --git a/core/src/test/resources/mimetools-testmsgs/german-qp.out b/core/src/test/resources/mimetools-testmsgs/german-qp.out index 1b6d3fb3..5f7516b5 100644 --- a/core/src/test/resources/mimetools-testmsgs/german-qp.out +++ b/core/src/test/resources/mimetools-testmsgs/german-qp.out @@ -12,6 +12,6 @@ From: =?ISO-8859-15?Q?J=F6rn?= Reder <[email protected]> =0AHallo,=0A=0Adas ist eine Testnachricht mit 8 Bit S=F6nderz=E4ichen, und = obendrein noch =0Aquoted-printable kodiert=2E=0A=0AGr=FC=DFe,=0A=0AJ=F6rn= =0A --- =0A =2E''`=2E J=F6rn Reder <joern@zyn=2Ede>=0A: :' : http://www=2Eexi= -t1=2Eorg/ http://www=2Ezyn=2Ede/=0A`=2E `'=0A `- Debian GNU/Linux -- Th= -e power of freedom=0A \ No newline at end of file +=2D- =0A =2E''`=2E J=F6rn Reder <joern@zyn=2Ede>=0A: :' : http://www=2Ee= +xit1=2Eorg/ http://www=2Ezyn=2Ede/=0A`=2E `'=0A `- Debian GNU/Linux -- = +The power of freedom=0A \ No newline at end of file diff --git a/core/src/test/resources/mimetools-testmsgs/german.out b/core/src/test/resources/mimetools-testmsgs/german.out index cba46313..024025b5 100644 --- a/core/src/test/resources/mimetools-testmsgs/german.out +++ b/core/src/test/resources/mimetools-testmsgs/german.out @@ -75,7 +75,7 @@ Dipl=2E-Ing=2E Guenter D E U S C H L E Deutsche Telekom AG Niederlassung 3 Hannover GrossKundenManagement - Techn=2E Vertriebs-Support: Team-Leiter Internet Online-Dienste ---------------------------------------------------- +=2D-------------------------------------------------- GrKM-TVS-IOD Tel: +49-511-333-2772 Vahrenwalder-Str=2E 245 FAX: +49-511-333-2751 30179 Hannover eMail: deuschle@mnh=2Etelekom=2Ede=20 diff --git a/core/src/test/resources/mimetools-testmsgs/jt-0498.out b/core/src/test/resources/mimetools-testmsgs/jt-0498.out index 768661e3..39e44634 100644 --- a/core/src/test/resources/mimetools-testmsgs/jt-0498.out +++ b/core/src/test/resources/mimetools-testmsgs/jt-0498.out @@ -98,7 +98,7 @@ Content-transfer-encoding: quoted-printable Hi Pat, I don't see any distortion in my tall Honda screen, nor any maginfication-= -- +=2D Visit the PC800 web page at <URL:http://members=2Eaol=2Ecom/wwwpc800/> To unsubscribe from the list, send "unsubscribe pc800" in the body of a message to majordomo@hpc=2Euh=2Eedu=2E diff --git a/core/src/test/resources/mimetools-testmsgs/mp-msg-rfc822.out b/core/src/test/resources/mimetools-testmsgs/mp-msg-rfc822.out index 4a1c757c..47eb461c 100644 --- a/core/src/test/resources/mimetools-testmsgs/mp-msg-rfc822.out +++ b/core/src/test/resources/mimetools-testmsgs/mp-msg-rfc822.out @@ -99,7 +99,7 @@ Dipl=2E-Ing=2E Guenter D E U S C H L E Deutsche Telekom AG Niederlassung 3 Hannover GrossKundenManagement - Techn=2E Vertriebs-Support: Team-Leiter Internet Online-Dienste ---------------------------------------------------- +=2D-------------------------------------------------- GrKM-TVS-IOD Tel: +49-511-333-2772 Vahrenwalder-Str=2E 245 FAX: +49-511-333-2751 30179 Hannover eMail: deuschle@mnh=2Etelekom=2Ede=20 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 476552e1..1ae6731e 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 @@ -424,4 +424,41 @@ public class DefaultMessageWriterTest { assertThat(new String(DefaultMessageWriter.asBytes(message), StandardCharsets.US_ASCII)).contains("--c--"); } + + private static Message reparse(byte[] bytes) throws Exception { + return new DefaultMessageBuilder().parseMessage(new ByteArrayInputStream(bytes)); + } + + private static void assertOnePartKept(String qpBody, String decoded) throws Exception { + byte[] input = ("Content-Type: multipart/mixed; boundary=b\r\n\r\n" + + "--b\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\n" + + qpBody + "\r\n--b--\r\n").getBytes(StandardCharsets.US_ASCII); + Message m1 = reparse(input); + Message m2 = reparse(DefaultMessageWriter.asBytes(m1)); + + Multipart multipart = (Multipart) m2.getBody(); + assertThat(multipart.getBodyParts()).hasSize(1); + SingleBody body = (SingleBody) multipart.getBodyParts().get(0).getBody(); + assertThat(new String(ContentUtil.buffer(body.getInputStream()), StandardCharsets.US_ASCII)) + .isEqualTo(decoded); + } + + @Test + public void writeMessageShouldNotLetAQuotedPrintableSoftBreakStartALineWithTheDelimiter() throws Exception { + String a75 = new String(new char[75]).replace('\0', 'a'); + String text = a75 + "--b\r\nContent-Type: text/html\r\n\r\ny"; + assertOnePartKept(text, text); + } + + @Test + public void writeMessageShouldNotWriteADecodedDelimiterLineOfAQuotedPrintableBody() throws Exception { + assertOnePartKept("x\r\n=2D-b\r\nContent-Type: text/html\r\n\r\ny", + "x\r\n--b\r\nContent-Type: text/html\r\n\r\ny"); + } + + @Test + public void writeMessageShouldNotWriteADelimiterJoinedByASoftBreak() throws Exception { + 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"); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
