|
42 | 42 | import static org.junit.Assert.assertEquals; |
43 | 43 | import static org.junit.Assert.assertFalse; |
44 | 44 | import static org.junit.Assert.assertThrows; |
| 45 | +import static org.mockito.ArgumentMatchers.any; |
45 | 46 | import static org.mockito.ArgumentMatchers.anyString; |
| 47 | +import static org.mockito.ArgumentMatchers.eq; |
46 | 48 | import static org.mockito.Mockito.mock; |
47 | 49 | import static org.mockito.Mockito.never; |
48 | 50 | import static org.mockito.Mockito.verify; |
@@ -124,6 +126,52 @@ public void testRedirectUnderWebInfIsNotForwarded() throws Exception { |
124 | 126 | assertFalse(servlet.invoked); |
125 | 127 | } |
126 | 128 |
|
| 129 | + @Test |
| 130 | + public void testRedirectToNonNormalizedWebInfIsNotForwarded() throws Exception { |
| 131 | + ServletContext sc = mock(ServletContext.class); |
| 132 | + RequestDispatcher rd = mock(RequestDispatcher.class); |
| 133 | + when(sc.getRequestDispatcher(anyString())).thenReturn(rd); |
| 134 | + TestServlet servlet = initServlet(sc, "redirects-list", "/.*\\.jsp"); |
| 135 | + |
| 136 | + for (String path : new String[] {"//WEB-INF/views/admin.jsp", "/./WEB-INF/views/admin.jsp", |
| 137 | + "/views/../WEB-INF/views/admin.jsp", "/WEB-INF\\views\\admin.jsp"}) { |
| 138 | + assertThrows(path, ServletException.class, |
| 139 | + () -> servlet.doGet(mockRequest(path), mock(HttpServletResponse.class))); |
| 140 | + } |
| 141 | + |
| 142 | + verify(sc, never()).getRequestDispatcher(anyString()); |
| 143 | + assertFalse(servlet.invoked); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void testRedirectForwardsNormalizedPath() throws Exception { |
| 148 | + ServletContext sc = mock(ServletContext.class); |
| 149 | + RequestDispatcher rd = mock(RequestDispatcher.class); |
| 150 | + when(sc.getRequestDispatcher("/views/index.jsp")).thenReturn(rd); |
| 151 | + TestServlet servlet = initServlet(sc, "redirects-list", "/.*\\.jsp"); |
| 152 | + HttpServletResponse response = mock(HttpServletResponse.class); |
| 153 | + |
| 154 | + servlet.doGet(mockRequest("/views//./index.jsp"), response); |
| 155 | + |
| 156 | + verify(rd).forward(any(HttpServletRequest.class), eq(response)); |
| 157 | + assertFalse(servlet.invoked); |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + public void testStaticResourceUnderNonNormalizedWebInfIsNotServed() throws Exception { |
| 162 | + ServletContext sc = mockServletContext("/WEB-INF/web.xml", WEB_XML); |
| 163 | + TestServlet servlet = initServlet(sc, "static-resources-list", "/.*\\.xml"); |
| 164 | + |
| 165 | + for (String path : new String[] {"//WEB-INF/web.xml", "/./WEB-INF/web.xml", "/a/../WEB-INF/web.xml"}) { |
| 166 | + HttpServletResponse response = mock(HttpServletResponse.class); |
| 167 | + ByteArrayOutputStream bos = captureOutput(response); |
| 168 | + assertThrows(path, ServletException.class, () -> servlet.doGet(mockRequest(path), response)); |
| 169 | + assertEquals("", bos.toString(StandardCharsets.UTF_8)); |
| 170 | + } |
| 171 | + |
| 172 | + verify(sc, never()).getResourceAsStream(anyString()); |
| 173 | + } |
| 174 | + |
127 | 175 | private static ServletContext mockServletContext(String path, String content) { |
128 | 176 | ServletContext sc = mock(ServletContext.class); |
129 | 177 | when(sc.getResourceAsStream(path)) |
|
0 commit comments