fix(apigateway): authorize management calls against the path ARN they name (#6825)

This commit is contained in:
galsuchetzky-act authored and GitHub committed 2026-10-29 14:51:56 -04:00
1 parent c3fdacc3ce
commit 981b885348
8 files changed
+391 -10

No files matched your search

@@ -1638,7 +1638,8 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
/**
* Logs, Secrets Manager and KMS targets depend on the serving operation and the request's partition, STS
* targets on the serving operation;
* Scheduler and Pipes targets on the request's partition, which those services mint their ARNs in; and the
* Scheduler and Pipes targets on the request's partition, which those services mint their ARNs in, as are
* API Gateway's path ARNs; and the
* targets of a namespace the table of request resources reads on {@code operation}, the one the request is
* dispatched as (see {@link RequestResources}).
*/
@@ -1656,7 +1657,8 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
if ("sts".equals(credentialScope)) {
return arnBuilder.buildStsResources(action, ctx);
}
if ("scheduler".equals(credentialScope) || "pipes".equals(credentialScope) || "dsql".equals(credentialScope)) {
if ("scheduler".equals(credentialScope) || "pipes".equals(credentialScope) || "dsql".equals(credentialScope)
|| "apigateway".equals(credentialScope)) {
return arnBuilder.buildResources(credentialScope, ctx, region, accountId, requestPartition());
}
return arnBuilder.buildResources(credentialScope, operation, ctx, region, accountId,
@@ -11,6 +11,7 @@ import io.github.hectorvent.floci.core.common.RequestResources;
import io.github.hectorvent.floci.core.common.ServicePrincipals;
import io.github.hectorvent.floci.core.common.SigV4CredentialScope;
import io.github.hectorvent.floci.core.common.StoredResourceArns;
import io.github.hectorvent.floci.core.common.auth.SigV4Canonicalization;
import io.github.hectorvent.floci.services.cloudwatch.logs.LogGroupTagTarget;
import io.github.hectorvent.floci.services.eks.EksController;
import io.github.hectorvent.floci.services.eks.EksService;
@@ -38,6 +39,7 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.StringJoiner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -70,6 +72,12 @@ public class ResourceArnBuilder {
/** Where an S3 Control tagging route carries the ARN of the resource it acts on. */
private static final String S3_CONTROL_TAGS_PREFIX = "/v20180820/tags/";
/** The segment that starts an API Gateway v2 management path, and is not part of the resource it names. */
private static final String API_GATEWAY_V2_SEGMENT = "v2";
/** The segment a tagging route names before the ARN it tags, which it captures whole, slashes included. */
private static final String API_GATEWAY_TAGS_SEGMENT = "tags";
private final ObjectMapper objectMapper;
/**
* Looked up lazily through a provider: this builder is constructed by the enforcement filter,
@@ -190,6 +198,7 @@ public class ResourceArnBuilder {
case "pipes" -> List.of(buildPipesArn(path, ctx, region, accountId, partition));
case "dsql" -> List.of(buildDsqlArn(path, ctx, region, accountId, partition));
case "eks" -> List.of(buildEksArn(path, ctx, region, accountId));
case "apigateway" -> List.of(buildApiGatewayArn(ctx, region, partition));
default -> namedResources(credentialScope, operation, ctx, region, accountId, partition);
};
}
@@ -1196,6 +1205,48 @@ public class ResourceArnBuilder {
return taggedResource(ctx);
}
/**
* API Gateway authorizes a management call as its HTTP method on the path it names, in the Region serving
* it and with no account: {@code GET /restapis/abc/stages} acts on
* {@code arn:<partition>:apigateway:<region>::/restapis/abc/stages}. The path is read as the router reads
* it: each segment decoded, then {@code .} dropped and {@code ..} resolved, then empty segments dropped, so
* {@code /restapis/%61bc}, {@code /restapis/x/%2E%2E/abc} and {@code /restapis//abc} all act on
* {@code ::/restapis/abc}, the API they reach, and a stage keeps
* its name as written, {@code ::/apis/<id>/stages/$default}. The {@code /v2} that starts an HTTP or
* WebSocket API path is not part of its resource, so {@code GET /v2/apis} acts on {@code ::/apis}. Only the
* ARN a tagging route names, everything after {@code /tags/}, is re-encoded, in SigV4 canonical form, as AWS
* names that resource {@code /tags/<UrlEncodedResourceARN>}; elsewhere a decoded {@code %} or {@code /}
* stays escaped, so a segment cannot pass for more than one.
*/
private static String buildApiGatewayArn(ContainerRequestContext ctx, String region, String partition) {
String[] raw = IamActionRegistry.routedPath(ctx.getUriInfo()).split("/", -1);
List<String> segments = new ArrayList<>();
for (int i = 1; i < raw.length; i++) {
String segment = pathSegment(raw[i]);
if (segment.equals("..")) {
if (!segments.isEmpty()) {
segments.removeLast();
}
} else if (!segment.equals(".")) {
segments.add(segment);
}
}
segments.removeIf(String::isEmpty);
if (!segments.isEmpty() && segments.getFirst().equals(API_GATEWAY_V2_SEGMENT)) {
segments.removeFirst();
}
if (segments.size() > 1 && segments.getFirst().equals(API_GATEWAY_TAGS_SEGMENT)) {
String tagged = String.join("/", segments.subList(1, segments.size()));
return new AwsArnUtils.Arn(partition, "apigateway", region, "",
"/" + API_GATEWAY_TAGS_SEGMENT + "/" + SigV4Canonicalization.uriEncode(tagged)).toString();
}
StringJoiner resource = new StringJoiner("/", "/", "");
for (String segment : segments) {
resource.add(segment.replace("%", "%25").replace("/", "%2F"));
}
return new AwsArnUtils.Arn(partition, "apigateway", region, "", resource.toString()).toString();
}
/**
* The resource of an EKS route, as the Service Reference gives it, in the caller's account, where EKS
* looks everything up. A cluster is the ARN EKS holds it under, or for one it holds none of, the ARN a