mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(lambdaweb): accept code up to 512 MiB and refuse more as AWS does (#6218)
* feat(lambdaweb): accept code up to 512 MiB and refuse more as AWS does AWS takes a Web Function package of up to 536870912 bytes, zipped and unzipped. A larger object is a 400 ValidationException at creation. A zip whose files add up to more gives a Failed revision. * fix(lambdaweb): add the declared sizes as unsigned numbers like AWS AWS adds the sizes as unsigned 64-bit numbers and stops at the largest one. A zip that declares sizes past the signed range is Failed too. * test(lambdaweb): declare the large sizes instead of writing them
This commit is contained in:
1 parent
5419b3cfd1
commit
2720f84884
9 files changed
+224
-33
No files matched your search
@@ -467,6 +467,22 @@ class ZipExtractorTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void aPackageMayExpandToTheLimitItIsGiven(@TempDir Path codeStore) throws IOException {
|
||||
long limit = 512L * 1024 * 1024;
|
||||
Path archive = codeStore.resolve("code.zip");
|
||||
Path target = codeStore.resolve("fn");
|
||||
Files.write(archive, zipWithDeclaredSizes(300L * 1024 * 1024, 212L * 1024 * 1024));
|
||||
|
||||
extractor.extractTo(archive, target, ZipExtractor.DEFAULT_MAX_ENTRIES, limit);
|
||||
assertTrue(Files.exists(target.resolve("entry0")));
|
||||
|
||||
Files.write(archive, zipWithDeclaredSizes(300L * 1024 * 1024, 212L * 1024 * 1024 + 1));
|
||||
IOException thrown = assertThrows(IOException.class,
|
||||
() -> extractor.extractTo(archive, target, ZipExtractor.DEFAULT_MAX_ENTRIES, limit));
|
||||
assertEquals("ZIP archive exceeds the 536870912 byte expanded limit", thrown.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsAnEntryWhoseCompressionMethodCannotBeRead(@TempDir Path target) throws IOException {
|
||||
// A package must never be half-deployed because an entry used a codec the reader does
|
||||
|
||||
+35
@@ -663,4 +663,39 @@ class LambdaWebIntegrationTest {
|
||||
|
||||
given().when().delete(BASE + "/web-functions/" + fn).then().statusCode(204);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(12)
|
||||
void codeThatUnzipsPastTheLimitFailsItsRevision() {
|
||||
String fn = FN + "-large";
|
||||
s3Service.putObject(BUCKET, "large.zip", LambdaWebZips.declaring(LambdaWebCode.MAX_BYTES + 1),
|
||||
"application/zip", Map.of());
|
||||
given().contentType("application/json")
|
||||
.body("""
|
||||
{"functionName": "%s",
|
||||
"revisionConfig": {"buildConfig": %s, "serviceConfig": {"executionRoleArn": "%s"}},
|
||||
"endpointConfig": {"endpointName": "dev", "endpointType": "HomeRegion",
|
||||
"authType": "ApplicationManaged", "autoDeploymentMode": "LatestRevision"}}
|
||||
""".formatted(fn, BUILD, ROLE))
|
||||
.when().put(BASE + "/web-functions")
|
||||
.then().statusCode(202);
|
||||
|
||||
String large = given().contentType("application/json")
|
||||
.body("""
|
||||
{"buildConfig": {"codeConfig": {"s3Object": {"bucket": "%s", "key": "large.zip"}},
|
||||
"runtimeConfig": {"runtime": "nodejs24.x"}},
|
||||
"serviceConfig": {"executionRoleArn": "%s"}}
|
||||
""".formatted(BUCKET, ROLE))
|
||||
.when().post(BASE + "/web-functions/" + fn + "/revisions")
|
||||
.then().statusCode(202)
|
||||
.body("state", equalTo("Pending"))
|
||||
.extract().path("revisionId");
|
||||
given().when().get(BASE + "/web-functions/" + fn + "/revisions/" + large)
|
||||
.then().statusCode(200)
|
||||
.body("state", equalTo("Failed"))
|
||||
.body("stateReason", equalTo("Uncompressed code size 536870913 bytes exceeds the maximum allowed size"
|
||||
+ " of 536870912 bytes"));
|
||||
|
||||
given().when().delete(BASE + "/web-functions/" + fn).then().statusCode(204);
|
||||
}
|
||||
}
|
||||
+70
-1
@@ -658,7 +658,7 @@ class LambdaWebServiceTest {
|
||||
Path copy = code.file(ACCOUNT_ID, REGION, "app", "rev-aaaaa");
|
||||
Files.createDirectories(copy);
|
||||
|
||||
assertThrows(UncheckedIOException.class, () -> code.opensAsZip(ACCOUNT_ID, REGION, "app", "rev-aaaaa"));
|
||||
assertThrows(UncheckedIOException.class, () -> code.unzippedSize(ACCOUNT_ID, REGION, "app", "rev-aaaaa"));
|
||||
assertFalse(Files.exists(copy));
|
||||
}
|
||||
|
||||
@@ -782,6 +782,69 @@ class LambdaWebServiceTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRevisionWhoseFilesAddUpPastTheLimitIsFailed() {
|
||||
S3Service s3 = mock(S3Service.class);
|
||||
LambdaWebService service = service(new MemoryStorageFactory(), "http://localhost:4566", s3);
|
||||
when(s3.openObject("code", "app.zip", null))
|
||||
.thenReturn(object(LambdaWebZips.declaring(LambdaWebCode.MAX_BYTES)));
|
||||
WebFunctionRevision full = service.createFunction(REGION, createRequest("app")).revisions.values()
|
||||
.iterator().next();
|
||||
when(s3.openObject("code", "app.zip", null))
|
||||
.thenReturn(object(LambdaWebZips.declaring(LambdaWebCode.MAX_BYTES + 1)));
|
||||
WebFunctionRevision over = service.createRevision(REGION, "app", revisionConfig(30));
|
||||
|
||||
assertNull(full.failureReason);
|
||||
assertEquals("Uncompressed code size 536870913 bytes exceeds the maximum allowed size of 536870912 bytes",
|
||||
over.failureReason);
|
||||
LambdaWebCode code = new LambdaWebCode(storageDir.resolve("code"));
|
||||
assertTrue(Files.exists(code.file(ACCOUNT_ID, REGION, "app", full.revisionId)));
|
||||
assertFalse(Files.exists(code.file(ACCOUNT_ID, REGION, "app", over.revisionId)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void declaredSizesAddUpWithoutWrappingAround() {
|
||||
S3Service s3 = mock(S3Service.class);
|
||||
LambdaWebService service = service(new MemoryStorageFactory(), "http://localhost:4566", s3);
|
||||
when(s3.openObject("code", "app.zip", null))
|
||||
.thenReturn(object(LambdaWebZips.declaring(Long.MAX_VALUE, 1)));
|
||||
WebFunctionRevision signed = service.createFunction(REGION, createRequest("app")).revisions.values()
|
||||
.iterator().next();
|
||||
when(s3.openObject("code", "app.zip", null))
|
||||
.thenReturn(object(LambdaWebZips.declaring(Long.MAX_VALUE, Long.MAX_VALUE, 2)));
|
||||
WebFunctionRevision unsigned = service.createRevision(REGION, "app", revisionConfig(30));
|
||||
|
||||
assertEquals("Uncompressed code size 9223372036854775808 bytes exceeds the maximum allowed size of "
|
||||
+ "536870912 bytes", signed.failureReason);
|
||||
assertEquals("Uncompressed code size 18446744073709551615 bytes exceeds the maximum allowed size of "
|
||||
+ "536870912 bytes", unsigned.failureReason);
|
||||
}
|
||||
|
||||
@Test
|
||||
void anObjectPastTheZippedLimitIsRefusedAndCreatesNothing() throws IOException {
|
||||
S3Service s3 = mock(S3Service.class);
|
||||
LambdaWebService service = service(new MemoryStorageFactory(), "http://localhost:4566", s3);
|
||||
byte[] zip = LambdaWebZips.indexJs("");
|
||||
String refusal = "Zipped size must be smaller than 536870912 bytes";
|
||||
when(s3.openObject("code", "app.zip", null)).thenReturn(object(zip, LambdaWebCode.MAX_BYTES + 1));
|
||||
|
||||
AwsException create = error(() -> service.createFunction(REGION, createRequest("app")));
|
||||
assertEquals("ValidationException", create.getErrorCode());
|
||||
assertEquals(400, create.getHttpStatus());
|
||||
assertEquals(refusal, create.getMessage());
|
||||
assertEquals("The function arn:aws:lambda:us-east-1:000000000000:web-function/app does not exist.",
|
||||
message(() -> service.getFunction(REGION, "app")));
|
||||
|
||||
when(s3.openObject("code", "app.zip", null)).thenReturn(object(zip, LambdaWebCode.MAX_BYTES));
|
||||
service.createFunction(REGION, createRequest("app"));
|
||||
when(s3.openObject("code", "app.zip", null)).thenReturn(object(zip, LambdaWebCode.MAX_BYTES + 1));
|
||||
assertEquals(refusal, message(() -> service.createRevision(REGION, "app", revisionConfig(30))));
|
||||
assertEquals(1, service.getFunction(REGION, "app").revisions.size());
|
||||
try (Stream<Path> copies = Files.walk(storageDir.resolve("code"))) {
|
||||
assertEquals(1, copies.filter(Files::isRegularFile).count());
|
||||
}
|
||||
}
|
||||
|
||||
private ObjectNode stateFilter(String state) {
|
||||
ObjectNode request = mapper.createObjectNode();
|
||||
request.putArray("filters").addObject().put("name", "state").putArray("values").add(state);
|
||||
@@ -793,6 +856,12 @@ class LambdaWebServiceTest {
|
||||
new ByteArrayInputStream(bytes));
|
||||
}
|
||||
|
||||
/** An object S3 reports as {@code size} bytes, whatever its body. */
|
||||
private static S3Service.ObjectRead object(byte[] bytes, long size) {
|
||||
return new S3Service.ObjectRead(new S3Object("code", "app.zip", size, "application/zip", "etag"),
|
||||
new ByteArrayInputStream(bytes));
|
||||
}
|
||||
|
||||
private static final class MemoryStorageFactory extends StorageFactory {
|
||||
private final Map<String, StorageBackend<String, ?>> stores = new HashMap<>();
|
||||
|
||||
|
||||
@@ -3,7 +3,10 @@ package io.github.hectorvent.floci.services.lambdaweb;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
@@ -24,4 +27,29 @@ public final class LambdaWebZips {
|
||||
}
|
||||
return bytes.toByteArray();
|
||||
}
|
||||
|
||||
/** Files whose ZIP64 fields declare these sizes over empty data. */
|
||||
public static byte[] declaring(long... sizes) {
|
||||
byte[] empty = {3, 0};
|
||||
ByteBuffer out = ByteBuffer.allocate(256 * sizes.length + 22).order(ByteOrder.LITTLE_ENDIAN);
|
||||
ByteBuffer central = ByteBuffer.allocate(128 * sizes.length).order(ByteOrder.LITTLE_ENDIAN);
|
||||
for (int i = 0; i < sizes.length; i++) {
|
||||
byte[] name = ("file" + i).getBytes(StandardCharsets.UTF_8);
|
||||
int offset = out.position();
|
||||
out.putInt(0x04034b50).putShort((short) 45).putShort((short) 0).putShort((short) 8)
|
||||
.putInt(0).putInt(0).putInt(-1).putInt(-1).putShort((short) name.length).putShort((short) 20)
|
||||
.put(name).putShort((short) 1).putShort((short) 16).putLong(sizes[i]).putLong(empty.length)
|
||||
.put(empty);
|
||||
central.putInt(0x02014b50).putShort((short) 45).putShort((short) 45).putShort((short) 0)
|
||||
.putShort((short) 8).putInt(0).putInt(0).putInt(-1).putInt(-1).putShort((short) name.length)
|
||||
.putShort((short) 20).putShort((short) 0).putShort((short) 0).putShort((short) 0).putInt(0)
|
||||
.putInt(offset).put(name).putShort((short) 1).putShort((short) 16).putLong(sizes[i])
|
||||
.putLong(empty.length);
|
||||
}
|
||||
int centralOffset = out.position();
|
||||
out.put(central.flip()).putInt(0x06054b50).putShort((short) 0).putShort((short) 0)
|
||||
.putShort((short) sizes.length).putShort((short) sizes.length).putInt(central.limit())
|
||||
.putInt(centralOffset).putShort((short) 0);
|
||||
return Arrays.copyOf(out.array(), out.position());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user