mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(apigateway): route CORS preflight (OPTIONS) to deployed API integ… (#1955)
* fix(apigateway): route CORS preflight (OPTIONS) to deployed API integrations Browsers send an OPTIONS preflight before a cross-origin call to a deployed API. GlobalCorsFilter short-circuited every OPTIONS preflight with Floci's own CORS response, so the request never reached the API Gateway integration — a Lambda (or a configured OPTIONS method) that handles CORS itself never ran. The deployed-API execute controllers also had no OPTIONS handler. - Skip GlobalCorsFilter for deployed-API paths (execute-api, _aws/execute-api, and the _user_request_ form) on both the request and response sides, so the integration owns CORS — matching the filter's documented "integrations keep precedence" contract. - Add @OPTIONS handlers to the three execute controllers, dispatching to the configured OPTIONS/ANY method like the other verbs. Adds unit tests for the deployed-API path matcher. Closes #1928 * test(apigateway): cover CORS preflight (OPTIONS) routing end-to-end Add ApiGatewayPreflightRoutingIntegrationTest driving a real browser preflight through the full chain: global CORS filter skip on deployed paths, route matching, OPTIONS/ANY method resolution, and integration invocation. A contrasting preflight to a management path (/restapis) asserts the filter still short-circuits non-deployed paths, so the deployed-path cases run with CORS genuinely enabled. * fix(apigatewayv2): preserve configured CORS preflight
This commit is contained in:
1 parent
43e5e903d1
commit
0e3391cac2
6 files changed
+437
-1
No files matched your search
@@ -0,0 +1,45 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class GlobalCorsFilterTest {
|
||||
|
||||
// Deployed API Gateway stages own their CORS — the global filter must skip them (#1928).
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"/execute-api/abc123/prod/graphql",
|
||||
"/execute-api/abc123/prod/",
|
||||
"/_aws/execute-api/abc123/prod/graphql",
|
||||
"/restapis/abc123/prod/_user_request_/graphql",
|
||||
"/restapis/abc123/prod/_user_request_",
|
||||
// tolerant of the leading-slash-less form UriInfo.getPath() can return
|
||||
"execute-api/abc123/prod/graphql",
|
||||
})
|
||||
void deployedApiPathsAreExcludedFromGlobalCors(String path) {
|
||||
assertTrue(GlobalCorsFilter.isDeployedApiPath(path));
|
||||
}
|
||||
|
||||
// Management API and everything else keep Floci's global CORS handling.
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"/restapis/abc123/resources/xyz/methods/GET",
|
||||
"/restapis/abc123/deployments",
|
||||
"/restapis",
|
||||
"/my-bucket/key.txt",
|
||||
"/",
|
||||
"/_health",
|
||||
})
|
||||
void managementAndOtherPathsKeepGlobalCors(String path) {
|
||||
assertFalse(GlobalCorsFilter.isDeployedApiPath(path));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullPathIsNotDeployedApi() {
|
||||
assertFalse(GlobalCorsFilter.isDeployedApiPath(null));
|
||||
}
|
||||
}
|
||||
+273
@@ -0,0 +1,273 @@
|
||||
package io.github.hectorvent.floci.services.apigateway;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.QuarkusTestProfile;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import io.restassured.http.ContentType;
|
||||
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 java.util.Map;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
|
||||
/**
|
||||
* End-to-end coverage for CORS preflight (OPTIONS) handling on deployed API Gateway
|
||||
* REST and HTTP APIs (#1928).
|
||||
*
|
||||
* <p>{@code GlobalCorsFilterTest} unit-tests {@link io.github.hectorvent.floci.core.common.GlobalCorsFilter#isDeployedApiPath}
|
||||
* in isolation, and {@code GlobalCorsFilterIntegrationTest} verifies the filter still
|
||||
* short-circuits preflights on <em>non-deployed</em> paths. Neither exercises the flow this
|
||||
* PR actually changes: with global CORS <em>enabled</em>, a browser preflight to a deployed
|
||||
* {@code /execute-api/...} path must be skipped by the filter and routed all the way to the
|
||||
* integration. This test drives that full chain — filter skip → route match → OPTIONS/ANY
|
||||
* method resolution → integration invocation — so a regression in any single stage fails here.</p>
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestProfile(ApiGatewayPreflightRoutingIntegrationTest.CorsEnabledProfile.class)
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class ApiGatewayPreflightRoutingIntegrationTest {
|
||||
|
||||
private static final String ORIGIN = "http://localhost:3000";
|
||||
private static final String STAGE = "cors";
|
||||
|
||||
private static String apiId;
|
||||
private static String rootId;
|
||||
private static String optionsResourceId;
|
||||
private static String anyResourceId;
|
||||
private static String deploymentId;
|
||||
private static String httpApiId;
|
||||
|
||||
@Test @Order(1)
|
||||
void createRestApi() {
|
||||
apiId = given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{\"name\":\"preflight-routing-test-api\"}")
|
||||
.when().post("/restapis")
|
||||
.then()
|
||||
.statusCode(201)
|
||||
.body("id", notNullValue())
|
||||
.extract().path("id");
|
||||
}
|
||||
|
||||
@Test @Order(2)
|
||||
void setupExplicitOptionsMethodIntegration() {
|
||||
rootId = given()
|
||||
.when().get("/restapis/" + apiId + "/resources")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("item[0].id");
|
||||
|
||||
optionsResourceId = given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{\"pathPart\":\"widgets\"}")
|
||||
.when().post("/restapis/" + apiId + "/resources/" + rootId)
|
||||
.then()
|
||||
.statusCode(201)
|
||||
.extract().path("id");
|
||||
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{\"authorizationType\":\"NONE\"}")
|
||||
.when().put("/restapis/" + apiId + "/resources/" + optionsResourceId + "/methods/OPTIONS")
|
||||
.then()
|
||||
.statusCode(201);
|
||||
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{\"responseParameters\":{}}")
|
||||
.when().put("/restapis/" + apiId + "/resources/" + optionsResourceId + "/methods/OPTIONS/responses/200")
|
||||
.then()
|
||||
.statusCode(201);
|
||||
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{\"type\":\"MOCK\",\"requestTemplates\":{\"application/json\":\"{\\\"statusCode\\\": 200}\"}}")
|
||||
.when().put("/restapis/" + apiId + "/resources/" + optionsResourceId + "/methods/OPTIONS/integration")
|
||||
.then()
|
||||
.statusCode(201);
|
||||
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{\"selectionPattern\":\"\",\"responseTemplates\":{\"application/json\":\"{\\\"handledBy\\\":\\\"options-integration\\\"}\"}}")
|
||||
.when().put("/restapis/" + apiId + "/resources/" + optionsResourceId + "/methods/OPTIONS/integration/responses/200")
|
||||
.then()
|
||||
.statusCode(201);
|
||||
}
|
||||
|
||||
@Test @Order(3)
|
||||
void setupAnyMethodIntegration() {
|
||||
anyResourceId = given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{\"pathPart\":\"any\"}")
|
||||
.when().post("/restapis/" + apiId + "/resources/" + rootId)
|
||||
.then()
|
||||
.statusCode(201)
|
||||
.extract().path("id");
|
||||
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{\"authorizationType\":\"NONE\"}")
|
||||
.when().put("/restapis/" + apiId + "/resources/" + anyResourceId + "/methods/ANY")
|
||||
.then()
|
||||
.statusCode(201);
|
||||
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{\"responseParameters\":{}}")
|
||||
.when().put("/restapis/" + apiId + "/resources/" + anyResourceId + "/methods/ANY/responses/200")
|
||||
.then()
|
||||
.statusCode(201);
|
||||
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{\"type\":\"MOCK\",\"requestTemplates\":{\"application/json\":\"{\\\"statusCode\\\": 200}\"}}")
|
||||
.when().put("/restapis/" + apiId + "/resources/" + anyResourceId + "/methods/ANY/integration")
|
||||
.then()
|
||||
.statusCode(201);
|
||||
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{\"selectionPattern\":\"\",\"responseTemplates\":{\"application/json\":\"{\\\"matched\\\":\\\"any\\\"}\"}}")
|
||||
.when().put("/restapis/" + apiId + "/resources/" + anyResourceId + "/methods/ANY/integration/responses/200")
|
||||
.then()
|
||||
.statusCode(201);
|
||||
}
|
||||
|
||||
@Test @Order(4)
|
||||
void createDeploymentAndStage() {
|
||||
deploymentId = given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{\"description\":\"v1\"}")
|
||||
.when().post("/restapis/" + apiId + "/deployments")
|
||||
.then()
|
||||
.statusCode(201)
|
||||
.extract().path("id");
|
||||
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{\"stageName\":\"" + STAGE + "\",\"deploymentId\":\"" + deploymentId + "\"}")
|
||||
.when().post("/restapis/" + apiId + "/stages")
|
||||
.then()
|
||||
.statusCode(201);
|
||||
}
|
||||
|
||||
/**
|
||||
* The regression guard: a real browser preflight (carries {@code Origin} and
|
||||
* {@code Access-Control-Request-Method}) to a deployed path must NOT be answered by the
|
||||
* global CORS filter (which would return 204 with no body). The deployed-path exclusion
|
||||
* lets it fall through to the explicit OPTIONS method's integration, whose body proves the
|
||||
* integration was actually invoked.
|
||||
*/
|
||||
@Test @Order(5)
|
||||
void preflightToDeployedPathReachesExplicitOptionsIntegration() {
|
||||
given()
|
||||
.header("Origin", ORIGIN)
|
||||
.header("Access-Control-Request-Method", "GET")
|
||||
.header("Access-Control-Request-Headers", "content-type")
|
||||
.when().options("/execute-api/" + apiId + "/" + STAGE + "/widgets")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("handledBy", equalTo("options-integration"));
|
||||
}
|
||||
|
||||
/** An OPTIONS preflight must also resolve to a resource configured with the ANY method. */
|
||||
@Test @Order(6)
|
||||
void preflightResolvesToAnyMethodIntegration() {
|
||||
given()
|
||||
.header("Origin", ORIGIN)
|
||||
.header("Access-Control-Request-Method", "POST")
|
||||
.when().options("/execute-api/" + apiId + "/" + STAGE + "/any")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("matched", equalTo("any"));
|
||||
}
|
||||
|
||||
/**
|
||||
* HTTP APIs differ from REST APIs: API Gateway itself answers preflight from the API's
|
||||
* CorsConfiguration, even when no OPTIONS route exists.
|
||||
*/
|
||||
@Test @Order(7)
|
||||
void setupHttpApiWithCorsConfiguration() {
|
||||
httpApiId = given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{
|
||||
"name":"preflight-routing-http-api",
|
||||
"protocolType":"HTTP",
|
||||
"corsConfiguration":{
|
||||
"allowOrigins":["%s"],
|
||||
"allowMethods":["GET","POST"],
|
||||
"allowHeaders":["content-type"],
|
||||
"exposeHeaders":["x-request-id"],
|
||||
"maxAge":600,
|
||||
"allowCredentials":true
|
||||
}
|
||||
}
|
||||
""".formatted(ORIGIN))
|
||||
.when().post("/v2/apis")
|
||||
.then()
|
||||
.statusCode(201)
|
||||
.extract().path("apiId");
|
||||
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{\"routeKey\":\"GET /items\"}")
|
||||
.when().post("/v2/apis/" + httpApiId + "/routes")
|
||||
.then()
|
||||
.statusCode(201);
|
||||
}
|
||||
|
||||
@Test @Order(8)
|
||||
void httpApiPreflightUsesApiCorsConfigurationWithoutOptionsRoute() {
|
||||
given()
|
||||
.header("Origin", ORIGIN)
|
||||
.header("Access-Control-Request-Method", "GET")
|
||||
.header("Access-Control-Request-Headers", "content-type")
|
||||
.when().options("/execute-api/" + httpApiId + "/$default/items")
|
||||
.then()
|
||||
.statusCode(204)
|
||||
.header("Access-Control-Allow-Origin", equalTo(ORIGIN))
|
||||
.header("Access-Control-Allow-Methods", equalTo("GET, POST"))
|
||||
.header("Access-Control-Allow-Headers", equalTo("content-type"))
|
||||
.header("Access-Control-Expose-Headers", equalTo("x-request-id"))
|
||||
.header("Access-Control-Max-Age", equalTo("600"))
|
||||
.header("Access-Control-Allow-Credentials", equalTo("true"));
|
||||
}
|
||||
|
||||
/**
|
||||
* The discriminator: a preflight to a non-deployed management path ({@code /restapis}) is
|
||||
* still owned by the global CORS filter — short-circuited with 204 and CORS headers, never
|
||||
* routed to an integration. Proves the deployed-path exclusion is scoped, not blanket.
|
||||
*/
|
||||
@Test @Order(9)
|
||||
void preflightToManagementPathIsShortCircuitedByFilter() {
|
||||
given()
|
||||
.header("Origin", ORIGIN)
|
||||
.header("Access-Control-Request-Method", "GET")
|
||||
.when().options("/restapis")
|
||||
.then()
|
||||
.statusCode(204)
|
||||
.header("Access-Control-Allow-Origin", equalTo(ORIGIN))
|
||||
.body(equalTo(""));
|
||||
}
|
||||
|
||||
@Test @Order(10)
|
||||
void cleanup() {
|
||||
given().when().delete("/restapis/" + apiId).then().statusCode(202);
|
||||
given().when().delete("/v2/apis/" + httpApiId).then().statusCode(204);
|
||||
}
|
||||
|
||||
public static final class CorsEnabledProfile implements QuarkusTestProfile {
|
||||
@Override
|
||||
public Map<String, String> getConfigOverrides() {
|
||||
// Global CORS on: without the deployed-path exclusion, this filter would answer the
|
||||
// preflight itself and the integration would never run.
|
||||
return Map.of("floci.security.extra-cors-allowed-origins", ORIGIN);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user