Skip to content

Possible infinite forward loop with MockMvcWebConnection #29483

@svschouw-bb

Description

@svschouw-bb

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

Metadata

Metadata

Assignees

Labels

in: testIssues in the test modulein: webIssues in web modules (web, webmvc, webflux, websocket)status: backportedAn issue that has been backported to maintenance branchestype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions