Skip to content

Commit 030e9d0

Browse files
authored
Fix for #981, patch 2 (#988)
1 parent 2723653 commit 030e9d0

File tree

3 files changed

+44
-16
lines changed

3 files changed

+44
-16
lines changed

src/main/java/spark/resource/ClassPathResource.java

+25-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import spark.utils.Assert;
2525
import spark.utils.ClassUtils;
26+
import spark.utils.ResourceUtils;
2627
import spark.utils.StringUtils;
2728

2829
/**
@@ -74,7 +75,7 @@ public ClassPathResource(String path) {
7475
*/
7576
public ClassPathResource(String path, ClassLoader classLoader) {
7677
Assert.notNull(path, "Path must not be null");
77-
Assert.state(doesNotContainFileColon(path), "Path must not contain 'file:'");
78+
Assert.isTrue(isValid(path), "Path is not valid");
7879

7980
String pathToUse = StringUtils.cleanPath(path);
8081

@@ -86,8 +87,27 @@ public ClassPathResource(String path, ClassLoader classLoader) {
8687
this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
8788
}
8889

89-
private static boolean doesNotContainFileColon(String path) {
90-
return !path.contains("file:");
90+
private static boolean isValid(final String path) {
91+
return !isInvalidPath(path);
92+
}
93+
94+
private static boolean isInvalidPath(String path) {
95+
if (path.contains("WEB-INF") || path.contains("META-INF")) {
96+
return true;
97+
}
98+
if (path.contains(":/")) {
99+
String relativePath = (path.charAt(0) == '/' ? path.substring(1) : path);
100+
if (ResourceUtils.isUrl(relativePath) || relativePath.startsWith("url:")) {
101+
return true;
102+
}
103+
}
104+
if (path.contains("")) {
105+
path = StringUtils.cleanPath(path);
106+
if (path.contains("../")) {
107+
return true;
108+
}
109+
}
110+
return false;
91111
}
92112

93113
/**
@@ -236,8 +256,8 @@ public boolean equals(Object obj) {
236256
ClassLoader otherLoader = otherRes.classLoader;
237257

238258
return (this.path.equals(otherRes.path) &&
239-
thisLoader.equals(otherLoader) &&
240-
this.clazz.equals(otherRes.clazz));
259+
thisLoader.equals(otherLoader) &&
260+
this.clazz.equals(otherRes.clazz));
241261
}
242262
return false;
243263
}

src/test/java/spark/embeddedserver/jetty/EmbeddedJettyFactoryTest.java

+17-9
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@
44
import org.eclipse.jetty.util.thread.QueuedThreadPool;
55
import org.junit.After;
66
import org.junit.Test;
7+
78
import spark.embeddedserver.EmbeddedServer;
89
import spark.route.Routes;
910
import spark.staticfiles.StaticFilesConfiguration;
1011

11-
import static org.mockito.Mockito.*;
12+
import static org.mockito.Mockito.mock;
13+
import static org.mockito.Mockito.times;
14+
import static org.mockito.Mockito.verify;
15+
import static org.mockito.Mockito.verifyNoMoreInteractions;
16+
import static org.mockito.Mockito.when;
1217

1318
public class EmbeddedJettyFactoryTest {
19+
1420
private EmbeddedServer embeddedServer;
1521

1622
@Test
@@ -19,14 +25,14 @@ public void create() throws Exception {
1925
final StaticFilesConfiguration staticFilesConfiguration = mock(StaticFilesConfiguration.class);
2026
final Routes routes = mock(Routes.class);
2127

22-
when(jettyServerFactory.create(100,10,10000)).thenReturn(new Server());
28+
when(jettyServerFactory.create(100, 10, 10000)).thenReturn(new Server());
2329

2430
final EmbeddedJettyFactory embeddedJettyFactory = new EmbeddedJettyFactory(jettyServerFactory);
2531
embeddedServer = embeddedJettyFactory.create(routes, staticFilesConfiguration, false);
2632

27-
embeddedServer.ignite("localhost", 8080, null, 100,10,10000);
33+
embeddedServer.ignite("localhost", 6757, null, 100, 10, 10000);
2834

29-
verify(jettyServerFactory, times(1)).create(100,10,10000);
35+
verify(jettyServerFactory, times(1)).create(100, 10, 10000);
3036
verifyNoMoreInteractions(jettyServerFactory);
3137
}
3238

@@ -42,7 +48,7 @@ public void create_withThreadPool() throws Exception {
4248
final EmbeddedJettyFactory embeddedJettyFactory = new EmbeddedJettyFactory(jettyServerFactory).withThreadPool(threadPool);
4349
embeddedServer = embeddedJettyFactory.create(routes, staticFilesConfiguration, false);
4450

45-
embeddedServer.ignite("localhost", 8080, null, 0,0,0);
51+
embeddedServer.ignite("localhost", 6758, null, 0, 0, 0);
4652

4753
verify(jettyServerFactory, times(1)).create(threadPool);
4854
verifyNoMoreInteractions(jettyServerFactory);
@@ -54,19 +60,21 @@ public void create_withNullThreadPool() throws Exception {
5460
final StaticFilesConfiguration staticFilesConfiguration = mock(StaticFilesConfiguration.class);
5561
final Routes routes = mock(Routes.class);
5662

57-
when(jettyServerFactory.create(100,10,10000)).thenReturn(new Server());
63+
when(jettyServerFactory.create(100, 10, 10000)).thenReturn(new Server());
5864

5965
final EmbeddedJettyFactory embeddedJettyFactory = new EmbeddedJettyFactory(jettyServerFactory).withThreadPool(null);
6066
embeddedServer = embeddedJettyFactory.create(routes, staticFilesConfiguration, false);
6167

62-
embeddedServer.ignite("localhost", 8080, null, 100,10,10000);
68+
embeddedServer.ignite("localhost", 6759, null, 100, 10, 10000);
6369

64-
verify(jettyServerFactory, times(1)).create(100,10,10000);
70+
verify(jettyServerFactory, times(1)).create(100, 10, 10000);
6571
verifyNoMoreInteractions(jettyServerFactory);
6672
}
6773

6874
@After
6975
public void tearDown() throws Exception {
70-
if(embeddedServer != null) embeddedServer.extinguish();
76+
if (embeddedServer != null) {
77+
embeddedServer.extinguish();
78+
}
7179
}
7280
}

src/test/java/spark/examples/staticresources/StaticResources.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package spark.examples.staticresources;
1818

1919
import static spark.Spark.get;
20-
import static spark.Spark.staticFileLocation;
20+
import static spark.Spark.staticFiles;
2121

2222
/**
2323
* Example showing how serve static resources.
@@ -27,7 +27,7 @@ public class StaticResources {
2727
public static void main(String[] args) {
2828

2929
// Will serve all static file are under "/public" in classpath if the route isn't consumed by others routes.
30-
staticFileLocation("/public");
30+
staticFiles.location("/public");
3131

3232
get("/hello", (request, response) -> {
3333
return "Hello World!";

0 commit comments

Comments
 (0)