PathResourceResolver should not resolve root path

When resolving resources, the PathResourceResolver creates a Resource
instance and checks whether this resource `exists()` and `isReadable()`.
While that last call returns false for folders on the file system, both
calls return true for folders located inside JARs.

If a JAR location is configured as a resource location, then
PathResourceResolver can resolve folders in JARs as valid locations and
candidates for paths resolution.

Prior to this change, the PathResourceResolver would resolve "" as a
valid resource path (here, the "/META-INF/resources/webjars" if
configured, for example) and return a "" path for this resource,
effectively turning all "/" URLs into empty ones "".

This commit fixes the resolveUrlPathInternal implementation by not
allowing empty paths as valid resource paths.

Issue: SPR-13241
master
Brian Clozel 9 years ago
parent 8e479e28ce
commit 064abad9d8
  1. 2
      spring-webmvc/src/main/java/org/springframework/web/servlet/resource/PathResourceResolver.java
  2. 11
      spring-webmvc/src/test/java/org/springframework/web/servlet/resource/PathResourceResolverTests.java

@ -82,7 +82,7 @@ public class PathResourceResolver extends AbstractResourceResolver {
protected String resolveUrlPathInternal(String resourcePath, List<? extends Resource> locations, protected String resolveUrlPathInternal(String resourcePath, List<? extends Resource> locations,
ResourceResolverChain chain) { ResourceResolverChain chain) {
return (getResource(resourcePath, locations) != null ? resourcePath : null); return (StringUtils.hasText(resourcePath) && getResource(resourcePath, locations) != null ? resourcePath : null);
} }
private Resource getResource(String resourcePath, List<? extends Resource> locations) { private Resource getResource(String resourcePath, List<? extends Resource> locations) {

@ -124,4 +124,15 @@ public class PathResourceResolverTests {
assertTrue(this.resolver.checkResource(resource, resource)); assertTrue(this.resolver.checkResource(resource, resource));
} }
// SPR-13241
@Test
public void resolvePathRootResource() throws Exception {
Resource webjarsLocation = new ClassPathResource("/META-INF/resources/webjars/", PathResourceResolver.class);
Resource actual = this.resolver.resolveResource(null, "", Arrays.asList(webjarsLocation), null);
String path = this.resolver.resolveUrlPathInternal("", Arrays.asList(webjarsLocation), null);
assertNotNull(actual);
assertTrue(actual.exists() && actual.isReadable());
assertNull(path);
}
} }

Loading…
Cancel
Save