fix(apigateway): enforce ApiKeyRequired on REST API methods (#3537)

* fix(apigateway): enforce ApiKeyRequired on REST API methods

A method with ApiKeyRequired=true accepted requests missing or using an
unrecognized x-api-key header instead of rejecting them with 403, since the
property was never read from PutMethod, the AWS::ApiGateway::Method
CloudFormation resource, or the execute-api data plane.

MethodConfig now carries apiKeyRequired (wired through putMethod, the PATCH
/apiKeyRequired path, and CloudFormation's ApiKeyRequired property), and
ApiGatewayExecuteController rejects the request with 403 {"message":"Forbidden"}
when the method requires a key and none resolves via the existing usage-plan
lookup.

Fixes #3507 (ApiKeyRequired half; the resource Policy gap remains open).

* fix(apigateway): validate patch operation and boolean value for /apiKeyRequired in UpdateMethod (#3507)
This commit is contained in:
Vũ Thế Huy authored and GitHub committed 2026-09-13 18:20:01 -04:00
1 parent 3c6568bac5
commit 3b31c5a663
7 files changed
+304 -6

No files matched your search

@@ -1963,6 +1963,7 @@ public class ApiGatewayController {
ObjectNode node = objectMapper.createObjectNode();
node.put("httpMethod", m.getHttpMethod());
node.put("authorizationType", m.getAuthorizationType());
node.put("apiKeyRequired", m.isApiKeyRequired());
if (m.getAuthorizerId() != null) node.put("authorizerId", m.getAuthorizerId());
if (m.getRequestValidatorId() != null) node.put("requestValidatorId", m.getRequestValidatorId());
if (m.getRequestModels() != null && !m.getRequestModels().isEmpty()) {
@@ -386,6 +386,15 @@ public class ApiGatewayExecuteController {
AuthorizerResult authorizerResult = invokeAuthorizer(region, apiId, stageName, httpMethod, path, matched.getPath(), matched.getId(), stage, method, headers, uriInfo, resolvedApiKey);
if (authorizerResult.errorResponse() != null) return authorizerResult.errorResponse();
// API Gateway checks the API key requirement after authorization but before request
// validation and throttling. resolvedApiKey is null when the header is missing or matches
// no enabled key linked to this (apiId, stage) through a usage plan.
if (method.isApiKeyRequired() && resolvedApiKey == null) {
return Response.status(403)
.entity(jsonMessage("Forbidden"))
.type(MediaType.APPLICATION_JSON).build();
}
// 2. Request validation
Response validationResponse = validateRequest(region, apiId, method, headers, uriInfo, body);
if (validationResponse != null) return validationResponse;
@@ -378,6 +378,7 @@ public class ApiGatewayService {
method.setAuthorizationType((String) request.getOrDefault("authorizationType", "NONE"));
method.setAuthorizerId((String) request.get("authorizerId"));
method.setRequestValidatorId((String) request.get("requestValidatorId"));
method.setApiKeyRequired(Boolean.TRUE.equals(request.get("apiKeyRequired")));
@SuppressWarnings("unchecked")
Map<String, Boolean> reqParams = (Map<String, Boolean>) request.get("requestParameters");
@@ -1943,11 +1944,25 @@ public class ApiGatewayService {
MethodConfig method = getMethod(region, apiId, resourceId, httpMethod);
if (patchOperations != null) {
for (Map<String, String> op : patchOperations) {
if (!"replace" .equals(op.get("op"))) continue;
String path = op.getOrDefault("path", "");
String opType = op.get("op");
String value = op.get("value");
if ("/apiKeyRequired".equals(path)) {
if (!"replace".equals(opType)) {
throw new AwsException("BadRequestException", "Invalid patch operation '" + opType + "' for path '" + path + "'. Supported operation is: replace", 400);
}
if (value == null || (!"true".equalsIgnoreCase(value) && !"false".equalsIgnoreCase(value))) {
throw new AwsException("BadRequestException", "Invalid boolean value '" + value + "' for apiKeyRequired. Must be 'true' or 'false'", 400);
}
}
}
for (Map<String, String> op : patchOperations) {
if (!"replace".equals(op.get("op"))) continue;
String path = op.getOrDefault("path", "");
String value = op.get("value");
if ("/authorizationType" .equals(path)) method.setAuthorizationType(value);
else if ("/authorizerId" .equals(path)) method.setAuthorizerId(value);
if ("/authorizationType".equals(path)) method.setAuthorizationType(value);
else if ("/authorizerId".equals(path)) method.setAuthorizerId(value);
else if ("/apiKeyRequired".equals(path)) method.setApiKeyRequired(Boolean.parseBoolean(value));
}
}
resourceStore.put(resourceKey(region, apiId, resourceId), getResource(region, apiId, resourceId));
@@ -15,6 +15,7 @@ public class MethodConfig {
private String authorizationType;
private String authorizerId;
private String requestValidatorId;
private boolean apiKeyRequired;
private Map<String, Boolean> requestParameters = new HashMap<>();
private Map<String, String> requestModels = new HashMap<>();
private Map<String, MethodResponse> methodResponses = new HashMap<>();
@@ -32,6 +33,9 @@ public class MethodConfig {
public String getRequestValidatorId() { return requestValidatorId; }
public void setRequestValidatorId(String requestValidatorId) { this.requestValidatorId = requestValidatorId; }
public boolean isApiKeyRequired() { return apiKeyRequired; }
public void setApiKeyRequired(boolean apiKeyRequired) { this.apiKeyRequired = apiKeyRequired; }
public Map<String, String> getRequestModels() { return requestModels; }
public void setRequestModels(Map<String, String> requestModels) {
this.requestModels = requestModels != null ? requestModels : new HashMap<>();
@@ -3319,6 +3319,7 @@ public class CloudFormationResourceProvisioner {
if (authorizerId != null) {
req.put("authorizerId", authorizerId);
}
req.put("apiKeyRequired", Boolean.parseBoolean(resolveOrDefault(props, "ApiKeyRequired", engine, "false")));
apiGatewayService.putMethod(region, apiId, resourceId, httpMethod, req);
r.setPhysicalId(apiId + "-" + resourceId + "-" + httpMethod);