Skip to content

Commit

Permalink
Validate Resource type in ResourceHttpRequestHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Jan 11, 2021
1 parent e5ab67b commit fceceb4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.support.ServletContextResource;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.servlet.HandlerMapping;
Expand Down Expand Up @@ -445,6 +446,12 @@ private void resolveResourceLocations() {
location = location.substring(endIndex + 1);
}
Resource resource = applicationContext.getResource(location);
if (location.equals("/") && !(resource instanceof ServletContextResource)) {
throw new IllegalStateException(
"The String-based location \"/\" should be relative to the web application root " +
"but resolved to a Resource of type: " + resource.getClass() + ". " +
"If this is intentional, please pass it as a pre-configured Resource via setLocations.");
}
this.locationsToUse.add(resource);
if (charset != null) {
if (!(resource instanceof UrlResource)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.junit.jupiter.api.extension.ExtendWith;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpMethod;
Expand All @@ -38,6 +39,7 @@
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.accept.ContentNegotiationManagerFactoryBean;
import org.springframework.web.context.support.StaticWebApplicationContext;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
Expand Down Expand Up @@ -723,6 +725,25 @@ public void ignoreLastModified() throws Exception {
assertThat(this.response.getContentAsString()).isEqualTo("h1 { color:red; }");
}

@Test
public void servletContextRootValidation() {
StaticWebApplicationContext context = new StaticWebApplicationContext() {
@Override
public Resource getResource(String location) {
return new FileSystemResource("/");
}
};

ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
handler.setLocationValues(Collections.singletonList("/"));
handler.setApplicationContext(context);

assertThatIllegalStateException().isThrownBy(handler::afterPropertiesSet)
.withMessage("The String-based location \"/\" should be relative to the web application root but " +
"resolved to a Resource of type: class org.springframework.core.io.FileSystemResource. " +
"If this is intentional, please pass it as a pre-configured Resource via setLocations.");
}


private long resourceLastModified(String resourceName) throws IOException {
return new ClassPathResource(resourceName, getClass()).getFile().lastModified();
Expand Down

0 comments on commit fceceb4

Please sign in to comment.