Skip to content

Commit b71e686

Browse files
drgnchanbclozel
authored andcommitted
Fix ServletContextResource isFile check
Prior to this commit, `ServletContextResource` could rely on `ServletContext#getRealPath` to check whether a resource exists. This behavior is not enforced on some Servlet containers, as this method is only meant to translate virtual paths to real paths, but not necessarily check for the existence of the file. See https://bz.apache.org/bugzilla/show_bug.cgi?id=55837#c3 for a rationale of this behavior in Tomcat. This commit enforces an additional check, resolving the path as a `File` and checking that is exists and is a file. Closes gh-26707
1 parent 433a23a commit b71e686

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

spring-web/src/main/java/org/springframework/web/context/support/ServletContextResource.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,15 @@ public boolean isFile() {
139139
return true;
140140
}
141141
else {
142-
return (this.servletContext.getRealPath(this.path) != null);
142+
String realPath = this.servletContext.getRealPath(this.path);
143+
if (realPath == null) {
144+
return false;
145+
}
146+
File file = new File(realPath);
147+
return (file.exists() && file.isFile());
143148
}
144149
}
145-
catch (MalformedURLException ex) {
150+
catch (IOException ex) {
146151
return false;
147152
}
148153
}

spring-web/src/test/java/org/springframework/web/context/support/ResourceTests.java

+10
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public void testServletContextResource() throws IOException {
3636
MockServletContext sc = new MockServletContext();
3737
Resource resource = new ServletContextResource(sc, "org/springframework/core/io/Resource.class");
3838
doTestResource(resource);
39+
Resource resourceNotExists = new ServletContextResource(sc, "org/springframework/core/io/ResourceNotExists.class");
40+
doTestNotExistsResource(resourceNotExists);
3941
assertThat(new ServletContextResource(sc, "org/springframework/core/../core/io/./Resource.class")).isEqualTo(resource);
4042
}
4143

@@ -48,6 +50,9 @@ public void testServletContextResourceWithRelativePath() throws IOException {
4850
}
4951

5052
private void doTestResource(Resource resource) throws IOException {
53+
assertThat(resource.getFile()).isNotNull();
54+
assertThat(resource.exists()).isTrue();
55+
assertThat(resource.isFile()).isTrue();
5156
assertThat(resource.getFilename()).isEqualTo("Resource.class");
5257
assertThat(resource.getURL().getFile().endsWith("Resource.class")).isTrue();
5358

@@ -61,4 +66,9 @@ private void doTestResource(Resource resource) throws IOException {
6166
assertThat(relative2.getURL().getFile().endsWith("ResourcePatternResolver.class")).isTrue();
6267
assertThat(relative2.exists()).isTrue();
6368
}
69+
70+
private void doTestNotExistsResource(Resource resource) throws IOException {
71+
assertThat(resource.exists()).isFalse();
72+
assertThat(resource.isFile()).isFalse();
73+
}
6474
}

0 commit comments

Comments
 (0)