Skip to content

Commit 7cbbd95

Browse files
committed
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
1 parent 4761515 commit 7cbbd95

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/error/DefaultErrorWebExceptionHandler.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.boot.web.reactive.error.ErrorAttributes;
3333
import org.springframework.context.ApplicationContext;
3434
import org.springframework.http.HttpStatus;
35+
import org.springframework.http.InvalidMediaTypeException;
3536
import org.springframework.http.MediaType;
3637
import org.springframework.web.reactive.function.BodyInserters;
3738
import org.springframework.web.reactive.function.server.RequestPredicate;
@@ -184,11 +185,16 @@ protected HttpStatus getHttpStatus(Map<String, Object> errorAttributes) {
184185
*/
185186
protected RequestPredicate acceptsTextHtml() {
186187
return (serverRequest) -> {
187-
List<MediaType> acceptedMediaTypes = serverRequest.headers().accept();
188-
acceptedMediaTypes.remove(MediaType.ALL);
189-
MediaType.sortBySpecificityAndQuality(acceptedMediaTypes);
190-
return acceptedMediaTypes.stream()
191-
.anyMatch(MediaType.TEXT_HTML::isCompatibleWith);
188+
try {
189+
List<MediaType> acceptedMediaTypes = serverRequest.headers().accept();
190+
acceptedMediaTypes.remove(MediaType.ALL);
191+
MediaType.sortBySpecificityAndQuality(acceptedMediaTypes);
192+
return acceptedMediaTypes.stream()
193+
.anyMatch(MediaType.TEXT_HTML::isCompatibleWith);
194+
}
195+
catch (InvalidMediaTypeException ex) {
196+
return false;
197+
}
192198
};
193199
}
194200

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/error/DefaultErrorWebExceptionHandlerIntegrationTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,16 @@ public void whitelabelDisabled() {
279279
});
280280
}
281281

282+
@Test
283+
public void invalidAcceptMediaType() {
284+
this.contextRunner.run((context) -> {
285+
WebTestClient client = WebTestClient.bindToApplicationContext(context)
286+
.build();
287+
client.get().uri("/notfound").header("Accept", "v=3.0").exchange()
288+
.expectStatus().isEqualTo(HttpStatus.NOT_FOUND);
289+
});
290+
}
291+
282292
@Configuration
283293
public static class Application {
284294

0 commit comments

Comments
 (0)