Skip to content

Commit ea73ec5

Browse files
committed
ResourceUrlProvider handles sanitizes double slashes
Issue: SPR-16296
1 parent cdf2ab9 commit ea73ec5

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceUrlProvider.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.springframework.lang.Nullable;
3535
import org.springframework.util.AntPathMatcher;
3636
import org.springframework.util.PathMatcher;
37+
import org.springframework.util.StringUtils;
3738
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
3839
import org.springframework.web.util.UrlPathHelper;
3940

@@ -217,6 +218,14 @@ private int getEndPathIndex(String lookupPath) {
217218
*/
218219
@Nullable
219220
public final String getForLookupPath(String lookupPath) {
221+
222+
// Clean duplicate slashes or pathWithinPattern won't match lookupPath
223+
String previous;
224+
do {
225+
previous = lookupPath;
226+
lookupPath = StringUtils.replace(lookupPath, "//", "/");
227+
} while (!lookupPath.equals(previous));
228+
220229
if (logger.isTraceEnabled()) {
221230
logger.trace("Getting resource URL for lookup path \"" + lookupPath + "\"");
222231
}

spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceUrlProviderTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
3636

3737
import static org.junit.Assert.*;
38+
import static org.mockito.ArgumentMatchers.any;
39+
import static org.mockito.Mockito.mock;
40+
import static org.mockito.Mockito.when;
3841

3942

4043
/**
@@ -136,6 +139,25 @@ public void initializeOnce() throws Exception {
136139
assertFalse(urlProviderBean.isAutodetect());
137140
}
138141

142+
@Test // SPR-16296
143+
public void getForLookupPathShouldNotFailIfPathContainsDoubleSlashes() {
144+
// given
145+
ResourceResolver mockResourceResolver = mock(ResourceResolver.class);
146+
when(mockResourceResolver.resolveUrlPath(any(), any(), any())).thenReturn("some-path");
147+
148+
ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
149+
handler.getResourceResolvers().add(mockResourceResolver);
150+
151+
ResourceUrlProvider provider = new ResourceUrlProvider();
152+
provider.getHandlerMap().put("/some-pattern/**", handler);
153+
154+
// when
155+
String lookupForPath = provider.getForLookupPath("/some-pattern/some-lib//some-resource");
156+
157+
// then
158+
assertEquals("/some-pattern/some-path", lookupForPath);
159+
}
160+
139161

140162
@Configuration
141163
@SuppressWarnings({"unused", "WeakerAccess"})

0 commit comments

Comments
 (0)