mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -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
@@ -88,6 +88,13 @@ public class GlobalCorsFilter implements ContainerRequestFilter, ContainerRespon
|
||||
return;
|
||||
}
|
||||
|
||||
// The API Gateway data plane owns CORS for deployed routes. REST APIs dispatch
|
||||
// OPTIONS to an integration, while HTTP APIs answer from CorsConfiguration in
|
||||
// ApiGatewayExecuteController; neither should be short-circuited here (#1928).
|
||||
if (isDeployedApiPath(requestContext.getUriInfo().getPath())) {
|
||||
return;
|
||||
}
|
||||
|
||||
String origin = requestContext.getHeaderString(ORIGIN);
|
||||
if (origin == null || !s.matchesOrigin(origin)) {
|
||||
return;
|
||||
@@ -123,6 +130,12 @@ public class GlobalCorsFilter implements ContainerRequestFilter, ContainerRespon
|
||||
return;
|
||||
}
|
||||
|
||||
// See filter(request): deployed API Gateway responses carry either integration-owned
|
||||
// REST CORS headers or API-owned HTTP API CORS headers.
|
||||
if (isDeployedApiPath(requestContext.getUriInfo().getPath())) {
|
||||
return;
|
||||
}
|
||||
|
||||
String origin = requestContext.getHeaderString(ORIGIN);
|
||||
if (origin == null || !s.matchesOrigin(origin)) {
|
||||
return;
|
||||
@@ -146,6 +159,24 @@ public class GlobalCorsFilter implements ContainerRequestFilter, ContainerRespon
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* True for requests to a <em>deployed</em> API Gateway stage — the {@code execute-api}
|
||||
* paths and the LocalStack-compatible {@code _user_request_} form. Floci's global CORS
|
||||
* must not intercept these: the API Gateway data plane owns their CORS behavior, and
|
||||
* short-circuiting here prevents REST integrations or HTTP API configuration from handling
|
||||
* the preflight (#1928). Management endpoints such as {@code /restapis/{id}/resources/...}
|
||||
* are unaffected.
|
||||
*/
|
||||
static boolean isDeployedApiPath(String path) {
|
||||
if (path == null) {
|
||||
return false;
|
||||
}
|
||||
String p = path.startsWith("/") ? path : "/" + path;
|
||||
return p.startsWith("/execute-api/")
|
||||
|| p.startsWith("/_aws/execute-api/")
|
||||
|| p.contains("/_user_request_");
|
||||
}
|
||||
|
||||
private record CorsSettings(boolean disabled,
|
||||
Set<String> allowedOrigins,
|
||||
Set<String> extraAllowedHeaders,
|
||||
|
||||
+10
@@ -71,4 +71,14 @@ public class ApiGatewayAwsExecuteController {
|
||||
byte[] body) {
|
||||
return executeController.dispatch("PATCH", apiId, stageName, proxy, headers, uriInfo, body);
|
||||
}
|
||||
|
||||
@OPTIONS
|
||||
@Path("/{proxy: .*}")
|
||||
public Response handleOptions(@Context HttpHeaders headers, @Context UriInfo uriInfo,
|
||||
@PathParam("apiId") String apiId,
|
||||
@PathParam("stageName") String stageName,
|
||||
@PathParam("proxy") String proxy,
|
||||
byte[] body) {
|
||||
return executeController.dispatch("OPTIONS", apiId, stageName, proxy, headers, uriInfo, body);
|
||||
}
|
||||
}
|
||||
+68
-1
@@ -15,6 +15,7 @@ import io.github.hectorvent.floci.services.apigateway.model.UsagePlan;
|
||||
import io.github.hectorvent.floci.services.apigateway.model.UsagePlanKey;
|
||||
import io.github.hectorvent.floci.services.apigatewayv2.ApiGatewayV2Service;
|
||||
import io.github.hectorvent.floci.services.apigatewayv2.JwtSignatureVerifier;
|
||||
import io.github.hectorvent.floci.services.apigatewayv2.model.Api;
|
||||
import io.github.hectorvent.floci.services.apigatewayv2.model.Authorizer;
|
||||
import io.github.hectorvent.floci.services.apigatewayv2.model.Route;
|
||||
import io.github.hectorvent.floci.services.apigatewayv2.websocket.ConnectionInfo;
|
||||
@@ -253,6 +254,17 @@ public class ApiGatewayExecuteController {
|
||||
return dispatch("PATCH", apiId, stageName, proxy, headers, uriInfo, body);
|
||||
}
|
||||
|
||||
@OPTIONS
|
||||
@Blocking
|
||||
@Path("/{proxy: .*}")
|
||||
public Response handleOptions(@Context HttpHeaders headers, @Context UriInfo uriInfo,
|
||||
@PathParam("apiId") String apiId,
|
||||
@PathParam("stageName") String stageName,
|
||||
@PathParam("proxy") String proxy,
|
||||
byte[] body) {
|
||||
return dispatch("OPTIONS", apiId, stageName, proxy, headers, uriInfo, body);
|
||||
}
|
||||
|
||||
// ──────────────────────────── Core dispatch ────────────────────────────
|
||||
|
||||
Response dispatch(String httpMethod, String apiId, String stageName,
|
||||
@@ -1342,6 +1354,51 @@ public class ApiGatewayExecuteController {
|
||||
|
||||
// ──────────────────────────── API Gateway v2 dispatch ────────────────────────────
|
||||
|
||||
private static Response httpApiCorsPreflight(Api.Cors cors, String requestOrigin) {
|
||||
Response.ResponseBuilder response = Response.noContent().type(MediaType.TEXT_PLAIN_TYPE);
|
||||
String allowOrigin = matchingCorsOrigin(cors.allowOrigins(), requestOrigin);
|
||||
if (allowOrigin != null) {
|
||||
response.header("Access-Control-Allow-Origin", allowOrigin);
|
||||
if (!"*".equals(allowOrigin)) {
|
||||
response.header("Vary", "Origin");
|
||||
}
|
||||
}
|
||||
putCorsListHeader(response, "Access-Control-Allow-Methods", cors.allowMethods());
|
||||
putCorsListHeader(response, "Access-Control-Allow-Headers", cors.allowHeaders());
|
||||
putCorsListHeader(response, "Access-Control-Expose-Headers", cors.exposeHeaders());
|
||||
if (cors.maxAge() != null) {
|
||||
response.header("Access-Control-Max-Age", cors.maxAge());
|
||||
}
|
||||
if (Boolean.TRUE.equals(cors.allowCredentials())) {
|
||||
response.header("Access-Control-Allow-Credentials", "true");
|
||||
}
|
||||
return response.build();
|
||||
}
|
||||
|
||||
private static String matchingCorsOrigin(List<String> allowedOrigins, String requestOrigin) {
|
||||
if (allowedOrigins == null || requestOrigin == null) {
|
||||
return null;
|
||||
}
|
||||
for (String allowedOrigin : allowedOrigins) {
|
||||
if ("*".equals(allowedOrigin)) {
|
||||
return "*";
|
||||
}
|
||||
if (allowedOrigin != null && (allowedOrigin.equals(requestOrigin)
|
||||
|| (allowedOrigin.endsWith("*")
|
||||
&& requestOrigin.startsWith(allowedOrigin.substring(0, allowedOrigin.length() - 1))))) {
|
||||
return requestOrigin;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static void putCorsListHeader(Response.ResponseBuilder response, String headerName,
|
||||
List<String> values) {
|
||||
if (values != null && !values.isEmpty()) {
|
||||
response.header(headerName, String.join(", ", values));
|
||||
}
|
||||
}
|
||||
|
||||
private Response dispatchV2(String httpMethod, String apiId, String stageName,
|
||||
String proxy, HttpHeaders headers, UriInfo uriInfo,
|
||||
byte[] body, String region) {
|
||||
@@ -1350,8 +1407,10 @@ public class ApiGatewayExecuteController {
|
||||
// and the direct /execute-api/{apiId}/{stage}/... route land here. Checking at the single
|
||||
// choke point keeps the two entry points from disagreeing about whether an API is invokable.
|
||||
// A missing API is not this method's error to report — findMatchingRoute below 404s.
|
||||
Api api = null;
|
||||
try {
|
||||
if (apiGatewayV2Service.getApi(region, apiId).isDisableExecuteApiEndpoint()) {
|
||||
api = apiGatewayV2Service.getApi(region, apiId);
|
||||
if (api.isDisableExecuteApiEndpoint()) {
|
||||
return Response.status(Response.Status.NOT_FOUND)
|
||||
.entity(jsonMessage("Not Found"))
|
||||
.type(MediaType.APPLICATION_JSON).build();
|
||||
@@ -1361,6 +1420,14 @@ public class ApiGatewayExecuteController {
|
||||
apiId, region);
|
||||
}
|
||||
|
||||
if (api != null && api.getCorsConfiguration() != null
|
||||
&& "OPTIONS".equalsIgnoreCase(httpMethod)
|
||||
&& headers != null
|
||||
&& headers.getHeaderString("Origin") != null
|
||||
&& headers.getHeaderString("Access-Control-Request-Method") != null) {
|
||||
return httpApiCorsPreflight(api.getCorsConfiguration(), headers.getHeaderString("Origin"));
|
||||
}
|
||||
|
||||
String path = "/" + (proxy == null ? "" : proxy);
|
||||
|
||||
Route route = apiGatewayV2Service.findMatchingRoute(region, apiId, httpMethod, path);
|
||||
|
||||
+10
@@ -71,4 +71,14 @@ public class ApiGatewayUserRequestController {
|
||||
byte[] body) {
|
||||
return executeController.dispatch("PATCH", apiId, stageName, proxy, headers, uriInfo, body);
|
||||
}
|
||||
|
||||
@OPTIONS
|
||||
@Path("/{proxy: .*}")
|
||||
public Response handleOptions(@Context HttpHeaders headers, @Context UriInfo uriInfo,
|
||||
@PathParam("apiId") String apiId,
|
||||
@PathParam("stageName") String stageName,
|
||||
@PathParam("proxy") String proxy,
|
||||
byte[] body) {
|
||||
return executeController.dispatch("OPTIONS", apiId, stageName, proxy, headers, uriInfo, body);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user