diff --git a/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/core/MimeUtils.java b/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/core/MimeUtils.java index 37f96010f..0cd0e8473 100644 --- a/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/core/MimeUtils.java +++ b/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/core/MimeUtils.java @@ -224,10 +224,10 @@ private static String decodeWord(final String word) throws ParseException, Unsup final var encodedData = encodedText.getBytes(StandardCharsets.US_ASCII); - // Base64 encoded? - if (encoding.equals(BASE64_ENCODING_MARKER)) { + // Base64 encoded? RFC 2047 section 2 defines the encoding token as case-independent, so 'b' and 'q' are also valid. + if (encoding.equalsIgnoreCase(BASE64_ENCODING_MARKER)) { out.write(Base64.getMimeDecoder().decode(encodedData)); - } else if (encoding.equals(QUOTEDPRINTABLE_ENCODING_MARKER)) { // maybe quoted printable. + } else if (encoding.equalsIgnoreCase(QUOTEDPRINTABLE_ENCODING_MARKER)) { // maybe quoted printable. QuotedPrintableDecoder.decode(encodedData, out); } else { throw new UnsupportedEncodingException("Unknown RFC 2047 encoding: " + encoding); diff --git a/commons-fileupload2-core/src/test/java/org/apache/commons/fileupload2/core/MimeUtilityTestCase.java b/commons-fileupload2-core/src/test/java/org/apache/commons/fileupload2/core/MimeUtilityTestCase.java index 9186b9b6c..62867c5d3 100644 --- a/commons-fileupload2-core/src/test/java/org/apache/commons/fileupload2/core/MimeUtilityTestCase.java +++ b/commons-fileupload2-core/src/test/java/org/apache/commons/fileupload2/core/MimeUtilityTestCase.java @@ -54,11 +54,23 @@ void testDecodeUtf8Base64Encoded() throws Exception { assertEncoded(" h\u00e9! \u00e0\u00e8\u00f4u !!!", "=?UTF-8?B?IGjDqSEgw6DDqMO0dSAhISE=?="); } + @Test + void testDecodeUtf8Base64EncodedLowerCaseMarker() throws Exception { + // RFC 2047 section 2: the encoding token is case-independent, so 'b' decodes like 'B'. + assertEncoded(" h\u00e9! \u00e0\u00e8\u00f4u !!!", "=?UTF-8?b?IGjDqSEgw6DDqMO0dSAhISE=?="); + } + @Test void testDecodeUtf8QuotedPrintableEncoded() throws Exception { assertEncoded(" h\u00e9! \u00e0\u00e8\u00f4u !!!", "=?UTF-8?Q?_h=C3=A9!_=C3=A0=C3=A8=C3=B4u_!!!?="); } + @Test + void testDecodeUtf8QuotedPrintableEncodedLowerCaseMarker() throws Exception { + // RFC 2047 section 2: the encoding token is case-independent, so 'q' decodes like 'Q'. + assertEncoded(" h\u00e9! \u00e0\u00e8\u00f4u !!!", "=?UTF-8?q?_h=C3=A9!_=C3=A0=C3=A8=C3=B4u_!!!?="); + } + @Test void testNoNeedToDecode() throws Exception { assertEncoded("abc", "abc");