Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for #981, patch 2 #988

Merged
merged 1 commit into from
Mar 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions src/main/java/spark/resource/ClassPathResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import spark.utils.Assert;
import spark.utils.ClassUtils;
import spark.utils.ResourceUtils;
import spark.utils.StringUtils;

/**
Expand Down Expand Up @@ -74,7 +75,7 @@ public ClassPathResource(String path) {
*/
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);

Expand All @@ -86,8 +87,27 @@ public ClassPathResource(String path, ClassLoader classLoader) {
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;
}

/**
Expand Down Expand Up @@ -236,8 +256,8 @@ public boolean equals(Object obj) {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@
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
Expand All @@ -19,14 +25,14 @@ public void create() throws Exception {
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);
}

Expand All @@ -42,7 +48,7 @@ public void create_withThreadPool() throws Exception {
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);
Expand All @@ -54,19 +60,21 @@ public void create_withNullThreadPool() throws Exception {
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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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!";
Expand Down