This is an automated email from the ASF dual-hosted git repository.
coheigea pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cxf.git
The following commit(s) were added to refs/heads/main by this push:
new fc48f250f55 Keep default protected paths, return 404 (#3802)
fc48f250f55 is described below
commit fc48f250f55cde9adf0b5e758bee20d2504d4903
Author: Colm O hEigeartaigh <[email protected]>
AuthorDate: Mon Nov 30 16:36:13 2026 +0000
Keep default protected paths, return 404 (#3802)
* Keep default protected paths, warn on static-resources-list, return 404
* Review comments
---
.../cxf/transport/servlet/AbstractHTTPServlet.java | 24 +++++++++--
.../transport/servlet/AbstractHTTPServletTest.java | 50 +++++++++++++++-------
.../servlet/CXFStaticResourcesServletTest.java | 4 +-
3 files changed, 57 insertions(+), 21 deletions(-)
diff --git
a/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java
b/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java
index 3d002d503ef..1b3eefaed1f 100644
---
a/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java
+++
b/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractHTTPServlet.java
@@ -122,9 +122,11 @@ public abstract class AbstractHTTPServlet extends
HttpServlet implements Filter
super.init(servletConfig);
staticResourcesList =
parseListSequence(servletConfig.getInitParameter(STATIC_RESOURCES_PARAMETER));
+ // custom protected paths are added to the defaults, they never
replace them
String protectedPaths =
servletConfig.getInitParameter(STATIC_RESOURCES_PROTECTED_PATHS_PARAMETER);
staticResourcesProtectedPathList = parseListSequence(protectedPaths !=
null
- ? protectedPaths : DEFAULT_STATIC_RESOURCES_PROTECTED_PATHS);
+ ? DEFAULT_STATIC_RESOURCES_PROTECTED_PATHS + " " + protectedPaths
+ : DEFAULT_STATIC_RESOURCES_PROTECTED_PATHS);
staticWelcomeFile =
servletConfig.getInitParameter(STATIC_WELCOME_FILE_PARAMETER);
redirectList =
parseListSequence(servletConfig.getInitParameter(REDIRECTS_PARAMETER));
redirectQueryCheck =
Boolean.valueOf(servletConfig.getInitParameter(REDIRECT_QUERY_CHECK_PARAMETER));
@@ -376,11 +378,13 @@ public abstract class AbstractHTTPServlet extends
HttpServlet implements Filter
String pathInfo) throws ServletException
{
String path = UrlUtils.normalizePath(pathInfo);
if (path == null || isProtectedPath(path)) {
- throw new ServletException("Static resource " + pathInfo + " is
not available");
+ sendNotFound(response);
+ return;
}
try (InputStream is = getResourceAsStream(path)) {
if (is == null) {
- throw new ServletException("Static resource " + pathInfo + "
is not available");
+ sendNotFound(response);
+ return;
}
int ind = pathInfo.lastIndexOf('.');
if (ind > 0) {
@@ -405,6 +409,17 @@ public abstract class AbstractHTTPServlet extends
HttpServlet implements Filter
return staticContentTypes.get(extension);
}
+ /**
+ * Answers 404 without echoing the request path, as for a resource the
container does not have.
+ */
+ private static void sendNotFound(HttpServletResponse response) throws
ServletException {
+ try {
+ response.sendError(HttpServletResponse.SC_NOT_FOUND);
+ } catch (IOException ex) {
+ throw new ServletException(ex);
+ }
+ }
+
/**
* The container never serves WEB-INF or META-INF directly to a client,
but a RequestDispatcher
* and ServletContext.getResourceAsStream can reach them, so a request
path matching
@@ -425,7 +440,8 @@ public abstract class AbstractHTTPServlet extends
HttpServlet implements Filter
throws ServletException {
String path = UrlUtils.normalizePath(pathInfo);
if (path == null || isProtectedPath(path)) {
- throw new ServletException("No RequestDispatcher can be created
for path " + pathInfo);
+ sendNotFound(response);
+ return;
}
boolean customServletPath = dispatcherServletPath != null;
String theServletPath = customServletPath ? dispatcherServletPath :
"/";
diff --git
a/rt/transports/http/src/test/java/org/apache/cxf/transport/servlet/AbstractHTTPServletTest.java
b/rt/transports/http/src/test/java/org/apache/cxf/transport/servlet/AbstractHTTPServletTest.java
index 3033335a72f..08e2ecda305 100644
---
a/rt/transports/http/src/test/java/org/apache/cxf/transport/servlet/AbstractHTTPServletTest.java
+++
b/rt/transports/http/src/test/java/org/apache/cxf/transport/servlet/AbstractHTTPServletTest.java
@@ -41,7 +41,6 @@ import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
@@ -61,8 +60,9 @@ public class AbstractHTTPServletTest {
HttpServletResponse response = mock(HttpServletResponse.class);
ByteArrayOutputStream bos = captureOutput(response);
- assertThrows(ServletException.class, () ->
servlet.doGet(mockRequest("/WEB-INF/web.xml"), response));
+ servlet.doGet(mockRequest("/WEB-INF/web.xml"), response);
+ verify(response).sendError(HttpServletResponse.SC_NOT_FOUND);
verify(sc, never()).getResourceAsStream(anyString());
assertEquals("", bos.toString(StandardCharsets.UTF_8));
assertFalse(servlet.invoked);
@@ -75,28 +75,46 @@ public class AbstractHTTPServletTest {
HttpServletResponse response = mock(HttpServletResponse.class);
ByteArrayOutputStream bos = captureOutput(response);
- assertThrows(ServletException.class,
- () -> servlet.doGet(mockRequest("/meta-inf/context.xml"),
response));
+ servlet.doGet(mockRequest("/meta-inf/context.xml"), response);
+ verify(response).sendError(HttpServletResponse.SC_NOT_FOUND);
verify(sc, never()).getResourceAsStream(anyString());
assertEquals("", bos.toString(StandardCharsets.UTF_8));
}
@Test
- public void testCustomProtectedPathList() throws Exception {
+ public void testCustomProtectedPathListKeepsDefaults() throws Exception {
ServletContext sc = mockServletContext("/WEB-INF/web.xml", WEB_XML);
+ when(sc.getResourceAsStream("/private/a.xml"))
+ .thenReturn(new
ByteArrayInputStream(WEB_XML.getBytes(StandardCharsets.UTF_8)));
ServletConfig config = mockServletConfig(sc, "static-resources-list",
"/.*\\.xml");
when(config.getInitParameter("static-resources-protected-path-list")).thenReturn("/private/.*");
TestServlet servlet = new TestServlet(sc);
servlet.init(config);
- assertThrows(ServletException.class, () ->
servlet.doGet(mockRequest("/private/a.xml"),
-
mock(HttpServletResponse.class)));
+ for (String path : new String[] {"/private/a.xml",
"/WEB-INF/web.xml"}) {
+ HttpServletResponse response = mock(HttpServletResponse.class);
+ ByteArrayOutputStream bos = captureOutput(response);
+ servlet.doGet(mockRequest(path), response);
+ verify(response).sendError(HttpServletResponse.SC_NOT_FOUND);
+ assertEquals(path, "", bos.toString(StandardCharsets.UTF_8));
+ }
+ verify(sc, never()).getResourceAsStream(anyString());
+ }
+
+ @Test
+ public void testMissingStaticResourceIsNotFound() throws Exception {
+ ServletContext sc = mock(ServletContext.class);
+ TestServlet servlet = initServlet(sc, "static-resources-list",
"/.*\\.xml");
HttpServletResponse response = mock(HttpServletResponse.class);
ByteArrayOutputStream bos = captureOutput(response);
- servlet.doGet(mockRequest("/WEB-INF/web.xml"), response);
- assertEquals(WEB_XML, bos.toString(StandardCharsets.UTF_8));
+
+ servlet.doGet(mockRequest("/missing.xml"), response);
+
+ verify(response).sendError(HttpServletResponse.SC_NOT_FOUND);
+ assertEquals("", bos.toString(StandardCharsets.UTF_8));
+ assertFalse(servlet.invoked);
}
@Test
@@ -119,9 +137,9 @@ public class AbstractHTTPServletTest {
TestServlet servlet = initServlet(sc, "redirects-list", "/.*\\.jsp");
HttpServletResponse response = mock(HttpServletResponse.class);
- assertThrows(ServletException.class,
- () -> servlet.doGet(mockRequest("/WEB-INF/views/admin.jsp"),
response));
+ servlet.doGet(mockRequest("/WEB-INF/views/admin.jsp"), response);
+ verify(response).sendError(HttpServletResponse.SC_NOT_FOUND);
verify(sc, never()).getRequestDispatcher(anyString());
assertFalse(servlet.invoked);
}
@@ -135,8 +153,9 @@ public class AbstractHTTPServletTest {
for (String path : new String[] {"//WEB-INF/views/admin.jsp",
"/./WEB-INF/views/admin.jsp",
"/views/../WEB-INF/views/admin.jsp",
"/WEB-INF\\views\\admin.jsp"}) {
- assertThrows(path, ServletException.class,
- () -> servlet.doGet(mockRequest(path),
mock(HttpServletResponse.class)));
+ HttpServletResponse response = mock(HttpServletResponse.class);
+ servlet.doGet(mockRequest(path), response);
+ verify(response).sendError(HttpServletResponse.SC_NOT_FOUND);
}
verify(sc, never()).getRequestDispatcher(anyString());
@@ -165,8 +184,9 @@ public class AbstractHTTPServletTest {
for (String path : new String[] {"//WEB-INF/web.xml",
"/./WEB-INF/web.xml", "/a/../WEB-INF/web.xml"}) {
HttpServletResponse response = mock(HttpServletResponse.class);
ByteArrayOutputStream bos = captureOutput(response);
- assertThrows(path, ServletException.class, () ->
servlet.doGet(mockRequest(path), response));
- assertEquals("", bos.toString(StandardCharsets.UTF_8));
+ servlet.doGet(mockRequest(path), response);
+ verify(response).sendError(HttpServletResponse.SC_NOT_FOUND);
+ assertEquals(path, "", bos.toString(StandardCharsets.UTF_8));
}
verify(sc, never()).getResourceAsStream(anyString());
diff --git
a/systests/transports/src/test/java/org/apache/cxf/systest/servlet/CXFStaticResourcesServletTest.java
b/systests/transports/src/test/java/org/apache/cxf/systest/servlet/CXFStaticResourcesServletTest.java
index 83659cfcd26..42a18f2a812 100644
---
a/systests/transports/src/test/java/org/apache/cxf/systest/servlet/CXFStaticResourcesServletTest.java
+++
b/systests/transports/src/test/java/org/apache/cxf/systest/servlet/CXFStaticResourcesServletTest.java
@@ -77,7 +77,7 @@ public class CXFStaticResourcesServletTest extends
AbstractServletTest {
try (CloseableHttpClient client = newClient()) {
final HttpGet method = new
HttpGet(uri("/services/WEB-INF/beans.xml"));
try (CloseableHttpResponse res = client.execute(method)) {
- assertThat(res.getStatusLine().getStatusCode(), equalTo(500));
+ assertThat(res.getStatusLine().getStatusCode(), equalTo(404));
}
}
}
@@ -87,7 +87,7 @@ public class CXFStaticResourcesServletTest extends
AbstractServletTest {
try (CloseableHttpClient client = newClient()) {
final HttpGet method = new
HttpGet(uri("/services/WEB-INF/web.xml"));
try (CloseableHttpResponse res = client.execute(method)) {
- assertThat(res.getStatusLine().getStatusCode(), equalTo(500));
+ assertThat(res.getStatusLine().getStatusCode(), equalTo(404));
}
}
}