Skip to content

Commit be5d5fa

Browse files
committed
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
1 parent 69d5587 commit be5d5fa

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

spring-web/src/main/java/org/springframework/web/filter/UrlHandlerFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ protected AbstractTrailingSlashHandler(@Nullable Consumer<HttpServletRequest> in
279279
@Override
280280
public boolean canHandle(HttpServletRequest request, RequestPath path) {
281281
List<PathContainer.Element> elements = path.elements();
282-
return (!elements.isEmpty() && elements.get(elements.size() - 1).value().equals("/"));
282+
return (elements.size() > 1 && elements.get(elements.size() - 1).value().equals("/"));
283283
}
284284

285285
@Override

spring-web/src/test/java/org/springframework/web/filter/UrlHandlerFilterTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,23 @@ void testTrailingSlashWithRequestWrapping(
6969
assertThat(actual.getPathInfo()).isEqualTo(pathInfo);
7070
}
7171

72+
@Test
73+
void shouldNotSkipTrailingSlashForRootPath() throws Exception {
74+
UrlHandlerFilter filter = UrlHandlerFilter.trailingSlashHandler("/**").wrapRequest().build();
75+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
76+
request.setPathInfo("/");
77+
78+
MockFilterChain chain = new MockFilterChain();
79+
filter.doFilterInternal(request, new MockHttpServletResponse(), chain);
80+
81+
HttpServletRequest actual = (HttpServletRequest) chain.getRequest();
82+
assertThat(actual).isNotNull().isSameAs(request);
83+
assertThat(actual.getRequestURI()).isEqualTo("/");
84+
assertThat(actual.getRequestURL().toString()).isEqualTo("http://localhost/");
85+
assertThat(actual.getServletPath()).isEqualTo("");
86+
assertThat(actual.getPathInfo()).isEqualTo("/");
87+
}
88+
7289
@Test
7390
void noTrailingSlashWithRequestWrapping() throws Exception {
7491
testNoTrailingSlashWithRequestWrapping("/path/**", "/path/123");

0 commit comments

Comments
 (0)