Skip to content

Commit

Permalink
Escape strings in whitelabel error page (HTML)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Syer committed Oct 9, 2014
1 parent 6a503d5 commit 3135c7f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
Expand Up @@ -52,9 +52,11 @@
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.util.PropertyPlaceholderHelper;
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.BeanNameViewResolver;
import org.springframework.web.util.HtmlUtils;

/**
* {@link EnableAutoConfiguration Auto-configuration} to render errors via a MVC error
Expand Down Expand Up @@ -173,7 +175,7 @@ public String resolvePlaceholder(String name) {
Expression expression = SpelView.this.parser.parseExpression(name);
try {
Object value = expression.getValue(SpelView.this.context);
return (value == null ? null : value.toString());
return (value == null ? null : HtmlUtils.htmlEscape(value.toString()));
}
catch (Exception ex) {
return null;
Expand Down
Expand Up @@ -16,6 +16,10 @@

package org.springframework.boot.autoconfigure.web;

import static org.junit.Assert.assertTrue;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand All @@ -41,10 +45,6 @@
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.junit.Assert.assertTrue;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
* @author Dave Syer
*/
Expand Down Expand Up @@ -74,6 +74,22 @@ public void testErrorForBrowserClient() throws Exception {
assertTrue("Wrong content: " + content, content.contains("999"));
}

@Test
public void testErrorWithEscape() throws Exception {
MvcResult response = this.mockMvc
.perform(
get("/error").requestAttr(
"javax.servlet.error.exception",
new RuntimeException(
"<script>alert('Hello World')</script>")).accept(
MediaType.TEXT_HTML)).andExpect(status().isOk())
.andReturn();
String content = response.getResponse().getContentAsString();
assertTrue("Wrong content: " + content, content.contains("&lt;script&gt;"));
assertTrue("Wrong content: " + content, content.contains("Hello World"));
assertTrue("Wrong content: " + content, content.contains("999"));
}

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
Expand Down

0 comments on commit 3135c7f

Please sign in to comment.