UrlHandlerFilter should not strip slash for "/" request paths
This commit ensures that the `UrlHandlerFilter` does not handle "/" paths in general, as they should not be altered and are meaningful for web applications. Closes gh-33444
This commit is contained in:
@@ -279,7 +279,7 @@ public final class UrlHandlerFilter extends OncePerRequestFilter {
|
|||||||
@Override
|
@Override
|
||||||
public boolean canHandle(HttpServletRequest request, RequestPath path) {
|
public boolean canHandle(HttpServletRequest request, RequestPath path) {
|
||||||
List<PathContainer.Element> elements = path.elements();
|
List<PathContainer.Element> elements = path.elements();
|
||||||
return (!elements.isEmpty() && elements.get(elements.size() - 1).value().equals("/"));
|
return (elements.size() > 1 && elements.get(elements.size() - 1).value().equals("/"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -69,6 +69,23 @@ public class UrlHandlerFilterTests {
|
|||||||
assertThat(actual.getPathInfo()).isEqualTo(pathInfo);
|
assertThat(actual.getPathInfo()).isEqualTo(pathInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldNotSkipTrailingSlashForRootPath() throws Exception {
|
||||||
|
UrlHandlerFilter filter = UrlHandlerFilter.trailingSlashHandler("/**").wrapRequest().build();
|
||||||
|
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
|
||||||
|
request.setPathInfo("/");
|
||||||
|
|
||||||
|
MockFilterChain chain = new MockFilterChain();
|
||||||
|
filter.doFilterInternal(request, new MockHttpServletResponse(), chain);
|
||||||
|
|
||||||
|
HttpServletRequest actual = (HttpServletRequest) chain.getRequest();
|
||||||
|
assertThat(actual).isNotNull().isSameAs(request);
|
||||||
|
assertThat(actual.getRequestURI()).isEqualTo("/");
|
||||||
|
assertThat(actual.getRequestURL().toString()).isEqualTo("http://localhost/");
|
||||||
|
assertThat(actual.getServletPath()).isEqualTo("");
|
||||||
|
assertThat(actual.getPathInfo()).isEqualTo("/");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void noTrailingSlashWithRequestWrapping() throws Exception {
|
void noTrailingSlashWithRequestWrapping() throws Exception {
|
||||||
testNoTrailingSlashWithRequestWrapping("/path/**", "/path/123");
|
testNoTrailingSlashWithRequestWrapping("/path/**", "/path/123");
|
||||||
|
|||||||
Reference in New Issue
Block a user