Skip to content

Commit f621937

Browse files
mbhavephilwebb
authored andcommitted
Skip error page security filter for non-error dispatch type
Update `ErrorPageSecurityFilter` to defensively check that the `DispatcherType` is `ERROR`. Although this check isn't necessary for regular applications, it is needed if MockMvc is being used. Fixes gh-28759
1 parent 4eed637 commit f621937

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/filter/ErrorPageSecurityFilter.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.IOException;
2020

21+
import javax.servlet.DispatcherType;
2122
import javax.servlet.FilterChain;
2223
import javax.servlet.RequestDispatcher;
2324
import javax.servlet.ServletException;
@@ -54,10 +55,12 @@ public ErrorPageSecurityFilter(ApplicationContext context) {
5455
@Override
5556
public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
5657
throws IOException, ServletException {
57-
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
58-
if (!getPrivilegeEvaluator().isAllowed(request.getRequestURI(), authentication)) {
59-
sendError(request, response);
60-
return;
58+
if (DispatcherType.ERROR.equals(request.getDispatcherType())) {
59+
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
60+
if (!getPrivilegeEvaluator().isAllowed(request.getRequestURI(), authentication)) {
61+
sendError(request, response);
62+
return;
63+
}
6164
}
6265
chain.doFilter(request, response);
6366
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/filter/ErrorPageSecurityFilterTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.web.servlet.filter;
1818

19+
import javax.servlet.DispatcherType;
1920
import javax.servlet.FilterChain;
2021
import javax.servlet.RequestDispatcher;
2122

@@ -58,6 +59,7 @@ class ErrorPageSecurityFilterTests {
5859

5960
@BeforeEach
6061
void setup() {
62+
this.request.setDispatcherType(DispatcherType.ERROR);
6163
given(this.context.getBean(WebInvocationPrivilegeEvaluator.class)).willReturn(this.privilegeEvaluator);
6264
this.securityFilter = new ErrorPageSecurityFilter(this.context);
6365
}
@@ -95,4 +97,13 @@ void whenPrivilegeEvaluatorIsNotPresentAccessIsAllowed() throws Exception {
9597
verify(this.filterChain).doFilter(this.request, this.response);
9698
}
9799

100+
@Test
101+
void ignorePrivilegeEvaluationForNonErrorDispatchType() throws Exception {
102+
this.request.setDispatcherType(DispatcherType.REQUEST);
103+
given(this.privilegeEvaluator.isAllowed(anyString(), any())).willReturn(false);
104+
this.securityFilter.doFilter(this.request, this.response, this.filterChain);
105+
verifyNoInteractions(this.privilegeEvaluator);
106+
verify(this.filterChain).doFilter(this.request, this.response);
107+
}
108+
98109
}

0 commit comments

Comments
 (0)