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 <pickjog@gmail.com>
Co-authored-by: Yash <190389954+yashsiwacha@users.noreply.github.com>
This commit is contained in:
junhyeong9812
2026-08-31 16:23:20 +02:00
committed by Brian Clozel
co-authored by Yash
parent dd110d4604
commit bc66622342
2 changed files with 8 additions and 2 deletions
@@ -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 + "'");
@@ -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\"'");
}
}