Skip to content

Commit 064abad

Browse files
committed
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
1 parent 8e479e2 commit 064abad

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ protected Resource resolveResourceInternal(HttpServletRequest request, String re
8282
protected String resolveUrlPathInternal(String resourcePath, List<? extends Resource> locations,
8383
ResourceResolverChain chain) {
8484

85-
return (getResource(resourcePath, locations) != null ? resourcePath : null);
85+
return (StringUtils.hasText(resourcePath) && getResource(resourcePath, locations) != null ? resourcePath : null);
8686
}
8787

8888
private Resource getResource(String resourcePath, List<? extends Resource> locations) {

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,15 @@ public void checkFileLocation() throws Exception {
124124
assertTrue(this.resolver.checkResource(resource, resource));
125125
}
126126

127+
// SPR-13241
128+
@Test
129+
public void resolvePathRootResource() throws Exception {
130+
Resource webjarsLocation = new ClassPathResource("/META-INF/resources/webjars/", PathResourceResolver.class);
131+
Resource actual = this.resolver.resolveResource(null, "", Arrays.asList(webjarsLocation), null);
132+
String path = this.resolver.resolveUrlPathInternal("", Arrays.asList(webjarsLocation), null);
133+
134+
assertNotNull(actual);
135+
assertTrue(actual.exists() && actual.isReadable());
136+
assertNull(path);
137+
}
127138
}

0 commit comments

Comments
 (0)