From d96f65afcac3b8fbf391841a63e7316e90eb9038 Mon Sep 17 00:00:00 2001 From: Ben Younes Date: Thu, 11 Jun 2026 04:23:54 +0200 Subject: [PATCH] fix(lambda): normalize backslash ZIP path separators on extraction (#1215) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(lambda): normalize backslash ZIP path separators on extraction PowerShell's Compress-Archive writes ZIP entry names with '\' separators, violating the ZIP spec (APPNOTE.TXT 4.4.17 mandates '/'). On Linux '\' is a literal filename character, so a .NET/Blazor publish archive extracted to a single flat file named "wwwroot\app.css" instead of nested directories, breaking static-file lookups under /var/task. Normalize '\' -> '/' before any path handling so such archives extract to the correct nested paths, matching how real AWS Lambda unpacks them. A trailing separator is also treated as a directory entry, since the backslash form would otherwise slip past ZipEntry.isDirectory(). The path-traversal guard now runs on the normalized name, so "..\..\evil" is still rejected. Closes #1198 * fix(lambda): warn on backslash ZIP separators instead of normalizing Real AWS Lambda does NOT normalize '\' to '/' in ZIP entry names — a PowerShell Compress-Archive entry extracts as a literal-named file. The previous normalization made Floci more forgiving than AWS, letting broken packages pass locally and fail on deploy. Replace the normalization with a clear warning that tells the developer why the backslash entry is problematic and how to repackage correctly (tar, pwsh, or dotnet lambda CLI). The traversal guard and containment check stay as-is. --- .../services/lambda/zip/ZipExtractor.java | 12 +++ .../services/lambda/zip/ZipExtractorTest.java | 73 +++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 src/test/java/io/github/hectorvent/floci/services/lambda/zip/ZipExtractorTest.java 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.java index 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 comparable Path 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 traversal if (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.java new file mode 100644 index 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"); + } +}