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:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-10-22 10:40:03 -04:00
1 parent 5419b3cfd1
commit 2720f84884
9 files changed
+224 -33

No files matched your search

+1 -1
View File
@@ -35,7 +35,7 @@ The `Lambda Web` service model (apiVersion 2025-03-07, `aws lambda-web` in the A
## Emulation Behavior
- **Instant convergence:** a revision and an endpoint are stored `Active`, or `Failed` as below, and an endpoint update is stored `Successful`. The create and update responses still report the transitional state AWS reports (`Pending`, `updateStatus: InProgress`, and the per-region block as it was before the update), so a waiter sees one poll.
- **Failed revisions:** a revision whose code is not a zip is stored `Failed`, as on AWS. It cannot be deployed: weights that name it, or `LatestRevision` while it is the newest revision, are `409 ConflictException`, and `LatestRevision` endpoints stay on their revision. The endpoint of a `CreateWebFunction` whose revision fails is `Failed` too. It answers every request with `409` and `Platform.EndpointNotActive` and refuses updates until it is deleted. In a stack, the revision's resource fails with the revision's `stateReason` and the rollback deletes it. An empty object is a `500 InternalServerException` with the body `{}`, as on AWS.
- **Failed revisions:** a revision whose code is not a zip, or whose files add up to more than 512 MiB, is stored `Failed`, as on AWS. It cannot be deployed: weights that name it, or `LatestRevision` while it is the newest revision, are `409 ConflictException`, and `LatestRevision` endpoints stay on their revision. The endpoint of a `CreateWebFunction` whose revision fails is `Failed` too. It answers every request with `409` and `Platform.EndpointNotActive` and refuses updates until it is deleted. In a stack, the revision's resource fails with the revision's `stateReason` and the rollback deletes it. An empty object is a `500 InternalServerException` with the body `{}`, and an object over 512 MiB is a `400 ValidationException`, as on AWS.
- **Code comes from S3 only.** The object named in `buildConfig.codeConfig.s3Object` must exist in Floci's S3, as on AWS. AWS also requires bucket versioning and a bucket policy for the Lambda service principal. Floci does not check them yet.
- **Each revision keeps its own copy of the code**, read at creation from the version the request names or the current one, under the Lambda `code-path`. As on AWS, a later write or delete in the bucket does not change what the revision runs. AWS moves a revision to `Failed` when it later finds its object version gone, which Floci does not do.
- **Execution role and network connector** ARNs are checked for shape only. The assume-role check AWS makes is not done yet.
@@ -58,7 +58,7 @@ public class ZipExtractor {
Path staged = Files.createTempFile(absTarget.getParent(), ".floci-staging-", ".zip");
try {
Files.write(staged, zipBytes);
extract(staged, absTarget, maxEntries, beforeInstall);
extract(staged, absTarget, maxEntries, MAX_ENTRY_BYTES, MAX_EXPANDED_BYTES, beforeInstall);
} finally {
Files.deleteIfExists(staged);
}
@@ -73,20 +73,33 @@ public class ZipExtractor {
checkLimits(maxEntries, Files.size(zipFile));
Path absTarget = targetDir.toAbsolutePath().normalize();
Files.createDirectories(absTarget.getParent());
extract(zipFile, absTarget, maxEntries, beforeInstall);
extract(zipFile, absTarget, maxEntries, MAX_ENTRY_BYTES, MAX_EXPANDED_BYTES, beforeInstall);
}
/** For a package whose archive, files and expanded total may each reach {@code maxBytes}. */
public void extractTo(Path zipFile, Path targetDir, int maxEntries, long maxBytes) throws IOException {
checkLimits(maxEntries, Files.size(zipFile), maxBytes);
Path absTarget = targetDir.toAbsolutePath().normalize();
Files.createDirectories(absTarget.getParent());
extract(zipFile, absTarget, maxEntries, maxBytes, maxBytes, null);
}
static void checkLimits(int maxEntries, long compressedBytes) throws IOException {
checkLimits(maxEntries, compressedBytes, S3_MAX_COMPRESSED_BYTES);
}
private static void checkLimits(int maxEntries, long compressedBytes, long maxCompressedBytes) throws IOException {
if (maxEntries < 1) {
throw new IOException("ZIP archive entry limit must be at least 1: " + maxEntries);
}
if (compressedBytes > S3_MAX_COMPRESSED_BYTES) {
throw new IOException("ZIP archive exceeds the " + S3_MAX_COMPRESSED_BYTES
if (compressedBytes > maxCompressedBytes) {
throw new IOException("ZIP archive exceeds the " + maxCompressedBytes
+ " byte compressed limit: " + compressedBytes);
}
}
private void extract(Path archive, Path absTarget, int maxEntries, Runnable beforeInstall) throws IOException {
private void extract(Path archive, Path absTarget, int maxEntries, long maxEntryBytes, long maxExpandedBytes,
Runnable beforeInstall) throws IOException {
// Extract into a staging directory and swap it in, rather than writing over the target
// in place. A deployment package is the complete contents of the function, so a file
// dropped between two deploys has to disappear — writing in place only ever adds and
@@ -143,13 +156,13 @@ public class ZipExtractor {
}
long declaredSize = entry.getSize();
if (declaredSize > MAX_ENTRY_BYTES) {
throw new IOException("ZIP entry exceeds the " + MAX_ENTRY_BYTES
if (declaredSize > maxEntryBytes) {
throw new IOException("ZIP entry exceeds the " + maxEntryBytes
+ " byte limit: " + entryName);
}
long accountedSize = declaredSize >= 0 ? declaredSize : 0;
if (accountedSize > MAX_EXPANDED_BYTES - expandedBytes) {
throw new IOException("ZIP archive exceeds the " + MAX_EXPANDED_BYTES
if (accountedSize > maxExpandedBytes - expandedBytes) {
throw new IOException("ZIP archive exceeds the " + maxExpandedBytes
+ " byte expanded limit");
}
@@ -158,10 +171,10 @@ public class ZipExtractor {
} else {
Files.createDirectories(targetPath.getParent());
long copyLimit = declaredSize >= 0
? Math.min(declaredSize, MAX_EXPANDED_BYTES - expandedBytes)
: MAX_EXPANDED_BYTES - expandedBytes;
? Math.min(declaredSize, maxExpandedBytes - expandedBytes)
: maxExpandedBytes - expandedBytes;
long copiedBytes = copyVerified(zip, entry, targetPath,
MAX_ENTRY_BYTES, copyLimit);
maxEntryBytes, copyLimit);
accountedSize = declaredSize >= 0 ? declaredSize : copiedBytes;
}
expandedBytes += accountedSize;
@@ -19,8 +19,10 @@ import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.OptionalLong;
import java.util.Set;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
@@ -36,6 +38,9 @@ public class LambdaWebCode {
/** The {@code @} keeps this tree apart from the Lambda function directories, whose names never have one. */
private static final String ROOT = "@web-functions";
/** AWS refuses a larger object and fails a revision whose files add up to more. */
static final long MAX_BYTES = 512L * 1024 * 1024;
private final Path baseDir;
@Inject
@@ -59,17 +64,24 @@ public class LambdaWebCode {
}
}
boolean opensAsZip(String accountId, String region, String functionName, String revisionId) {
try (ZipFile ignored = new ZipFile(file(accountId, region, functionName, revisionId).toFile())) {
return true;
/** The unsigned sum of the sizes the zip declares for its files, or empty when the code is not a zip. */
OptionalLong unzippedSize(String accountId, String region, String functionName, String revisionId) {
try (ZipFile zip = new ZipFile(file(accountId, region, functionName, revisionId).toFile())) {
return OptionalLong.of(zip.stream().mapToLong(ZipEntry::getSize).reduce(0, LambdaWebCode::addSaturating));
} catch (ZipException e) {
return false;
return OptionalLong.empty();
} catch (IOException e) {
deleteRevision(accountId, region, functionName, revisionId);
throw new UncheckedIOException("Could not read the code of " + functionName + " revision " + revisionId, e);
}
}
/** AWS adds the sizes as unsigned 64-bit numbers and stops at the largest one. */
private static long addSaturating(long total, long size) {
long sum = total + size;
return Long.compareUnsigned(sum, total) < 0 ? -1L : sum;
}
void deleteRevision(String accountId, String region, String functionName, String revisionId) {
try {
Files.deleteIfExists(file(accountId, region, functionName, revisionId));
@@ -690,7 +690,7 @@ public class LambdaWebEnvironmentManager implements ContainerTeardown {
throw new UncheckedIOException("Could not make room for the code of " + fn.functionName, e);
}
try {
zipExtractor.extractTo(zip, dir, config.services().lambda().zipMaxEntries());
zipExtractor.extractTo(zip, dir, config.services().lambda().zipMaxEntries(), LambdaWebCode.MAX_BYTES);
RetryingTarCopier.copyStreamed(lifecycleManager.getDockerClient(), containerId, TASK_DIR,
"web-" + fn.functionName, out -> tar(dir, out));
} catch (IOException e) {
@@ -43,6 +43,7 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalLong;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
@@ -54,10 +55,10 @@ import java.util.regex.Pattern;
/**
* Control plane of Lambda Web Functions (apiVersion 2025-03-07). A revision is stored Active, or
* Failed when its code is not a zip. An endpoint is stored Active, or Failed when CreateWebFunction
* gives it a Failed revision, and an endpoint update is stored Successful. The create and update
* responses show the transitional state AWS reports. Invocation through an endpoint domain is not
* part of this service.
* Failed when its code is not a zip or unzips past the size limit. An endpoint is stored Active, or
* Failed when CreateWebFunction gives it a Failed revision, and an endpoint update is stored
* Successful. The create and update responses show the transitional state AWS reports. Invocation
* through an endpoint domain is not part of this service.
*/
@ApplicationScoped
public class LambdaWebService implements Resettable {
@@ -594,10 +595,36 @@ public class LambdaWebService implements Resettable {
String accountId = AwsArnUtils.parse(fn.functionArn).accountId();
WebFunctionRevision.BuildConfig build = revision.buildConfig;
long size;
try (S3Service.ObjectRead object = s3Service.openObject(build.bucket, build.key, build.versionId)) {
try (S3Service.ObjectRead object = openCode(build)) {
// AWS accepts an object of exactly this size, despite the wording.
if (LambdaWebCode.MAX_BYTES < object.object().getSize()) {
throw new AwsException("ValidationException", "Zipped size must be smaller than "
+ LambdaWebCode.MAX_BYTES + " bytes", 400);
}
size = code.store(accountId, region, fn.functionName, revision.revisionId, object.body());
} catch (IOException e) {
throw new UncheckedIOException("Could not close the code object s3://" + build.bucket + "/" + build.key, e);
}
if (size == 0) {
// An empty object is an internal error on AWS.
code.deleteRevision(accountId, region, fn.functionName, revision.revisionId);
throw new LambdaWebInternalServerException();
}
OptionalLong unzipped = code.unzippedSize(accountId, region, fn.functionName, revision.revisionId);
if (unzipped.isEmpty()) {
revision.failureReason = NOT_A_ZIP;
} else if (Long.compareUnsigned(LambdaWebCode.MAX_BYTES, unzipped.getAsLong()) < 0) {
revision.failureReason = "Uncompressed code size " + Long.toUnsignedString(unzipped.getAsLong())
+ " bytes exceeds the maximum allowed size of " + LambdaWebCode.MAX_BYTES + " bytes";
}
if (revision.failureReason != null) {
code.deleteRevision(accountId, region, fn.functionName, revision.revisionId);
}
}
private S3Service.ObjectRead openCode(WebFunctionRevision.BuildConfig build) {
try {
return s3Service.openObject(build.bucket, build.key, build.versionId);
} catch (AwsException e) {
LOG.debugv("Web function code object s3://{0}/{1} is not readable: {2}", build.bucket, build.key,
e.getMessage());
@@ -614,15 +641,6 @@ public class LambdaWebService implements Resettable {
throw new AwsException("ValidationException", "The S3 object " + object + " (versionId=" + build.versionId
+ ") is inaccessible. Verify the request parameters and permissions are correct.", 400);
}
if (size == 0) {
// An empty object is an internal error on AWS.
code.deleteRevision(accountId, region, fn.functionName, revision.revisionId);
throw new LambdaWebInternalServerException();
}
if (!code.opensAsZip(accountId, region, fn.functionName, revision.revisionId)) {
code.deleteRevision(accountId, region, fn.functionName, revision.revisionId);
revision.failureReason = NOT_A_ZIP;
}
}
private static void validateRevisionShape(LambdaWebValidation v, JsonNode request, String prefix) {
@@ -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
@@ -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);
}
}
@@ -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());
}
}