fix(iam): check STS requests on the resources the generated table gives them (#9033)

STS was built by hand: ResourceArnBuilder checked AssumeRole on the role
its RoleArn named and every other operation on *, IamEnforcementFilter
sent STS requests there by action, and the request resources table held
no STS operation. sts moves from HAND_BUILT to NAMESPACES, so the table
gives each operation what the Service Reference and botocore give it:
AssumeRole the role its RoleArn names, as before, GetFederationToken the
federated user its Name names and AssumeRoot the root user, where the
hand builder checked both on *, and the operations that act on no
resource type * as before. StsStoredResourceArns answers that a role IAM
holds in the account its ARN names is assumed there, as the handler
finds it, so a cross-account AssumeRole is checked on that role alone
and not also on the caller's role of that name. ResourceArnBuilder's STS
section and the filter's STS case go. Only STS's operations change in
the table.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-19 21:56:53 -04:00
1 parent a769b77124
commit 1a888867ad
9 files changed
+165 -49

No files matched your search

@@ -1801,10 +1801,10 @@ 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; API Gateway's path ARNs on the request's partition, which that service mints
* them in; 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}).
* Logs, Secrets Manager and KMS targets depend on the serving operation and the request's partition; API
* Gateway's path ARNs on the request's partition, which that service mints them in; 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}).
*/
private List<String> targetResources(String credentialScope, String action, String operation,
ContainerRequestContext ctx, String region, String accountId) {
@@ -1814,9 +1814,6 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
if ("kms".equals(credentialScope)) {
return arnBuilder.buildKmsResources(action, ctx, region, accountId, requestPartition());
}
if ("sts".equals(credentialScope)) {
return arnBuilder.buildStsResources(action, ctx);
}
if ("apigateway".equals(credentialScope)) {
return arnBuilder.buildResources(credentialScope, ctx, region, accountId, requestPartition());
}
@@ -165,9 +165,10 @@ public interface StoredResourceArns {
/**
* Whether an ARN naming another account than the caller's, {@code accountId}, names a resource the service holds
* in that account and acts on there, never as the caller's resource of that name (the sink an OAM link is made
* to, a RAM invitation sent to the caller or a share another account owns), so a request naming it is checked on
* that ARN alone; false when that account holds no such resource. Whether the caller may use it is the handler's
* to answer after the check. Read by the ARN alone, as it runs on every such request.
* to, a RAM invitation sent to the caller, a share another account owns or a role STS assumes in its account), so
* a request naming it is checked on that ARN alone; false when that account holds no such resource. Whether the
* caller may use it is the handler's to answer after the check. Read by the ARN alone, as it runs on every such
* request.
*/
default boolean reachedInItsAccount(String arn, String accountId) {
return false;
@@ -1614,22 +1614,6 @@ public class ResourceArnBuilder {
}
}
// ── STS ─────────────────────────────────────────────────────────────────────
/**
* Uses the serving operation, as {@link #buildLogResources} does: {@code AssumeRole} is
* authorized against the role it names, so a policy can allow assuming one role and not another.
* Every other operation keeps {@code *}, so a {@code RoleArn} it never reads cannot choose its resource.
*/
public List<String> buildStsResources(String action, ContainerRequestContext ctx) {
return List.of("sts:AssumeRole".equals(action) ? buildAssumeRoleArn(ctx) : "*");
}
private String buildAssumeRoleArn(ContainerRequestContext ctx) {
String roleArn = firstFormParam(ctx, "RoleArn");
// Untrimmed: the handler assumes the role named by this exact string.
return roleArn == null || roleArn.isBlank() ? "*" : roleArn;
}
/**
* A Query-protocol parameter, read from the same place the handler reads it. For a form POST
* that is the body alone: {@code AwsQueryController} dispatches on the form parameters and never
@@ -0,0 +1,58 @@
package io.floci.aws.services.iam;
import io.floci.aws.core.common.AwsArnUtils;
import io.floci.aws.core.common.StoredResourceArns;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.util.List;
import java.util.Optional;
/**
* What IAM asks of STS for the {@code sts} namespace: whether the role a request names in another account is one STS
* assumes there. STS holds no resource of its own; the roles it assumes are IAM's.
*/
@ApplicationScoped
public class StsStoredResourceArns implements StoredResourceArns {
private static final String NAMESPACE = "sts";
private static final String ROLE = "role/";
private final IamService iam;
@Inject
public StsStoredResourceArns(IamService iam) {
this.iam = iam;
}
@Override
public String namespace() {
return NAMESPACE;
}
/** A request names its role, federated user or root user outright: nothing to find. */
@Override
public Optional<String> find(String type, List<String> names, String region) {
return Optional.empty();
}
/**
* Whether a role ARN naming another account than the caller's names a role IAM holds in that account, which STS
* assumes there, as its handler finds it: in the ARN's account, by the last segment of the ARN's resource. Such a
* request is checked on that ARN alone, not also on the caller's role of that name. False when that account holds
* no such role.
*/
@Override
public boolean reachedInItsAccount(String arn, String accountId) {
if (!AwsArnUtils.isArnFor(arn, "iam")) {
return false;
}
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
String resource = parsed.resource();
if (parsed.accountId().isEmpty() || parsed.accountId().equals(accountId) || !resource.startsWith(ROLE)) {
return false;
}
String[] path = resource.split("/", -1);
return iam.findRole(parsed.accountId(), path[path.length - 1]).isPresent();
}
}