|
| 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