mirror of
https://github.com/spring-projects/spring-framework
synced 2026-06-08 17:33:33 +00:00
Backport clean duplicate separators in resource URLs
Issue: SPR-16616
This commit is contained in:
+61
-17
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -46,6 +46,7 @@ import org.springframework.web.accept.ContentNegotiationManagerFactoryBean;
|
||||
import org.springframework.web.servlet.HandlerMapping;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ResourceHttpRequestHandler}.
|
||||
@@ -310,41 +311,84 @@ public class ResourceHttpRequestHandlerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invalidPath() throws Exception {
|
||||
public void testInvalidPath() throws Exception {
|
||||
|
||||
// Use mock ResourceResolver: i.e. we're only testing upfront validations...
|
||||
|
||||
Resource resource = mock(Resource.class);
|
||||
when(resource.getFilename()).thenThrow(new AssertionError("Resource should not be resolved"));
|
||||
when(resource.getInputStream()).thenThrow(new AssertionError("Resource should not be resolved"));
|
||||
ResourceResolver resolver = mock(ResourceResolver.class);
|
||||
when(resolver.resolveResource(any(), any(), any(), any())).thenReturn(resource);
|
||||
|
||||
ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
|
||||
handler.setLocations(Collections.singletonList(new ClassPathResource("test/", getClass())));
|
||||
handler.setResourceResolvers(Collections.singletonList(resolver));
|
||||
handler.setServletContext(new TestServletContext());
|
||||
handler.afterPropertiesSet();
|
||||
|
||||
testInvalidPath("../testsecret/secret.txt", handler);
|
||||
testInvalidPath("test/../../testsecret/secret.txt", handler);
|
||||
testInvalidPath(":/../../testsecret/secret.txt", handler);
|
||||
|
||||
Resource location = new UrlResource(getClass().getResource("./test/"));
|
||||
this.handler.setLocations(Collections.singletonList(location));
|
||||
Resource secretResource = new UrlResource(getClass().getResource("testsecret/secret.txt"));
|
||||
String secretPath = secretResource.getURL().getPath();
|
||||
|
||||
testInvalidPath("file:" + secretPath, handler);
|
||||
testInvalidPath("/file:" + secretPath, handler);
|
||||
testInvalidPath("url:" + secretPath, handler);
|
||||
testInvalidPath("/url:" + secretPath, handler);
|
||||
testInvalidPath("/../.." + secretPath, handler);
|
||||
testInvalidPath("/%2E%2E/testsecret/secret.txt", handler);
|
||||
testInvalidPath("/%2E%2E/testsecret/secret.txt", handler);
|
||||
testInvalidPath("%2F%2F%2E%2E%2F%2F%2E%2E" + secretPath, handler);
|
||||
}
|
||||
|
||||
private void testInvalidPath(String requestPath, ResourceHttpRequestHandler handler) throws Exception {
|
||||
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, requestPath);
|
||||
this.response = new MockHttpServletResponse();
|
||||
handler.handleRequest(this.request, this.response);
|
||||
assertEquals(HttpStatus.NOT_FOUND.value(), this.response.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolvePathWithTraversal() throws Exception {
|
||||
for (HttpMethod method : HttpMethod.values()) {
|
||||
this.request = new MockHttpServletRequest("GET", "");
|
||||
this.response = new MockHttpServletResponse();
|
||||
testInvalidPath(method);
|
||||
testResolvePathWithTraversal(method);
|
||||
}
|
||||
}
|
||||
|
||||
private void testInvalidPath(HttpMethod httpMethod) throws Exception {
|
||||
private void testResolvePathWithTraversal(HttpMethod httpMethod) throws Exception {
|
||||
this.request.setMethod(httpMethod.name());
|
||||
|
||||
Resource location = new ClassPathResource("test/", getClass());
|
||||
this.handler.setLocations(Collections.singletonList(location));
|
||||
|
||||
testInvalidPath(location, "../testsecret/secret.txt");
|
||||
testInvalidPath(location, "test/../../testsecret/secret.txt");
|
||||
testInvalidPath(location, ":/../../testsecret/secret.txt");
|
||||
testResolvePathWithTraversal(location, "../testsecret/secret.txt");
|
||||
testResolvePathWithTraversal(location, "test/../../testsecret/secret.txt");
|
||||
testResolvePathWithTraversal(location, ":/../../testsecret/secret.txt");
|
||||
|
||||
location = new UrlResource(getClass().getResource("./test/"));
|
||||
this.handler.setLocations(Collections.singletonList(location));
|
||||
Resource secretResource = new UrlResource(getClass().getResource("testsecret/secret.txt"));
|
||||
String secretPath = secretResource.getURL().getPath();
|
||||
|
||||
testInvalidPath(location, "file:" + secretPath);
|
||||
testInvalidPath(location, "/file:" + secretPath);
|
||||
testInvalidPath(location, "url:" + secretPath);
|
||||
testInvalidPath(location, "/url:" + secretPath);
|
||||
testInvalidPath(location, "/" + secretPath);
|
||||
testInvalidPath(location, "////../.." + secretPath);
|
||||
testInvalidPath(location, "/%2E%2E/testsecret/secret.txt");
|
||||
testInvalidPath(location, "/ " + secretPath);
|
||||
testInvalidPath(location, "url:" + secretPath);
|
||||
testResolvePathWithTraversal(location, "file:" + secretPath);
|
||||
testResolvePathWithTraversal(location, "/file:" + secretPath);
|
||||
testResolvePathWithTraversal(location, "url:" + secretPath);
|
||||
testResolvePathWithTraversal(location, "/url:" + secretPath);
|
||||
testResolvePathWithTraversal(location, "/" + secretPath);
|
||||
testResolvePathWithTraversal(location, "////../.." + secretPath);
|
||||
testResolvePathWithTraversal(location, "/%2E%2E/testsecret/secret.txt");
|
||||
testResolvePathWithTraversal(location, "%2F%2F%2E%2E%2F%2Ftestsecret/secret.txt");
|
||||
testResolvePathWithTraversal(location, "/ " + secretPath);
|
||||
}
|
||||
|
||||
private void testInvalidPath(Resource location, String requestPath) throws Exception {
|
||||
private void testResolvePathWithTraversal(Resource location, String requestPath) throws Exception {
|
||||
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, requestPath);
|
||||
this.response = new MockHttpServletResponse();
|
||||
this.handler.handleRequest(this.request, this.response);
|
||||
|
||||
Reference in New Issue
Block a user