fix(lambda): enforce function policies when API Gateway invokes a function (#8786)

With IAM enforcement on, a REST API AWS_PROXY or AWS Lambda integration invoked the function whatever its function policy said. On AWS API Gateway invokes the function as apigateway.amazonaws.com and only the function policy decides the call, so a missing or mis-scoped AddPermission breaks the integration.

Before invoking, the integration now asks ServiceCallAuthorizer whether apigateway may perform lambda:InvokeFunction on the function, with aws:SourceArn set to the request's execute-api ARN, carrying the concrete request path and the function's Region, and aws:SourceAccount to the API's account. For an ANY method the ARN carries the request's verb, as a Lambda authorizer's methodArn does. A refused call answers 500 {"message": "Internal server error"} through the API_CONFIGURATION_ERROR gateway response, and the function is not invoked. A function that does not exist is refused the same way, since Lambda authorizes before it checks existence. An integration with credentials invokes as that role and is not checked.

With enforcement off nothing changes: a missing function still answers 404 Function not found.

Part of #4898
This commit is contained in:
Tyler Webb authored and GitHub committed 2026-11-21 15:19:56 -04:00
1 parent 5b9e842cd9
commit 27de6fd46e
7 files changed
+471 -3

No files matched your search

@@ -27,6 +27,7 @@ import io.floci.aws.core.common.CookieHeaders;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.common.RequestContext;
import io.floci.aws.core.common.ResteasyRequests;
import io.floci.aws.core.common.ServiceCallAuthorizer;
import io.floci.aws.core.common.auth.SigV4AuthorizationHeader;
import io.floci.aws.services.apigateway.model.ApiGatewayResource;
import io.floci.aws.services.apigateway.model.ApiKey;
@@ -56,6 +57,7 @@ import io.floci.aws.services.apigatewayv2.websocket.WebSocketConnectionManager;
import io.floci.aws.services.elbv2.ElbV2Service;
import io.floci.aws.services.elbv2.model.Listener;
import io.floci.aws.services.elbv2.model.LoadBalancer;
import io.floci.aws.services.iam.IamPolicyEvaluator.Decision;
import io.floci.aws.services.lambda.LambdaArnUtils;
import io.floci.aws.services.lambda.LambdaService;
import io.floci.aws.services.lambda.model.InvocationType;
@@ -151,9 +153,31 @@ public class ApiGatewayExecuteController {
private final CloudMapInstanceResolver cloudMapInstanceResolver;
private final AwsServiceIntegrationMapper awsServiceIntegrationMapper;
private final HttpProxyInvoker httpProxyInvoker;
private final ServiceCallAuthorizer serviceCallAuthorizer;
private final ResponseParameterMapper responseParameterMapper =
new ResponseParameterMapper(new ContextValueResolver());
public ApiGatewayExecuteController(ApiGatewayService apiGatewayService, CognitoUserPoolAuthorizer cognitoAuthorizer,
ApiGatewayV2Service apiGatewayV2Service,
LambdaService lambdaService, RegionResolver regionResolver,
ObjectMapper objectMapper, VtlTemplateEngine vtlEngine,
AwsServiceRouter serviceRouter,
WebSocketConnectionManager webSocketConnectionManager,
ElbV2Service elbV2Service,
SqsQueryHandler sqsQueryHandler,
ApiGatewayExecuteRouteContext routeContext,
JwtSignatureVerifier jwtSignatureVerifier,
RequestContext requestContext,
ExecuteApiSigV4Authorizer sigV4Authorizer, RestLambdaAuthorizer restLambdaAuthorizer,
AuthorizerPolicyEvaluator authorizerPolicyEvaluator,
CloudMapInstanceResolver cloudMapInstanceResolver,
HttpProxyInvoker httpProxyInvoker) {
this(apiGatewayService, cognitoAuthorizer, apiGatewayV2Service, lambdaService, regionResolver, objectMapper,
vtlEngine, serviceRouter, webSocketConnectionManager, elbV2Service, sqsQueryHandler, routeContext,
jwtSignatureVerifier, requestContext, sigV4Authorizer, restLambdaAuthorizer,
authorizerPolicyEvaluator, cloudMapInstanceResolver, httpProxyInvoker, null);
}
@Inject
public ApiGatewayExecuteController(ApiGatewayService apiGatewayService, CognitoUserPoolAuthorizer cognitoAuthorizer,
ApiGatewayV2Service apiGatewayV2Service,
@@ -169,7 +193,8 @@ public class ApiGatewayExecuteController {
ExecuteApiSigV4Authorizer sigV4Authorizer, RestLambdaAuthorizer restLambdaAuthorizer,
AuthorizerPolicyEvaluator authorizerPolicyEvaluator,
CloudMapInstanceResolver cloudMapInstanceResolver,
HttpProxyInvoker httpProxyInvoker) {
HttpProxyInvoker httpProxyInvoker,
ServiceCallAuthorizer serviceCallAuthorizer) {
this.apiGatewayService = apiGatewayService;
this.cognitoAuthorizer = cognitoAuthorizer;
this.apiGatewayV2Service = apiGatewayV2Service;
@@ -194,6 +219,7 @@ public class ApiGatewayExecuteController {
this.authorizerPolicyEvaluator = authorizerPolicyEvaluator;
this.cloudMapInstanceResolver = cloudMapInstanceResolver;
this.httpProxyInvoker = httpProxyInvoker;
this.serviceCallAuthorizer = serviceCallAuthorizer;
}
/** Matches an ELBv2 listener ARN (ALB {@code app/} or NLB {@code net/}); group 1 = region. */
@@ -612,6 +638,9 @@ public class ApiGatewayExecuteController {
return gatewayResponse(scope, GatewayResponseType.API_CONFIGURATION_ERROR, 500,
"Cannot resolve function from URI: " + integration.getUri());
}
if (!apiGatewayMayInvoke(integration, functionName, region, apiId, stageName, httpMethod, path)) {
return gatewayResponse(scope, GatewayResponseType.API_CONFIGURATION_ERROR, 500, "Internal server error");
}
String requestId = routeContext.requestId(headers);
String eventJson = buildProxyEvent(region, apiId, httpMethod, path, resource.getPath(),
@@ -1484,6 +1513,29 @@ public class ApiGatewayExecuteController {
return AwsArnUtils.Arn.of("execute-api", arnRegion, regionResolver.getAccountId(), apiId + "/" + stageName + "/" + httpMethod + "/" + normalizedPath).toString();
}
/**
* Whether the function policy lets API Gateway invoke {@code functionName} for this request. The
* source ARN names the API's account, the stage, the request verb and the concrete request path,
* in the function's Region rather than the API's, as Lambda checks it for a cross-Region API; a
* function reference without a Region is taken to be in the API's. An integration with
* {@code credentials} invokes as that role instead, so the function policy is not consulted for it.
*/
private boolean apiGatewayMayInvoke(Integration integration, String functionName, String apiRegion,
String apiId, String stageName, String httpMethod, String requestPath) {
if (serviceCallAuthorizer == null
|| (integration.getCredentials() != null && !integration.getCredentials().isBlank())) {
return true;
}
String functionRegion = LambdaArnUtils.resolve(functionName).region();
String sourceArn = buildMethodArn(functionRegion != null ? functionRegion : apiRegion, apiId, stageName,
httpMethod, requestPath);
AwsArnUtils.Arn source = AwsArnUtils.parse(sourceArn);
String functionArn = LambdaArnUtils.functionArn(functionName, source.partition(), source.region(),
source.accountId());
return serviceCallAuthorizer.authorize("apigateway", "lambda:InvokeFunction", functionArn, sourceArn,
source.accountId()) == Decision.ALLOW;
}
/**
* Extracts function name from integration URI like
* {@code arn:aws:apigateway:...:lambda:path/2015-03-31/functions/{fnArn}/invocations}.
@@ -1980,6 +2032,11 @@ public class ApiGatewayExecuteController {
throw new AwsException("InvalidParameterValueException",
"Cannot resolve Lambda function name from URI: " + integration.getUri(), 400);
}
if (!apiGatewayMayInvoke(integration, functionName, region, scope.apiId(), stageName, httpMethod,
path)) {
return gatewayResponse(scope, GatewayResponseType.API_CONFIGURATION_ERROR, 500,
"Internal server error");
}
byte[] payload = transformedBody != null ? transformedBody.getBytes(StandardCharsets.UTF_8) : new byte[0];
InvokeResult invokeResult = lambdaService.invokeTarget(region, functionName, payload, InvocationType.RequestResponse);
String lambdaResponseBody = invokeResult.getPayload() != null