From c233125f1d376bd1232d32ccd26fa7751b7ba4b1 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Thu, 18 Jan 2018 11:36:39 +0100 Subject: [PATCH] Don't render error view if response is committed This commit prevents the default error view from rendering itself if the response has been committed already. In this case, it is impossible to change the HTTP response status and write a proper response - trying to do so often results in a `IllegalStateException` since the response body has already been written to. Fixes gh-11580 --- .../error/ErrorMvcAutoConfiguration.java | 29 ++++++- .../error/ErrorMvcAutoConfigurationTests.java | 75 +++++++++++++++++++ 2 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfigurationTests.java diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfiguration.java index 128a00e068..4aaa2661ea 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -25,6 +25,9 @@ import javax.servlet.Servlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + import org.springframework.aop.framework.autoproxy.AutoProxyUtils; import org.springframework.beans.BeansException; import org.springframework.beans.factory.ObjectProvider; @@ -206,6 +209,8 @@ public class ErrorMvcAutoConfiguration { */ private static class SpelView implements View { + private static final Log logger = LogFactory.getLog(SpelView.class); + private final NonRecursivePropertyPlaceholderHelper helper; private final String template; @@ -225,16 +230,32 @@ public class ErrorMvcAutoConfiguration { @Override public void render(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception { + if (response.isCommitted()) { + String message = getMessage(model); + logger.error(message); + return; + } if (response.getContentType() == null) { response.setContentType(getContentType()); } - Map map = new HashMap<>(model); - map.put("path", request.getContextPath()); - PlaceholderResolver resolver = new ExpressionResolver(getExpressions(), map); + PlaceholderResolver resolver = new ExpressionResolver(getExpressions(), model); String result = this.helper.replacePlaceholders(this.template, resolver); response.getWriter().append(result); } + private String getMessage(Map model) { + StringBuilder builder = new StringBuilder(); + builder.append("Cannot render error page for request [") + .append(model.get("path")).append("]"); + if (model.get("message") != null) { + builder.append(" and exception [").append(model.get("message")) + .append("]"); + } + return builder.append("] as the response has already been committed.") + .append("As a result, the response may have the wrong status code.") + .toString(); + } + private Map getExpressions() { if (this.expressions == null) { synchronized (this) { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfigurationTests.java new file mode 100644 index 0000000000..b9b44cbfc7 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfigurationTests.java @@ -0,0 +1,75 @@ +/* + * Copyright 2012-2018 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.web.servlet.error; + +import org.junit.Rule; +import org.junit.Test; + +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.WebApplicationContextRunner; +import org.springframework.boot.test.rule.OutputCapture; +import org.springframework.boot.web.servlet.error.ErrorAttributes; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.web.context.request.WebRequest; +import org.springframework.web.servlet.View; +import org.springframework.web.servlet.handler.DispatcherServletWebRequest; + +import static org.hamcrest.Matchers.allOf; +import static org.hamcrest.Matchers.containsString; + +/** + * Tests for {@link ErrorMvcAutoConfiguration}. + * + * @author Brian Clozel + */ +public class ErrorMvcAutoConfigurationTests { + + private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(ErrorMvcAutoConfiguration.class)); + + @Rule + public OutputCapture outputCapture = new OutputCapture(); + + @Test + public void testDefaultViewWithResponseAlreadyCommitted() { + this.contextRunner.run((context) -> { + View errorView = context.getBean("error", View.class); + ErrorAttributes errorAttributes = context.getBean(ErrorAttributes.class); + DispatcherServletWebRequest webRequest = + createCommittedWebRequest(new IllegalStateException("Exception message")); + errorView.render(errorAttributes.getErrorAttributes(webRequest, true), + webRequest.getRequest(), webRequest.getResponse()); + this.outputCapture.expect(allOf( + containsString("Cannot render error page for request [/path]" + + " and exception [Exception message]] as the response has already been committed."), + containsString("As a result, the response may have the wrong status code."))); + }); + } + + protected DispatcherServletWebRequest createCommittedWebRequest(Exception e) { + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/path"); + MockHttpServletResponse response = new MockHttpServletResponse(); + DispatcherServletWebRequest webRequest = new DispatcherServletWebRequest(request, response); + webRequest.setAttribute("javax.servlet.error.exception", e, WebRequest.SCOPE_REQUEST); + webRequest.setAttribute("javax.servlet.error.request_uri", "/path", WebRequest.SCOPE_REQUEST); + response.setCommitted(true); + response.setOutputStreamAccessAllowed(false); + response.setWriterAccessAllowed(false); + return webRequest; + } +}