Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

fix(lambda): refuse a deployment package that is not a zip (#2414)

Jeff Heller388aecfa994e
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b586695c..f3e75f75 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
### Fixed
+- **Lambda — a package that is not a zip is refused** — `CreateFunction`, `UpdateFunctionCode` and `PublishLayerVersion` accepted a package that is not a zip (an empty file included) and answered success, `UpdateFunctionCode` keeping the previous code. They now answer `InvalidParameterValueException` `Could not unzip uploaded file. Please check your file, then try to upload again.` Contributed by @skialpine.
- **DynamoDB — `TransactWriteItems` follows DynamoDB's check order** — an `Update` whose `ConditionExpression` failed was cancelled with `ValidationError` when its `UpdateExpression` could not be applied to the item (for example `SET #c = #c + :one` on a missing item); it now cancels with `ConditionalCheckFailed`, as `UpdateItem` answers `ConditionalCheckFailedException` for the same request. Such an update-expression error on one member no longer hides a failed condition on another: both reasons are returned, while wrong-typed keys and index keys still cancel before any condition. A missing table answers `ResourceNotFoundException` even when another member has a `ValidationError`, and a `begins_with` condition with a number operand answers `ValidationException` instead of an internal error, as on AWS. Contributed by @jun-ut.
- **ALB — instance and IP targets receive the path as the client sent it** — the data plane forwarded the decoded path, so `%20` became a space and the request was refused with 502 (`URL can't contain control characters`), and `%2F` became `/`. The path now reaches the target with its percent-encoding, as AWS sends it; rules still match the decoded path. Contributed by @skialpine.
- **ALB — `authenticate-oidc` on a 443 listener names its callback without the client's port** — the `redirect_uri` kept a port the client sent in `Host`. On a 443 listener it is now `https://<host>/oauth2/idpresponse`, as AWS sends it. Contributed by @skialpine.
diff --git a/ministack/services/lambda_svc.py b/ministack/services/lambda_svc.py
index 526654d4..61172ca1 100644
--- a/ministack/services/lambda_svc.py
+++ b/ministack/services/lambda_svc.py
@@ -1302,6 +1302,17 @@ def _unzipped_size(zip_data: bytes | None) -> int:
return 0
+def _reject_unreadable_zip(zip_data: bytes | None):
+ """A deployment package that is not a zip (an empty file included) is refused; None means no bytes were given."""
+ if zip_data is None or zipfile.is_zipfile(io.BytesIO(zip_data)):
+ return None
+ return error_response_json(
+ "InvalidParameterValueException",
+ "Could not unzip uploaded file. Please check your file, then try to upload again.",
+ 400,
+ )
+
+
def _validate_unzipped_size(zip_data: bytes | None):
"""A single deployment package's unzipped size against the 250 MB quota —
used when publishing a layer version, whose own content must fit on its own."""
@@ -2346,7 +2357,7 @@ def _create_function(data: dict):
layers_cfg, err = _normalize_layer_attachments(data.get("Layers"))
if err:
return err
- err = _validate_total_unzipped_size(code_zip, layers_cfg)
+ err = _reject_unreadable_zip(code_zip) or _validate_total_unzipped_size(code_zip, layers_cfg)
if err:
return err
if data.get("Layers") is not None:
@@ -2835,7 +2846,7 @@ def _update_code(name: str, data: dict):
f"Failed to fetch code from s3://{data['S3Bucket']}/{data['S3Key']}",
400,
)
- err = _validate_total_unzipped_size(code_zip, func["config"].get("Layers"))
+ err = _reject_unreadable_zip(code_zip) or _validate_total_unzipped_size(code_zip, func["config"].get("Layers"))
if err is not None:
return err
if code_zip:
@@ -6407,7 +6418,7 @@ def _publish_layer_version(layer_name: str, data: dict):
version_id=content.get("S3ObjectVersion"),
)
- err = _validate_unzipped_size(zip_data)
+ err = _reject_unreadable_zip(zip_data) or _validate_unzipped_size(zip_data)
if err is not None:
return err
diff --git a/tests/test_lambda.py b/tests/test_lambda.py
index 48a76c95..73479395 100644
--- a/tests/test_lambda.py
+++ b/tests/test_lambda.py
@@ -2,6 +2,7 @@ import asyncio
import base64
import contextlib
import copy
+import functools
import io
import json
import os
@@ -2078,6 +2079,48 @@ def test_lambda_create_from_s3(lam, s3):
assert resp["StatusCode"] == 200
assert json.loads(resp["Payload"].read())["s3"] is True
+_NOT_A_ZIP = b"this is not a zipfile, just a random string"
+_UNZIP_MSG = "Could not unzip uploaded file. Please check your file, then try to upload again."
+
+
+def _refused_package_errors(lam, s3, package, tag):
+ """The error each operation answers to ``package``; UpdateFunctionCode reads it from S3."""
+ bucket = "lambda-code-bucket"
+ s3.create_bucket(Bucket=bucket)
+ s3.put_object(Bucket=bucket, Key=f"{tag}-ok.zip", Body=_make_zip("def handler(event, context): return 1"))
+ s3.put_object(Bucket=bucket, Key=f"{tag}-bad.zip", Body=package)
+ created = lam.create_function(
+ FunctionName=f"lam-{tag}-update", Runtime="python3.11", Role=_LAMBDA_ROLE, Handler="index.handler",
+ Code={"S3Bucket": bucket, "S3Key": f"{tag}-ok.zip"},
+ )
+ calls = (
+ lambda: lam.create_function(FunctionName=f"lam-{tag}-create", Runtime="python3.11", Role=_LAMBDA_ROLE,
+ Handler="index.handler", Code={"ZipFile": package}),
+ lambda: lam.update_function_code(FunctionName=f"lam-{tag}-update", S3Bucket=bucket, S3Key=f"{tag}-bad.zip"),
+ lambda: lam.publish_layer_version(LayerName=f"lam-{tag}-layer", Content={"ZipFile": package}),
+ )
+ errors = []
+ for call in calls:
+ with pytest.raises(ClientError) as exc:
+ call()
+ errors.append(exc.value.response["Error"])
+ # a refused update keeps the previous code, revision and modification time
+ after = lam.get_function_configuration(FunctionName=f"lam-{tag}-update")
+ assert {k: after[k] for k in ("CodeSha256", "RevisionId", "LastModified")} == {
+ k: created[k] for k in ("CodeSha256", "RevisionId", "LastModified")}
+ return errors
+
+
+def test_lambda_rejects_a_package_that_is_not_a_zip(lam, s3):
+ errors = _refused_package_errors(lam, s3, _NOT_A_ZIP, "notzip")
+ assert [(e["Code"], e["Message"]) for e in errors] == [("InvalidParameterValueException", _UNZIP_MSG)] * 3
+
+
+def test_lambda_rejects_an_empty_package(lam, s3):
+ errors = _refused_package_errors(lam, s3, b"", "empty")
+ assert [(e["Code"], e["Message"]) for e in errors] == [("InvalidParameterValueException", _UNZIP_MSG)] * 3
+
+
def test_lambda_update_code_from_s3(lam, s3):
bucket = "lambda-code-bucket"
buf = io.BytesIO()
@@ -15484,6 +15527,12 @@ def test_lambda_env_disables_host_prefix_injection_by_default(lam):
lam.delete_function(FunctionName=own)
+def _revision_zip(content):
+ """One zip per content, so equal content compares equal (zip entries carry a timestamp)."""
+ return _make_zip(content)
+
+
@pytest.fixture
def functions(monkeypatch):
store = AccountRegionScopedDict()
@@ -15491,7 +15540,7 @@ def functions(monkeypatch):
store[name] = {
"config": {"RevisionId": "function-before", "Architectures": ["x86_64"],
"Description": "original", "Version": "$LATEST"},
- "code_zip": b"original-code", "versions": {}, "next_version": 1,
+ "code_zip": _revision_zip("original-code"), "versions": {}, "next_version": 1,
"aliases": {alias: {"RevisionId": "alias-before", "FunctionVersion": "1",
"Description": "original"} for alias in ("live", "other")},
}
@@ -15505,7 +15554,7 @@ def functions(monkeypatch):
def _update(operation, revision="function-before", value="changed", name="revision-test"):
data = {"RevisionId": revision} if revision is not None else {}
if operation == "code":
- return _lambda_mod._update_code(name, {**data, "ZipFile": base64.b64encode(value.encode()).decode()})
+ return _lambda_mod._update_code(name, {**data, "ZipFile": base64.b64encode(_revision_zip(value)).decode()})
if operation == "config":
return _lambda_mod._update_config(name, {**data, "Description": value})
return _lambda_mod._update_alias(name, "live", {**data, "Description": value, "FunctionVersion": "2"})
@@ -15615,11 +15664,11 @@ def test_competing_updates_with_same_revision_have_one_winner(functions, monkeyp
if first == "alias":
assert record["aliases"]["live"]["Description"] == "winner"
elif first == "code":
- assert record["code_zip"] == b"winner"
+ assert record["code_zip"] == _revision_zip("winner")
assert record["config"]["Description"] == "original"
else:
assert record["config"]["Description"] == "winner"
- assert record["code_zip"] == b"original-code"
+ assert record["code_zip"] == _revision_zip("original-code")
def test_slow_code_update_does_not_block_independent_resources(functions, monkeypatch):
@@ -15629,7 +15678,7 @@ def test_slow_code_update_does_not_block_independent_resources(functions, monkey
def fetch(*args, **kwargs):
entered.set()
assert release.wait(5)
- return b"new-code"
+ return _revision_zip("new-code")
monkeypatch.setattr(_lambda_mod, "_fetch_code_from_s3", fetch)
with ThreadPoolExecutor(max_workers=2) as pool:
diff --git a/tests/test_multitenancy.py b/tests/test_multitenancy.py
index d11e4a82..120b3b86 100644
--- a/tests/test_multitenancy.py
+++ b/tests/test_multitenancy.py
@@ -6,7 +6,9 @@ in all ARN generation. Non-numeric keys (like "test") fall back to the default
000000000000.
"""
+import io
import os
+import zipfile
import boto3
from botocore.config import Config
@@ -103,13 +105,16 @@ def test_sqs_queues_isolated_by_account():
def test_lambda_function_arn_uses_dynamic_account():
"""Lambda function ARN reflects the 12-digit access key."""
lam = _client("lambda", access_key="999888777666")
+ buf = io.BytesIO()
+ with zipfile.ZipFile(buf, "w") as zf:
+ zf.writestr("index.py", "def handler(event, context): return 1")
try:
lam.create_function(
FunctionName="mt-func",
Runtime="python3.12",
Role="arn:aws:iam::999888777666:role/test",
Handler="index.handler",
- Code={"ZipFile": b"fake"},
+ Code={"ZipFile": buf.getvalue()},
)
resp = lam.get_function(FunctionName="mt-func")
arn = resp["Configuration"]["FunctionArn"]