fix(cognito): fail authentication when token triggers fail (#4727)

* fix(cognito): fail authentication when token triggers fail

* fix(cognito): reject empty Lambda trigger responses
This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-09-30 13:29:20 -04:00
1 parent c6b8a70ea8
commit 45b00779b7
3 files changed
+92 -21

No files matched your search

@@ -307,8 +307,7 @@ class CognitoLambdaTriggersTest {
}
@Test
@SuppressWarnings("unchecked")
void postAuthenticationLambdaErrorDoesNotBlockAuth() {
void postAuthenticationLambdaErrorBlocksAuth() {
UserPool pool = createPoolWithLambdaConfig(Map.of("PostAuthentication", "arn:aws:lambda:::post"));
seedUser(pool, "alice", "Perm1234!");
UserPoolClient client = createClient(pool);
@@ -316,12 +315,10 @@ class CognitoLambdaTriggersTest {
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::post"), any(byte[].class), any()))
.thenReturn(lambdaError("Unhandled"));
Map<String, Object> result = service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!"));
Map<String, Object> auth = (Map<String, Object>) result.get("AuthenticationResult");
assertNotNull(auth, "Auth should still succeed when PostAuthentication errors");
assertNotNull(auth.get("AccessToken"));
AwsException error = assertThrows(AwsException.class,
() -> service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!")));
assertEquals("UserLambdaValidationException", error.getErrorCode());
}
// =========================================================================
@@ -491,6 +488,53 @@ class CognitoLambdaTriggersTest {
// PreTokenGeneration
// =========================================================================
@Test
void preTokenGenerationLambdaErrorBlocksTokenIssuance() {
UserPool pool = createPoolWithLambdaConfig(Map.of("PreTokenGeneration", "arn:aws:lambda:::pre-token"));
seedUser(pool, "alice", "Perm1234!");
UserPoolClient client = createClient(pool);
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::pre-token"), any(byte[].class), any()))
.thenReturn(lambdaError("Unhandled"));
AwsException error = assertThrows(AwsException.class,
() -> service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!")));
assertEquals("UserLambdaValidationException", error.getErrorCode());
}
@Test
void preTokenGenerationMalformedResponseBlocksTokenIssuance() {
UserPool pool = createPoolWithLambdaConfig(Map.of("PreTokenGeneration", "arn:aws:lambda:::pre-token"));
seedUser(pool, "alice", "Perm1234!");
UserPoolClient client = createClient(pool);
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::pre-token"), any(byte[].class), any()))
.thenReturn(rawPayload("not-json"));
AwsException error = assertThrows(AwsException.class,
() -> service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!")));
assertEquals("InvalidLambdaResponseException", error.getErrorCode());
}
@ParameterizedTest
@ValueSource(booleans = {true, false})
void preTokenGenerationEmptyResponseBlocksTokenIssuance(boolean nullPayload) {
UserPool pool = createPoolWithLambdaConfig(Map.of("PreTokenGeneration", "arn:aws:lambda:::pre-token"));
seedUser(pool, "alice", "Perm1234!");
UserPoolClient client = createClient(pool);
byte[] payload = nullPayload ? null : new byte[0];
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::pre-token"), any(byte[].class), any()))
.thenReturn(new InvokeResult(200, null, payload, null, "req-id"));
AwsException error = assertThrows(AwsException.class,
() -> service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!")));
assertEquals("InvalidLambdaResponseException", error.getErrorCode());
}
@Test
void newPasswordMfaSetupPreservesPreTokenGenerationSource() throws Exception {
MutableClock clock = new MutableClock();