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 a026fedb17d069376e8c0deaa8b6f87c33797814
Author: Benoit TELLIER <[email protected]>
AuthorDate: Tue Nov 10 12:09:59 2026 +0100

    [FIX] EncoderUtil: a parameter value holding "=?" reads back unchanged
    
    encodeHeaderParameter wrote any value that is not a token as a quoted
    string. The readers decode the encoded words of quoted parameter values,
    as many mailers write filename="=?utf-8?B?...?=": RawFieldParser decodes a
    quoted value starting with "=?", and MimeParameterMapping a plain value
    holding one anywhere. So a filename or a parameter given to the builders
    as "=?utf-8?q?x?=" (a JMAP attachment name) was read back as "x" by every
    reader, from the built field and from the written message.
    
    When the field's parser decodes RFC 2231 (Content-Disposition), such a
    value is now written as an extended parameter, name*=utf-8''%3D%3F...,
    which is percent decoded and nothing more; in sections when it is too
    long. Content-Type has no RFC 2231 reader, and its lenient reader only
    decodes a value starting with "=?": such a value is written as one B
    encoded word of the whole value. Values without "=?" are written as
    before.
---
 .../org/apache/james/mime4j/codec/EncoderUtil.java | 34 +++++++-
 .../FieldsLiteralEncodedWordParameterTest.java     | 93 ++++++++++++++++++++++
 2 files changed, 125 insertions(+), 2 deletions(-)

