CVE-2018-9159 (published 2018-03-31T21:29Z)

In Spark before 2.7.2, a remote attacker can read unintended static files via various representations of absolute or relative pathnames, as demonstrated by file: URLs and directory traversal sequences. NOTE: this product is unrelated to Ignite Realtime Spark.

Severity: MEDIUM
CVSS v2 base score: 5.0 (AV:N/AC:L/Au:N/C:P/I:N/A:N)
CVSS v3 base score: 5.3, MEDIUM (CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:N/A:N)

Weakness information:
cwe_id: CWE-22
cwe_name: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
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.
extended_description: 
            Many file operations are intended to take place within a restricted directory. By using special elements such as ".." and "/" separators, attackers can escape outside of the restricted location to access files or directories that are elsewhere on the system. One of the most common special elements is the "../" sequence, which in most modern operating systems is interpreted as the parent directory of the current location. This is referred to as relative path traversal. Path traversal also covers the use of absolute pathnames such as "/usr/local/bin" to access unexpected files. This is referred to as absolute path traversal.
         
         

Fix information:
repo_url: https://github.com/perwendel/spark
commit_details: repo_url: https://github.com/perwendel/spark
author: Per Wendel
author_date: 2018-03-07 14:43:02+01:00
msg: Fix for #981, patch 2 (#988)
repository: repo_url: https://github.com/perwendel/spark
repo_name: perwendel/spark
description: A simple expressive web framework for java. Spark has a kotlin DSL https://github.com/perwendel/spark-kotlin
repo_language: Java
owner: perwendel
file_changes: filename: ClassPathResource.java
change_type: ModificationType.MODIFY
diff: @@ -23,6 +23,7 @@ import java.net.URL;
 
 import spark.utils.Assert;
 import spark.utils.ClassUtils;
