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 335c95a8cd1f1d20756dd5b85beb9f54bcec2f4b
Author: Benoit TELLIER <[email protected]>
AuthorDate: Fri Nov 13 13:59:15 2026 +0100

    [FIX] ContentTypeFieldImpl: a quoted parameter value starting with "=?" is 
decoded as the lenient reader does
    
    Since the fix of #126, EncoderUtil#encodeHeaderParameter writes a
    Content-Type parameter value starting with "=?" as one quoted B encoded
    word, which the lenient reader decodes. The strict ContentTypeFieldImpl
    (DefaultFieldParser, strict DefaultMessageBuilder, 
DefaultBodyDescriptorBuilder
    by default) kept the quoted-string as is: Fields.contentType("text/plain",
    name="=?utf-8?q?x?=") read back as "=?utf-8?B?PT91dGYtOD9xP3g/PQ==?=".
    
    A parameter value starting with "=?" can only come from a quoted string, as
    '=' and '?' are not token characters: the strict reader now decodes it as
    encoded words, as RawFieldParser and many mailers do (name="=?...?="). The
    boundary, a byte exact delimiter, is still never decoded. Both readers then
    agree, on built fields and on received ones.
    
    Fixes #133
---
 .../james/mime4j/field/ContentTypeFieldImpl.java   | 17 ++++++++++++-
 .../org/apache/james/mime4j/field/FieldsTest.java  | 29 ++++++++++++++++++++++
 2 files changed, 45 insertions(+), 1 deletion(-)

diff --git 
a/dom/src/main/java/org/apache/james/mime4j/field/ContentTypeFieldImpl.java 
b/dom/src/main/java/org/apache/james/mime4j/field/ContentTypeFieldImpl.java
index 6fc2604b..3eed84fc 100644
--- a/dom/src/main/java/org/apache/james/mime4j/field/ContentTypeFieldImpl.java
+++ b/dom/src/main/java/org/apache/james/mime4j/field/ContentTypeFieldImpl.java
@@ -27,6 +27,7 @@ import java.util.Locale;
 import java.util.Map;
 
 import org.apache.james.mime4j.codec.DecodeMonitor;
+import org.apache.james.mime4j.codec.DecoderUtil;
 import org.apache.james.mime4j.dom.FieldParser;
 import org.apache.james.mime4j.dom.field.ContentTypeField;
 import org.apache.james.mime4j.field.contenttype.parser.ContentTypeParser;
@@ -214,7 +215,7 @@ public class ContentTypeFieldImpl extends AbstractField 
implements ContentTypeFi
                 final int len = Math.min(paramNames.size(), 
paramValues.size());
                 for (int i = 0; i < len; i++) {
                     String paramName = 
paramNames.get(i).toLowerCase(Locale.US);
-                    String paramValue = paramValues.get(i);
+                    String paramValue = decodeEncodedWordValue(paramName, 
paramValues.get(i));
                     parameters.put(paramName, paramValue);
                 }
             }
@@ -223,6 +224,20 @@ public class ContentTypeFieldImpl extends AbstractField 
implements ContentTypeFi
         parsed = true;
     }
 
+    /**
+     * A value starting with "=?" is a quoted string, as '=' and '?' are not 
token characters. As
+     * the lenient reader (RawFieldParser) and many mailers do 
(name="=?utf-8?B?...?="), and as
+     * EncoderUtil#encodeHeaderParameter writes such a value for Content-Type, 
it is decoded as
+     * encoded words, though RFC 2047 section 5 does not allow them there. 
Never a boundary, which
+     * is a byte exact delimiter.
+     */
+    private static String decodeEncodedWordValue(final String name, final 
String value) {
+        if (value == null || !value.startsWith("=?") || 
PARAM_BOUNDARY.equals(name)) {
+            return value;
+        }
+        return DecoderUtil.decodeEncodedWords(value, DecodeMonitor.SILENT);
+    }
+
     public static final FieldParser<ContentTypeField> PARSER = new 
FieldParser<ContentTypeField>() {
 
         public ContentTypeField parse(final Field rawField, final 
DecodeMonitor monitor) {
diff --git a/dom/src/test/java/org/apache/james/mime4j/field/FieldsTest.java 
b/dom/src/test/java/org/apache/james/mime4j/field/FieldsTest.java
index 03c0a75a..2d210464 100644
--- a/dom/src/test/java/org/apache/james/mime4j/field/FieldsTest.java
+++ b/dom/src/test/java/org/apache/james/mime4j/field/FieldsTest.java
@@ -40,6 +40,8 @@ import org.apache.james.mime4j.dom.field.MailboxListField;
 import org.apache.james.mime4j.dom.field.UnstructuredField;
 import org.apache.james.mime4j.field.address.DefaultAddressParser;
 import org.apache.james.mime4j.stream.Field;
+import org.apache.james.mime4j.stream.NameValuePair;
+import org.apache.james.mime4j.stream.RawFieldParser;
 import org.apache.james.mime4j.util.ByteSequence;
 import org.apache.james.mime4j.util.ContentUtil;
 import org.apache.james.mime4j.util.MimeUtil;
@@ -508,4 +510,31 @@ public class FieldsTest {
             // expected
         }
     }
+
+    @Test
+    public void 
contentTypeParameterStartingWithAnEncodedWordLookalikeShouldReadBackUnchangedWithBothParsers()
 throws Exception {
+        String[] values = {"=?utf-8?q?x?=", "=?utf-8?q?x?=\"", "=?x???="};
+        for (String value : values) {
+            ContentTypeField field = Fields.contentType("text/plain",
+                    Collections.singletonList(new NameValuePair("name", 
value)));
+
+            Assert.assertEquals(value, field.getParameter("name"));
+            Assert.assertEquals(value, ((ContentTypeField) 
LenientFieldParser.getParser()
+                    
.parse(RawFieldParser.DEFAULT.parseField(ContentUtil.encode("Content-Type: " + 
field.getBody())), DecodeMonitor.SILENT)).getParameter("name"));
+            Assert.assertEquals(value, ((ContentTypeField) 
DefaultFieldParser.getParser()
+                    
.parse(RawFieldParser.DEFAULT.parseField(ContentUtil.encode("Content-Type: " + 
field.getBody())), DecodeMonitor.SILENT)).getParameter("name"));
+        }
+    }
+
+    @Test
+    public void 
strictContentTypeShouldDecodeAQuotedEncodedWordLikeTheLenientParser() throws 
Exception {
+        Field raw = DefaultFieldParser.parse("Content-Type: text/plain; 
name=\"=?utf-8?q?caf=C3=A9.txt?=\"; "
+            + "boundary=\"=?utf-8?q?b?=\"", DecodeMonitor.SILENT);
+        ContentTypeField strict = (ContentTypeField) 
DefaultFieldParser.getParser().parse(raw, DecodeMonitor.SILENT);
+        ContentTypeField lenient = (ContentTypeField) 
LenientFieldParser.getParser().parse(raw, DecodeMonitor.SILENT);
+        Assert.assertEquals("caf\u00e9.txt", strict.getParameter("name"));
+        Assert.assertEquals(lenient.getParameter("name"), 
strict.getParameter("name"));
+        Assert.assertEquals("=?utf-8?q?b?=", strict.getBoundary());
+        Assert.assertEquals(lenient.getBoundary(), strict.getBoundary());
+    }
 }


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

Reply via email to