From 49bbcceda9d065260a1be9ea28427d85f5ca7c3f Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Tue, 14 Apr 2020 14:15:39 +0200 Subject: [PATCH] Fix Undertow compression config with invalid Mime Types Prior to this commit, the Undertow compression configuration provided by Spring Boot would fail and throw an exception for invalid MIME Types when trying to check them against the list of configured types for compression. This commit ensures that invalid MIME Types are ignored and that compression is disabled for those. Fixes gh-20955 --- .../undertow/UndertowCompressionConfigurer.java | 13 ++++++++++--- .../AbstractReactiveWebServerFactoryTests.java | 2 ++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowCompressionConfigurer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowCompressionConfigurer.java index c6f26573984..174639af3a7 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowCompressionConfigurer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowCompressionConfigurer.java @@ -32,6 +32,7 @@ import io.undertow.util.HttpString; import org.springframework.boot.web.server.Compression; import org.springframework.http.HttpHeaders; +import org.springframework.util.InvalidMimeTypeException; import org.springframework.util.MimeType; import org.springframework.util.MimeTypeUtils; @@ -90,11 +91,17 @@ final class UndertowCompressionConfigurer { public boolean resolve(HttpServerExchange value) { String contentType = value.getResponseHeaders().getFirst(HttpHeaders.CONTENT_TYPE); if (contentType != null) { - for (MimeType mimeType : this.mimeTypes) { - if (mimeType.isCompatibleWith(MimeTypeUtils.parseMimeType(contentType))) { - return true; + try { + MimeType parsed = MimeTypeUtils.parseMimeType(contentType); + for (MimeType mimeType : this.mimeTypes) { + if (mimeType.isCompatibleWith(parsed)) { + return true; + } } } + catch (InvalidMimeTypeException ex) { + return false; + } } return false; } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/server/AbstractReactiveWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/server/AbstractReactiveWebServerFactoryTests.java index 2f0947eb4c5..8dfbe18a5e0 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/server/AbstractReactiveWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/server/AbstractReactiveWebServerFactoryTests.java @@ -357,10 +357,12 @@ public abstract class AbstractReactiveWebServerFactoryTests { } protected void assertResponseIsCompressed(ResponseEntity response) { + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(response.getHeaders().getFirst("X-Test-Compressed")).isEqualTo("true"); } protected void assertResponseIsNotCompressed(ResponseEntity response) { + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(response.getHeaders().keySet()).doesNotContain("X-Test-Compressed"); }