fix(iam): authorize sts:AssumeRole against the role it names (#6334)

IAM enforcement evaluated every STS action against resource *, so a
policy allowing sts:AssumeRole on specific role ARNs never matched and
the call was denied. sts:AssumeRole is now authorized against the
request's RoleArn, as on AWS. Every other STS action stays at *: like
Logs and Secrets Manager, the resource follows the serving operation,
so a RoleArn added to a call that never reads it, such as
GetFederationToken, cannot make a role-scoped Allow grant that call.

Query-protocol parameters were only read from the URL, never from the
form body the SDKs and CLI POST, so SQS and SNS resource ARNs fell back
to a wildcard the same way. For a form POST they are now read from the
body alone: AwsQueryController dispatches on the body and never sees the
URL query, so authorizing a URL value would check one resource and act
on another. SQS preferred a QueueUrl in the URL over the body's, so it
now reads the body first as well.

A resource field with an invalid percent escape is read as absent, so
the check falls back to the default resource instead of throwing out of
the filter; the request then fails in the form reader exactly as it does
without the resource check. The RoleArn is checked untrimmed, as the
handler uses it. docs/services/sts.md no longer lists the * check as a
known limitation.

Co-authored-by: Robin Baeckman <27041454+RobinBaeckman@users.noreply.github.com>
This commit is contained in:
Robin BaeckmanandRobin Baeckman authored and GitHub committed 2026-10-24 08:55:41 -04:00
1 parent 1476b0e219
commit b4c71b9e25
6 files changed
+237 -16

No files matched your search

@@ -767,6 +767,65 @@ class ResourceArnBuilderTest {
}
}
// ── STS ──────────────────────────────────────────────────────────────────────
@Test
void stsAssumeRoleIsAuthorizedAgainstTheRoleItNames() {
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);
}
@Test
void stsAssumeRoleIgnoresARoleArnInTheUrlOfAFormPost() {
// The handler acts on the body's RoleArn, so authorization must check that one, never a
// decoy in the query string.
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);
}
@Test
void stsReadsRoleArnOnlyForAssumeRole() {
// 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"));
}
@Test
void stsMalformedRoleArnIsAuthorizedAsNoRole() {
// Read as absent, so the request reaches the form reader, which refuses it as it does today.
setFormBody("Action=AssumeRole&Version=2011-06-15&RoleArn=arn%3Aaws%ZZ&RoleSessionName=robin");
assertEquals("*", sts("sts:AssumeRole"));
}
@Test
void stsRoleArnIsCheckedAsTheHandlerReadsIt() {
// The handler does not trim, so neither does the check.
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"));
}
private String sts(String action) {
return builder.buildStsResources(action, ctx).getFirst();
}
@Test
void sqsBuildsArnFromFormBodyQueueUrl() {
// The handler acts on the body's QueueUrl, so a decoy in the query string must not be checked.
queryParams.add("QueueUrl", "http://localhost:4566/000000000000/allowed");
setFormBody("Action=SendMessage&QueueUrl=http%3A%2F%2Flocalhost%3A4566%2F000000000000%2Forders&MessageBody=x");
String arn = builder.build("sqs", ctx, "us-east-1", "000000000000");
assertEquals("arn:aws:sqs:us-east-1:000000000000:orders", arn);
}
// ── DynamoDB ─────────────────────────────────────────────────────────────────
@Test
@@ -1102,6 +1161,14 @@ class ResourceArnBuilderTest {
// ── SNS ─────────────────────────────────────────────────────────────────────
@Test
void snsBuildsArnFromFormBodyTopicArn() {
queryParams.add("TopicArn", "arn:aws:sns:us-east-1:000000000000:allowed");
setFormBody("Action=Publish&TopicArn=arn%3Aaws%3Asns%3Aus-east-1%3A000000000000%3Aalerts&Message=hi");
String arn = builder.build("sns", ctx, "us-east-1", "000000000000");
assertEquals("arn:aws:sns:us-east-1:000000000000:alerts", arn);
}
@Test
void snsBuildsArnFromJsonTopicArn() {
String topicArn = "arn:aws:sns:us-east-1:000000000000:alerts";
@@ -0,0 +1,122 @@
package io.github.hectorvent.floci.services.iam;
import io.github.hectorvent.floci.testing.EnforcementFixtures;
import io.github.hectorvent.floci.testing.EnforcementFixtures.UserCredentials;
import io.github.hectorvent.floci.testing.IamEnforcementProfile;
import io.github.hectorvent.floci.testing.PartitionCleanup;
import io.quarkus.test.common.http.TestHTTPResource;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import io.restassured.response.Response;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import java.net.URI;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.UUID;
import static io.github.hectorvent.floci.testing.EnforcementFixtures.auth;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
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).
*/
@QuarkusTest
@TestProfile(IamEnforcementProfile.class)
class StsAssumeRoleResourceEnforcementIntegrationTest {
private static final String ACCOUNT = "111111111111";
@TestHTTPResource
URI baseUri;
@RegisterExtension
final PartitionCleanup cleanup = new PartitionCleanup();
@Test
void aPolicyNamingOneRoleAllowsAssumingThatRoleOnly() {
String suffix = UUID.randomUUID().toString().substring(0, 8);
String allowedArn = createRole("assume-allowed-" + suffix);
String otherArn = createRole("assume-other-" + suffix);
String user = "assume-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(allowedArn));
assumeRole(caller, allowedArn).then().statusCode(200);
assumeRole(caller, otherArn).then().statusCode(403);
}
@Test
void aPolicyNamingOneRoleDoesNotAllowOtherStsActionsThatCarryItsArn() {
// GetFederationToken never acts on a role, so adding the allowed role's ARN to it must not
// make a policy scoped to that role allow it.
String suffix = UUID.randomUUID().toString().substring(0, 8);
String allowedArn = createRole("federation-allowed-" + suffix);
String user = "federation-caller-" + suffix;
String iamAuth = auth(ACCOUNT, "iam");
UserCredentials caller = EnforcementFixtures.createUser(cleanup, baseUri.getPort(), iamAuth, user);
EnforcementFixtures.putUserPolicy(baseUri.getPort(), iamAuth, user, "sts-all", """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"sts:*","Resource":"%s"}]}""".formatted(allowedArn));
given().port(baseUri.getPort())
.contentType("application/x-www-form-urlencoded")
.header("Authorization", auth(caller.accessKeyId(), "sts"))
.formParam("Action", "GetFederationToken")
.formParam("Name", "federated")
.formParam("RoleArn", allowedArn)
.when().post("/")
.then().statusCode(403);
assumeRole(caller, allowedArn).then().statusCode(200);
}
@Test
void aMalformedRoleArnCannotDodgeADenyOnTheRoleItSpells() {
// A RoleArn with an invalid percent escape cannot be decoded, so it is authorized as * like
// a request naming no role, and the Deny on the role it spells does not match. The Allow on *
// lets it through, but the form reader behind the filter cannot decode the body either, so no
// role is assumed: the request fails exactly as it does without the resource check. This pins
// the outcome only; ResourceArnBuilderTest pins that the filter reads the value as absent.
String suffix = UUID.randomUUID().toString().substring(0, 8);
String deniedArn = createRole("malformed-denied-" + suffix);
String user = "malformed-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":"*"},
{"Effect":"Deny","Action":"sts:AssumeRole","Resource":"%s"}]}""".formatted(deniedArn));
given().port(baseUri.getPort()).urlEncodingEnabled(false)
.contentType("application/x-www-form-urlencoded")
.header("Authorization", auth(caller.accessKeyId(), "sts"))
.body("Action=AssumeRole&RoleSessionName=s&RoleArn="
+ URLEncoder.encode(deniedArn, StandardCharsets.UTF_8) + "%ZZ")
.when().post("/")
.then().statusCode(not(200)).body(not(containsString("AccessKeyId")));
}
private String createRole(String role) {
return EnforcementFixtures.createRole(cleanup, baseUri.getPort(), ACCOUNT, "/", role, """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"iam:ListUsers","Resource":"*"}]}""");
}
private Response assumeRole(UserCredentials caller, String roleArn) {
return given().port(baseUri.getPort())
.contentType("application/x-www-form-urlencoded")
.header("Authorization", auth(caller.accessKeyId(), "sts"))
.formParam("Action", "AssumeRole")
.formParam("RoleArn", roleArn)
.formParam("RoleSessionName", "s")
.when().post("/");
}
}