fix(verifiedpermissions): accept tokens issued by Floci's own Cognito pools (#8133)

* fix(verifiedpermissions): accept tokens issued by Floci's own Cognito pools
* fix(verifiedpermissions): verify emulated pool tokens with Cognito's own check
This commit is contained in:
Giovanni Brethenoux authored and GitHub committed 2026-11-13 16:25:52 -04:00
1 parent eea61cd42a
commit 294ad9679f
3 files changed
+150 -6

No files matched your search

@@ -6227,6 +6227,19 @@ public class CognitoService implements ResourceProvider {
}
}
/**
* The id of the Floci pool that issued and signed this token, after the same alg, issuer, kid
* and signature checks Cognito applies to its own tokens; empty when any of them fails.
*/
public Optional<String> verifiedIssuingPoolId(String token) {
try {
return Optional.of(verifyJwtSignatureAndIssuer(token).poolId());
} catch (Exception e) {
LOG.debug("Token not verified as issued by a Floci pool", e);
return Optional.empty();
}
}
private VerifiedJwt verifyJwtSignatureAndIssuer(String token) throws Exception {
if (token == null || token.isBlank()) {
throw new IllegalArgumentException("missing token");
@@ -4,6 +4,8 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.services.cognito.CognitoService;
import io.floci.aws.services.verifiedpermissions.model.EntityIdentifier;
import io.floci.aws.services.verifiedpermissions.model.IdentitySource;
import jakarta.enterprise.context.ApplicationScoped;
@@ -24,16 +26,19 @@ public class VerifiedPermissionsTokenService {
private final CedarAuthorizationEvaluator evaluator;
private final VerifiedPermissionsOidcSignatureVerifier signatureVerifier;
private final ObjectMapper objectMapper;
private final CognitoService cognitoService;
@Inject
public VerifiedPermissionsTokenService(VerifiedPermissionsService service,
CedarAuthorizationEvaluator evaluator,
VerifiedPermissionsOidcSignatureVerifier signatureVerifier,
ObjectMapper objectMapper) {
ObjectMapper objectMapper,
CognitoService cognitoService) {
this.service = service;
this.evaluator = evaluator;
this.signatureVerifier = signatureVerifier;
this.objectMapper = objectMapper;
this.cognitoService = cognitoService;
}
public PreparedTokenRequest prepare(JsonNode request, String region) {
@@ -72,13 +77,28 @@ public class VerifiedPermissionsTokenService {
if (issuer == null || issuer.isBlank()) {
throw VerifiedPermissionsService.validation("Token has no issuer claim.");
}
IdentitySource source = service.identitySourcesForStore(store, region).stream()
List<IdentitySource> sources = service.identitySourcesForStore(store, region);
// A Cognito identity source names a pool. When that pool is one of Floci's own, its
// tokens carry Floci's issuer (CognitoService#getIssuer), not the AWS host stored in the
// source's configuration, and are verified against the pool's signing key here: the OIDC
// verifier only fetches public HTTPS JWKS, which an emulated pool never has.
IdentitySource local = sources.stream()
.filter(candidate -> issuer.equals(emulatedCognitoIssuer(candidate.configuration())))
.findFirst().orElse(null);
IdentitySource source = local != null ? local : sources.stream()
.filter(candidate -> issuer.equals(VerifiedPermissionsService.identityIssuer(candidate.configuration())))
.findFirst().orElseThrow(() -> VerifiedPermissionsService.validation("No identity source matches the token issuer."));
try {
signatureVerifier.verify(token, issuer);
} catch (VerifiedPermissionsOidcSignatureVerifier.VerificationException e) {
throw VerifiedPermissionsService.validation("Token signature validation failed: " + e.getMessage());
if (local != null) {
String poolId = cognitoPoolId(local.configuration());
if (!cognitoService.verifiedIssuingPoolId(token).map(poolId::equals).orElse(false)) {
throw VerifiedPermissionsService.validation("Token signature validation failed.");
}
} else {
try {
signatureVerifier.verify(token, issuer);
} catch (VerifiedPermissionsOidcSignatureVerifier.VerificationException e) {
throw VerifiedPermissionsService.validation("Token signature validation failed: " + e.getMessage());
}
}
validateTimes(claims);
JsonNode configuration = source.configuration();
@@ -217,6 +237,37 @@ public class VerifiedPermissionsTokenService {
};
}
/** The pool id of a Cognito identity source, or null for an OIDC one. */
private static String cognitoPoolId(JsonNode configuration) {
JsonNode cognito = configuration.get("cognitoUserPoolConfiguration");
if (cognito == null) {
return null;
}
String arn = cognito.path("userPoolArn").asText("");
int slash = arn.indexOf("userpool/");
return slash < 0 ? null : arn.substring(slash + "userpool/".length());
}
/**
* The issuer Floci's Cognito signs with for this source's pool, or null when the source does
* not name one of Floci's pools: the full ARN must match, not only the pool id.
*/
private String emulatedCognitoIssuer(JsonNode configuration) {
String poolId = cognitoPoolId(configuration);
if (poolId == null) {
return null;
}
String configuredArn = configuration.path("cognitoUserPoolConfiguration").path("userPoolArn").asText();
try {
if (!configuredArn.equals(cognitoService.describeUserPool(poolId).getArn())) {
return null;
}
} catch (AwsException e) {
return null;
}
return cognitoService.getIssuer(poolId);
}
private JsonNode decodeClaims(String token) {
String[] parts = token == null ? new String[0] : token.split("\\.", -1);
if (parts.length != 3) {