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 55f76d94820..214139d5016 100644 --- a/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java +++ b/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java @@ -187,7 +187,7 @@ public abstract class MimeTypeUtils { /** * Parse the given String into a single {@code MimeType}. - * Recently parsed {@code MimeType} are cached for further retrieval. + *

Recently parsed {@code MimeType} values are cached for future retrieval. * @param mimeType the string to parse * @return the mime type * @throws InvalidMimeTypeException if the string cannot be parsed @@ -238,7 +238,7 @@ public abstract class MimeTypeUtils { break; } } - else if (ch == '"' && mimeType.charAt(nextIndex - 1) != '\\') { + else if (ch == '"' && (nextIndex == 0 || mimeType.charAt(nextIndex - 1) != '\\')) { quoted = !quoted; } nextIndex++; 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 cffa8d31e5f..29cf7119b50 100644 --- a/spring-core/src/test/java/org/springframework/util/MimeTypeTests.java +++ b/spring-core/src/test/java/org/springframework/util/MimeTypeTests.java @@ -108,7 +108,7 @@ class MimeTypeTests { assertThat(mimeType.getParameter("type")).isEqualTo("\"application/soap+xml;action=\\\"https://x.y.z\\\"\""); } - @Test + @Test // gh-36730 void parseParameterWithQuotedPair() { String s = "text/plain;twelve=\"1\\\"2\""; MimeType mimeType = MimeType.valueOf(s); @@ -303,6 +303,13 @@ class MimeTypeTests { MimeTypeUtils.parseMimeType("audio/*;attr=\"")); } + @Test // gh-36971 + void parseMimeTypeWrappedInQuotes() { + assertThatExceptionOfType(InvalidMimeTypeException.class) + .isThrownBy(() -> MimeTypeUtils.parseMimeType("\"application/xml\"")) + .withMessageContaining("Invalid token character '\"'"); + } + @Test void parseMimeTypeNull() { assertThatExceptionOfType(InvalidMimeTypeException.class).isThrownBy(() ->