Skip to content

Commit b1ffa65

Browse files
committed
Polish contribution
See gh-50205
1 parent f273f5e commit b1ffa65

2 files changed

Lines changed: 22 additions & 13 deletions

File tree

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,9 @@ void escapeHtmlInDefaultErrorView() {
461461
}
462462

463463
@Test
464-
void escapeHtmlInTimestampAndRequestIdAttributes() {
464+
void escapeHtmlInErrorAttributes() {
465465
this.contextRunner.withPropertyValues("spring.mustache.prefix=classpath:/unknown/")
466-
.withUserConfiguration(CustomErrorAttributesWithHtmlInTimestampAndRequestId.class)
466+
.withUserConfiguration(CustomErrorAttributesWithEscaping.class)
467467
.run((context) -> {
468468
WebTestClient client = getWebClient(context);
469469
String body = client.get()
@@ -477,7 +477,9 @@ void escapeHtmlInTimestampAndRequestIdAttributes() {
477477
.expectBody(String.class)
478478
.returnResult()
479479
.getResponseBody();
480-
assertThat(body).doesNotContain("<script>").contains("&lt;script&gt;");
480+
assertThat(body).doesNotContain("<script>")
481+
.contains("&lt;script&gt;")
482+
.contains("xss-error", "xss-message", "xss-requestId", "xss-timestamp", "xss-trace");
481483
});
482484
}
483485

@@ -803,19 +805,20 @@ public Map<String, Object> getErrorAttributes(ServerRequest request, ErrorAttrib
803805
}
804806

805807
@Configuration(proxyBeanMethods = false)
806-
static class CustomErrorAttributesWithHtmlInTimestampAndRequestId {
808+
static class CustomErrorAttributesWithEscaping {
807809

808810
@Bean
809811
ErrorAttributes errorAttributes() {
810812
return new DefaultErrorAttributes() {
811813

812814
@Override
813-
public Map<String, Object> getErrorAttributes(ServerRequest request,
814-
ErrorAttributeOptions options) {
815-
Map<String, Object> attributes = new LinkedHashMap<>(
816-
super.getErrorAttributes(request, options));
817-
attributes.put("timestamp", "<script>alert('xss')</script>");
818-
attributes.put("requestId", "<script>alert('xss')</script>");
815+
public Map<String, Object> getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) {
816+
Map<String, Object> attributes = new LinkedHashMap<>(super.getErrorAttributes(request, options));
817+
attributes.put("error", "<script>alert('xss-error')</script>");
818+
attributes.put("message", "<script>alert('xss-message')</script>");
819+
attributes.put("requestId", "<script>alert('xss-requestId')</script>");
820+
attributes.put("timestamp", "<script>alert('xss-timestamp')</script>");
821+
attributes.put("trace", "<script>alert('xss-trace')</script>");
819822
return attributes;
820823
}
821824

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfigurationTests.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,25 @@ void renderCanUseJavaTimeTypeAsTimestamp() { // gh-23256
8686
}
8787

8888
@Test
89-
void renderEscapesHtmlInTimestampAttribute() {
89+
void renderEscapesHtmlInErrorAttributes() {
9090
this.contextRunner.run((context) -> {
9191
View errorView = context.getBean("error", View.class);
9292
ErrorAttributes errorAttributes = context.getBean(ErrorAttributes.class);
9393
DispatcherServletWebRequest webRequest = createWebRequest(new IllegalStateException("Exception message"),
9494
false);
9595
Map<String, Object> attributes = errorAttributes.getErrorAttributes(webRequest, withAllOptions());
96-
attributes.put("timestamp", "<script>alert('xss')</script>");
96+
attributes.put("error", "<script>alert('xss-error')</script>");
97+
attributes.put("message", "<script>alert('xss-message')</script>");
98+
attributes.put("timestamp", "<script>alert('xss-timestamp')</script>");
99+
attributes.put("trace", "<script>alert('xss-trace')</script>");
97100
HttpServletResponse response = webRequest.getResponse();
98101
assertThat(response).isNotNull();
99102
errorView.render(attributes, webRequest.getRequest(), response);
100103
String responseString = ((MockHttpServletResponse) response).getContentAsString();
101-
assertThat(responseString).contains("&lt;script&gt;alert(&#39;xss&#39;)&lt;/script&gt;")
104+
assertThat(responseString).contains("&lt;script&gt;alert(&#39;xss-error&#39;)&lt;/script&gt;")
105+
.contains("&lt;script&gt;alert(&#39;xss-message&#39;)&lt;/script&gt;")
106+
.contains("&lt;script&gt;alert(&#39;xss-timestamp&#39;)&lt;/script&gt;")
107+
.contains("&lt;script&gt;alert(&#39;xss-trace&#39;)&lt;/script&gt;")
102108
.doesNotContain("<script>");
103109
});
104110
}

0 commit comments

Comments
 (0)