From bc6662234215ecbf68235d87a87a48be52fc9491 Mon Sep 17 00:00:00 2001 From: junhyeong9812 Date: Thu, 13 Aug 2026 15:27:31 +0900 Subject: [PATCH] Reject MIME type parameters differing only in case MIME type parameter names are case-insensitive, but MimeTypeParser accumulates parameters in a case-sensitive LinkedHashMap. As a result, duplicate parameters differing only in case (such as "charset" and "CHARSET") were not rejected and were silently collapsed to the last value by the case-insensitive parameter map of MimeType. Accumulate parameters in a LinkedCaseInsensitiveMap so that duplicates differing only in case map to the same key and are rejected consistently with exact duplicates. Signed-off-by: junhyeong9812 Co-authored-by: Yash <190389954+yashsiwacha@users.noreply.github.com> --- .../main/java/org/springframework/util/MimeTypeUtils.java | 4 ++-- .../test/java/org/springframework/util/MimeTypeTests.java | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java b/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java index b97877970c4..663ca1006c6 100644 --- a/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java +++ b/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java @@ -23,8 +23,8 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Iterator; -import java.util.LinkedHashMap; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Random; import java.util.function.BiPredicate; @@ -437,7 +437,7 @@ public abstract class MimeTypeUtils { private void putParameter(String name, String value) { if (this.parameters == null) { - this.parameters = new LinkedHashMap<>(4); + this.parameters = new LinkedCaseInsensitiveMap<>(4, Locale.ROOT); } if (this.parameters.put(name, value) != null) { throw new InvalidMimeTypeException(this.input, "duplicate parameter '" + name + "=" + value + "'"); diff --git a/spring-core/src/test/java/org/springframework/util/MimeTypeTests.java b/spring-core/src/test/java/org/springframework/util/MimeTypeTests.java index bef9ebfde42..fa4327520f0 100644 --- a/spring-core/src/test/java/org/springframework/util/MimeTypeTests.java +++ b/spring-core/src/test/java/org/springframework/util/MimeTypeTests.java @@ -174,6 +174,12 @@ class MimeTypeTests { .hasMessageContaining("Invalid mime type \"text/plain;dupe=\"1\";dupe=\"2\"\": duplicate parameter 'dupe=\"2\"'"); } + @Test + void valueOfDuplicateParameterWithDifferentCase() { + assertThatThrownBy(() -> MimeType.valueOf("text/plain;dupe=\"1\";DUPE=\"2\"")).isInstanceOf(InvalidMimeTypeException.class) + .hasMessageContaining("Invalid mime type \"text/plain;dupe=\"1\";DUPE=\"2\"\": duplicate parameter 'DUPE=\"2\"'"); + } + }