ResourceUrlProvider handles sanitizes double slashes

Issue: SPR-16296
This commit is contained in:
Rossen Stoyanchev
2018-01-10 17:48:43 -05:00
parent cdf2ab9737
commit ea73ec5c41
2 changed files with 31 additions and 0 deletions

View File

@@ -34,6 +34,7 @@ import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.lang.Nullable;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
import org.springframework.web.util.UrlPathHelper;
@@ -217,6 +218,14 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
*/
@Nullable
public final String getForLookupPath(String lookupPath) {
// Clean duplicate slashes or pathWithinPattern won't match lookupPath
String previous;
do {
previous = lookupPath;
lookupPath = StringUtils.replace(lookupPath, "//", "/");
} while (!lookupPath.equals(previous));
if (logger.isTraceEnabled()) {
logger.trace("Getting resource URL for lookup path \"" + lookupPath + "\"");
}

View File

@@ -35,6 +35,9 @@ import org.springframework.web.context.support.AnnotationConfigWebApplicationCon
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
@@ -136,6 +139,25 @@ public class ResourceUrlProviderTests {
assertFalse(urlProviderBean.isAutodetect());
}
@Test // SPR-16296
public void getForLookupPathShouldNotFailIfPathContainsDoubleSlashes() {
// given
ResourceResolver mockResourceResolver = mock(ResourceResolver.class);
when(mockResourceResolver.resolveUrlPath(any(), any(), any())).thenReturn("some-path");
ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
handler.getResourceResolvers().add(mockResourceResolver);
ResourceUrlProvider provider = new ResourceUrlProvider();
provider.getHandlerMap().put("/some-pattern/**", handler);
// when
String lookupForPath = provider.getForLookupPath("/some-pattern/some-lib//some-resource");
// then
assertEquals("/some-pattern/some-path", lookupForPath);
}
@Configuration
@SuppressWarnings({"unused", "WeakerAccess"})