fix(apigatewayv2): CloudFormation AuthorizationScopes pass-through and scope-ingestion hardening (follow-up to #2011) (#2431)

* fix(apigatewayv2): pass AuthorizationScopes through CloudFormation routes and harden scope ingestion

Follow-ups from PR #2011 review:

- AWS::ApiGatewayV2::Route now passes AuthorizationScopes to route
  create/update, so CFN-provisioned scoped routes enforce the 403 path.
  The property is always sent (empty when absent) so removing it from a
  template clears the scopes on UpdateStack.
- authorizationScopes elements are normalized to strings at ingestion
  instead of unchecked-cast; a non-string JSON element used to throw
  ClassCastException during response serialization.
- Add JSON 1.1 round-trip coverage for AuthorizationScopes on routes
  (the ApiGatewayV2JsonHandler rendering block was previously unguarded).

* fix(apigatewayv2): reject non-array authorizationScopes with 400

A present-but-non-array value was silently normalized to null, which
created the route without scope enforcement or cleared an existing
route's scopes on update. It now fails with BadRequestException, and
an update that 400s leaves the stored scopes untouched.

---------

Co-authored-by: mitsukado-shinagawa <mitsukado-shinagawa@users.noreply.github.com>
This commit is contained in:
mitsukado-shinagawaandmitsukado-shinagawa authored and GitHub committed 2026-08-20 23:44:21 -04:00
1 parent 760200223a
commit d0a96d87b1
5 files changed
+293 -6

No files matched your search

@@ -361,9 +361,7 @@ public class ApiGatewayV2Service {
route.setRouteKey((String) request.get("routeKey"));
route.setAuthorizationType((String) request.getOrDefault("authorizationType", "NONE"));
route.setAuthorizerId((String) request.get("authorizerId"));
@SuppressWarnings("unchecked")
List<String> authorizationScopes = (List<String>) request.get("authorizationScopes");
route.setAuthorizationScopes(authorizationScopes);
route.setAuthorizationScopes(toStringList(request.get("authorizationScopes")));
route.setTarget((String) request.get("target"));
route.setRouteResponseSelectionExpression((String) request.get("routeResponseSelectionExpression"));
@@ -400,9 +398,7 @@ public class ApiGatewayV2Service {
route.setAuthorizerId((String) request.get("authorizerId"));
}
if (request.containsKey("authorizationScopes") && request.get("authorizationScopes") != null) {
@SuppressWarnings("unchecked")
List<String> authorizationScopes = (List<String>) request.get("authorizationScopes");
route.setAuthorizationScopes(authorizationScopes);
route.setAuthorizationScopes(toStringList(request.get("authorizationScopes")));
}
if (request.containsKey("target") && request.get("target") != null) {
route.setTarget((String) request.get("target"));
@@ -415,6 +411,25 @@ public class ApiGatewayV2Service {
return route;
}
// JSON bodies can carry non-string elements (numbers, booleans); downstream code
// (response serialization, scope enforcement) assumes String elements, so normalize
// at ingestion instead of unchecked-casting. A present-but-non-array value must be
// rejected, not treated as absent — silently returning null would create the route
// without scope enforcement (or clear it on update).
private static List<String> toStringList(Object value) {
if (value == null) {
return null;
}
if (!(value instanceof List<?> list)) {
throw new AwsException("BadRequestException",
"authorizationScopes must be a list of strings", 400);
}
return list.stream()
.filter(java.util.Objects::nonNull)
.map(String::valueOf)
.toList();
}
/**
* Finds the best matching route for the given HTTP method and path.
* Priority: exact match > path-template match > $default.
@@ -5305,6 +5305,9 @@ public class CloudFormationResourceProvisioner {
req.put("routeKey", resolveOptional(props, "RouteKey", engine));
req.put("authorizationType", resolveOrDefault(props, "AuthorizationType", engine, "NONE"));
req.put("authorizerId", resolveOptional(props, "AuthorizerId", engine));
// Always present (empty when the property is absent) so an UpdateStack that removes
// AuthorizationScopes from the template clears the route's scopes instead of keeping them.
req.put("authorizationScopes", resolveStringListOrEmpty(props, "AuthorizationScopes", engine));
req.put("target", resolveOptional(props, "Target", engine));
Route route;