|
| 1 | +diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java |
| 2 | +index e36c08c..acb9459 100644 |
| 3 | +--- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java |
| 4 | ++++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java |
| 5 | +@@ -36,6 +36,7 @@ import org.springframework.http.MediaType; |
| 6 | + import org.springframework.util.Assert; |
| 7 | + import org.springframework.util.ClassUtils; |
| 8 | + import org.springframework.util.CollectionUtils; |
| 9 | ++import org.springframework.util.ResourceUtils; |
| 10 | + import org.springframework.util.StreamUtils; |
| 11 | + import org.springframework.util.StringUtils; |
| 12 | + import org.springframework.web.HttpRequestHandler; |
| 13 | +@@ -237,14 +238,29 @@ public class ResourceHttpRequestHandler extends WebContentGenerator implements H |
| 14 | + } |
| 15 | + |
| 16 | + /** |
| 17 | +- * Validates the given path: returns {@code true} if the given path is not a valid resource path. |
| 18 | +- * <p>The default implementation rejects paths containing "WEB-INF" or "META-INF" as well as paths |
| 19 | +- * with relative paths ("../") that result in access of a parent directory. |
| 20 | ++ * Identifies invalid resource paths. By default rejects: |
| 21 | ++ * <ul> |
| 22 | ++ * <li>Paths that contain "WEB-INF" or "META-INF". |
| 23 | ++ * <li>Paths that contain "../" after {@link org.springframework.util.StringUtils#cleanPath}. |
| 24 | ++ * <li>Paths identified as a URL via {@link org.springframework.util.ResourceUtils#isUrl}. |
| 25 | ++ * </ul> |
| 26 | + * @param path the path to validate |
| 27 | +- * @return {@code true} if the path has been recognized as invalid, {@code false} otherwise |
| 28 | ++ * @return {@code true} if the path is invalid, {@code false} otherwise |
| 29 | + */ |
| 30 | + protected boolean isInvalidPath(String path) { |
| 31 | +- return (path.contains("WEB-INF") || path.contains("META-INF") || StringUtils.cleanPath(path).startsWith("..")); |
| 32 | ++ if (path.contains("WEB-INF") || path.contains("META-INF")) { |
| 33 | ++ return true; |
| 34 | ++ } |
| 35 | ++ if (path.contains("../")) { |
| 36 | ++ path = StringUtils.cleanPath(path); |
| 37 | ++ if (path.contains("../")) { |
| 38 | ++ return true; |
| 39 | ++ } |
| 40 | ++ } |
| 41 | ++ if (path.contains(":") && ResourceUtils.isUrl(path)) { |
| 42 | ++ return true; |
| 43 | ++ } |
| 44 | ++ return false; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | +diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java |
| 49 | +index 07268a3..69ca52f 100644 |
| 50 | +--- a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java |
| 51 | ++++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java |
| 52 | +@@ -16,25 +16,26 @@ |
| 53 | + |
| 54 | + package org.springframework.web.servlet.resource; |
| 55 | + |
| 56 | ++import static org.junit.Assert.assertEquals; |
| 57 | ++import static org.junit.Assert.assertTrue; |
| 58 | ++ |
| 59 | + import java.io.IOException; |
| 60 | + import java.util.ArrayList; |
| 61 | +-import java.util.Arrays; |
| 62 | + import java.util.List; |
| 63 | ++ |
| 64 | + import javax.servlet.http.HttpServletResponse; |
| 65 | + |
| 66 | + import org.junit.Before; |
| 67 | + import org.junit.Test; |
| 68 | +- |
| 69 | + import org.springframework.core.io.ClassPathResource; |
| 70 | + import org.springframework.core.io.Resource; |
| 71 | ++import org.springframework.core.io.UrlResource; |
| 72 | + import org.springframework.mock.web.test.MockHttpServletRequest; |
| 73 | + import org.springframework.mock.web.test.MockHttpServletResponse; |
| 74 | + import org.springframework.mock.web.test.MockServletContext; |
| 75 | + import org.springframework.web.HttpRequestMethodNotSupportedException; |
| 76 | + import org.springframework.web.servlet.HandlerMapping; |
| 77 | + |
| 78 | +-import static org.junit.Assert.*; |
| 79 | +- |
| 80 | + /** |
| 81 | + * Unit tests for ResourceHttpRequestHandler. |
| 82 | + * |
| 83 | +@@ -126,7 +127,7 @@ public class ResourceHttpRequestHandlerTests { |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | +- public void getResourceViaDirectoryTraversal() throws Exception { |
| 88 | ++ public void directoryTraversalWithRelativePath() throws Exception { |
| 89 | + this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "../testsecret/secret.txt"); |
| 90 | + this.handler.handleRequest(this.request, this.response); |
| 91 | + assertEquals(404, this.response.getStatus()); |
| 92 | +@@ -136,13 +137,40 @@ public class ResourceHttpRequestHandlerTests { |
| 93 | + this.handler.handleRequest(this.request, this.response); |
| 94 | + assertEquals(404, this.response.getStatus()); |
| 95 | + |
| 96 | +- this.handler.setLocations(Arrays.<Resource>asList(new ClassPathResource("testsecret/", getClass()))); |
| 97 | +- this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "secret.txt"); |
| 98 | ++ // SPR-12354 for ":/../" |
| 99 | ++ |
| 100 | ++ this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, ":/../../testsecret/secret.txt"); |
| 101 | ++ this.response = new MockHttpServletResponse(); |
| 102 | ++ this.handler.handleRequest(this.request, this.response); |
| 103 | ++ assertEquals(404, this.response.getStatus()); |
| 104 | ++ |
| 105 | ++ // Also "prove" secret.txt does exist |
| 106 | ++ Resource location = this.handler.getLocations().get(0); |
| 107 | ++ assertTrue(location.createRelative("../testsecret/secret.txt").exists()); |
| 108 | ++ } |
| 109 | ++ |
| 110 | ++ // SPR-12354 for "file:/" |
| 111 | ++ |
| 112 | ++ @Test |
| 113 | ++ public void directoryTraversalWithUrl() throws Exception { |
| 114 | ++ |
| 115 | ++ // Add any file based location |
| 116 | ++ this.handler.getLocations().add(new UrlResource(getClass().getResource("."))); |
| 117 | ++ |
| 118 | ++ Resource resource = new UrlResource(getClass().getResource("testsecret/secret.txt")); |
| 119 | ++ this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, resource.getURL().toString()); |
| 120 | ++ this.handler.handleRequest(this.request, this.response); |
| 121 | ++ assertEquals(404, this.response.getStatus()); |
| 122 | ++ |
| 123 | ++ resource = new UrlResource(getClass().getResource("testsecret/secret.txt")); |
| 124 | ++ this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "../" + resource.getURL().toString()); |
| 125 | + this.response = new MockHttpServletResponse(); |
| 126 | + this.handler.handleRequest(this.request, this.response); |
| 127 | +- assertEquals(200, this.response.getStatus()); |
| 128 | +- assertEquals("text/plain", this.response.getContentType()); |
| 129 | +- assertEquals("big secret", this.response.getContentAsString()); |
| 130 | ++ assertEquals(404, this.response.getStatus()); |
| 131 | ++ |
| 132 | ++ // Also "prove" secret.txt does exist |
| 133 | ++ Resource location = this.handler.getLocations().get(0); |
| 134 | ++ assertTrue(location.createRelative("../testsecret/secret.txt").exists()); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
0 commit comments