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 449e42e8eddeab8d08527e920d6446e6457d3dd3 Author: Benoit TELLIER <[email protected]> AuthorDate: Tue Dec 1 21:06:37 2026 +0100 [FIX] DecoderUtil: a word left encoded comes after the words decoded before it The bytes decoded from adjacent words of a charset wait in a buffer until a separator or a change of charset or encoding. A word that failed to decode was appended as is without that buffer being written first, so the words decoded before it came after it: "=?utf-8?B?QQ==?= =?utf-8?B?Q===?=" read "=?utf-8?B?Q===?=A". The buffer is now written before the raw word. The DecodeMonitor instances of mime4j cannot reach it (a change of charset or encoding already writes the buffer); a monitor that makes the stream decoder fail without making decodeEncodedWords throw does. --- .../org/apache/james/mime4j/codec/DecoderUtil.java | 2 ++ .../apache/james/mime4j/codec/DecoderUtilTest.java | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/core/src/main/java/org/apache/james/mime4j/codec/DecoderUtil.java b/core/src/main/java/org/apache/james/mime4j/codec/DecoderUtil.java index 4867a172..49e0cd73 100644 --- a/core/src/main/java/org/apache/james/mime4j/codec/DecoderUtil.java +++ b/core/src/main/java/org/apache/james/mime4j/codec/DecoderUtil.java @@ -434,6 +434,8 @@ public class DecoderUtil { } if (!tryDecodeEncodedWord(mimeCurCharset, curEncoding, encodedText, monitor, fallback, charsetOverrides, out)) { + // the words decoded before it come first + appendStringBuffer(fallback, charsetOverrides, out, sb, mimeCharset); sb.append(body, startPattern, encodedTextEnd + 2); } position = encodedTextEnd + 2; diff --git a/core/src/test/java/org/apache/james/mime4j/codec/DecoderUtilTest.java b/core/src/test/java/org/apache/james/mime4j/codec/DecoderUtilTest.java index 204f742c..bc56d6f1 100644 --- a/core/src/test/java/org/apache/james/mime4j/codec/DecoderUtilTest.java +++ b/core/src/test/java/org/apache/james/mime4j/codec/DecoderUtilTest.java @@ -281,6 +281,27 @@ public class DecoderUtilTest { () -> DecoderUtil.decodeEncodedWords("=?us-ascii?B?YWJ?=", DecodeMonitor.STRICT)); } + // The bytes of the words decoded before a word left encoded were written after it + @Test + public void aWordLeftEncodedShouldComeAfterTheWordsDecodedBeforeIt() { + // fails the base64 decoder, then leaves the word encoded + DecodeMonitor monitor = new DecodeMonitor() { + @Override + public boolean warn(String error, String dropDesc) { + return error.startsWith("Unexpected"); + } + + @Override + public boolean isListening() { + return true; + } + }; + Assert.assertEquals("A=?utf-8?B?Q===?=", + DecoderUtil.decodeEncodedWords("=?utf-8?B?QQ==?= =?utf-8?B?Q===?=", monitor)); + Assert.assertEquals("AB=?utf-8?B?Q===?=C", + DecoderUtil.decodeEncodedWords("=?utf-8?B?QQ==?= =?utf-8?B?Qg==?= =?utf-8?B?Q===?= =?utf-8?B?Qw==?=", monitor)); + } + private static String randomText(Random random, String alphabet) { int length = random.nextInt(13); StringBuilder sb = new StringBuilder(length); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
