Skip to content

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

fix(apigatewayv2): 401 on a missing identity source without caching (#1892)

Jeff Heller369fdc21f8f7
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5d518141..b405e752 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,9 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
+### Fixed
+
+- **API Gateway v2 (HTTP API) — a missing identity source is a 401 without caching too** — a REQUEST authorizer's short circuit for a declared identity source missing from the request only fired with `authorizerResultTtlInSeconds` caching on; with caching off the authorizer was invoked. AWS answers `401 {"message":"Unauthorized"}` (compact JSON) without invoking it either way, as observed on a deployed HTTP API with a TTL of 0. `$context.*` identity sources, which MiniStack does not model, do not count as missing without caching.
### Added
- **IoT — `DescribeEventConfigurations` and `UpdateEventConfigurations`** — both answered `Unsupported IoT path`; they now store the registry event switches per account and region, every type starting disabled, an update changing only the types it names, and `creationDate` / `lastModifiedDate` set from the first update on, as on AWS. Contributed by @iot-rocket.
diff --git a/ministack/services/apigateway.py b/ministack/services/apigateway.py
index ea4f6f0b..12c10000 100644
--- a/ministack/services/apigateway.py
+++ b/ministack/services/apigateway.py
@@ -971,11 +971,16 @@ async def _authorize_request_v2(
# there is nothing to key on and every caller would otherwise be served
# the first caller's result.
caching = ttl > 0 and bool(identity_source)
- # A missing declared identity source is a 401 without invoking the
- # Lambda — same AWS-verified shortcut apigateway_v1 uses for REST
- # REQUEST authorizers.
- if caching and not all_present:
- return _jwt_unauthorized(), None
+ # A declared identity source missing from the request is a 401 without
+ # invoking the Lambda, cached or not, with AWS's compact body (observed on a
+ # deployed HTTP API). $context.* sources are not modeled, so uncached they
+ # never count as missing.
+ modeled_present, _ = _request_authorizer_identity_sources(
+ [s for s in identity_source if not str(s).startswith("$context.")],
+ headers, query_params, stage_vars,
+ )
+ if identity_source and not (all_present if caching else modeled_present):
+ return (401, {"Content-Type": "application/json"}, b'{"message":"Unauthorized"}'), None
identity_values = tuple(id_values)
if payload_version == "1.0":
diff --git a/tests/test_apigatewayv2.py b/tests/test_apigatewayv2.py
index bd4fa780..7b3bbf4b 100644
--- a/tests/test_apigatewayv2.py
+++ b/tests/test_apigatewayv2.py
@@ -4191,6 +4191,59 @@ def test_apigwv2_authorizer_without_identity_source_does_not_cache(apigw, lam, s
_v2_auth_delete_queue(sqs, qname)
+def test_apigwv2_authorizer_missing_identity_source_401s_without_invoking(apigw, lam, sqs):
+ """A declared identity source missing from the request is a 401 without
+ invoking the Lambda, even with caching disabled (``AuthorizerResultTtlInSeconds=0``).
+
+ AWS applies this identity-source short circuit independent of caching;
+ it is not only a caching optimization. The body matches AWS's own
+ compact-JSON gateway response exactly (no space after the colon).
+ """
+ qname = _v2_auth_counter_queue(sqs)
+ backend = _v2_auth_make_lambda(lam, "be", _V2_AUTH_ECHO_BACKEND)
+ authz = _v2_auth_make_lambda(lam, "pol", _v2_auth_policy_authorizer_code(qname))
+ api_id, _ = _v2_auth_build_api(
+ apigw, authz, backend,
+ dict(Name="pol", AuthorizerPayloadFormatVersion="2.0",
+ IdentitySource=["$request.header.Authorization"],
+ AuthorizerResultTtlInSeconds=0),
+ )
+ try:
+ url = _v2_auth_execute_url(api_id, "test", "secure")
+ status, body = _v2_auth_http(url) # no Authorization header sent
+ assert status == 401
+ assert body == b'{"message":"Unauthorized"}'
+ assert _v2_auth_count(sqs, qname) == 0, "the authorizer Lambda must not be invoked"
+ finally:
+ _v2_auth_drop_api(apigw, api_id)
+ _v2_auth_drop_lambda(lam, backend)
+ _v2_auth_drop_lambda(lam, authz)
+ _v2_auth_delete_queue(sqs, qname)
+
+
+def test_apigwv2_authorizer_context_identity_source_still_invokes(apigw, lam, sqs):
+ """A $context.* identity source, which MiniStack does not model, is not a missing source: uncached, the
+ authorizer is still invoked."""
+ qname = _v2_auth_counter_queue(sqs)
+ backend = _v2_auth_make_lambda(lam, "be", _V2_AUTH_ECHO_BACKEND)
+ authz = _v2_auth_make_lambda(lam, "pol", _v2_auth_policy_authorizer_code(qname))
+ api_id, _ = _v2_auth_build_api(
+ apigw, authz, backend,
+ dict(Name="pol", AuthorizerPayloadFormatVersion="2.0",
+ IdentitySource=["$context.identity.sourceIp"],
+ AuthorizerResultTtlInSeconds=0),
+ )
+ try:
+ status, _body = _v2_auth_http(_v2_auth_execute_url(api_id, "test", "secure"))
+ assert status != 401
+ assert _v2_auth_count(sqs, qname) == 1, "the authorizer Lambda must be invoked"
+ finally:
+ _v2_auth_drop_api(apigw, api_id)
+ _v2_auth_drop_lambda(lam, backend)
+ _v2_auth_drop_lambda(lam, authz)
+ _v2_auth_delete_queue(sqs, qname)
+
+
def test_apigwv2_authorizer_cache_is_bounded():
"""The result cache is keyed on caller-supplied identity values, so it is
capped rather than left to grow for the life of the process. Expired entries