mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(sts): reject unverified web identity tokens (#3151)
* fix(sts): reject unverified web identity tokens * test(sts): expect invalid web identity tokens to fail * fix(sts): make unknown issuer rejection opt in
This commit is contained in:
1 parent
dd33308e7e
commit
1de6b80035
6 files changed
+78
-29
No files matched your search
@@ -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);
|
||||
|
||||
@@ -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.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public 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();
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user