Light, fluffy, and always free - The AWS Local Emulator alternative
fix(sts): reject unverified web identity tokens (#3151)
Niels Kootstra1de6b8003506
diff --git a/docs/services/sts.md b/docs/services/sts.mdindex f79735c0f7..c2b9c62b11 100644--- a/docs/services/sts.md+++ b/docs/services/sts.md@@ -41,7 +41,7 @@ created through IAM with a real trust policy.## Web Identity Validation (IRSA)-`AssumeRoleWithWebIdentity` validates tokens minted by an OIDC issuer Floci hosts - currently an EKS cluster's IRSA provider. This needs no configuration flag and is independent of `FLOCI_SERVICES_IAM_ENFORCEMENT_ENABLED`.+`AssumeRoleWithWebIdentity` validates tokens minted by an OIDC issuer Floci hosts - currently an EKS cluster's IRSA provider.When the token's `iss` names a known Floci issuer, all of the following are enforced:@@ -53,7 +53,7 @@ When the token's `iss` names a known Floci issuer, all of the following are enfoThe response carries the token's real claims in `SubjectFromWebIdentityToken`, `Provider`, and `Audience`. A bad token returns `InvalidIdentityToken` (400); a trust policy that does not permit the subject returns `AccessDenied` (403).-Tokens from an issuer Floci does not host are treated as opaque and accepted, since Floci cannot verify a third-party provider's signature. See [EKS](eks.md) for the full IRSA walkthrough and the token-minting endpoint.+By default, tokens from an issuer Floci does not host, or tokens that are not parseable JWTs, are treated as opaque and accepted for compatibility with existing workflows. When `FLOCI_SERVICES_IAM_ENFORCEMENT_ENABLED=true`, those tokens return `InvalidIdentityToken` because Floci cannot verify a third-party provider's signature. See [EKS](eks.md) for the full IRSA walkthrough and the token-minting endpoint.## Examplesdiff --git a/src/main/java/io/github/hectorvent/floci/core/common/OidcIssuerKeyLookup.java b/src/main/java/io/github/hectorvent/floci/core/common/OidcIssuerKeyLookup.javaindex 791df7eae1..d5f1892d75 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/OidcIssuerKeyLookup.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/OidcIssuerKeyLookup.java@@ -10,14 +10,13 @@ import java.util.Optional;* {@code services.iam}) can verify {@code sts:AssumeRoleWithWebIdentity} tokens minted for an* EKS cluster's IRSA issuer without {@code services.iam} depending on {@code services.eks}.*- * <p>An empty result means the issuer is unknown to Floci. Callers treat that as "not a- * Floci-issued token" and fall back to permissive handling rather than rejecting, so- * third-party web-identity tokens keep working.+ * <p>An empty result means the issuer is unknown to Floci. Callers must reject tokens from+ * unknown issuers because they cannot be verified against a trusted key.*/public interface OidcIssuerKeyLookup {/**- * Returns the RSA public key for {@code issuer}, or {@link Optional#empty()} if no+ * Returns the RSA public key for {@code issuer}, or {@link Optional#empty()} if no trusted* resource known to Floci issues tokens under that issuer URL.*/Optional<RSAPublicKey> findVerificationKey(String issuer);diff --git a/src/main/java/io/github/hectorvent/floci/core/common/WebIdentityTokenVerifier.java b/src/main/java/io/github/hectorvent/floci/core/common/WebIdentityTokenVerifier.javaindex 80b8c7fcb7..531698032a 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/WebIdentityTokenVerifier.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/WebIdentityTokenVerifier.java@@ -26,10 +26,10 @@ import jakarta.inject.Inject;* JWTs with plain {@code java.security} primitives, and this only needs the one algorithm EKS IRSA* uses.*- * <p>Two-stage design, which is what keeps existing permissive behaviour intact. {@link #peekIssuer}- * reads the {@code iss} claim <em>without</em> verifying anything, so a caller can ask "is this a- * token Floci issued?" before deciding to enforce. Only when the issuer resolves to a known key does- * {@link #verify} run and reject on any failure.+ * <p>Two-stage design lets the caller select a trusted key before verification. {@link #peekIssuer}+ * reads the {@code iss} claim <em>without</em> verifying anything. The caller must reject tokens+ * when the issuer does not resolve to a trusted key; {@link #verify} then rejects any verification+ * failure for a known issuer.*/@ApplicationScopedpublic class WebIdentityTokenVerifier {@@ -55,7 +55,7 @@ public class WebIdentityTokenVerifier {/*** Reads the {@code iss} claim without verifying the signature. Returns empty when {@code token}- * is not a parseable JWT at all — the caller then treats it as an opaque third-party token.+ * is not a parseable JWT at all — the caller must reject it.*/public Optional<String> peekIssuer(String token) {return parseClaims(token).map(claims -> claims.path("iss").asText(null))@@ -169,8 +169,7 @@ public class WebIdentityTokenVerifier {return Optional.empty();}// Limit -1 for the same reason as verify(): without it an unsigned "header.payload." token- // splits into two parts, so its issuer would go unseen and the caller would treat a token- // that names a Floci issuer as opaque, skipping validation entirely.+ // splits into two parts, so its issuer would go unseen and validation could be skipped.String[] parts = token.split("\\.", -1);return parts.length == 3 ? decodeJson(parts[1]) : Optional.empty();}diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/StsQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/iam/StsQueryHandler.javaindex 6d8fd608c1..5801b38b9d 100644--- a/src/main/java/io/github/hectorvent/floci/services/iam/StsQueryHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/iam/StsQueryHandler.java@@ -199,9 +199,6 @@ public class StsQueryHandler {String callerAccountId = regionResolver.getAccountId();String accountId = AwsArnUtils.accountOrDefault(roleArn, callerAccountId);- // Only tokens naming an issuer Floci itself hosts (an EKS cluster's IRSA OIDC provider) are- // verifiable, so only those are enforced. An opaque or third-party token keeps the historical- // permissive behaviour rather than failing a workflow Floci cannot adjudicate.WebIdentityOutcome outcome = verifyWebIdentityToken(webIdentityToken, roleName, accountId, roleArn);if (outcome.denial() != null) {return outcome.denial();@@ -242,16 +239,11 @@ public class StsQueryHandler {/** The claims of a token Floci issued and verified, used to fill the response accurately. */private record VerifiedWebIdentity(String issuer, String subject, String audience) {}- /**- * The result of inspecting a web identity token: at most one field is non-null. Both null means- * the issuer is unknown to Floci, so the token is treated as opaque and accepted.- */private record WebIdentityOutcome(VerifiedWebIdentity verified, Response denial) {static WebIdentityOutcome unverifiable() {return new WebIdentityOutcome(null, null);}-static WebIdentityOutcome allow(VerifiedWebIdentity verified) {return new WebIdentityOutcome(verified, null);}@@ -261,20 +253,22 @@ public class StsQueryHandler {}}- /**- * Inspects {@code token} and decides whether it may assume {@code roleArn}. Returns an outcome- * carrying the verified claims, a denial response, or neither when the token's issuer is not one- * Floci hosts (preserving the historical permissive behaviour for third-party providers).- */+ /** Inspects {@code token} and decides whether it may assume {@code roleArn}. */private WebIdentityOutcome verifyWebIdentityToken(String token, String roleName, String roleAccountId,String roleArn) {Optional<String> issuer = tokenVerifier.peekIssuer(token);if (issuer.isEmpty()) {- return WebIdentityOutcome.unverifiable();+ return config.services().iam().enforcementEnabled()+ ? WebIdentityOutcome.deny(AwsQueryResponse.error("InvalidIdentityToken",+ "The web identity token does not identify a trusted issuer.", AwsNamespaces.STS, 400))+ : WebIdentityOutcome.unverifiable();}Optional<RSAPublicKey> key = oidcIssuerKeys.findVerificationKey(issuer.get());if (key.isEmpty()) {- return WebIdentityOutcome.unverifiable();+ return config.services().iam().enforcementEnabled()+ ? WebIdentityOutcome.deny(AwsQueryResponse.error("InvalidIdentityToken",+ "The web identity token issuer is not trusted.", AwsNamespaces.STS, 400))+ : WebIdentityOutcome.unverifiable();}WebIdentityToken claims;diff --git a/src/test/java/io/github/hectorvent/floci/services/eks/EksIrsaEndToEndIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/eks/EksIrsaEndToEndIntegrationTest.javaindex 6bd4e3066a..7e73ed3161 100644--- a/src/test/java/io/github/hectorvent/floci/services/eks/EksIrsaEndToEndIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/eks/EksIrsaEndToEndIntegrationTest.java@@ -18,7 +18,7 @@ import static org.hamcrest.Matchers.notNullValue;* it via {@code sts:AssumeRoleWithWebIdentity}.** <p>Also covers the negative cases that matter — wrong service account, tampered signature, wrong- * audience — and the compatibility guarantee that an opaque third-party token is still accepted.+ * audience, and the compatibility guarantee that an opaque third-party token is still accepted.*/@QuarkusTest@TestMethodOrder(MethodOrderer.OrderAnnotation.class)diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/StsWebIdentityEnforcementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/StsWebIdentityEnforcementIntegrationTest.javanew file mode 100644index 0000000000..58bef2cf6e--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/iam/StsWebIdentityEnforcementIntegrationTest.java@@ -0,0 +1,57 @@+package io.github.hectorvent.floci.services.iam;++import io.quarkus.test.junit.QuarkusTest;+import io.quarkus.test.junit.QuarkusTestProfile;+import io.quarkus.test.junit.TestProfile;+import org.junit.jupiter.api.Test;++import java.util.Map;++import static io.restassured.RestAssured.given;+import static org.hamcrest.Matchers.containsString;++@QuarkusTest+@TestProfile(StsWebIdentityEnforcementIntegrationTest.EnforcementProfile.class)+class StsWebIdentityEnforcementIntegrationTest {++ @Test+ void rejectsOpaqueTokenWhenIamEnforcementIsEnabled() {+ assumeRole("dummy-token")+ .then()+ .statusCode(400)+ .body(containsString("InvalidIdentityToken"));+ }++ @Test+ void rejectsUnknownIssuerWhenIamEnforcementIsEnabled() {+ String header = java.util.Base64.getUrlEncoder().withoutPadding().encodeToString(+ "{\"alg\":\"none\"}".getBytes(java.nio.charset.StandardCharsets.UTF_8));+ String payload = java.util.Base64.getUrlEncoder().withoutPadding().encodeToString(+ "{\"iss\":\"https://untrusted.example\",\"aud\":\"sts.amazonaws.com\"}"+ .getBytes(java.nio.charset.StandardCharsets.UTF_8));++ assumeRole(header + "." + payload + ".")+ .then()+ .statusCode(400)+ .body(containsString("InvalidIdentityToken"));+ }++ private static io.restassured.response.Response assumeRole(String token) {+ return given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "AssumeRoleWithWebIdentity")+ .formParam("Version", "2011-06-15")+ .formParam("RoleArn", "arn:aws:iam::000000000000:role/web-identity-role")+ .formParam("RoleSessionName", "enforcement-test-session")+ .formParam("WebIdentityToken", token)+ .when()+ .post("/");+ }++ public static final class EnforcementProfile implements QuarkusTestProfile {+ @Override+ public Map<String, String> getConfigOverrides() {+ return Map.of("floci.services.iam.enforcement-enabled", "true");+ }+ }+}