From 50c11028d5816a6cc390f85167736fa412d1eb29 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Mon, 18 Apr 2016 10:01:18 -0400 Subject: [PATCH] Improve illegal MimeType checks Issue: SPR-14124 --- .../java/org/springframework/util/MimeTypeUtils.java | 5 ++++- .../java/org/springframework/util/MimeTypeTests.java | 9 +++++++-- 2 files changed, 11 insertions(+), 3 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 0bf62d58e3..a0ad5cc095 100644 --- a/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java +++ b/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -220,6 +220,9 @@ public abstract class MimeTypeUtils { throw new InvalidMimeTypeException(mimeType, "'mimeType' must not be empty"); } String[] parts = StringUtils.tokenizeToStringArray(mimeType, ";"); + if (parts.length == 0) { + throw new InvalidMimeTypeException(mimeType, "'mimeType' must not be empty"); + } String fullType = parts[0].trim(); // java.net.HttpURLConnection returns a *; q=.2 Accept header 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 5508fa26fe..99c5a82611 100644 --- a/spring-core/src/test/java/org/springframework/util/MimeTypeTests.java +++ b/spring-core/src/test/java/org/springframework/util/MimeTypeTests.java @@ -189,6 +189,11 @@ public class MimeTypeTests { MimeTypeUtils.parseMimeType("audio/basic)"); } + @Test(expected = InvalidMimeTypeException.class) + public void parseMimeTypeMissingTypeAndSubtype() throws Exception { + MimeTypeUtils.parseMimeType(" ;a=b"); + } + @Test(expected = InvalidMimeTypeException.class) public void parseMimeTypeEmptyParameterAttribute() { MimeTypeUtils.parseMimeType("audio/*;=value"); @@ -263,13 +268,13 @@ public class MimeTypeTests { assertTrue("Invalid comparison result", audioBasicLevel.compareTo(audio) > 0); - List expected = new ArrayList(); + List expected = new ArrayList<>(); expected.add(audio); expected.add(audioBasic); expected.add(audioBasicLevel); expected.add(audioWave); - List result = new ArrayList(expected); + List result = new ArrayList<>(expected); Random rnd = new Random(); // shuffle & sort 10 times for (int i = 0; i < 10; i++) {