mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
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
This commit is contained in:
1 parent
02ad6aa768
commit
bdbc7e93d3
5 files changed
+185
-6
No files matched your search
+88
@@ -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)
|
||||
|
||||
+28
@@ -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());
|
||||
}
|
||||
}
|
||||
+49
@@ -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
|
||||
|
||||
Reference in new issue
Block a user