-
Notifications
You must be signed in to change notification settings - Fork 38.7k
Description
When a filter is configured to conditionally forward, and it is configured to handle FORWARD dispatches as well, and it prevents infinite forward loops by either extending OncePerRequestFilter
or otherwise using request attributes, this can result in infinite forward loops in WebClient
tests using MockMvcWebConnection
. Because in this case request attributes are not propagated from the original request to the forwards.
Example RequireSettingFilter
:
@Component
public class RequireSettingFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
if (System.getProperty("mysetting") == null) {
request.getRequestDispatcher("/error/missingsetting").forward(request, response);
return;
}
filterChain.doFilter(request, response);
}
}
RedirectTest
:
@WebMvcTest
public class RedirectTest {
@Autowired
WebClient webClient;
@Test
void testForwardToError() throws FailingHttpStatusCodeException, MalformedURLException, IOException {
HtmlPage page = webClient.getPage("/demo.html");
assertEquals("A demo", page.asNormalizedText());
}
}
Suggested fix: In org.springframework.test.web.servlet.htmlunit.MockMvcWebConnection.getResponse(WebRequest)
forwards are handled. Limit this to e.g. 100 forwards and afterwards throw an exception that the page is not forwarding properly. Infinite loops are very bad, because they can make the build system hang.
Note that just copying request attributes from the original request to the new one wouldn't help for the case of OncePerRequestFilter
, because it clears the attributes when exiting doFilterInternal(...)
.
Affects: 5.3.23