Skip to content

Commit 3135c7f

Browse files
author
Dave Syer
committed
Escape strings in whitelabel error page (HTML)
1 parent 6a503d5 commit 3135c7f

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@
5252
import org.springframework.expression.spel.support.StandardEvaluationContext;
5353
import org.springframework.util.PropertyPlaceholderHelper;
5454
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
55+
import org.springframework.web.bind.ServletRequestUtils;
5556
import org.springframework.web.servlet.DispatcherServlet;
5657
import org.springframework.web.servlet.View;
5758
import org.springframework.web.servlet.view.BeanNameViewResolver;
59+
import org.springframework.web.util.HtmlUtils;
5860

5961
/**
6062
* {@link EnableAutoConfiguration Auto-configuration} to render errors via a MVC error
@@ -173,7 +175,7 @@ public String resolvePlaceholder(String name) {
173175
Expression expression = SpelView.this.parser.parseExpression(name);
174176
try {
175177
Object value = expression.getValue(SpelView.this.context);
176-
return (value == null ? null : value.toString());
178+
return (value == null ? null : HtmlUtils.htmlEscape(value.toString()));
177179
}
178180
catch (Exception ex) {
179181
return null;

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/DefaultErrorViewIntegrationTests.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
package org.springframework.boot.autoconfigure.web;
1818

19+
import static org.junit.Assert.assertTrue;
20+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
21+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
22+
1923
import java.lang.annotation.Documented;
2024
import java.lang.annotation.ElementType;
2125
import java.lang.annotation.Retention;
@@ -41,10 +45,6 @@
4145
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
4246
import org.springframework.web.context.WebApplicationContext;
4347

44-
import static org.junit.Assert.assertTrue;
45-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
46-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
47-
4848
/**
4949
* @author Dave Syer
5050
*/
@@ -74,6 +74,22 @@ public void testErrorForBrowserClient() throws Exception {
7474
assertTrue("Wrong content: " + content, content.contains("999"));
7575
}
7676

77+
@Test
78+
public void testErrorWithEscape() throws Exception {
79+
MvcResult response = this.mockMvc
80+
.perform(
81+
get("/error").requestAttr(
82+
"javax.servlet.error.exception",
83+
new RuntimeException(
84+
"<script>alert('Hello World')</script>")).accept(
85+
MediaType.TEXT_HTML)).andExpect(status().isOk())
86+
.andReturn();
87+
String content = response.getResponse().getContentAsString();
88+
assertTrue("Wrong content: " + content, content.contains("&lt;script&gt;"));
89+
assertTrue("Wrong content: " + content, content.contains("Hello World"));
90+
assertTrue("Wrong content: " + content, content.contains("999"));
91+
}
92+
7793
@Target(ElementType.TYPE)
7894
@Retention(RetentionPolicy.RUNTIME)
7995
@Documented

0 commit comments

Comments
 (0)