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 b5c1e958795abbb0f48b9d3e7d96f42832cedcdc Author: Benoit TELLIER <[email protected]> AuthorDate: Sat Nov 21 07:09:21 2026 +0100 [FIX] EncoderUtil: a boundary parameter starting with "=?" is written as a plain value Since a026fedb, a parameter value starting with "=?" is written as an encoded word of the whole value, for the readers that decode the quoted parameter values. The Content-Type readers never decode a boundary though (RawFieldParser, ContentTypeFieldImpl): Fields#contentType given the boundary "=?x" wrote boundary="=?utf-8?B?PT94?=", and getBoundary() returned that encoded word, on the built field and once re-read. Without RFC 2231 parameters, as for Content-Type, a boundary is written as a plain value, as the readers read it. Content-Disposition, whose readers decode every parameter, keeps the RFC 2231 form. --- .../org/apache/james/mime4j/codec/EncoderUtil.java | 9 +++++++-- .../field/FieldsLiteralEncodedWordParameterTest.java | 20 ++++++++++++++++++++ 2 files changed, 27 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 6b10650e..acbed069 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 @@ -271,7 +271,9 @@ public class EncoderUtil { * <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> + * lenient readers decode), as an encoded word of the whole value. Without RFC 2231 + * parameters, a boundary, which the Content-Type readers never decode, is written as a + * plain value.</p> * * @param name * parameter name. @@ -286,7 +288,10 @@ public class EncoderUtil { */ public static String encodeHeaderParameter(String name, String value, boolean rfc2231Sections) { String parameter = encodeHeaderParameter(name, value); - if (name.indexOf('*') < 0 && value.contains("=?")) { + // The Content-Type readers never decode a boundary (the fix of #29): it is written as a + // plain value. The readers of RFC 2231 parameters decode it as any other parameter + boolean plainBoundary = !rfc2231Sections && "boundary".equalsIgnoreCase(name); + if (name.indexOf('*') < 0 && value.contains("=?") && !plainBoundary) { String lowerName = name.toLowerCase(Locale.US); if (rfc2231Sections) { return encodeExtended(lowerName, value); 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 index c40ef205..2d34515a 100644 --- a/dom/src/test/java/org/apache/james/mime4j/field/FieldsLiteralEncodedWordParameterTest.java +++ b/dom/src/test/java/org/apache/james/mime4j/field/FieldsLiteralEncodedWordParameterTest.java @@ -90,4 +90,24 @@ public class FieldsLiteralEncodedWordParameterTest { ContentDispositionField field = Fields.contentDisposition("attachment", "a b.txt", -1, null, null, null); assertEquals("Content-Disposition: attachment; filename=\"a b.txt\"", raw(field)); } + + @Test + public void boundaryThatStartsLikeAnEncodedWordShouldBeReadBackUnchanged() throws Exception { + for (String name : new String[] {"boundary", "Boundary"}) { + for (String boundary : new String[] {"=?x", "=?utf-8?q?x?=", "=?\"\""}) { + ContentTypeField field = Fields.contentType("multipart/mixed", + Collections.singletonMap(name, boundary)); + assertEquals(boundary, field.getBoundary()); + String raw = raw(field); + assertEquals(raw, boundary, ((ContentTypeField) DefaultFieldParser.parse(raw)).getBoundary()); + assertEquals(raw, boundary, ((ContentTypeField) LenientFieldParser.parse(raw)).getBoundary()); + } + } + // Content-Disposition decodes every parameter: there, it is still written as RFC 2231 + ContentDispositionField disposition = Fields.contentDisposition("attachment", + Collections.singletonMap("boundary", "=?utf-8?q?x?=")); + String raw = raw(disposition); + assertEquals(raw, "=?utf-8?q?x?=", ((ContentDispositionField) DefaultFieldParser.parse(raw)).getParameter("boundary")); + assertEquals(raw, "=?utf-8?q?x?=", ((ContentDispositionField) LenientFieldParser.parse(raw)).getParameter("boundary")); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