diff --git a/core/src/main/java/org/apache/james/mime4j/codec/EncoderUtil.java 
b/core/src/main/java/org/apache/james/mime4j/codec/EncoderUtil.java
index 316986c7..d1d2e9f2 100644
--- a/core/src/main/java/org/apache/james/mime4j/codec/EncoderUtil.java
+++ b/core/src/main/java/org/apache/james/mime4j/codec/EncoderUtil.java
@@ -263,15 +263,35 @@ public class EncoderUtil {
      *            parameter name.
      * @param value
      *            parameter value.
+     * <p>The readers of these fields decode the RFC 2047 encoded words of a 
parameter value, as
+     * many mailers write <code>filename="=?utf-8?B?...?="</code>, so a value 
holding a literal
+     * <code>=?</code> cannot be written as a plain quoted string. It is 
written as an RFC 2231
+     * extended parameter (<code>name*=utf-8''%3D%3F...</code>) when the 
parser of the field
+     * decodes them, and otherwise, when the value starts with <code>=?</code> 
(the only case the
+     * lenient readers decode), as an encoded word of the whole value.</p>
+     *
+     * @param name
+     *            parameter name.
+     * @param value
+     *            parameter value.
      * @param rfc2231Sections
-     *            whether a parameter too long for one line may be split into
-     *            RFC 2231 sections.
+     *            whether the parser of the field decodes RFC 2231 parameters, 
so that a
+     *            parameter too long for one line may be split into RFC 2231 
sections.
      * @return encoded result.
      * @throws IllegalArgumentException if the name is invalid, or if the
      *             parameter is too long for one line and cannot be split.
      */
     public static String encodeHeaderParameter(String name, String value, 
boolean rfc2231Sections) {
         String parameter = encodeHeaderParameter(name, value);
+        if (name.indexOf('*') < 0 && value.contains("=?")) {
+            String lowerName = name.toLowerCase(Locale.US);
+            if (rfc2231Sections) {
+                return encodeExtended(lowerName, value);
+            } else if (value.startsWith("=?")) {
+                parameter = lowerName + "=\"" + ENC_WORD_PREFIX + "utf-8?B?"
+                        + encodeB(value.getBytes(Charsets.UTF_8)) + 
ENC_WORD_SUFFIX + "\"";
+            }
+        }
         if (fitsOnFoldedLines(parameter)) {
             return parameter;
         }
@@ -345,6 +365,16 @@ public class EncoderUtil {
         return sb.toString();
     }
 
+    // RFC 2231 section 4: name*=utf-8''%XX..., in sections when too long for 
one
+    private static String encodeExtended(String name, String value) {
+        String sections = encodeEncodedSections(name, value);
+        String single = name + "*0*=";
+        if (sections.indexOf(';') < 0 && sections.startsWith(single)) {
+            return name + "*=" + sections.substring(single.length());
+        }
+        return sections;
+    }
+
     private static void appendEncodedSection(StringBuilder sb, String name, 
int section, CharSequence value) {
         if (section > 0) {
             sb.append("; ");
diff --git 
a/dom/src/test/java/org/apache/james/mime4j/field/FieldsLiteralEncodedWordParameterTest.java
 
b/dom/src/test/java/org/apache/james/mime4j/field/FieldsLiteralEncodedWordParameterTest.java
new file mode 100644
index 00000000..c40ef205
--- /dev/null
+++ 
b/dom/src/test/java/org/apache/james/mime4j/field/FieldsLiteralEncodedWordParameterTest.java
@@ -0,0 +1,93 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mime4j.field;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Collections;
+
+import org.apache.james.mime4j.dom.field.ContentDispositionField;
+import org.apache.james.mime4j.dom.field.ContentTypeField;
+import org.apache.james.mime4j.stream.Field;
+import org.apache.james.mime4j.stream.NameValuePair;
+import org.junit.Test;
+
+/**
+ * A parameter value holding a literal "=?...?=" is read back unchanged, 
although the readers
+ * decode the encoded words of the quoted parameter values they receive.
+ */
+public class FieldsLiteralEncodedWordParameterTest {
+
+    private static String raw(Field field) {
+        return field.getName() + ": " + field.getBody();
+    }
+
+    private static void assertFilenameRoundTrips(String filename) throws 
Exception {
+        ContentDispositionField field = 
Fields.contentDisposition("attachment", filename, -1, null, null, null);
+        assertEquals(raw(field), filename, field.getFilename());
+        String raw = raw(field);
+        assertEquals(raw, filename, ((ContentDispositionField) 
DefaultFieldParser.parse(raw)).getFilename());
+        assertEquals(raw, filename, ((ContentDispositionField) 
LenientFieldParser.parse(raw)).getFilename());
+    }
+
+    @Test
+    public void filenameThatLooksLikeAnEncodedWordShouldBeReadBackUnchanged() 
throws Exception {
+        assertFilenameRoundTrips("=?utf-8?q?x?=");
+        assertFilenameRoundTrips("a =?utf-8?q?x?= b");
+        assertFilenameRoundTrips("=?utf-8?q?x?=aaaaaaaaa\"\"\"\"\"\"\"");
+        // long enough for RFC 2231 sections
+        StringBuilder sb = new StringBuilder("=?utf-8?q?x?=");
+        for (int i = 0; i < 100; i++) {
+            sb.append("y");
+        }
+        assertFilenameRoundTrips(sb.toString());
+    }
+
+    @Test
+    public void contentDispositionParameterMapShouldKeepALiteralEncodedWord() 
throws Exception {
+        ContentDispositionField field = Fields.contentDisposition("6",
+                Collections.singletonMap("report-type", 
"=?utf-8?q?x?=aaaaaaaaa\"\"\"\"\"\"\""));
+        assertEquals("=?utf-8?q?x?=aaaaaaaaa\"\"\"\"\"\"\"", 
field.getParameter("report-type"));
+    }
+
+    @Test
+    public void 
contentTypeParameterThatHoldsAnEncodedWordShouldBeReadBackUnchanged() throws 
Exception {
+        String[] values = {"=?utf-8?q?x?=\"", "=?utf-8?q?x?=", "a 
=?utf-8?q?x?="};
+        for (String value : values) {
+            ContentTypeField field = Fields.contentType("text/plain", new 
NameValuePair("name", value));
+            String raw = raw(field);
+            assertEquals(raw, value, field.getParameter("name"));
+            assertEquals(raw, value, ((ContentTypeField) 
LenientFieldParser.parse(raw)).getParameter("name"));
+        }
+    }
+
+    @Test
+    public void receivedEncodedFilenamesShouldStillBeDecoded() throws 
Exception {
+        String raw = "Content-Disposition: attachment; 
filename=\"=?utf-8?q?caf=C3=A9.txt?=\"";
+        assertEquals("café.txt", ((ContentDispositionField) 
LenientFieldParser.parse(raw)).getFilename());
+        assertEquals("café.txt", ((ContentDispositionField) 
DefaultFieldParser.parse(raw)).getFilename());
+    }
+
+    @Test
+    public void plainFilenamesShouldStillBeQuoted() {
+        ContentDispositionField field = 
Fields.contentDisposition("attachment", "a b.txt", -1, null, null, null);
+        assertEquals("Content-Disposition: attachment; filename=\"a b.txt\"", 
raw(field));
+    }
+}


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

Reply via email to