Skip to content

Commit e2d6e70

Browse files
committed
Normalize resource URL in ResourceServlet
Issue: SPR-14946
1 parent bd7fee5 commit e2d6e70

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -273,18 +273,18 @@ private void doInclude(HttpServletRequest request, HttpServletResponse response,
273273
if (this.contentType != null) {
274274
response.setContentType(this.contentType);
275275
}
276-
277276
String[] resourceUrls = StringUtils.tokenizeToStringArray(resourceUrl, RESOURCE_URL_DELIMITERS);
278277
for (String url : resourceUrls) {
278+
String path = StringUtils.cleanPath(url);
279279
// Check whether URL matches allowed resources
280-
if (this.allowedResources != null && !this.pathMatcher.match(this.allowedResources, url)) {
281-
throw new ServletException("Resource [" + url +
280+
if (this.allowedResources != null && !this.pathMatcher.match(this.allowedResources, path)) {
281+
throw new ServletException("Resource [" + path +
282282
"] does not match allowed pattern [" + this.allowedResources + "]");
283283
}
284284
if (logger.isDebugEnabled()) {
285-
logger.debug("Including resource [" + url + "]");
285+
logger.debug("Including resource [" + path + "]");
286286
}
287-
RequestDispatcher rd = request.getRequestDispatcher(url);
287+
RequestDispatcher rd = request.getRequestDispatcher(path);
288288
rd.include(request, response);
289289
}
290290
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.web.servlet;
17+
18+
import java.io.IOException;
19+
import javax.servlet.ServletException;
20+
21+
import org.junit.Test;
22+
23+
import org.springframework.mock.web.test.MockHttpServletRequest;
24+
import org.springframework.mock.web.test.MockHttpServletResponse;
25+
import org.springframework.mock.web.test.MockServletConfig;
26+
27+
/**
28+
* @author Rossen Stoyanchev
29+
*/
30+
public class ResourceServletTests {
31+
32+
@Test(expected = ServletException.class)
33+
public void example1() throws Exception {
34+
testInvalidResourceUrl("/resources/**", "/resources/../WEB-INF/web.xml");
35+
}
36+
37+
@Test(expected = ServletException.class)
38+
public void example2() throws Exception {
39+
testInvalidResourceUrl("/resources/*", "/resources/..\\WEB-INF\\web.xml");
40+
}
41+
42+
@Test(expected = ServletException.class)
43+
public void example3() throws Exception {
44+
testInvalidResourceUrl("/resources/*", "/resources/..\\Servlet2?param=111");
45+
}
46+
47+
private void testInvalidResourceUrl(String allowedResources, String resourceParam)
48+
throws ServletException, IOException {
49+
50+
ResourceServlet servlet = new ResourceServlet();
51+
servlet.setAllowedResources(allowedResources);
52+
servlet.init(new MockServletConfig());
53+
54+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
55+
request.addParameter("resource", resourceParam);
56+
MockHttpServletResponse response = new MockHttpServletResponse();
57+
58+
servlet.service(request, response);
59+
}
60+
61+
}

0 commit comments

Comments
 (0)