+import spark.utils.ResourceUtils;
 import spark.utils.StringUtils;
 
 /**
@@ -74,7 +75,7 @@ public class ClassPathResource extends AbstractFileResolvingResource {
      */
     public ClassPathResource(String path, ClassLoader classLoader) {
         Assert.notNull(path, "Path must not be null");
-        Assert.state(doesNotContainFileColon(path), "Path must not contain 'file:'");
+        Assert.isTrue(isValid(path), "Path is not valid");
 
         String pathToUse = StringUtils.cleanPath(path);
 
@@ -86,8 +87,27 @@ public class ClassPathResource extends AbstractFileResolvingResource {
         this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
     }
 
-    private static boolean doesNotContainFileColon(String path) {
-        return !path.contains("file:");
+    private static boolean isValid(final String path) {
+        return !isInvalidPath(path);
+    }
+
+    private static boolean isInvalidPath(String path) {
+        if (path.contains("WEB-INF") || path.contains("META-INF")) {
+            return true;
+        }
+        if (path.contains(":/")) {
+            String relativePath = (path.charAt(0) == '/' ? path.substring(1) : path);
+            if (ResourceUtils.isUrl(relativePath) || relativePath.startsWith("url:")) {
+                return true;
+            }
+        }
+        if (path.contains("")) {
+            path = StringUtils.cleanPath(path);
+            if (path.contains("../")) {
+                return true;
+            }
+        }
+        return false;
     }
 
     /**
@@ -236,8 +256,8 @@ public class ClassPathResource extends AbstractFileResolvingResource {
             ClassLoader otherLoader = otherRes.classLoader;
 
             return (this.path.equals(otherRes.path) &&
-                thisLoader.equals(otherLoader) &&
-                this.clazz.equals(otherRes.clazz));
+                    thisLoader.equals(otherLoader) &&
+                    this.clazz.equals(otherRes.clazz));
         }
         return false;
     }

programming_language: Java
filename: EmbeddedJettyFactoryTest.java
change_type: ModificationType.MODIFY
diff: @@ -4,13 +4,19 @@ import org.eclipse.jetty.server.Server;
 import org.eclipse.jetty.util.thread.QueuedThreadPool;
 import org.junit.After;
 import org.junit.Test;
+
 import spark.embeddedserver.EmbeddedServer;
 import spark.route.Routes;
 import spark.staticfiles.StaticFilesConfiguration;
 
-import static org.mockito.Mockito.*;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
 
 public class EmbeddedJettyFactoryTest {
+
     private EmbeddedServer embeddedServer;
 
     @Test
@@ -19,14 +25,14 @@ public class EmbeddedJettyFactoryTest {
         final StaticFilesConfiguration staticFilesConfiguration = mock(StaticFilesConfiguration.class);
         final Routes routes = mock(Routes.class);
 
-        when(jettyServerFactory.create(100,10,10000)).thenReturn(new Server());
+        when(jettyServerFactory.create(100, 10, 10000)).thenReturn(new Server());
 
         final EmbeddedJettyFactory embeddedJettyFactory = new EmbeddedJettyFactory(jettyServerFactory);
         embeddedServer = embeddedJettyFactory.create(routes, staticFilesConfiguration, false);
 
-        embeddedServer.ignite("localhost", 8080, null, 100,10,10000);
+        embeddedServer.ignite("localhost", 6757, null, 100, 10, 10000);
 
-        verify(jettyServerFactory, times(1)).create(100,10,10000);
+        verify(jettyServerFactory, times(1)).create(100, 10, 10000);
         verifyNoMoreInteractions(jettyServerFactory);
     }
 
@@ -42,7 +48,7 @@ public class EmbeddedJettyFactoryTest {
         final EmbeddedJettyFactory embeddedJettyFactory = new EmbeddedJettyFactory(jettyServerFactory).withThreadPool(threadPool);
         embeddedServer = embeddedJettyFactory.create(routes, staticFilesConfiguration, false);
 
-        embeddedServer.ignite("localhost", 8080, null, 0,0,0);
+        embeddedServer.ignite("localhost", 6758, null, 0, 0, 0);
 
         verify(jettyServerFactory, times(1)).create(threadPool);
         verifyNoMoreInteractions(jettyServerFactory);
@@ -54,19 +60,21 @@ public class EmbeddedJettyFactoryTest {
         final StaticFilesConfiguration staticFilesConfiguration = mock(StaticFilesConfiguration.class);
         final Routes routes = mock(Routes.class);
 
-        when(jettyServerFactory.create(100,10,10000)).thenReturn(new Server());
+        when(jettyServerFactory.create(100, 10, 10000)).thenReturn(new Server());
 
         final EmbeddedJettyFactory embeddedJettyFactory = new EmbeddedJettyFactory(jettyServerFactory).withThreadPool(null);
         embeddedServer = embeddedJettyFactory.create(routes, staticFilesConfiguration, false);
 
-        embeddedServer.ignite("localhost", 8080, null, 100,10,10000);
+        embeddedServer.ignite("localhost", 6759, null, 100, 10, 10000);
 
-        verify(jettyServerFactory, times(1)).create(100,10,10000);
+        verify(jettyServerFactory, times(1)).create(100, 10, 10000);
         verifyNoMoreInteractions(jettyServerFactory);
     }
 
     @After
     public void tearDown() throws Exception {
-        if(embeddedServer != null) embeddedServer.extinguish();
+        if (embeddedServer != null) {
+            embeddedServer.extinguish();
+        }
     }
 }

programming_language: Java
filename: StaticResources.java
change_type: ModificationType.MODIFY
diff: @@ -17,7 +17,7 @@
 package spark.examples.staticresources;
 
 import static spark.Spark.get;
-import static spark.Spark.staticFileLocation;
+import static spark.Spark.staticFiles;
 
 /**
  * Example showing how serve static resources.
@@ -27,7 +27,7 @@ public class StaticResources {
     public static void main(String[] args) {
 
         // Will serve all static file are under "/public" in classpath if the route isn't consumed by others routes.
-        staticFileLocation("/public");
+        staticFiles.location("/public");
 
         get("/hello", (request, response) -> {
             return "Hello World!";

programming_language: Java