diff --git a/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/servlet/AwsServletContext.java b/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/servlet/AwsServletContext.java index a7ce5bd9a..7aff594d6 100644 --- a/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/servlet/AwsServletContext.java +++ b/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/servlet/AwsServletContext.java @@ -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; @@ -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 //------------------------------------------------------------- @@ -199,15 +205,41 @@ public Set getResourcePaths(String s) { } + /** + * Returns a web resource. Lookups are resolved against META-INF/resources 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 /index.html to META-INF/resources/index.html. + * 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; } @@ -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; } diff --git a/aws-serverless-java-container-core/src/test/java/com/amazonaws/serverless/proxy/internal/servlet/AwsServletContextTest.java b/aws-serverless-java-container-core/src/test/java/com/amazonaws/serverless/proxy/internal/servlet/AwsServletContextTest.java index 58d0b0542..b2eecd101 100644 --- a/aws-serverless-java-container-core/src/test/java/com/amazonaws/serverless/proxy/internal/servlet/AwsServletContextTest.java +++ b/aws-serverless-java-container-core/src/test/java/com/amazonaws/serverless/proxy/internal/servlet/AwsServletContextTest.java @@ -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.*; @@ -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 diff --git a/aws-serverless-java-container-core/src/test/resources/META-INF/resources/web-root-test.txt b/aws-serverless-java-container-core/src/test/resources/META-INF/resources/web-root-test.txt new file mode 100644 index 000000000..a48cf0de7 --- /dev/null +++ b/aws-serverless-java-container-core/src/test/resources/META-INF/resources/web-root-test.txt @@ -0,0 +1 @@ +public diff --git a/aws-serverless-java-container-springboot4/src/test/java/com/amazonaws/serverless/proxy/spring/ServletAppTest.java b/aws-serverless-java-container-springboot4/src/test/java/com/amazonaws/serverless/proxy/spring/ServletAppTest.java index 3e7cd0cf6..54af4d69b 100644 --- a/aws-serverless-java-container-springboot4/src/test/java/com/amazonaws/serverless/proxy/spring/ServletAppTest.java +++ b/aws-serverless-java-container-springboot4/src/test/java/com/amazonaws/serverless/proxy/spring/ServletAppTest.java @@ -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")); + } } diff --git a/aws-serverless-java-container-springboot4/src/test/resources/META-INF/resources/public-test.txt b/aws-serverless-java-container-springboot4/src/test/resources/META-INF/resources/public-test.txt new file mode 100644 index 000000000..27751cc25 --- /dev/null +++ b/aws-serverless-java-container-springboot4/src/test/resources/META-INF/resources/public-test.txt @@ -0,0 +1 @@ +public-static-file diff --git a/aws-serverless-java-container-springboot4/src/test/resources/classpath-root-test.properties b/aws-serverless-java-container-springboot4/src/test/resources/classpath-root-test.properties new file mode 100644 index 000000000..4ce549da0 --- /dev/null +++ b/aws-serverless-java-container-springboot4/src/test/resources/classpath-root-test.properties @@ -0,0 +1 @@ +root.test.value=classpath-root