From 7cbbd95fc9a4578a5a60725febd680dc33931998 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Wed, 6 Jun 2018 14:02:04 +0200 Subject: [PATCH] Ignore invalid Accept headers in WebFlux error handling Prior to this commit, the `DefaultErrorWebExceptionHandler` would parse the HTTP "Accept" headers when routing the request to the error handler; if an error occured during parsing, an `InvalidMediaTypeException` would be thrown and break the error handling for this request. This commit ignores those exceptions and makes sure that the error handling function does not override the response status or the error itself with those exceptions. Closes: gh-13372 --- .../error/DefaultErrorWebExceptionHandler.java | 16 +++++++++++----- ...ErrorWebExceptionHandlerIntegrationTests.java | 10 ++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/error/DefaultErrorWebExceptionHandler.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/error/DefaultErrorWebExceptionHandler.java index 8a59cb0e4c1..4c5b4f141e9 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/error/DefaultErrorWebExceptionHandler.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/error/DefaultErrorWebExceptionHandler.java @@ -32,6 +32,7 @@ import org.springframework.boot.autoconfigure.web.ResourceProperties; import org.springframework.boot.web.reactive.error.ErrorAttributes; import org.springframework.context.ApplicationContext; import org.springframework.http.HttpStatus; +import org.springframework.http.InvalidMediaTypeException; import org.springframework.http.MediaType; import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.server.RequestPredicate; @@ -184,11 +185,16 @@ public class DefaultErrorWebExceptionHandler extends AbstractErrorWebExceptionHa */ protected RequestPredicate acceptsTextHtml() { return (serverRequest) -> { - List acceptedMediaTypes = serverRequest.headers().accept(); - acceptedMediaTypes.remove(MediaType.ALL); - MediaType.sortBySpecificityAndQuality(acceptedMediaTypes); - return acceptedMediaTypes.stream() - .anyMatch(MediaType.TEXT_HTML::isCompatibleWith); + try { + List acceptedMediaTypes = serverRequest.headers().accept(); + acceptedMediaTypes.remove(MediaType.ALL); + MediaType.sortBySpecificityAndQuality(acceptedMediaTypes); + return acceptedMediaTypes.stream() + .anyMatch(MediaType.TEXT_HTML::isCompatibleWith); + } + catch (InvalidMediaTypeException ex) { + return false; + } }; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/error/DefaultErrorWebExceptionHandlerIntegrationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/error/DefaultErrorWebExceptionHandlerIntegrationTests.java index 97f7da1e95b..1cd64935c18 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/error/DefaultErrorWebExceptionHandlerIntegrationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/error/DefaultErrorWebExceptionHandlerIntegrationTests.java @@ -279,6 +279,16 @@ public class DefaultErrorWebExceptionHandlerIntegrationTests { }); } + @Test + public void invalidAcceptMediaType() { + this.contextRunner.run((context) -> { + WebTestClient client = WebTestClient.bindToApplicationContext(context) + .build(); + client.get().uri("/notfound").header("Accept", "v=3.0").exchange() + .expectStatus().isEqualTo(HttpStatus.NOT_FOUND); + }); + } + @Configuration public static class Application {