Light, fluffy, and always free - The AWS Local Emulator alternative
fix(lambda): normalize backslash ZIP path separators on extraction (#1215)
Ben Younesd96f65afcac3
diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/zip/ZipExtractor.java b/src/main/java/io/github/hectorvent/floci/services/lambda/zip/ZipExtractor.javaindex 8a05bed7bd..ca28933a21 100644--- a/src/main/java/io/github/hectorvent/floci/services/lambda/zip/ZipExtractor.java+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/zip/ZipExtractor.java@@ -20,6 +20,8 @@ public class ZipExtractor {private static final Logger LOG = Logger.getLogger(ZipExtractor.class);+ private static final char BACKSLASH = '\\';+public void extractTo(byte[] zipBytes, Path targetDir) throws IOException {// Resolve to absolute path so that normalize() on entry paths stays comparablePath absTarget = targetDir.toAbsolutePath().normalize();@@ -30,6 +32,16 @@ public class ZipExtractor {while ((entry = zis.getNextEntry()) != null) {String entryName = entry.getName();+ // PowerShell 5 Compress-Archive writes '\' as a literal filename byte on+ // Linux. Real AWS Lambda does NOT normalize this (the archive extracts to+ // a flat "wwwroot\app.css" file), so neither do we; masking it would let+ // a broken package pass locally and then fail on deploy.+ if (entryName.indexOf(BACKSLASH) >= 0) {+ LOG.warnv("ZIP entry \"{0}\" uses backslash separators (PowerShell Compress-Archive). "+ + "It extracts as a literal filename and will also fail on real AWS Lambda. "+ + "Repackage with tar, PowerShell Core (pwsh), or the dotnet lambda CLI.", entryName);+ }+// Security: prevent path traversalif (entryName.contains("..") || entryName.startsWith("/")) {LOG.warnv("Skipping suspicious ZIP entry: {0}", entryName);diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/zip/ZipExtractorTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/zip/ZipExtractorTest.javanew file mode 100644index 0000000000..14261346bb--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/zip/ZipExtractorTest.java@@ -0,0 +1,73 @@+package io.github.hectorvent.floci.services.lambda.zip;++import org.junit.jupiter.api.Test;+import org.junit.jupiter.api.io.TempDir;++import java.io.ByteArrayOutputStream;+import java.io.IOException;+import java.nio.charset.StandardCharsets;+import java.nio.file.Files;+import java.nio.file.Path;+import java.util.zip.ZipEntry;+import java.util.zip.ZipOutputStream;++import static org.junit.jupiter.api.Assertions.*;++class ZipExtractorTest {++ private final ZipExtractor extractor = new ZipExtractor();++ /** Build a ZIP whose entry names use the given separator, as PowerShell does. */+ private static byte[] zipWith(String entryName, String content) throws IOException {+ ByteArrayOutputStream baos = new ByteArrayOutputStream();+ try (ZipOutputStream zos = new ZipOutputStream(baos)) {+ zos.putNextEntry(new ZipEntry(entryName));+ zos.write(content.getBytes(StandardCharsets.UTF_8));+ zos.closeEntry();+ }+ return baos.toByteArray();+ }++ @Test+ void extractsBackslashEntriesAsLiteralFilename(@TempDir Path target) throws IOException {+ // PowerShell 5 Compress-Archive writes '\' separators (issue #1198).+ // Real AWS Lambda does NOT normalize these — the entry extracts as a single+ // literal-named file. Floci must match AWS, not silently fix broken packages.+ byte[] zip = zipWith("wwwroot\\_framework\\blazor.web.js", "// js");++ extractor.extractTo(zip, target);++ // AWS-congruent: the backslashed entry lands as a literal filename, not nested.+ Path flat = target.resolve("wwwroot\\_framework\\blazor.web.js");+ assertTrue(Files.isRegularFile(flat),+ "backslashed entry must extract as a literal filename (matching AWS Lambda)");+ assertEquals("// js", Files.readString(flat));+ // It must NOT create a nested path.+ Path nested = target.resolve("wwwroot").resolve("_framework").resolve("blazor.web.js");+ assertFalse(Files.exists(nested),+ "backslashed entry must NOT create a nested path (AWS does not normalize)");+ }++ @Test+ void stillExtractsStandardForwardSlashEntries(@TempDir Path target) throws IOException {+ byte[] zip = zipWith("conf/app.css", "body{}");++ extractor.extractTo(zip, target);++ Path nested = target.resolve("conf").resolve("app.css");+ assertTrue(Files.isRegularFile(nested));+ assertEquals("body{}", Files.readString(nested));+ }++ @Test+ void rejectsBackslashTraversalEntries(@TempDir Path target) throws IOException {+ // "..\..\evil" contains ".." in the original entry name, so the traversal+ // guard catches it even without normalization.+ byte[] zip = zipWith("..\\..\\evil.sh", "rm -rf");++ extractor.extractTo(zip, target);++ assertFalse(Files.exists(target.getParent().getParent().resolve("evil.sh")),+ "traversal entry must not escape the target dir");+ }+}