CVE-2023-24815: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal') (CWE-22)
value: Vert.x-Web is a set of building blocks for building web applications in the java programming language. When running vertx web applications that serve files using `StaticHandler` on Windows Operating Systems and Windows File Systems, if the mount point is a wildcard (`*`) then an attacker can exfiltrate any class path resource. When computing the relative path to locate the resource, in case of wildcards, the code: `return "/" + rest;` from `Utils.java` returns the user input (without validation) as the segment to lookup. Even though checks are performed to avoid escaping the sandbox, given that the input was not sanitized `\` are not properly handled and an attacker can build a path that is valid within the classpath. This issue only affects users deploying in windows environments and upgrading is the advised remediation path. There are no known workarounds for this vulnerability.
CWE description: The product uses external input to construct a pathname that is intended to identify a file or directory that is located underneath a restricted parent directory, but the product does not properly neutralize special elements within the pathname that can cause the pathname to resolve to a location that is outside of the restricted directory.
CVSS v3 base score: 5.3
Published: 2023-02-09T18:15Z
Repository: https://github.com/vert-x3/vertx-web
Language: Java
Files changed: vertx-web/src/main/java/io/vertx/ext/web/impl/Utils.java, vertx-web/src/test/java/io/vertx/ext/web/handler/StaticHandlerWindowsTest.java
Commit date: 2023-02-09 10:35:27 +0100
Commit message: Fix the webroot escape to classpath on windows

Signed-off-by: Paulo Lopes <pmlopes@gmail.com>

Diff: diff --git a/vertx-web/src/main/java/io/vertx/ext/web/impl/Utils.java b/vertx-web/src/main/java/io/vertx/ext/web/impl/Utils.java
index 38818b960..2e9823bcd 100644
--- a/vertx-web/src/main/java/io/vertx/ext/web/impl/Utils.java
+++ b/vertx-web/src/main/java/io/vertx/ext/web/impl/Utils.java
@@ -18,10 +18,11 @@ package io.vertx.ext.web.impl;
 
 import io.vertx.core.MultiMap;
 import io.vertx.core.http.HttpHeaders;
 import io.vertx.core.http.HttpServerRequest;
 import io.vertx.core.http.HttpServerResponse;
+import io.vertx.core.http.impl.HttpUtils;
 import io.vertx.ext.web.Route;
 import io.vertx.ext.web.RoutingContext;
 
 import java.time.*;
 import java.time.format.DateTimeFormatter;
@@ -71,14 +72,16 @@ public class Utils {
     if (route == null) {
       return path;
     }
 
     if (!route.isExactPath()) {
-      final String rest = context.pathParam("*");
+      String rest = context.pathParam("*");
       if (rest != null) {
         // normalize
         if (rest.length() > 0) {
+          // remove any attempt to escape the web root and use UNIX style path separators
+          rest = HttpUtils.removeDots(rest.replace('\\', '/'));
           if (rest.charAt(0) == '/') {
             return rest;
           } else {
             return "/" + rest;
           }
diff --git a/vertx-web/src/test/java/io/vertx/ext/web/handler/StaticHandlerWindowsTest.java b/vertx-web/src/test/java/io/vertx/ext/web/handler/StaticHandlerWindowsTest.java
new file mode 100644
index 000000000..ce39c12ba
--- /dev/null
+++ b/vertx-web/src/test/java/io/vertx/ext/web/handler/StaticHandlerWindowsTest.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2014 Red Hat, Inc.
+ *
+ *  All rights reserved. This program and the accompanying materials
+ *  are made available under the terms of the Eclipse Public License v1.0
+ *  and Apache License v2.0 which accompanies this distribution.
+ *
+ *  The Eclipse Public License is available at
+ *  http://www.eclipse.org/legal/epl-v10.html
+ *
+ *  The Apache License v2.0 is available at
+ *  http://www.opensource.org/licenses/apache2.0.php
+ *
+ *  You may elect to redistribute this code under either of these licenses.
+ */
+
+package io.vertx.ext.web.handler;
+
+import io.vertx.core.http.HttpMethod;
+import io.vertx.ext.web.WebTestBase;
+import org.junit.Test;
+
+public class StaticHandlerWindowsTest extends WebTestBase {
+
+  @Test
+  public void testEscapeToClasspathFromWildcard() throws Exception {
+    router.clear();
+    router.route("/*").handler(StaticHandler.create("www"));
+    // attempt to escape to classpath, given that the handler is mounted on a wildcard,
+    // reading the wildcard must return a sanitized path and therefore not allow to escape.
+    testRequest(HttpMethod.GET, "/..\\.htdigest", 404, "Not Found");
+  }
+
+  @Test
+  public void testEscapeToClasspathFromNull() throws Exception {
+    router.clear();
+    router.route().handler(StaticHandler.create("www"));
+    // attempt to escape to classpath, given that the handler is mounted on a catch all path
+    testRequest(HttpMethod.GET, "/..\\.htdigest", 404, "Not Found");
+  }
+
+  @Test
+  public void testEscapeToClasspathFromRegEx() throws Exception {
+    router.clear();
+    router.routeWithRegex(".*").handler(StaticHandler.create("www"));
+    // attempt to escape to classpath, given that the handler is mounted on a regex,
+    testRequest(HttpMethod.GET, "/..\\.htdigest", 404, "Not Found");
+  }
+
+  @Test
+  public void testEscapeToClasspathFromFixedPath() throws Exception {
+    router.clear();
+    router.routeWithRegex("/").handler(StaticHandler.create("www"));
+    // attempt to escape to classpath, given that the handler is mounted on a regex,
+    testRequest(HttpMethod.GET, "/..\\.htdigest", 404, "Not Found");
+  }
+}

Vulnerable code:       final String rest = context.pathParam("*");
Fixed code: import io.vertx.core.http.impl.HttpUtils;
      String rest = context.pathParam("*");
          // remove any attempt to escape the web root and use UNIX style path separators
          rest = HttpUtils.removeDots(rest.replace('\\', '/'));
/*
 * Copyright 2014 Red Hat, Inc.
 *
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  and Apache License v2.0 which accompanies this distribution.
 *
 *  The Eclipse Public License is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 *  The Apache License v2.0 is available at
 *  http://www.opensource.org/licenses/apache2.0.php
 *
 *  You may elect to redistribute this code under either of these licenses.
 */

package io.vertx.ext.web.handler;

import io.vertx.core.http.HttpMethod;
import io.vertx.ext.web.WebTestBase;
import org.junit.Test;

public class StaticHandlerWindowsTest extends WebTestBase {

  @Test
  public void testEscapeToClasspathFromWildcard() throws Exception {
    router.clear();
    router.route("/*").handler(StaticHandler.create("www"));
    // attempt to escape to classpath, given that the handler is mounted on a wildcard,
    // reading the wildcard must return a sanitized path and therefore not allow to escape.
    testRequest(HttpMethod.GET, "/..\\.htdigest", 404, "Not Found");
  }

  @Test
  public void testEscapeToClasspathFromNull() throws Exception {
    router.clear();
    router.route().handler(StaticHandler.create("www"));
    // attempt to escape to classpath, given that the handler is mounted on a catch all path
    testRequest(HttpMethod.GET, "/..\\.htdigest", 404, "Not Found");
  }

  @Test
  public void testEscapeToClasspathFromRegEx() throws Exception {
    router.clear();
    router.routeWithRegex(".*").handler(StaticHandler.create("www"));
    // attempt to escape to classpath, given that the handler is mounted on a regex,
    testRequest(HttpMethod.GET, "/..\\.htdigest", 404, "Not Found");
  }

  @Test
  public void testEscapeToClasspathFromFixedPath() throws Exception {
    router.clear();
    router.routeWithRegex("/").handler(StaticHandler.create("www"));
    // attempt to escape to classpath, given that the handler is mounted on a regex,
    testRequest(HttpMethod.GET, "/..\\.htdigest", 404, "Not Found");
  }
}