fix(cognito): omit AvailableChallenges from RespondToAuthChallenge responses (#4025)

AvailableChallenges is declared on InitiateAuthResponse and
AdminInitiateAuthResponse only, but the SELECT_CHALLENGE path copied it
onto the RespondToAuthChallenge and AdminRespondToAuthChallenge
responses as well. Keep it on the InitiateAuth responses and drop it
from the challenge responses issued when answering SELECT_CHALLENGE.

Closes #4003
This commit is contained in:
Matheus André authored and GitHub committed 2026-09-20 16:46:07 -04:00
1 parent 173854cdeb
commit db2e0ab8d7
3 files changed
+110 -8

No files matched your search

@@ -2751,6 +2751,52 @@ class CognitoIntegrationTest {
@Test
@Order(107)
void respondToSelectChallengeOmitsAvailableChallengesOverTheWire() throws Exception {
// Reproduces #4003: AvailableChallenges is declared on InitiateAuthResponse only, yet the
// SELECT_CHALLENGE path used to copy it onto the RespondToAuthChallenge response too.
String userAuthPoolId = cognitoJson("CreateUserPool", """
{ "PoolName": "UserAuthWireSelectChallengePool" }
""").path("UserPool").path("Id").asText();
String userAuthClientId = cognitoJson("CreateUserPoolClient", """
{ "UserPoolId": "%s", "ClientName": "user-auth-client" }
""".formatted(userAuthPoolId)).path("UserPoolClient").path("ClientId").asText();
cognitoAction("AdminCreateUser", """
{ "UserPoolId": "%s", "Username": "gale",
"UserAttributes": [{ "Name": "email", "Value": "gale@example.com" }] }
""".formatted(userAuthPoolId))
.then().statusCode(200);
cognitoAction("AdminSetUserPassword", """
{ "UserPoolId": "%s", "Username": "gale", "Password": "Gale1234!", "Permanent": true }
""".formatted(userAuthPoolId))
.then().statusCode(200);
JsonNode init = cognitoJson("InitiateAuth", """
{
"ClientId": "%s",
"AuthFlow": "USER_AUTH",
"AuthParameters": { "USERNAME": "gale" }
}
""".formatted(userAuthClientId));
assertEquals("SELECT_CHALLENGE", init.path("ChallengeName").asText());
assertTrue(init.has("AvailableChallenges"), "InitiateAuth should keep advertising AvailableChallenges");
String session = init.path("Session").asText();
JsonNode challenge = cognitoJson("RespondToAuthChallenge", """
{
"ClientId": "%s",
"ChallengeName": "SELECT_CHALLENGE",
"Session": "%s",
"ChallengeResponses": { "USERNAME": "gale", "ANSWER": "PASSWORD" }
}
""".formatted(userAuthClientId, session));
assertEquals("PASSWORD", challenge.path("ChallengeName").asText());
assertFalse(challenge.has("AvailableChallenges"),
"RespondToAuthChallengeResponse does not declare AvailableChallenges: " + challenge);
}
@Test
@Order(108)
void initiateAuthWithUserAuthRejectsALiteTierPoolOverTheWire() throws Exception {
String liteTierPoolId = cognitoJson("CreateUserPool", """
{ "PoolName": "UserAuthLiteTierPool", "UserPoolTier": "LITE" }
@@ -2781,7 +2827,7 @@ class CognitoIntegrationTest {
}
@Test
@Order(108)
@Order(109)
void respondToAuthChallengeRejectsPasswordWithNoSessionOverTheWire() throws Exception {
// Reproduces the bypass reported on #3930: without session tracking, RespondToAuthChallenge
// would accept a bare USERNAME/PASSWORD pair with ChallengeName=PASSWORD and sign the user
@@ -2256,6 +2256,47 @@ class CognitoServiceTest {
"selecting PASSWORD with the password already attached should complete sign-in immediately");
}
@Test
void selectChallengeResponseDoesNotCarryAvailableChallenges() {
// Only InitiateAuthResponse and AdminInitiateAuthResponse declare AvailableChallenges;
// RespondToAuthChallengeResponse is AuthenticationResult, ChallengeName,
// ChallengeParameters and Session only.
UserPool pool = createPoolAndUser();
UserPoolClient client = service.createUserPoolClient(pool.getId(), "c", false, false, List.of(), List.of());
Map<String, Object> initResult = service.initiateAuth(client.getClientId(), "USER_AUTH",
Map.of("USERNAME", "alice"));
assertNotNull(initResult.get("AvailableChallenges"),
"InitiateAuth should still advertise AvailableChallenges on SELECT_CHALLENGE");
String session = (String) initResult.get("Session");
Map<String, Object> result = service.respondToAuthChallenge(client.getClientId(), "SELECT_CHALLENGE",
session, Map.of("USERNAME", "alice", "ANSWER", "PASSWORD"));
assertEquals("PASSWORD", result.get("ChallengeName"));
assertFalse(result.containsKey("AvailableChallenges"),
"RespondToAuthChallenge does not declare AvailableChallenges: " + result.keySet());
}
@Test
void adminSelectChallengeResponseDoesNotCarryAvailableChallenges() {
UserPool pool = createPoolAndUser();
UserPoolClient client = service.createUserPoolClient(pool.getId(), "c", false, false, List.of(), List.of());
Map<String, Object> initResult = service.adminInitiateAuth(pool.getId(), client.getClientId(), "USER_AUTH",
Map.of("USERNAME", "alice"));
assertNotNull(initResult.get("AvailableChallenges"),
"AdminInitiateAuth should still advertise AvailableChallenges on SELECT_CHALLENGE");
String session = (String) initResult.get("Session");
Map<String, Object> result = service.adminRespondToAuthChallenge(pool.getId(), client.getClientId(),
"SELECT_CHALLENGE", session, Map.of("USERNAME", "alice", "ANSWER", "PASSWORD_SRP"));
assertEquals("PASSWORD_SRP", result.get("ChallengeName"));
assertFalse(result.containsKey("AvailableChallenges"),
"AdminRespondToAuthChallenge does not declare AvailableChallenges: " + result.keySet());
}
@Test
void selectChallengeWithPasswordSrpAnswerAndSrpAReturnsPasswordVerifier() {
UserPool pool = createPoolAndUser();