diff --git a/docs/services/sts.md b/docs/services/sts.md index fd13dc72f7..0c91c817b3 100644 --- a/docs/services/sts.md +++ b/docs/services/sts.md @@ -226,7 +226,8 @@ subject is signed and outlives the call, so a session that has already gone does naming the account root. An unknown long-term key keeps the stand-in. The claim layout is the one the IAM User Guide publishes. The standard OIDC claims sit at the top -level, `iss`, `aud`, `sub`, `iat`, `exp` and `jti`, with the caller's ARN as the subject, and the +level, `iss`, `aud`, `sub`, `iat`, `exp` and `jti`, with the caller's ARN as the subject (for a role +session, the role's ARN, path included, rather than the assumed-role session ARN), and the custom claims are nested under the `https://sts.amazonaws.com/` namespace: `aws_account` for the account the token was asked for, `source_region` for the region it was asked from, and the request's tags under `request_tags`. The guide notes that "all these claims may not be present in diff --git a/src/main/java/io/floci/aws/services/iam/StsQueryHandler.java b/src/main/java/io/floci/aws/services/iam/StsQueryHandler.java index 2b6d1b3889..d15ce8cd3d 100644 --- a/src/main/java/io/floci/aws/services/iam/StsQueryHandler.java +++ b/src/main/java/io/floci/aws/services/iam/StsQueryHandler.java @@ -1487,7 +1487,9 @@ public class StsQueryHandler { + " lifetime of a session beyond what was granted when the session was" + " originally created.", AwsNamespaces.STS, 403); } - String subject = iamService.resolveCallerArn(accessKeyId).orElseGet(() -> + // "For a role session, this is the ARN of the role": the principal's ARN, path included, + // rather than the assumed-role session ARN + String subject = iamService.resolvePrincipalArn(accessKeyId).orElseGet(() -> regionResolver.buildGlobalArn("iam", regionResolver.getAccountId(), "root")); String token = tokenIssuer.mint( WebIdentityTokenIssuer.Algorithm.valueOf(params.getFirst("SigningAlgorithm")), diff --git a/src/test/java/io/floci/aws/services/iam/WebIdentityTokenIntegrationTest.java b/src/test/java/io/floci/aws/services/iam/WebIdentityTokenIntegrationTest.java index ee3e357cf6..e25b55897a 100644 --- a/src/test/java/io/floci/aws/services/iam/WebIdentityTokenIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/iam/WebIdentityTokenIntegrationTest.java @@ -140,6 +140,34 @@ class WebIdentityTokenIntegrationTest { "a request that named no tags carries no request_tags claim"); } + /** + * "For a role session, this is the ARN of the role": the subject names the role that was + * assumed, path included, rather than the assumed-role session ARN, which names the session + * and drops the path. A relying party checks the subject against the role ARN. + */ + @Test + void aRoleSessionsTokenNamesTheRoleAsItsSubject() throws Exception { + enabledIssuer(); + String roleArn = iam("CreateRole") + .formParam("RoleName", "web-identity-subject-role") + .formParam("Path", "/service/") + .formParam("AssumeRolePolicyDocument", """ + {"Version":"2012-10-17","Statement":[{"Effect":"Allow",\ + "Principal":{"AWS":"*"},"Action":"sts:AssumeRole"}]}""") + .when().post("/").then().statusCode(200) + .extract().path("CreateRoleResponse.CreateRoleResult.Role.Arn"); + String sessionKey = sts("AssumeRole") + .formParam("RoleArn", roleArn) + .formParam("RoleSessionName", "local") + .when().post("/").then().statusCode(200) + .extract().path("AssumeRoleResponse.AssumeRoleResult.Credentials.AccessKeyId"); + + String jwt = session(sessionKey).when().post("/").then().statusCode(200).extract().path(TOKEN); + + assertTrue(roleArn.endsWith(":role/service/web-identity-subject-role"), roleArn); + assertEquals(roleArn, part(jwt, 1).path("sub").asText()); + } + /** * {@code ES384} is "ECDSA using P-384 curve with SHA-384", and JWS defines that signature as * the raw {@code R || S} pair: two 48-byte halves for P-384. A DER sequence would be close to