Skip to content

Commit 50c1102

Browse files
committed
Improve illegal MimeType checks
Issue: SPR-14124
1 parent aafd46a commit 50c1102

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -220,6 +220,9 @@ public static MimeType parseMimeType(String mimeType) {
220220
throw new InvalidMimeTypeException(mimeType, "'mimeType' must not be empty");
221221
}
222222
String[] parts = StringUtils.tokenizeToStringArray(mimeType, ";");
223+
if (parts.length == 0) {
224+
throw new InvalidMimeTypeException(mimeType, "'mimeType' must not be empty");
225+
}
223226

224227
String fullType = parts[0].trim();
225228
// java.net.HttpURLConnection returns a *; q=.2 Accept header

spring-core/src/test/java/org/springframework/util/MimeTypeTests.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ public void parseMimeTypeIllegalSubtype() {
189189
MimeTypeUtils.parseMimeType("audio/basic)");
190190
}
191191

192+
@Test(expected = InvalidMimeTypeException.class)
193+
public void parseMimeTypeMissingTypeAndSubtype() throws Exception {
194+
MimeTypeUtils.parseMimeType(" ;a=b");
195+
}
196+
192197
@Test(expected = InvalidMimeTypeException.class)
193198
public void parseMimeTypeEmptyParameterAttribute() {
194199
MimeTypeUtils.parseMimeType("audio/*;=value");
@@ -263,13 +268,13 @@ public void compareTo() {
263268

264269
assertTrue("Invalid comparison result", audioBasicLevel.compareTo(audio) > 0);
265270

266-
List<MimeType> expected = new ArrayList<MimeType>();
271+
List<MimeType> expected = new ArrayList<>();
267272
expected.add(audio);
268273
expected.add(audioBasic);
269274
expected.add(audioBasicLevel);
270275
expected.add(audioWave);
271276

272-
List<MimeType> result = new ArrayList<MimeType>(expected);
277+
List<MimeType> result = new ArrayList<>(expected);
273278
Random rnd = new Random();
274279
// shuffle & sort 10 times
275280
for (int i = 0; i < 10; i++) {

0 commit comments

Comments
 (0)