fix(apigatewayv2): accept array aud claim in HTTP API JWT authorizer (#7377)

This commit is contained in:
Rafael Padilla authored and GitHub committed 2026-11-06 20:10:56 -04:00
1 parent 4617e4a4f8
commit feb9364760
2 files changed
+112 -9

No files matched your search

@@ -3248,11 +3248,7 @@ public class ApiGatewayExecuteController {
List<String> audiences = authorizer.getJwtConfiguration().audience();
if (audiences != null && !audiences.isEmpty()) {
// Cognito access tokens omit `aud` and use `client_id` instead.
// Match either to support both ID tokens and access tokens.
boolean audMatch = audiences.stream().anyMatch(a ->
a.equals(claims.aud) || a.equals(claims.clientId));
if (!audMatch) {
if (!audienceMatches(audiences, claims)) {
return new JwtAuthorizerResult(Response.status(401)
.entity(jsonMessage("Unauthorized"))
.type(MediaType.APPLICATION_JSON).build(), null);
@@ -3690,9 +3686,44 @@ public class ApiGatewayExecuteController {
// `scopes` is the token's own scope list (scp claim first, else scope) or null when it has
// neither - kept separate from `raw` because scope matching needs the pre-rendered values.
// Package-private (like buildV2ProxyEvent) so the wire-format rendering is unit-testable.
record JwtClaims(String iss, String aud, String clientId, long exp,
// `audPresent` records whether the token carries an `aud` claim at all, separately from
// `audiences`: an `aud` that is present but empty or malformed must not fall back to client_id.
record JwtClaims(String iss, boolean audPresent, List<String> audiences, String clientId, long exp,
Map<String, String> raw, List<String> scopes) {}
/**
* Whether a token satisfies a JWT authorizer's configured audience list. Like real API
* Gateway, the token's {@code aud} may be a single string or an array (RFC 7519 §4.1.3);
* it matches when any configured audience is among the token's audiences. Auth0, for one,
* issues an array {@code aud} (the API plus its {@code /userinfo} endpoint) whenever the
* {@code openid} scope is requested. Cognito access tokens omit {@code aud} and carry
* {@code client_id} instead, so {@code client_id} is checked only when {@code aud} is absent;
* when both are present, API Gateway evaluates {@code aud} alone.
*/
static boolean audienceMatches(List<String> configuredAudiences, JwtClaims claims) {
if (claims.audPresent()) {
return configuredAudiences.stream().anyMatch(claims.audiences()::contains);
}
return claims.clientId() != null && configuredAudiences.contains(claims.clientId());
}
/** The token's {@code aud} claim as a list: a string becomes one entry, an array keeps its strings. */
private static List<String> parseAudiences(JsonNode aud) {
if (aud.isTextual()) {
return List.of(aud.asText());
}
if (aud.isArray()) {
List<String> audiences = new ArrayList<>();
for (JsonNode item : aud) {
if (item.isTextual()) {
audiences.add(item.asText());
}
}
return List.copyOf(audiences);
}
return List.of();
}
JwtClaims parseJwtClaims(String token) {
try {
String[] parts = token.split("\\.");
@@ -3701,9 +3732,10 @@ public class ApiGatewayExecuteController {
String payload = new String(payloadBytes, StandardCharsets.UTF_8);
JsonNode claims = objectMapper.readTree(payload);
String iss = claims.path("iss").asText(null);
String aud = claims.path("aud").asText(null);
boolean audPresent = claims.hasNonNull("aud");
List<String> audiences = parseAudiences(claims.path("aud"));
// Cognito access tokens omit `aud` and use `client_id` instead. AWS HTTP API
// JWT authorizers accept either when matching the configured audience list.
// JWT authorizers fall back to `client_id` only when `aud` is absent.
String clientId = claims.path("client_id").asText(null);
long exp = claims.path("exp").asLong(0);
@@ -3719,7 +3751,7 @@ public class ApiGatewayExecuteController {
if (rendered != null) raw.put(field.getKey(), rendered);
}
return new JwtClaims(iss, aud, clientId, exp, raw, deriveJwtScopes(claims));
return new JwtClaims(iss, audPresent, audiences, clientId, exp, raw, deriveJwtScopes(claims));
} catch (Exception e) {
LOG.debugv("JWT parse error: {0}", e.getMessage());
return null;
@@ -65,6 +65,77 @@ class JwtClaimsWireFormatTest {
"nested object claims fall back to JSON text");
}
@Test
void parsesStringAudienceAsSingleEntry() {
ApiGatewayExecuteController.JwtClaims claims = controller.parseJwtClaims(unsignedToken("""
{"sub": "u", "aud": "https://api.example.com"}
"""));
assertNotNull(claims);
assertEquals(List.of("https://api.example.com"), claims.audiences());
}
@Test
void parsesArrayAudienceKeepingEveryEntry() {
// Auth0 issues this shape whenever the openid scope is requested.
ApiGatewayExecuteController.JwtClaims claims = controller.parseJwtClaims(unsignedToken("""
{"sub": "u", "aud": ["https://api.example.com", "https://tenant.auth0.com/userinfo"]}
"""));
assertNotNull(claims);
assertEquals(List.of("https://api.example.com", "https://tenant.auth0.com/userinfo"),
claims.audiences());
}
@Test
void audienceMatches_arrayAudContainingConfiguredAudience_matches() {
ApiGatewayExecuteController.JwtClaims claims = controller.parseJwtClaims(unsignedToken("""
{"sub": "u", "aud": ["https://api.example.com", "https://tenant.auth0.com/userinfo"]}
"""));
assertTrue(ApiGatewayExecuteController.audienceMatches(
List.of("https://api.example.com"), claims));
}
@Test
void audienceMatches_arrayAudWithoutConfiguredAudience_doesNotMatch() {
ApiGatewayExecuteController.JwtClaims claims = controller.parseJwtClaims(unsignedToken("""
{"sub": "u", "aud": ["https://other.example.com", "https://tenant.auth0.com/userinfo"]}
"""));
assertFalse(ApiGatewayExecuteController.audienceMatches(
List.of("https://api.example.com"), claims));
}
@Test
void audienceMatches_cognitoAccessTokenClientId_matches() {
ApiGatewayExecuteController.JwtClaims claims = controller.parseJwtClaims(unsignedToken("""
{"sub": "u", "client_id": "app-client-123"}
"""));
assertNotNull(claims);
assertEquals(List.of(), claims.audiences());
assertTrue(ApiGatewayExecuteController.audienceMatches(List.of("app-client-123"), claims));
}
@Test
void audienceMatches_mismatchedAudWithMatchingClientId_doesNotMatch() {
ApiGatewayExecuteController.JwtClaims claims = controller.parseJwtClaims(unsignedToken("""
{"sub": "u", "aud": ["other"], "client_id": "expected"}
"""));
assertFalse(ApiGatewayExecuteController.audienceMatches(List.of("expected"), claims));
}
@Test
void audienceMatches_emptyAudArrayWithMatchingClientId_doesNotMatch() {
ApiGatewayExecuteController.JwtClaims claims = controller.parseJwtClaims(unsignedToken("""
{"sub": "u", "aud": [], "client_id": "expected"}
"""));
assertFalse(ApiGatewayExecuteController.audienceMatches(List.of("expected"), claims));
}
@Test
void derivesScopesFromScpArrayFirst() {
ApiGatewayExecuteController.JwtClaims claims = controller.parseJwtClaims(unsignedToken("""