From bdbc7e93d3a38d6f243cc4aa27e16a7e9b79ab41 Mon Sep 17 00:00:00 2001 From: Hyun Lee <43563245+dlwhdgus0810@users.noreply.github.com> Date: Thu, 24 Sep 2026 13:41:18 -0500 Subject: [PATCH] fix(apigatewayv2): reject an out-of-range AuthorizerResultTtlInSeconds (#4294) CreateAuthorizer and UpdateAuthorizer read AuthorizerResultTtlInSeconds with a bare Number cast and stored any integer, so -1 or 99999 was accepted and returned by GetAuthorizer. The API Gateway v2 model constrains the member to 0-3600 (IntegerWithLengthBetween0And3600) and lists BadRequestException (HTTP 400) as the error for invalid input, which is what the service now throws. UpdateAuthorizer checks the value before changing any field, because the store hands back the live authorizer and a rejected request must not leave half of the patch applied. ImportApi and ReimportApi check the x-amazon-apigateway-authorizer TTL while planning, so a rejected reimport keeps the previous definition. AWS::ApiGatewayV2::Authorizer goes through the same service call, so an out-of-range value now fails the resource. Closes #4072 --- .../ApiGatewayV2OpenApiImporter.java | 3 +- .../apigatewayv2/ApiGatewayV2Service.java | 23 +++-- .../ApiGatewayV2IntegrationTest.java | 88 +++++++++++++++++++ .../ApiGatewayV2OpenApiImportTest.java | 28 ++++++ .../CloudFormationIntegrationTest.java | 49 +++++++++++ 5 files changed, 185 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2OpenApiImporter.java b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2OpenApiImporter.java index 303440f907..0e51db4882 100644 --- a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2OpenApiImporter.java +++ b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2OpenApiImporter.java @@ -526,7 +526,8 @@ public class ApiGatewayV2OpenApiImporter { putIfPresent(request, "authorizerPayloadFormatVersion", stringValue(authDef.get("authorizerPayloadFormatVersion"))); if (authDef.get("authorizerResultTtlInSeconds") instanceof Number ttl) { - request.put("authorizerResultTtlInSeconds", ttl); + // Range-checked while planning so a rejected ReimportApi keeps the previous definition. + request.put("authorizerResultTtlInSeconds", ApiGatewayV2Service.authorizerResultTtl(ttl)); } if (authDef.get("enableSimpleResponses") != null) { request.put("enableSimpleResponses", authDef.get("enableSimpleResponses")); diff --git a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java index 273f6338f0..17803580e3 100644 --- a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java +++ b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java @@ -365,9 +365,7 @@ public class ApiGatewayV2Service { auth.setAuthorizerUri((String) request.get("authorizerUri")); auth.setAuthorizerPayloadFormatVersion((String) request.get("authorizerPayloadFormatVersion")); - if (request.get("authorizerResultTtlInSeconds") != null) { - auth.setAuthorizerResultTtlInSeconds(((Number) request.get("authorizerResultTtlInSeconds")).intValue()); - } + auth.setAuthorizerResultTtlInSeconds(authorizerResultTtl(request.get("authorizerResultTtlInSeconds"))); if (request.get("enableSimpleResponses") != null) { auth.setEnableSimpleResponses(Boolean.parseBoolean(String.valueOf(request.get("enableSimpleResponses")))); } @@ -412,6 +410,8 @@ public class ApiGatewayV2Service { public Authorizer updateAuthorizer(String region, String apiId, String authorizerId, Map request) { Authorizer auth = getAuthorizer(region, apiId, authorizerId); + // Validated before any field changes: the store hands back the live authorizer. + Integer ttl = authorizerResultTtl(request.get("authorizerResultTtlInSeconds")); if (request.containsKey("name") && request.get("name") != null) { auth.setName((String) request.get("name")); @@ -443,8 +443,8 @@ public class ApiGatewayV2Service { if (request.containsKey("authorizerPayloadFormatVersion") && request.get("authorizerPayloadFormatVersion") != null) { auth.setAuthorizerPayloadFormatVersion((String) request.get("authorizerPayloadFormatVersion")); } - if (request.containsKey("authorizerResultTtlInSeconds") && request.get("authorizerResultTtlInSeconds") != null) { - auth.setAuthorizerResultTtlInSeconds(((Number) request.get("authorizerResultTtlInSeconds")).intValue()); + if (ttl != null) { + auth.setAuthorizerResultTtlInSeconds(ttl); } if (request.containsKey("enableSimpleResponses") && request.get("enableSimpleResponses") != null) { auth.setEnableSimpleResponses(Boolean.parseBoolean(String.valueOf(request.get("enableSimpleResponses")))); @@ -454,6 +454,19 @@ public class ApiGatewayV2Service { return auth; } + // AuthorizerResultTtlInSeconds is modeled as an integer in [0, 3600]. doubleValue() keeps the + // bounds check exact for a Long or BigInteger body value that intValue() would wrap into range. + static Integer authorizerResultTtl(Object value) { + if (value == null) { + return null; + } + if (!(value instanceof Number ttl) || ttl.doubleValue() < 0 || ttl.doubleValue() > 3600) { + throw new AwsException("BadRequestException", + "authorizerResultTtlInSeconds must be an integer between 0 and 3600", 400); + } + return ttl.intValue(); + } + // ──────────────────────────── Route CRUD ──────────────────────────── public Route createRoute(String region, String apiId, Map request) { diff --git a/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2IntegrationTest.java index 1668e67d97..d0396e63a7 100644 --- a/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2IntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2IntegrationTest.java @@ -10,6 +10,8 @@ import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import java.util.List; import java.util.Map; @@ -278,6 +280,92 @@ class ApiGatewayV2IntegrationTest { assertEquals(List.of("api"), restored.getJwtConfiguration().audience()); } + @ParameterizedTest @Order(34) + @ValueSource(ints = {0, 3600}) + void createAuthorizer_acceptsResultTtlAtTheBounds(int ttl) { + given() + .contentType(ContentType.JSON) + .body(requestAuthorizer("ttl-create-" + ttl, ttl)) + .when().post("/v2/apis/" + apiId + "/authorizers") + .then() + .statusCode(201) + .body("authorizerResultTtlInSeconds", equalTo(ttl)); + } + + @ParameterizedTest @Order(35) + @ValueSource(ints = {-1, 3601}) + void createAuthorizer_rejectsResultTtlOutOfRange(int ttl) { + String name = "ttl-create-rejected-" + ttl; + given() + .contentType(ContentType.JSON) + .body(requestAuthorizer(name, ttl)) + .when().post("/v2/apis/" + apiId + "/authorizers") + .then() + .statusCode(400) + .body("__type", equalTo("BadRequestException")) + .body("message", containsString("between 0 and 3600")); + + given() + .when().get("/v2/apis/" + apiId + "/authorizers") + .then() + .statusCode(200) + .body("items.name", not(hasItem(name))); + } + + @ParameterizedTest @Order(36) + @ValueSource(ints = {0, 3600}) + void updateAuthorizer_acceptsResultTtlAtTheBounds(int ttl) { + String id = createRequestAuthorizer("ttl-update-" + ttl, 300); + + given() + .contentType(ContentType.JSON) + .body("{\"authorizerResultTtlInSeconds\":" + ttl + "}") + .when().patch("/v2/apis/" + apiId + "/authorizers/" + id) + .then() + .statusCode(200) + .body("authorizerResultTtlInSeconds", equalTo(ttl)); + } + + @ParameterizedTest @Order(37) + @ValueSource(ints = {-1, 3601}) + void updateAuthorizer_rejectsResultTtlOutOfRangeWithoutApplyingThePatch(int ttl) { + String name = "ttl-update-rejected-" + ttl; + String id = createRequestAuthorizer(name, 300); + + given() + .contentType(ContentType.JSON) + .body("{\"name\":\"renamed\",\"authorizerResultTtlInSeconds\":" + ttl + "}") + .when().patch("/v2/apis/" + apiId + "/authorizers/" + id) + .then() + .statusCode(400) + .body("__type", equalTo("BadRequestException")) + .body("message", containsString("between 0 and 3600")); + + // The name in the same patch must not stick either: the store returns the live authorizer. + given() + .when().get("/v2/apis/" + apiId + "/authorizers/" + id) + .then() + .statusCode(200) + .body("name", equalTo(name)) + .body("authorizerResultTtlInSeconds", equalTo(300)); + } + + private static String requestAuthorizer(String name, int ttl) { + return """ + {"authorizerType":"REQUEST","name":"%s","identitySource":["$request.header.Authorization"],"authorizerUri":"arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:000000000000:function:auth/invocations","authorizerPayloadFormatVersion":"2.0","authorizerResultTtlInSeconds":%d} + """.formatted(name, ttl); + } + + private static String createRequestAuthorizer(String name, int ttl) { + return given() + .contentType(ContentType.JSON) + .body(requestAuthorizer(name, ttl)) + .when().post("/v2/apis/" + apiId + "/authorizers") + .then() + .statusCode(201) + .extract().path("authorizerId"); + } + // ──────────────────────────── Deployments ──────────────────────────── @Test @Order(40) diff --git a/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2OpenApiImportTest.java b/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2OpenApiImportTest.java index db394dc659..d84cbcd972 100644 --- a/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2OpenApiImportTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2OpenApiImportTest.java @@ -658,4 +658,32 @@ class ApiGatewayV2OpenApiImportTest { .then().statusCode(400); assertNotNull(findRoute(get("/v2/apis/" + apiId + "/routes"), "GET /j")); } + + @Test + @Order(19) + void outOfRangeAuthorizerTtlLeavesThePreviousDefinitionIntact() throws Exception { + // The TTL range is checked while planning, before ReimportApi deletes anything. + String created = given().contentType(ContentType.JSON) + .body("{\"name\": \"ttlRangeTarget\", \"protocolType\": \"HTTP\"}") + .when().post("/v2/apis") + .then().statusCode(201).extract().asString(); + String apiId = mapper.readTree(created).get("apiId").asText(); + + given().contentType(ContentType.JSON) + .body(envelope(SPEC_WITH_AUTHORIZER)) + .when().put("/v2/apis/" + apiId) + .then().statusCode(201); + + String outOfRangeTtl = SPEC_WITH_AUTHORIZER.replace( + "\"authorizerResultTtlInSeconds\": 300", "\"authorizerResultTtlInSeconds\": 3601"); + given().contentType(ContentType.JSON) + .body(envelope(outOfRangeTtl)) + .when().put("/v2/apis/" + apiId) + .then().statusCode(400); + + assertEquals(2, get("/v2/apis/" + apiId + "/routes").get("items").size()); + JsonNode authorizers = get("/v2/apis/" + apiId + "/authorizers").get("items"); + assertEquals(1, authorizers.size()); + assertEquals(300, authorizers.get(0).get("authorizerResultTtlInSeconds").asInt()); + } } diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java index 674191a0d4..235d0daa4f 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java @@ -8296,6 +8296,55 @@ class CloudFormationIntegrationTest { .body("Items[0].IdentitySource", hasItem("$request.header.Authorization")); } + @Test + void createStack_apiGatewayV2AuthorizerRejectsOutOfRangeResultTtl() { + String template = """ + { + "Resources": { + "HttpApi": { + "Type": "AWS::ApiGatewayV2::Api", + "Properties": { "Name": "cfn-apigwv2-authz-ttl-api", "ProtocolType": "HTTP" } + }, + "Authorizer": { + "Type": "AWS::ApiGatewayV2::Authorizer", + "Properties": { + "ApiId": { "Ref": "HttpApi" }, + "Name": "cfn-request-authorizer-ttl", + "AuthorizerType": "REQUEST", + "AuthorizerUri": "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:000000000000:function:auth/invocations", + "AuthorizerPayloadFormatVersion": "2.0", + "IdentitySource": ["$request.header.Authorization"], + "AuthorizerResultTtlInSeconds": 3601 + } + } + } + } + """; + + String stackName = "cfn-apigwv2-authorizer-ttl-stack"; + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "CreateStack") + .formParam("StackName", stackName) + .formParam("TemplateBody", template) + .when() + .post("/") + .then() + .statusCode(200); + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "DescribeStackEvents") + .formParam("StackName", stackName) + .when() + .post("/") + .then() + .statusCode(200) + .body(containsString("ROLLBACK_COMPLETE")) + .body(containsString("authorizerResultTtlInSeconds must be an integer between 0 and 3600")); + } + @Test void createStack_apiGatewayV2RouteResolvesAuthorizerIdViaGetAtt() { // Ref already resolved AuthorizerId via the physical id (covered above); Fn::GetAtt reads