Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
Expand All @@ -54,6 +53,13 @@ public class AwsServletContext
public static final String SERVER_INFO = LambdaContainerHandler.SERVER_INFO + "/" + SERVLET_API_MAJOR_VERSION + "." + SERVLET_API_MINOR_VERSION;


//-------------------------------------------------------------
// Constants - Private
//-------------------------------------------------------------
// Servlet spec location for web content packaged in jars.
private static final String WEB_ROOT = "META-INF/resources";


//-------------------------------------------------------------
// Variables - Private
//-------------------------------------------------------------
Expand Down Expand Up @@ -199,15 +205,41 @@ public Set<String> getResourcePaths(String s) {
}


/**
* Returns a web resource. Lookups are resolved against <code>META-INF/resources</code> on the classpath, the
* Servlet spec location for web content packaged in jars.
*/
@Override
public URL getResource(String s) throws MalformedURLException {
return AwsServletContext.class.getResource(s);
String webRootPath = toWebRootPath(s);
return webRootPath == null ? null : AwsServletContext.class.getClassLoader().getResource(webRootPath);
}


@Override
public InputStream getResourceAsStream(String s) {
return AwsServletContext.class.getResourceAsStream(s);
String webRootPath = toWebRootPath(s);
return webRootPath == null ? null : AwsServletContext.class.getClassLoader().getResourceAsStream(webRootPath);
}


/**
* Maps a Servlet resource path such as <code>/index.html</code> to <code>META-INF/resources/index.html</code>.
* Returns null for paths that do not start with "/" or that could escape the web root.
*/
static String toWebRootPath(String path) {
if (path == null || !path.startsWith("/")) {
return null;
}
if (path.indexOf('\\') >= 0 || path.indexOf('\0') >= 0) {
return null;
}
for (String segment : path.split("/")) {
if ("..".equals(segment)) {
return null;
}
}
return WEB_ROOT + path;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything looks good to me.
Just one optional thing, for '/' or any path that points to the folder this resolvees to the "META-INF/resources" dir itself.
Do we want to return null for the said case or is the returning intended?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that should be expected behavior, either way not worried about it. Good point about the edge case, though.

}


Expand Down Expand Up @@ -273,16 +305,8 @@ public void log(String s, Throwable throwable) {

@Override
public String getRealPath(String s) {
String absPath = null;
URL fileUrl = ClassLoader.getSystemResource(s);
if (fileUrl != null) {
try {
absPath = new File(fileUrl.toURI()).getAbsolutePath();
} catch (URISyntaxException e) {
log.error("Error while looking for real path {}: {}", SecurityUtils.encode(s), SecurityUtils.encode(e.getMessage()));
}
}
return absPath;
// There is no web root directory on disk in Lambda, so no real path is available.
return null;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CountDownLatch;

import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -275,6 +277,45 @@ public String getId() {
}
}

@Test
void getResource_fileInWebRoot_expectFound() throws IOException {
assertNotNull(STATIC_CTX.getResource("/web-root-test.txt"));
try (InputStream in = STATIC_CTX.getResourceAsStream("/web-root-test.txt")) {
assertNotNull(in);
assertEquals("public", new String(in.readAllBytes(), StandardCharsets.UTF_8).trim());
}
}

@Test
void getResource_classpathRootFile_expectNull() throws IOException {
// This class file is on the classpath but outside META-INF/resources.
String classFile = "/" + AwsServletContextTest.class.getName().replace('.', '/') + ".class";
assertNotNull(AwsServletContextTest.class.getResource(classFile), "test setup: class file should exist");
assertNull(STATIC_CTX.getResource(classFile));
assertNull(STATIC_CTX.getResourceAsStream(classFile));
}

@Test
void getResource_pathEscapingWebRoot_expectNull() throws IOException {
String classFile = "/../../" + AwsServletContextTest.class.getName().replace('.', '/') + ".class";
assertNull(STATIC_CTX.getResource(classFile));
assertNull(STATIC_CTX.getResourceAsStream(classFile));
assertNull(STATIC_CTX.getResource("/a/../../web-root-test.txt"));
assertNull(STATIC_CTX.getResource("/..\\..\\web-root-test.txt"));
}

@Test
void getResource_pathWithoutLeadingSlash_expectNull() throws IOException {
assertNull(STATIC_CTX.getResource("web-root-test.txt"));
assertNull(STATIC_CTX.getResource(null));
}

@Test
void getRealPath_anyPath_expectNull() {
assertNull(STATIC_CTX.getRealPath("/web-root-test.txt"));
assertNull(STATIC_CTX.getRealPath("/"));
}

public static class TestFilter implements Filter {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,34 @@ void echoMessage_populatesSingleValueHeadersForHttpApiV2(String reqType) {
assertNull(resp.getHeaders());
}
}

@MethodSource("data")
@ParameterizedTest
void staticResource_classpathRootFile_notServed(String reqType) {
initServletAppTest(reqType);
AwsProxyRequestBuilder req = new AwsProxyRequestBuilder("/classpath-root-test.properties", "GET");
AwsProxyResponse resp = handler.handleRequest(req, lambdaContext);
assertEquals(404, resp.getStatusCode());
assertFalse(resp.getBody() != null && resp.getBody().contains("classpath-root"));
}

@MethodSource("data")
@ParameterizedTest
void staticResource_classFile_notServed(String reqType) {
initServletAppTest(reqType);
String classFile = "/" + ServletApplication.class.getName().replace('.', '/') + ".class";
AwsProxyRequestBuilder req = new AwsProxyRequestBuilder(classFile, "GET");
AwsProxyResponse resp = handler.handleRequest(req, lambdaContext);
assertEquals(404, resp.getStatusCode());
}

@MethodSource("data")
@ParameterizedTest
void staticResource_metaInfResourcesFile_served(String reqType) {
initServletAppTest(reqType);
AwsProxyRequestBuilder req = new AwsProxyRequestBuilder("/public-test.txt", "GET");
AwsProxyResponse resp = handler.handleRequest(req, lambdaContext);
assertEquals(200, resp.getStatusCode());
assertTrue(resp.getBody().contains("public-static-file"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public-static-file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
root.test.value=classpath-root
Loading