From 1f3d5ad1a38ef2bfcd74e3372056a718d4683d94 Mon Sep 17 00:00:00 2001 From: Reed Hamilton Date: Thu, 3 Dec 2026 10:58:56 -0800 Subject: [PATCH 1/2] fix: resolve ServletContext resources from META-INF/resources Align getResource, getResourceAsStream and getRealPath with the Servlet spec web resource lookup. --- .../internal/servlet/AwsServletContext.java | 50 ++++++++++++++----- .../servlet/AwsServletContextTest.java | 41 +++++++++++++++ .../META-INF/resources/web-root-test.txt | 1 + .../proxy/spring/ServletAppTest.java | 30 +++++++++++ .../META-INF/resources/public-test.txt | 1 + .../resources/classpath-root-test.properties | 1 + 6 files changed, 111 insertions(+), 13 deletions(-) create mode 100644 aws-serverless-java-container-core/src/test/resources/META-INF/resources/web-root-test.txt create mode 100644 aws-serverless-java-container-springboot4/src/test/resources/META-INF/resources/public-test.txt create mode 100644 aws-serverless-java-container-springboot4/src/test/resources/classpath-root-test.properties 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..21b089a91 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 : getClassLoader().getResource(webRootPath); } @Override public InputStream getResourceAsStream(String s) { - return AwsServletContext.class.getResourceAsStream(s); + String webRootPath = toWebRootPath(s); + return webRootPath == null ? null : 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 From 55c3a76d943dc90a974392d833b9bf66e889bcf6 Mon Sep 17 00:00:00 2001 From: Reed Hamilton Date: Thu, 3 Dec 2026 14:48:57 -0800 Subject: [PATCH 2/2] fix: use the library class loader for web resource lookup --- .../serverless/proxy/internal/servlet/AwsServletContext.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 21b089a91..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 @@ -212,14 +212,14 @@ public Set getResourcePaths(String s) { @Override public URL getResource(String s) throws MalformedURLException { String webRootPath = toWebRootPath(s); - return webRootPath == null ? null : getClassLoader().getResource(webRootPath); + return webRootPath == null ? null : AwsServletContext.class.getClassLoader().getResource(webRootPath); } @Override public InputStream getResourceAsStream(String s) { String webRootPath = toWebRootPath(s); - return webRootPath == null ? null : getClassLoader().getResourceAsStream(webRootPath); + return webRootPath == null ? null : AwsServletContext.class.getClassLoader().getResourceAsStream(webRootPath); }