mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(lambda): bound ZIP extraction resources (#3127)
* fix(lambda): bound ZIP extraction resources Limit Lambda ZIP compressed and expanded sizes and configurable entry counts for function and layer deployments. Preserve previous extracted code when a package exceeds a resource limit. * refactor(test): centralize ZIP fixture encoding * docs(test): explain ZIP fixture headers
This commit is contained in:
1 parent
daeea78ee6
commit
0fce2d2407
9 files changed
+308
-59
No files matched your search
@@ -871,6 +871,11 @@ class LambdaServiceTest {
|
||||
// ──────────────────────────── Hot-reload ────────────────────────────
|
||||
|
||||
private LambdaService serviceWithHotReload(boolean enabled, List<String> allowedPaths) {
|
||||
return serviceWithHotReload(enabled, allowedPaths, ZipExtractor.DEFAULT_MAX_ENTRIES);
|
||||
}
|
||||
|
||||
private LambdaService serviceWithHotReload(boolean enabled, List<String> allowedPaths,
|
||||
int zipMaxEntries) {
|
||||
EmulatorConfig cfg = mock(EmulatorConfig.class);
|
||||
EmulatorConfig.ServicesConfig svc = mock(EmulatorConfig.ServicesConfig.class);
|
||||
EmulatorConfig.LambdaServiceConfig lambdaCfg = mock(EmulatorConfig.LambdaServiceConfig.class);
|
||||
@@ -881,6 +886,7 @@ class LambdaServiceTest {
|
||||
when(lambdaCfg.hotReload()).thenReturn(hr);
|
||||
when(lambdaCfg.defaultTimeoutSeconds()).thenReturn(3);
|
||||
when(lambdaCfg.defaultMemoryMb()).thenReturn(128);
|
||||
when(lambdaCfg.zipMaxEntries()).thenReturn(zipMaxEntries);
|
||||
when(hr.enabled()).thenReturn(enabled);
|
||||
when(hr.allowedPaths()).thenReturn(allowedPaths == null ? Optional.empty() : Optional.of(allowedPaths));
|
||||
|
||||
@@ -892,6 +898,32 @@ class LambdaServiceTest {
|
||||
return new LambdaService(store, warmPool, codeStore, zipExtractor, cfg, regionResolver);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createFunctionRejectsConfiguredZipEntryLimit() throws Exception {
|
||||
LambdaService limited = serviceWithHotReload(true, null, 2);
|
||||
Map<String, Object> request = baseRequest("zip-entry-limit");
|
||||
request.put("Code", Map.of("ZipFile", createZipBase64("index.js", "one.js", "two.js")));
|
||||
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> limited.createFunction(REGION, request));
|
||||
|
||||
assertEquals("InvalidParameterValueException", error.getErrorCode());
|
||||
assertTrue(error.getMessage().contains("more than the configured 2 entries"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createFunctionRejectsOversizedDirectZipUploadWithAwsError() {
|
||||
Map<String, Object> request = baseRequest("oversized-zip");
|
||||
byte[] oversized = new byte[(int) ZipExtractor.DIRECT_UPLOAD_MAX_COMPRESSED_BYTES + 1];
|
||||
request.put("Code", Map.of("ZipFile", Base64.getEncoder().encodeToString(oversized)));
|
||||
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.createFunction(REGION, request));
|
||||
|
||||
assertEquals("RequestEntityTooLargeException", error.getErrorCode());
|
||||
assertEquals(413, error.getHttpStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void hotReload_disabledByDefault_throwsInvalidParameter() {
|
||||
// The package-private test constructor leaves config=null, which means disabled.
|
||||
|
||||
+179
-54
@@ -23,6 +23,17 @@ class ZipExtractorTest {
|
||||
|
||||
private final ZipExtractor extractor = new ZipExtractor();
|
||||
|
||||
private static final int LOCAL_FILE_HEADER_SIGNATURE = 0x04034b50;
|
||||
private static final int CENTRAL_DIRECTORY_HEADER_SIGNATURE = 0x02014b50;
|
||||
private static final int END_OF_CENTRAL_DIRECTORY_SIGNATURE = 0x06054b50;
|
||||
private static final int DATA_DESCRIPTOR_SIGNATURE = 0x08074b50;
|
||||
private static final int DATA_DESCRIPTOR_FLAG = 0x0008;
|
||||
|
||||
/** Entry metadata for fixtures that need to control central-directory declarations. */
|
||||
private record RawZipEntry(String name, int method, int flags, byte[] payload,
|
||||
long crc, long expandedSize, long descriptorExpandedSize) {
|
||||
}
|
||||
|
||||
/** 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();
|
||||
@@ -34,6 +45,30 @@ class ZipExtractorTest {
|
||||
return baos.toByteArray();
|
||||
}
|
||||
|
||||
private static byte[] zipWithEntries(String... names) throws IOException {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
try (ZipOutputStream zos = new ZipOutputStream(baos)) {
|
||||
for (String name : names) {
|
||||
zos.putNextEntry(new ZipEntry(name));
|
||||
zos.write(name.getBytes(StandardCharsets.UTF_8));
|
||||
zos.closeEntry();
|
||||
}
|
||||
}
|
||||
return baos.toByteArray();
|
||||
}
|
||||
|
||||
private static byte[] zipWithDeclaredSizes(long... sizes) throws IOException {
|
||||
byte[] content = new byte[]{'x'};
|
||||
CRC32 crc = new CRC32();
|
||||
crc.update(content);
|
||||
RawZipEntry[] entries = new RawZipEntry[sizes.length];
|
||||
for (int i = 0; i < sizes.length; i++) {
|
||||
entries[i] = new RawZipEntry("entry" + i, 0, 0, content,
|
||||
crc.getValue(), sizes[i], 0);
|
||||
}
|
||||
return rawZip(entries);
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractsBackslashEntriesAsLiteralFilename(@TempDir Path target) throws IOException {
|
||||
// PowerShell 5 Compress-Archive writes '\' separators (issue #1198).
|
||||
@@ -77,6 +112,67 @@ class ZipExtractorTest {
|
||||
"traversal entry must not escape the target dir");
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsArchivesAboveTheConfiguredEntryCount(@TempDir Path target) throws IOException {
|
||||
byte[] zip = zipWithEntries("one.txt", "two.txt", "three.txt");
|
||||
|
||||
IOException thrown = assertThrows(IOException.class,
|
||||
() -> extractor.extractTo(zip, target, 2));
|
||||
|
||||
assertEquals("ZIP archive contains more than the configured 2 entries", thrown.getMessage());
|
||||
assertFalse(Files.exists(target.resolve("one.txt")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void acceptsArchivesAtTheConfiguredEntryCount(@TempDir Path target) throws IOException {
|
||||
byte[] zip = zipWithEntries("one.txt", "two.txt");
|
||||
|
||||
extractor.extractTo(zip, target, 2);
|
||||
|
||||
assertEquals("one.txt", Files.readString(target.resolve("one.txt")));
|
||||
assertEquals("two.txt", Files.readString(target.resolve("two.txt")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsAnInvalidEntryCountLimit(@TempDir Path target) throws IOException {
|
||||
IOException thrown = assertThrows(IOException.class,
|
||||
() -> extractor.extractTo(zipWith("one.txt", "one"), target, 0));
|
||||
|
||||
assertEquals("ZIP archive entry limit must be at least 1: 0", thrown.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsAnEntryAboveTheExpandedEntryLimit(@TempDir Path target) throws IOException {
|
||||
byte[] zip = zipWithDeclaredSizes(ZipExtractor.MAX_ENTRY_BYTES + 1);
|
||||
|
||||
IOException thrown = assertThrows(IOException.class, () -> extractor.extractTo(zip, target));
|
||||
|
||||
assertEquals("ZIP entry exceeds the 262144000 byte limit: entry0", thrown.getMessage());
|
||||
assertFalse(Files.exists(target.resolve("entry0")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsExpandedContentAboveTheArchiveLimit(@TempDir Path target) throws IOException {
|
||||
byte[] zip = zipWithDeclaredSizes(200L * 1024 * 1024, 200L * 1024 * 1024);
|
||||
|
||||
IOException thrown = assertThrows(IOException.class, () -> extractor.extractTo(zip, target));
|
||||
|
||||
assertEquals("ZIP archive exceeds the 262144000 byte expanded limit", thrown.getMessage());
|
||||
assertFalse(Files.exists(target.resolve("entry0")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void leavesThePreviousExtractionIntactAfterAResourceLimitFailure(@TempDir Path target)
|
||||
throws IOException {
|
||||
extractor.extractTo(zipWith("entry0", "previous"), target);
|
||||
|
||||
assertThrows(IOException.class,
|
||||
() -> extractor.extractTo(
|
||||
zipWithDeclaredSizes(200L * 1024 * 1024, 200L * 1024 * 1024), target));
|
||||
|
||||
assertEquals("previous", Files.readString(target.resolve("entry0")));
|
||||
}
|
||||
|
||||
/**
|
||||
* One entry for {@link #streamedZip}: {@code stored} selects STORED over DEFLATED,
|
||||
* as a packager does for content that does not compress.
|
||||
@@ -92,76 +188,105 @@ class ZipExtractorTest {
|
||||
* the bytes are assembled by hand.
|
||||
*/
|
||||
private static byte[] streamedZip(StreamedEntry... entries) throws IOException {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
List<int[]> localOffsets = new ArrayList<>();
|
||||
List<byte[]> names = new ArrayList<>();
|
||||
List<long[]> meta = new ArrayList<>();
|
||||
|
||||
for (StreamedEntry entry : entries) {
|
||||
byte[] name = entry.name().getBytes(StandardCharsets.UTF_8);
|
||||
RawZipEntry[] rawEntries = new RawZipEntry[entries.length];
|
||||
for (int i = 0; i < entries.length; i++) {
|
||||
StreamedEntry entry = entries[i];
|
||||
byte[] raw = entry.content().getBytes(StandardCharsets.UTF_8);
|
||||
byte[] payload = entry.stored() ? raw : deflate(raw);
|
||||
CRC32 crc = new CRC32();
|
||||
crc.update(raw);
|
||||
int method = entry.stored() ? 0 : 8;
|
||||
rawEntries[i] = new RawZipEntry(entry.name(), method, DATA_DESCRIPTOR_FLAG, payload,
|
||||
crc.getValue(), raw.length, raw.length);
|
||||
}
|
||||
return rawZip(rawEntries);
|
||||
}
|
||||
|
||||
localOffsets.add(new int[]{out.size()});
|
||||
le32(out, 0x04034b50L); // local file header signature
|
||||
le16(out, 20); // version needed
|
||||
le16(out, 0x0008); // general-purpose bit 3: data descriptor follows
|
||||
le16(out, method);
|
||||
le16(out, 0); // mod time
|
||||
le16(out, 0); // mod date
|
||||
le32(out, 0); // crc-32 — deferred to the descriptor
|
||||
le32(out, 0); // compressed — deferred to the descriptor
|
||||
le32(out, 0); // uncompressed — deferred to the descriptor
|
||||
le16(out, name.length);
|
||||
le16(out, 0); // extra length
|
||||
out.write(name);
|
||||
out.write(payload);
|
||||
le32(out, 0x08074b50L); // data descriptor signature
|
||||
le32(out, crc.getValue());
|
||||
le32(out, payload.length);
|
||||
le32(out, raw.length);
|
||||
/** Writes a raw archive so tests can use valid data with deliberately invalid declarations. */
|
||||
private static byte[] rawZip(RawZipEntry... entries) throws IOException {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
List<Integer> localOffsets = new ArrayList<>();
|
||||
|
||||
names.add(name);
|
||||
meta.add(new long[]{crc.getValue(), payload.length, raw.length, method});
|
||||
for (RawZipEntry entry : entries) {
|
||||
localOffsets.add(out.size());
|
||||
writeLocalHeader(out, entry);
|
||||
out.write(entry.payload());
|
||||
if ((entry.flags() & DATA_DESCRIPTOR_FLAG) != 0) {
|
||||
writeDataDescriptor(out, entry);
|
||||
}
|
||||
}
|
||||
|
||||
int centralOffset = out.size();
|
||||
for (int i = 0; i < entries.length; i++) {
|
||||
byte[] name = names.get(i);
|
||||
long[] m = meta.get(i);
|
||||
le32(out, 0x02014b50L); // central directory header signature
|
||||
le16(out, 20); // version made by
|
||||
le16(out, 20); // version needed
|
||||
le16(out, 0x0008); // same descriptor flag as the local header
|
||||
le16(out, (int) m[3]);
|
||||
le16(out, 0);
|
||||
le16(out, 0);
|
||||
le32(out, m[0]); // crc-32 — the real value lives here
|
||||
le32(out, m[1]); // compressed — the real value lives here
|
||||
le32(out, m[2]); // uncompressed — the real value lives here
|
||||
le16(out, name.length);
|
||||
le16(out, 0); // extra length
|
||||
le16(out, 0); // comment length
|
||||
le16(out, 0); // disk number start
|
||||
le16(out, 0); // internal attributes
|
||||
le32(out, 0); // external attributes
|
||||
le32(out, localOffsets.get(i)[0]);
|
||||
out.write(name);
|
||||
writeCentralDirectoryHeader(out, entries[i], localOffsets.get(i));
|
||||
}
|
||||
int centralSize = out.size() - centralOffset;
|
||||
writeEndOfCentralDirectory(out, entries.length, centralSize, centralOffset);
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
le32(out, 0x06054b50L); // end of central directory signature
|
||||
le16(out, 0); // this disk
|
||||
le16(out, 0); // disk with central directory
|
||||
le16(out, entries.length);
|
||||
le16(out, entries.length);
|
||||
private static void writeLocalHeader(ByteArrayOutputStream out, RawZipEntry entry) {
|
||||
boolean hasDescriptor = (entry.flags() & DATA_DESCRIPTOR_FLAG) != 0;
|
||||
byte[] name = entry.name().getBytes(StandardCharsets.UTF_8);
|
||||
// Local header: version, flags, method, timestamps, CRC, sizes, and file name.
|
||||
le32(out, LOCAL_FILE_HEADER_SIGNATURE);
|
||||
le16(out, 20);
|
||||
le16(out, entry.flags());
|
||||
le16(out, entry.method());
|
||||
le16(out, 0);
|
||||
le16(out, 0);
|
||||
le32(out, hasDescriptor ? 0 : entry.crc());
|
||||
le32(out, hasDescriptor ? 0 : entry.payload().length);
|
||||
le32(out, hasDescriptor ? 0 : entry.expandedSize());
|
||||
le16(out, name.length);
|
||||
le16(out, 0);
|
||||
out.writeBytes(name);
|
||||
}
|
||||
|
||||
private static void writeDataDescriptor(ByteArrayOutputStream out, RawZipEntry entry) {
|
||||
// Data descriptor: streamed entries write CRC and sizes after their payload.
|
||||
le32(out, DATA_DESCRIPTOR_SIGNATURE);
|
||||
le32(out, entry.crc());
|
||||
le32(out, entry.payload().length);
|
||||
le32(out, entry.descriptorExpandedSize());
|
||||
}
|
||||
|
||||
private static void writeCentralDirectoryHeader(ByteArrayOutputStream out,
|
||||
RawZipEntry entry, int localOffset) {
|
||||
byte[] name = entry.name().getBytes(StandardCharsets.UTF_8);
|
||||
// Central directory entry: metadata plus the offset of the matching local header.
|
||||
le32(out, CENTRAL_DIRECTORY_HEADER_SIGNATURE);
|
||||
le16(out, 20);
|
||||
le16(out, 20);
|
||||
le16(out, entry.flags());
|
||||
le16(out, entry.method());
|
||||
le16(out, 0);
|
||||
le16(out, 0);
|
||||
le32(out, entry.crc());
|
||||
le32(out, entry.payload().length);
|
||||
le32(out, entry.expandedSize());
|
||||
le16(out, name.length);
|
||||
le16(out, 0);
|
||||
le16(out, 0);
|
||||
le16(out, 0);
|
||||
le16(out, 0);
|
||||
le32(out, 0);
|
||||
le32(out, localOffset);
|
||||
out.writeBytes(name);
|
||||
}
|
||||
|
||||
private static void writeEndOfCentralDirectory(ByteArrayOutputStream out,
|
||||
int entryCount, int centralSize,
|
||||
int centralOffset) {
|
||||
// End record: entry count and the central directory's size and location.
|
||||
le32(out, END_OF_CENTRAL_DIRECTORY_SIGNATURE);
|
||||
le16(out, 0);
|
||||
le16(out, 0);
|
||||
le16(out, entryCount);
|
||||
le16(out, entryCount);
|
||||
le32(out, centralSize);
|
||||
le32(out, centralOffset);
|
||||
le16(out, 0); // comment length
|
||||
return out.toByteArray();
|
||||
le16(out, 0);
|
||||
}
|
||||
|
||||
private static byte[] deflate(byte[] raw) {
|
||||
|
||||
Reference in new issue
Block a user