CVE-2024-24749: Insufficient Information (NVD-CWE-noinfo)
value: GeoServer is an open source server that allows users to share and edit geospatial data. Prior to versions 2.23.5 and 2.24.3, if GeoServer is deployed in the Windows operating system using an Apache Tomcat web application server, it is possible to bypass existing input validation in the GeoWebCache ByteStreamController class and read arbitrary classpath resources with specific file name extensions. If GeoServer is also deployed as a web archive using the data directory embedded in the `geoserver.war` file (rather than an external data directory), it will likely be possible to read specific resources to gain administrator privileges. However, it is very unlikely that production environments will be using the embedded data directory since, depending on how GeoServer is deployed, it will be erased and re-installed (which would also reset to the default password) either every time the server restarts or every time a new GeoServer WAR is installed and is therefore difficult to maintain. An external data directory will always be used if GeoServer is running in standalone mode (via an installer or a binary). Versions 2.23.5 and 2.24.3 contain a patch for the issue. Some workarounds are available. One may change from a Windows environment to a Linux environment; or change from Apache Tomcat to Jetty application server. One may also disable anonymous access to the embeded GeoWebCache administration and status pages.
CWE description: There is insufficient information about the issue to classify it; details are unkown or unspecified.
Published: 2024-07-01T14:15Z
Repository: https://github.com/GeoWebCache/geowebcache
Language: Java
Files changed: geowebcache/rest/src/main/java/org/geowebcache/rest/controller/ByteStreamController.java, geowebcache/rest/src/test/java/org/geowebcache/rest/controller/ByteStreamControllerTest.java
Commit date: 2024-02-01 18:07:11 -0800
Commit message: [GWC-1210] Improve input validation in ByteStreamController (#1211)


Diff: diff --git a/geowebcache/rest/src/main/java/org/geowebcache/rest/controller/ByteStreamController.java b/geowebcache/rest/src/main/java/org/geowebcache/rest/controller/ByteStreamController.java
index 38e47179..fa23daf7 100644
--- a/geowebcache/rest/src/main/java/org/geowebcache/rest/controller/ByteStreamController.java
+++ b/geowebcache/rest/src/main/java/org/geowebcache/rest/controller/ByteStreamController.java
@@ -13,13 +13,13 @@
  * @author David Vick, Boundless, Copyright 2017
  *     <p>File was reworked from ByteStreamerRestlet.java
  */
 package org.geowebcache.rest.controller;
 
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.UnsupportedEncodingException;
 import java.net.URL;
 import java.net.URLDecoder;
 import java.util.List;
 import java.util.logging.Logger;
 import java.util.regex.Pattern;
@@ -77,26 +77,25 @@ protected URL getResource(String path) {
 
     static final Pattern UNSAFE_RESOURCE = Pattern.compile("^/|/\\.\\./|^\\.\\./|\\.class$");
 
     // "gwc/rest/web/openlayers3/ol.js" -> openlayers3/ol.js
     // "/rest/web/openlayers3/ol.js" -> openlayers3/ol.js
-    String getFileName(HttpServletRequest request) {
-        String path = request.getPathInfo();
-        if (path.indexOf("/rest/web") != 0) {
-            path = path.substring(path.indexOf("/rest/web"));
-        }
-        return path.substring("/rest/web/".length());
+    String getFileName(HttpServletRequest request) throws IOException {
+        String path =
+                URLDecoder.decode(request.getRequestURI(), "UTF-8")
+                        .substring(request.getContextPath().length())
+                        .replace(File.separatorChar, '/');
+        int index = path.indexOf("/rest/web/");
+        return index < 0 ? null : path.substring(index + "/rest/web/".length());
     }
 
     @RequestMapping(value = "/web/**", method = RequestMethod.GET)
-    ResponseEntity<?> doGet(HttpServletRequest request, HttpServletResponse response) {
-        final String filename;
-        try {
-            filename = URLDecoder.decode(getFileName(request), "UTF-8");
-        } catch (UnsupportedEncodingException e1) {
-            throw new IllegalStateException(
-                    "Could not decode encoding UTF-8", e1); // Should never happen
+    ResponseEntity<?> doGet(HttpServletRequest request, HttpServletResponse response)
+            throws IOException {
+        final String filename = getFileName(request);
+        if (filename == null || filename.isEmpty()) {
+            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
         }
 
         // Just to make sure we don't allow access to arbitrary resources
         if (UNSAFE_RESOURCE.matcher(filename).find()) {
             return new ResponseEntity<>(HttpStatus.FORBIDDEN);
diff --git a/geowebcache/rest/src/test/java/org/geowebcache/rest/controller/ByteStreamControllerTest.java b/geowebcache/rest/src/test/java/org/geowebcache/rest/controller/ByteStreamControllerTest.java
index 5e71cd2a..5dc52a4b 100644
--- a/geowebcache/rest/src/test/java/org/geowebcache/rest/controller/ByteStreamControllerTest.java
+++ b/geowebcache/rest/src/test/java/org/geowebcache/rest/controller/ByteStreamControllerTest.java
@@ -13,14 +13,16 @@
  * @author David Vick, Boundless, 2017
  */
 package org.geowebcache.rest.controller;
 
 import static org.hamcrest.Matchers.startsWith;
+import static org.junit.Assume.assumeTrue;
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
 
+import org.apache.commons.lang3.SystemUtils;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.junit.runner.RunWith;
@@ -98,6 +100,20 @@ public void testBackreference() throws Exception {
     @Test
     public void testBackreference2() throws Exception {
         mockMvc.perform(get("/rest/web/foo/../../../shouldnt/access/test.png"))
                 .andExpect(status().is4xxClientError());
     }
+
+    @Test
+    public void testBackreferenceWindows() throws Exception {
+        assumeTrue(SystemUtils.IS_OS_WINDOWS);
+        mockMvc.perform(get("/rest/web/..\\..\\shouldnt/access/test.png"))
+                .andExpect(status().is4xxClientError());
+    }
+
+    @Test
+    public void testBackreferenceWindows2() throws Exception {
+        assumeTrue(SystemUtils.IS_OS_WINDOWS);
+        mockMvc.perform(get("/rest/web/foo\\..\\..\\..\\shouldnt/access/test.png"))
+                .andExpect(status().is4xxClientError());
+    }
 }

Vulnerable code: import java.io.UnsupportedEncodingException;
    String getFileName(HttpServletRequest request) {
        String path = request.getPathInfo();
        if (path.indexOf("/rest/web") != 0) {
            path = path.substring(path.indexOf("/rest/web"));
        }
        return path.substring("/rest/web/".length());
    ResponseEntity<?> doGet(HttpServletRequest request, HttpServletResponse response) {
        final String filename;
        try {
            filename = URLDecoder.decode(getFileName(request), "UTF-8");
        } catch (UnsupportedEncodingException e1) {
            throw new IllegalStateException(
                    "Could not decode encoding UTF-8", e1); // Should never happen
Fixed code: import java.io.File;
    String getFileName(HttpServletRequest request) throws IOException {
        String path =
                URLDecoder.decode(request.getRequestURI(), "UTF-8")
                        .substring(request.getContextPath().length())
                        .replace(File.separatorChar, '/');
        int index = path.indexOf("/rest/web/");
        return index < 0 ? null : path.substring(index + "/rest/web/".length());
    ResponseEntity<?> doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        final String filename = getFileName(request);
        if (filename == null || filename.isEmpty()) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
import static org.junit.Assume.assumeTrue;
import org.apache.commons.lang3.SystemUtils;

    @Test
    public void testBackreferenceWindows() throws Exception {
        assumeTrue(SystemUtils.IS_OS_WINDOWS);
        mockMvc.perform(get("/rest/web/..\\..\\shouldnt/access/test.png"))
                .andExpect(status().is4xxClientError());
    }

    @Test
    public void testBackreferenceWindows2() throws Exception {
        assumeTrue(SystemUtils.IS_OS_WINDOWS);
        mockMvc.perform(get("/rest/web/foo\\..\\..\\..\\shouldnt/access/test.png"))
                .andExpect(status().is4xxClientError());
    }