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

@@ -61,6 +61,7 @@ import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.clearInvocations;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
@@ -1037,11 +1038,22 @@ class ResourceArnBuilderTest {
// ── STS ──────────────────────────────────────────────────────────────────────
@Test
void stsAssumeRoleIsAuthorizedAgainstTheRoleItNames() {
void stsAssumeRoleIsCheckedOnTheRoleItNamesInItsAccountFromTheTable() {
// STS assumes the role in the account its ARN names, so a role IAM holds there is checked alone.
setFormBody("Action=AssumeRole&Version=2011-06-15"
+ "&RoleArn=arn%3Aaws%3Aiam%3A%3A729951873967%3Arole%2FAdmin&RoleSessionName=robin");
String arn = sts("sts:AssumeRole");
assertEquals("arn:aws:iam::729951873967:role/Admin", arn);
assertEquals(List.of("arn:aws:iam::729951873967:role/Admin"), sts("sts:AssumeRole"));
setFormBody("Action=AssumeRole&Version=2011-06-15"
+ "&RoleArn=arn%3Aaws%3Aiam%3A%3A" + ACCOUNT + "%3Arole%2Fops%2FAdmin&RoleSessionName=robin");
assertEquals(List.of("arn:aws:iam::" + ACCOUNT + ":role/ops/Admin"), sts("sts:AssumeRole"));
}
@Test
void stsAssumeRoleOfARoleNoAccountHoldsIsCheckedAsTheCallersToo() {
setFormBody("Action=AssumeRole&Version=2011-06-15"
+ "&RoleArn=arn%3Aaws%3Aiam%3A%3A729951873967%3Arole%2FMissing&RoleSessionName=robin");
assertEquals(List.of("arn:aws:iam::729951873967:role/Missing", "arn:aws:iam::" + ACCOUNT + ":role/Missing"),
sts("sts:AssumeRole"));
}
@Test
@@ -1051,17 +1063,16 @@ class ResourceArnBuilderTest {
queryParams.add("RoleArn", "arn:aws:iam::729951873967:role/Allowed");
setFormBody("Action=AssumeRole&Version=2011-06-15"
+ "&RoleArn=arn%3Aaws%3Aiam%3A%3A729951873967%3Arole%2FAdmin&RoleSessionName=robin");
String arn = sts("sts:AssumeRole");
assertEquals("arn:aws:iam::729951873967:role/Admin", arn);
assertEquals(List.of("arn:aws:iam::729951873967:role/Admin"), sts("sts:AssumeRole"));
}
@Test
void stsReadsRoleArnOnlyForAssumeRole() {
void stsGetFederationTokenIsCheckedOnTheFederatedUserItNamesNotARoleArnItNeverReads() {
// GetFederationToken never acts on a role, so a RoleArn added to it must not let a policy
// scoped to that role allow it.
setFormBody("Action=GetFederationToken&Version=2011-06-15&Name=robin"
+ "&RoleArn=arn%3Aaws%3Aiam%3A%3A729951873967%3Arole%2FAdmin");
assertEquals("*", sts("sts:GetFederationToken"));
assertEquals(List.of("arn:aws:sts::" + ACCOUNT + ":federated-user/robin"), sts("sts:GetFederationToken"));
}
@Test
@@ -1072,15 +1083,21 @@ class ResourceArnBuilderTest {
}
@Test
void stsRoleArnIsCheckedAsTheHandlerReadsIt() {
// The handler does not trim, so neither does the check.
void stsRoleArnFittingNoRoleFormatNamesNoRole() {
// The handler does not trim, and neither does the reader: a value with a leading space fits no
// role's ARN format, so the request is checked on every resource, which a policy naming the role
// does not match.
setFormBody("Action=AssumeRole&Version=2011-06-15"
+ "&RoleArn=+arn%3Aaws%3Aiam%3A%3A729951873967%3Arole%2FAdmin&RoleSessionName=robin");
assertEquals(" arn:aws:iam::729951873967:role/Admin", sts("sts:AssumeRole"));
assertEquals(List.of("*"), sts("sts:AssumeRole"));
}
private String sts(String action) {
return builder.buildStsResources(action, ctx).getFirst();
private List<String> sts(String operation) {
IamService iam = mock(IamService.class);
when(iam.findRole(anyString(), anyString())).thenReturn(Optional.empty());
when(iam.findRole("729951873967", "Admin")).thenReturn(Optional.of(new IamRole()));
return reading(new StsStoredResourceArns(iam)).buildResources("sts", operation, ctx, "us-east-1", ACCOUNT,
"aws");
}
@@ -23,14 +23,16 @@ import static org.hamcrest.Matchers.not;
/**
* With {@code iam.enforcement-enabled=true}, {@code sts:AssumeRole} is authorized against the role it
* names, so an identity policy can allow assuming one role and not another (IAM User Guide, "Granting
* a user permissions to switch roles": the policy's Resource is the role's ARN).
* names, in the account its ARN names, so an identity policy can allow assuming one role and not another
* (IAM User Guide, "Granting a user permissions to switch roles": the policy's Resource is the role's ARN),
* and {@code sts:GetFederationToken} against the federated user its name names.
*/
@QuarkusTest
@TestProfile(IamEnforcementProfile.class)
class StsAssumeRoleResourceEnforcementIntegrationTest {
private static final String ACCOUNT = "111111111111";
private static final String OTHER_ACCOUNT = "222222222222";
@TestHTTPResource
URI baseUri;
@@ -54,6 +56,42 @@ class StsAssumeRoleResourceEnforcementIntegrationTest {
assumeRole(caller, otherArn).then().statusCode(403);
}
@Test
void aPolicyNamingAnotherAccountsRoleAllowsAssumingThatRole() {
// STS assumes the role in the account its ARN names, so the policy naming that role is all
// the caller needs, not also one naming a role of the same name in its own account.
String suffix = UUID.randomUUID().toString().substring(0, 8);
String roleArn = EnforcementFixtures.createRole(cleanup, baseUri.getPort(), OTHER_ACCOUNT, "/",
"cross-account-" + suffix, """
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
"Principal":{"AWS":"arn:aws:iam::%s:root"},"Action":"sts:AssumeRole"}]}""".formatted(ACCOUNT), """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"iam:ListUsers","Resource":"*"}]}""");
String user = "cross-account-caller-" + suffix;
String iamAuth = auth(ACCOUNT, "iam");
UserCredentials caller = EnforcementFixtures.createUser(cleanup, baseUri.getPort(), iamAuth, user);
EnforcementFixtures.putUserPolicy(baseUri.getPort(), iamAuth, user, "assume", """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"sts:AssumeRole","Resource":"%s"}]}""".formatted(roleArn));
assumeRole(caller, roleArn).then().statusCode(200);
}
@Test
void aPolicyNamingOneFederatedUserAllowsGetFederationTokenForThatNameOnly() {
String suffix = UUID.randomUUID().toString().substring(0, 8);
String user = "federation-named-" + suffix;
String iamAuth = auth(ACCOUNT, "iam");
UserCredentials caller = EnforcementFixtures.createUser(cleanup, baseUri.getPort(), iamAuth, user);
EnforcementFixtures.putUserPolicy(baseUri.getPort(), iamAuth, user, "federation", """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"sts:GetFederationToken",
"Resource":"arn:aws:sts::%s:federated-user/fed-%s"}]}""".formatted(ACCOUNT, suffix));
getFederationToken(caller, "fed-" + suffix).then().statusCode(200);
getFederationToken(caller, "other-" + suffix).then().statusCode(403);
}
@Test
void aPolicyNamingOneRoleDoesNotAllowOtherStsActionsThatCarryItsArn() {
// GetFederationToken never acts on a role, so adding the allowed role's ARN to it must not
@@ -110,6 +148,15 @@ class StsAssumeRoleResourceEnforcementIntegrationTest {
{"Effect":"Allow","Action":"iam:ListUsers","Resource":"*"}]}""");
}
private Response getFederationToken(UserCredentials caller, String name) {
return given().port(baseUri.getPort())
.contentType("application/x-www-form-urlencoded")
.header("Authorization", auth(caller.accessKeyId(), "sts"))
.formParam("Action", "GetFederationToken")
.formParam("Name", name)
.when().post("/");
}
private Response assumeRole(UserCredentials caller, String roleArn) {
return given().port(baseUri.getPort())
.contentType("application/x-www-form-urlencoded")