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

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


The following commit(s) were added to refs/heads/master by this push:
     new 89c2e579 [FIX] MessageMatcher: a body with an unknown charset is read 
as US-ASCII instead of ending the search
89c2e579 is described below

commit 89c2e57932cab6d0dd012b68e3940f8ee9b9c32f
Author: Benoit TELLIER <[email protected]>
AuthorDate: Sat Nov 28 07:47:39 2026 +0100

    [FIX] MessageMatcher: a body with an unknown charset is read as US-ASCII 
instead of ending the search
---
 .../james/mime4j/utils/search/MessageMatcher.java     | 17 ++++++++++++++++-
 .../james/mime4j/utils/search/MessageMatcherTest.java | 19 +++++++++++++++++++
 2 files changed, 35 insertions(+), 1 deletion(-)

diff --git 
a/james-utils/src/main/java/org/apache/james/mime4j/utils/search/MessageMatcher.java
 
b/james-utils/src/main/java/org/apache/james/mime4j/utils/search/MessageMatcher.java
index 466f79d4..26e7f2eb 100644
--- 
a/james-utils/src/main/java/org/apache/james/mime4j/utils/search/MessageMatcher.java
+++ 
b/james-utils/src/main/java/org/apache/james/mime4j/utils/search/MessageMatcher.java
@@ -32,8 +32,10 @@ import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.Reader;
 import java.io.StringReader;
+import java.io.UnsupportedEncodingException;
 import java.nio.CharBuffer;
 import java.nio.charset.IllegalCharsetNameException;
+import java.nio.charset.StandardCharsets;
 import java.nio.charset.UnsupportedCharsetException;
 import java.util.ArrayList;
 import java.util.Collections;
@@ -210,11 +212,24 @@ public class MessageMatcher {
     }
 
     private boolean checkBody(final List<NeedleMatcher> matchers, 
MimeTokenStream parser) throws IOException {
-        try (Reader reader = parser.getReader()) {
+        try (Reader reader = bodyReader(parser)) {
             return searchIn(reader, matchers);
         }
     }
 
+    /**
+     * A body whose charset is unknown is read as US-ASCII, as a body without 
charset: it is
+     * still searched, and the search goes on with the other parts.
+     */
+    private Reader bodyReader(MimeTokenStream parser) {
+        try {
+            return parser.getReader();
+        } catch (UnsupportedEncodingException e) {
+            logger.debug("Unknown body charset, read as US-ASCII.", e);
+            return new InputStreamReader(parser.getDecodedInputStream(), 
StandardCharsets.US_ASCII);
+        }
+    }
+
     private CharBuffer createBuffer(final CharSequence searchContent) {
         final CharBuffer buffer;
         if (isCaseInsensitive) {
diff --git 
a/james-utils/src/test/java/org/apache/james/mime4j/utils/search/MessageMatcherTest.java
 
b/james-utils/src/test/java/org/apache/james/mime4j/utils/search/MessageMatcherTest.java
index 40a29255..6c76bc75 100644
--- 
a/james-utils/src/test/java/org/apache/james/mime4j/utils/search/MessageMatcherTest.java
+++ 
b/james-utils/src/test/java/org/apache/james/mime4j/utils/search/MessageMatcherTest.java
@@ -225,6 +225,25 @@ public class MessageMatcherTest {
         assertThat(messageMatcher.messageMatches(stream("the first and the 
second"))).isFalse();
     }
 
+    @Test
+    public void bodyWithAnUnknownCharsetShouldStillBeSearched() throws 
Exception {
+        MessageMatcher messageMatcher = MessageMatcher.builder()
+            .searchContents(Lists.<CharSequence>newArrayList("aB"))
+            .build();
+        assertThat(messageMatcher.messageMatches(stream("Content-Type: 
text/plain; charset=x-unknown\r\n\r\naB"))).isTrue();
+    }
+
+    @Test
+    public void 
bodyWithAnUnknownCharsetShouldNotStopTheSearchOfTheOtherParts() throws 
Exception {
+        MessageMatcher messageMatcher = MessageMatcher.builder()
+            .searchContents(Lists.<CharSequence>newArrayList("needle"))
+            .build();
+        assertThat(messageMatcher.messageMatches(stream("Content-Type: 
multipart/mixed; boundary=b\r\n\r\n"
+            + "--b\r\nContent-Type: text/plain; 
charset=\"UC\"64\r\n\r\nother\r\n"
+            + "--b\r\nContent-Type: text/plain\r\n\r\nneedle\r\n"
+            + "--b--\r\n"))).isTrue();
+    }
+
     private InputStream stream(String content) {
         return new 
ByteArrayInputStream(content.getBytes(StandardCharsets.US_ASCII));
     }


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

Reply via email to