Skip to content

Commit 91b4cb9

Browse files
committed
Normalize servlet paths before the WEB-INF/META-INF check (#3801)
(cherry picked from commit 4ba009f)
1 parent fbc07f4 commit 91b4cb9

5 files changed

Lines changed: 101 additions & 16 deletions

File tree

‎core/src/main/java/org/apache/cxf/common/util/UrlUtils.java‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,33 @@ public static String pathDecode(String value) {
131131
}
132132

133133

134+
/**
135+
* Normalizes an already decoded URL path: empty and "." segments are collapsed and a
136+
* trailing '/' is kept. A path that has a ".." segment, a backslash or a NUL character
137+
* could resolve outside the location it names, so null is returned for it instead.
138+
* @param path decoded path, for example HttpServletRequest.getPathInfo()
139+
* @return the normalized path, always starting with '/', or null
140+
*/
141+
public static String normalizePath(String path) {
142+
if (path == null || path.indexOf('\\') >= 0 || path.indexOf('\0') >= 0) {
143+
return null;
144+
}
145+
StringBuilder sb = new StringBuilder(path.length());
146+
for (String segment : path.split("/")) {
147+
if (segment.isEmpty() || ".".equals(segment)) {
148+
continue;
149+
}
150+
if ("..".equals(segment)) {
151+
return null;
152+
}
153+
sb.append('/').append(segment);
154+
}
155+
if (sb.length() == 0 || path.endsWith("/")) {
156+
sb.append('/');
157+
}
158+
return sb.toString();
159+
}
160+
134161
/**
135162
* Create a map from String to String that represents the contents of the query
136163
* portion of a URL. For each x=y, x is the key and y is the value.

‎core/src/test/java/org/apache/cxf/common/util/UrlUtilsTest.java‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.junit.Test;
2323

2424
import static org.junit.Assert.assertEquals;
25+
import static org.junit.Assert.assertNull;
2526
import static org.junit.Assert.assertTrue;
2627
import static org.junit.Assert.fail;
2728

@@ -91,4 +92,18 @@ public void testUrlDecodeInvalidEscapePattern() {
9192
public void testPathDecode() {
9293
assertEquals("+++", UrlUtils.pathDecode("+%2B+"));
9394
}
94-
}
95+
96+
@Test
97+
public void testNormalizePath() {
98+
assertEquals("/", UrlUtils.normalizePath("/"));
99+
assertEquals("/", UrlUtils.normalizePath(""));
100+
assertEquals("/a/b", UrlUtils.normalizePath("//a/./b"));
101+
assertEquals("/a/b/", UrlUtils.normalizePath("/a//b//"));
102+
assertEquals("/a/b", UrlUtils.normalizePath("a/b"));
103+
assertNull(UrlUtils.normalizePath(null));
104+
assertNull(UrlUtils.normalizePath("/a/../b"));
105+
assertNull(UrlUtils.normalizePath(".."));
106+
assertNull(UrlUtils.normalizePath("/a\\b"));
107+
assertNull(UrlUtils.normalizePath("/a\0b"));
108+
}
109+
}

‎rt/rs/description-swagger-ui/src/main/java/org/apache/cxf/jaxrs/swagger/ui/SwaggerUiResourceLocator.java‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ public URL locate(String resourcePath) throws MalformedURLException {
6868

6969
/**
7070
* Checks whether the resource path could escape the Swagger UI root: whether it contains a ".."
71-
* segment or a backslash once percent-decoded. The path is decoded until it no longer changes,
72-
* as the URL handler (e.g. for file: URLs) may decode it again.
71+
* segment, a backslash or a NUL once percent-decoded (see UrlUtils.normalizePath). The path is
72+
* decoded until it no longer changes, as the URL handler (e.g. for file: URLs) may decode it again.
7373
*/
7474
private static boolean isTraversal(String resourcePath) {
7575
String decoded = resourcePath;
@@ -84,15 +84,7 @@ private static boolean isTraversal(String resourcePath) {
8484
// invalid percent-encoding
8585
return true;
8686
}
87-
if (!stable || decoded.indexOf('\\') != -1) {
88-
return true;
89-
}
90-
for (String segment : decoded.split("/")) {
91-
if ("..".equals(segment)) {
92-
return true;
93-
}
94-
}
95-
return false;
87+
return !stable || UrlUtils.normalizePath(decoded) == null;
9688
}
9789

9890
/**

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import org.apache.cxf.common.logging.LogUtils;
5555
import org.apache.cxf.common.util.PropertyUtils;
5656
import org.apache.cxf.common.util.StringUtils;
57+
import org.apache.cxf.common.util.UrlUtils;
5758
import org.apache.cxf.helpers.FileUtils;
5859
import org.apache.cxf.helpers.IOUtils;
5960
import org.apache.cxf.resource.ResourceManager;
@@ -373,10 +374,11 @@ private static boolean matchPath(boolean checkRedirect, List<Pattern> values, Ht
373374
protected void serveStaticContent(HttpServletRequest request,
374375
HttpServletResponse response,
375376
String pathInfo) throws ServletException {
376-
if (isProtectedPath(pathInfo)) {
377+
String path = UrlUtils.normalizePath(pathInfo);
378+
if (path == null || isProtectedPath(path)) {
377379
throw new ServletException("Static resource " + pathInfo + " is not available");
378380
}
379-
try (InputStream is = getResourceAsStream(pathInfo)) {
381+
try (InputStream is = getResourceAsStream(path)) {
380382
if (is == null) {
381383
throw new ServletException("Static resource " + pathInfo + " is not available");
382384
}
@@ -421,7 +423,8 @@ private boolean isProtectedPath(String path) {
421423

422424
protected void redirect(HttpServletRequest request, HttpServletResponse response, String pathInfo)
423425
throws ServletException {
424-
if (isProtectedPath(pathInfo)) {
426+
String path = UrlUtils.normalizePath(pathInfo);
427+
if (path == null || isProtectedPath(path)) {
425428
throw new ServletException("No RequestDispatcher can be created for path " + pathInfo);
426429
}
427430
boolean customServletPath = dispatcherServletPath != null;
@@ -430,7 +433,7 @@ protected void redirect(HttpServletRequest request, HttpServletResponse response
430433
ServletContext sc = super.getServletContext();
431434
RequestDispatcher rd = dispatcherServletName != null
432435
? sc.getNamedDispatcher(dispatcherServletName)
433-
: sc.getRequestDispatcher((theServletPath + pathInfo).replace("//", "/"));
436+
: sc.getRequestDispatcher((theServletPath + path).replace("//", "/"));
434437
if (rd == null) {
435438
String errorMessage = "No RequestDispatcher can be created for path " + pathInfo;
436439
if (dispatcherServletName != null) {

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@
4242
import static org.junit.Assert.assertEquals;
4343
import static org.junit.Assert.assertFalse;
4444
import static org.junit.Assert.assertThrows;
45+
import static org.mockito.ArgumentMatchers.any;
4546
import static org.mockito.ArgumentMatchers.anyString;
47+
import static org.mockito.ArgumentMatchers.eq;
4648
import static org.mockito.Mockito.mock;
4749
import static org.mockito.Mockito.never;
4850
import static org.mockito.Mockito.verify;
@@ -124,6 +126,52 @@ public void testRedirectUnderWebInfIsNotForwarded() throws Exception {
124126
assertFalse(servlet.invoked);
125127
}
126128

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+
127175
private static ServletContext mockServletContext(String path, String content) {
128176
ServletContext sc = mock(ServletContext.class);
129177
when(sc.getResourceAsStream(path))

0 commit comments

Comments
 (0)