mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -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
@@ -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));
|
||||
|
||||
+1
-1
@@ -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) {
|
||||
|
||||
Reference in new issue
Block a user