Skip to content

Commit fc48f25

Browse files
authored
Keep default protected paths, return 404 (#3802)
* Keep default protected paths, warn on static-resources-list, return 404 * Review comments
1 parent 974dc95 commit fc48f25

3 files changed

Lines changed: 57 additions & 21 deletions

File tree

‎rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,11 @@ public void init(ServletConfig servletConfig) throws ServletException {
122122
super.init(servletConfig);
123123

124124
staticResourcesList = parseListSequence(servletConfig.getInitParameter(STATIC_RESOURCES_PARAMETER));
125+
// custom protected paths are added to the defaults, they never replace them
125126
String protectedPaths = servletConfig.getInitParameter(STATIC_RESOURCES_PROTECTED_PATHS_PARAMETER);
126127
staticResourcesProtectedPathList = parseListSequence(protectedPaths != null
127-
? protectedPaths : DEFAULT_STATIC_RESOURCES_PROTECTED_PATHS);
128+
? DEFAULT_STATIC_RESOURCES_PROTECTED_PATHS + " " + protectedPaths
129+
: DEFAULT_STATIC_RESOURCES_PROTECTED_PATHS);
128130
staticWelcomeFile = servletConfig.getInitParameter(STATIC_WELCOME_FILE_PARAMETER);
129131
redirectList = parseListSequence(servletConfig.getInitParameter(REDIRECTS_PARAMETER));
130132
redirectQueryCheck = Boolean.valueOf(servletConfig.getInitParameter(REDIRECT_QUERY_CHECK_PARAMETER));
@@ -376,11 +378,13 @@ protected void serveStaticContent(HttpServletRequest request,
376378
String pathInfo) throws ServletException {
377379
String path = UrlUtils.normalizePath(pathInfo);
378380
if (path == null || isProtectedPath(path)) {
379-
throw new ServletException("Static resource " + pathInfo + " is not available");
381+
sendNotFound(response);
382+
return;
380383
}
381384
try (InputStream is = getResourceAsStream(path)) {
382385
if (is == null) {
383-
throw new ServletException("Static resource " + pathInfo + " is not available");
386+
sendNotFound(response);
387+
return;
384388
}
385389
int ind = pathInfo.lastIndexOf('.');
386390
if (ind > 0) {
@@ -405,6 +409,17 @@ protected String getStaticResourceContentType(String extension) {
405409
return staticContentTypes.get(extension);
406410
}
407411

412+
/**
413+
* Answers 404 without echoing the request path, as for a resource the container does not have.
414+
*/
415+
private static void sendNotFound(HttpServletResponse response) throws ServletException {
416+
try {
417+
response.sendError(HttpServletResponse.SC_NOT_FOUND);
418+
} catch (IOException ex) {
419+
throw new ServletException(ex);
420+
}
421+
}
422+
408423
/**
409424
* The container never serves WEB-INF or META-INF directly to a client, but a RequestDispatcher
410425
* and ServletContext.getResourceAsStream can reach them, so a request path matching
@@ -425,7 +440,8 @@ protected void redirect(HttpServletRequest request, HttpServletResponse response
425440
throws ServletException {
426441
String path = UrlUtils.normalizePath(pathInfo);
427442
if (path == null || isProtectedPath(path)) {
428-
throw new ServletException("No RequestDispatcher can be created for path " + pathInfo);
443+
sendNotFound(response);
444+
return;
429445
}
430446
boolean customServletPath = dispatcherServletPath != null;
431447
String theServletPath = customServletPath ? dispatcherServletPath : "/";

‎rt/transports/http/src/test/java/org/apache/cxf/transport/servlet/AbstractHTTPServletTest.java‎

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141

4242
import static org.junit.Assert.assertEquals;
4343
import static org.junit.Assert.assertFalse;
44-
import static org.junit.Assert.assertThrows;
4544
import static org.mockito.ArgumentMatchers.any;
4645
import static org.mockito.ArgumentMatchers.anyString;
4746
import static org.mockito.ArgumentMatchers.eq;
@@ -61,8 +60,9 @@ public void testStaticResourceUnderWebInfIsNotServed() throws Exception {
6160
HttpServletResponse response = mock(HttpServletResponse.class);
6261
ByteArrayOutputStream bos = captureOutput(response);
6362

64-
assertThrows(ServletException.class, () -> servlet.doGet(mockRequest("/WEB-INF/web.xml"), response));
63+
servlet.doGet(mockRequest("/WEB-INF/web.xml"), response);
6564

65+
verify(response).sendError(HttpServletResponse.SC_NOT_FOUND);
6666
verify(sc, never()).getResourceAsStream(anyString());
6767
assertEquals("", bos.toString(StandardCharsets.UTF_8));
6868
assertFalse(servlet.invoked);
@@ -75,28 +75,46 @@ public void testStaticResourceUnderMetaInfIsNotServed() throws Exception {
7575
HttpServletResponse response = mock(HttpServletResponse.class);
7676
ByteArrayOutputStream bos = captureOutput(response);
7777

78-
assertThrows(ServletException.class,
79-
() -> servlet.doGet(mockRequest("/meta-inf/context.xml"), response));
78+
servlet.doGet(mockRequest("/meta-inf/context.xml"), response);
8079

80+
verify(response).sendError(HttpServletResponse.SC_NOT_FOUND);
8181
verify(sc, never()).getResourceAsStream(anyString());
8282
assertEquals("", bos.toString(StandardCharsets.UTF_8));
8383
}
8484

8585
@Test
86-
public void testCustomProtectedPathList() throws Exception {
86+
public void testCustomProtectedPathListKeepsDefaults() throws Exception {
8787
ServletContext sc = mockServletContext("/WEB-INF/web.xml", WEB_XML);
88+
when(sc.getResourceAsStream("/private/a.xml"))
89+
.thenReturn(new ByteArrayInputStream(WEB_XML.getBytes(StandardCharsets.UTF_8)));
8890
ServletConfig config = mockServletConfig(sc, "static-resources-list", "/.*\\.xml");
8991
when(config.getInitParameter("static-resources-protected-path-list")).thenReturn("/private/.*");
9092
TestServlet servlet = new TestServlet(sc);
9193
servlet.init(config);
9294

93-
assertThrows(ServletException.class, () -> servlet.doGet(mockRequest("/private/a.xml"),
94-
mock(HttpServletResponse.class)));
95+
for (String path : new String[] {"/private/a.xml", "/WEB-INF/web.xml"}) {
96+
HttpServletResponse response = mock(HttpServletResponse.class);
97+
ByteArrayOutputStream bos = captureOutput(response);
98+
servlet.doGet(mockRequest(path), response);
99+
verify(response).sendError(HttpServletResponse.SC_NOT_FOUND);
100+
assertEquals(path, "", bos.toString(StandardCharsets.UTF_8));
101+
}
95102

103+
verify(sc, never()).getResourceAsStream(anyString());
104+
}
105+
106+
@Test
107+
public void testMissingStaticResourceIsNotFound() throws Exception {
108+
ServletContext sc = mock(ServletContext.class);
109+
TestServlet servlet = initServlet(sc, "static-resources-list", "/.*\\.xml");
96110
HttpServletResponse response = mock(HttpServletResponse.class);
97111
ByteArrayOutputStream bos = captureOutput(response);
98-
servlet.doGet(mockRequest("/WEB-INF/web.xml"), response);
99-
assertEquals(WEB_XML, bos.toString(StandardCharsets.UTF_8));
112+
113+
servlet.doGet(mockRequest("/missing.xml"), response);
114+
115+
verify(response).sendError(HttpServletResponse.SC_NOT_FOUND);
116+
assertEquals("", bos.toString(StandardCharsets.UTF_8));
117+
assertFalse(servlet.invoked);
100118
}
101119

102120
@Test
@@ -119,9 +137,9 @@ public void testRedirectUnderWebInfIsNotForwarded() throws Exception {
119137
TestServlet servlet = initServlet(sc, "redirects-list", "/.*\\.jsp");
120138
HttpServletResponse response = mock(HttpServletResponse.class);
121139

122-
assertThrows(ServletException.class,
123-
() -> servlet.doGet(mockRequest("/WEB-INF/views/admin.jsp"), response));
140+
servlet.doGet(mockRequest("/WEB-INF/views/admin.jsp"), response);
124141

142+
verify(response).sendError(HttpServletResponse.SC_NOT_FOUND);
125143
verify(sc, never()).getRequestDispatcher(anyString());
126144
assertFalse(servlet.invoked);
127145
}
@@ -135,8 +153,9 @@ public void testRedirectToNonNormalizedWebInfIsNotForwarded() throws Exception {
135153

136154
for (String path : new String[] {"//WEB-INF/views/admin.jsp", "/./WEB-INF/views/admin.jsp",
137155
"/views/../WEB-INF/views/admin.jsp", "/WEB-INF\\views\\admin.jsp"}) {
138-
assertThrows(path, ServletException.class,
139-
() -> servlet.doGet(mockRequest(path), mock(HttpServletResponse.class)));
156+
HttpServletResponse response = mock(HttpServletResponse.class);
157+
servlet.doGet(mockRequest(path), response);
158+
verify(response).sendError(HttpServletResponse.SC_NOT_FOUND);
140159
}
141160

142161
verify(sc, never()).getRequestDispatcher(anyString());
@@ -165,8 +184,9 @@ public void testStaticResourceUnderNonNormalizedWebInfIsNotServed() throws Excep
165184
for (String path : new String[] {"//WEB-INF/web.xml", "/./WEB-INF/web.xml", "/a/../WEB-INF/web.xml"}) {
166185
HttpServletResponse response = mock(HttpServletResponse.class);
167186
ByteArrayOutputStream bos = captureOutput(response);
168-
assertThrows(path, ServletException.class, () -> servlet.doGet(mockRequest(path), response));
169-
assertEquals("", bos.toString(StandardCharsets.UTF_8));
187+
servlet.doGet(mockRequest(path), response);
188+
verify(response).sendError(HttpServletResponse.SC_NOT_FOUND);
189+
assertEquals(path, "", bos.toString(StandardCharsets.UTF_8));
170190
}
171191

172192
verify(sc, never()).getResourceAsStream(anyString());

‎systests/transports/src/test/java/org/apache/cxf/systest/servlet/CXFStaticResourcesServletTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void testGetNonExistingStaticResource() throws Exception {
7777
try (CloseableHttpClient client = newClient()) {
7878
final HttpGet method = new HttpGet(uri("/services/WEB-INF/beans.xml"));
7979
try (CloseableHttpResponse res = client.execute(method)) {
80-
assertThat(res.getStatusLine().getStatusCode(), equalTo(500));
80+
assertThat(res.getStatusLine().getStatusCode(), equalTo(404));
8181
}
8282
}
8383
}
@@ -87,7 +87,7 @@ public void testGetProtectedStaticResource() throws Exception {
8787
try (CloseableHttpClient client = newClient()) {
8888
final HttpGet method = new HttpGet(uri("/services/WEB-INF/web.xml"));
8989
try (CloseableHttpResponse res = client.execute(method)) {
90-
assertThat(res.getStatusLine().getStatusCode(), equalTo(500));
90+
assertThat(res.getStatusLine().getStatusCode(), equalTo(404));
9191
}
9292
}
9393
}

0 commit comments

Comments
 (0)