Skip to content

Commit

Permalink
Do not expose exception error attribute by default
Browse files Browse the repository at this point in the history
  • Loading branch information
vpavic authored and snicoll committed Apr 27, 2017
1 parent 605ea48 commit afe0c6f
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public void filterAddsTimeTaken() throws Exception {

@Test
public void filterHasError() {
this.filter.setErrorAttributes(new DefaultErrorAttributes());
this.filter.setErrorAttributes(new DefaultErrorAttributes(false));
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
MockHttpServletResponse response = new MockHttpServletResponse();
response.setStatus(500);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2017 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.
Expand All @@ -23,6 +23,7 @@
*
* @author Michael Stummvoll
* @author Stephane Nicoll
* @author Vedran Pavic
* @since 1.3.0
*/
public class ErrorProperties {
Expand All @@ -33,6 +34,11 @@ public class ErrorProperties {
@Value("${error.path:/error}")
private String path = "/error";

/**
* Set whether to include "exception" attribute.
*/
private boolean includeException;

/**
* When to include a "stacktrace" attribute.
*/
Expand All @@ -46,6 +52,14 @@ public void setPath(String path) {
this.path = path;
}

public boolean isIncludeException() {
return this.includeException;
}

public void setIncludeException(boolean includeException) {
this.includeException = includeException;
}

public IncludeStacktrace getIncludeStacktrace() {
return this.includeStacktrace;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
* @author Phillip Webb
* @author Dave Syer
* @author Stephane Nicoll
* @author Vedran Pavic
* @since 1.1.0
* @see ErrorAttributes
*/
Expand All @@ -64,6 +65,16 @@ public class DefaultErrorAttributes
private static final String ERROR_ATTRIBUTE = DefaultErrorAttributes.class.getName()
+ ".ERROR";

private boolean includeException;

/**
* Create a new {@link DefaultErrorAttributes} instance.
* @param includeException whether to include "exception" attribute
*/
public DefaultErrorAttributes(boolean includeException) {
this.includeException = includeException;
}

@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE;
Expand Down Expand Up @@ -117,7 +128,9 @@ private void addErrorDetails(Map<String, Object> errorAttributes,
while (error instanceof ServletException && error.getCause() != null) {
error = ((ServletException) error).getCause();
}
errorAttributes.put("exception", error.getClass().getName());
if (this.includeException) {
errorAttributes.put("exception", error.getClass().getName());
}
addErrorMessage(errorAttributes, error);
if (includeStackTrace) {
addStackTrace(errorAttributes, error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ public ErrorMvcAutoConfiguration(ServerProperties serverProperties,
@Bean
@ConditionalOnMissingBean(value = ErrorAttributes.class, search = SearchStrategy.CURRENT)
public DefaultErrorAttributes errorAttributes() {
return new DefaultErrorAttributes();
return new DefaultErrorAttributes(
this.serverProperties.getError().isIncludeException());
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@
* Tests for {@link DefaultErrorAttributes}.
*
* @author Phillip Webb
* @author Vedran Pavic
*/
public class DefaultErrorAttributesTests {

private DefaultErrorAttributes errorAttributes = new DefaultErrorAttributes();
private DefaultErrorAttributes errorAttributes = new DefaultErrorAttributes(false);

private MockHttpServletRequest request = new MockHttpServletRequest();

Expand Down Expand Up @@ -87,8 +88,7 @@ public void mvcError() throws Exception {
.getErrorAttributes(this.requestAttributes, false);
assertThat(this.errorAttributes.getError(this.requestAttributes)).isSameAs(ex);
assertThat(modelAndView).isNull();
assertThat(attributes.get("exception"))
.isEqualTo(RuntimeException.class.getName());
assertThat(attributes.get("exception")).isNull();
assertThat(attributes.get("message")).isEqualTo("Test");
}

Expand All @@ -99,8 +99,7 @@ public void servletError() throws Exception {
Map<String, Object> attributes = this.errorAttributes
.getErrorAttributes(this.requestAttributes, false);
assertThat(this.errorAttributes.getError(this.requestAttributes)).isSameAs(ex);
assertThat(attributes.get("exception"))
.isEqualTo(RuntimeException.class.getName());
assertThat(attributes.get("exception")).isNull();
assertThat(attributes.get("message")).isEqualTo("Test");
}

Expand All @@ -120,8 +119,7 @@ public void nullMessage() throws Exception {
this.request.setAttribute("javax.servlet.error.message", "Test");
Map<String, Object> attributes = this.errorAttributes
.getErrorAttributes(this.requestAttributes, false);
assertThat(attributes.get("exception"))
.isEqualTo(RuntimeException.class.getName());
assertThat(attributes.get("exception")).isNull();
assertThat(attributes.get("message")).isEqualTo("Test");
}

Expand All @@ -134,8 +132,7 @@ public void unwrapServletException() throws Exception {
.getErrorAttributes(this.requestAttributes, false);
assertThat(this.errorAttributes.getError(this.requestAttributes))
.isSameAs(wrapped);
assertThat(attributes.get("exception"))
.isEqualTo(RuntimeException.class.getName());
assertThat(attributes.get("exception")).isNull();
assertThat(attributes.get("message")).isEqualTo("Test");
}

Expand All @@ -146,8 +143,7 @@ public void getError() throws Exception {
Map<String, Object> attributes = this.errorAttributes
.getErrorAttributes(this.requestAttributes, false);
assertThat(this.errorAttributes.getError(this.requestAttributes)).isSameAs(error);
assertThat(attributes.get("exception"))
.isEqualTo(OutOfMemoryError.class.getName());
assertThat(attributes.get("exception")).isNull();
assertThat(attributes.get("message")).isEqualTo("Test error");
}

Expand Down Expand Up @@ -179,6 +175,18 @@ private void testBindingResult(BindingResult bindingResult, Exception ex) {
assertThat(attributes.get("errors")).isEqualTo(bindingResult.getAllErrors());
}

@Test
public void withExceptionAttribute() throws Exception {
DefaultErrorAttributes errorAttributes = new DefaultErrorAttributes(true);
RuntimeException ex = new RuntimeException("Test");
this.request.setAttribute("javax.servlet.error.exception", ex);
Map<String, Object> attributes = errorAttributes
.getErrorAttributes(this.requestAttributes, false);
assertThat(attributes.get("exception"))
.isEqualTo(RuntimeException.class.getName());
assertThat(attributes.get("message")).isEqualTo("Test");
}

@Test
public void trace() throws Exception {
RuntimeException ex = new RuntimeException("Test");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ content into your application; rather pick only the properties that you need.
server.connection-timeout= # Time in milliseconds that connectors will wait for another HTTP request before closing the connection. When not set, the connector's container-specific default will be used. Use a value of -1 to indicate no (i.e. infinite) timeout.
server.display-name=application # Display name of the application.
server.max-http-header-size=0 # Maximum size in bytes of the HTTP message header.
server.error.include-exception=false # Set whether to include "exception" attribute.
server.error.include-stacktrace=never # When to include a "stacktrace" attribute.
server.error.path=/error # Path of the error controller.
server.error.whitelabel.enabled=true # Enable the default error page displayed in browsers in case of a server error.
Expand Down

0 comments on commit afe0c6f

Please sign in to comment.