Light, fluffy, and always free - The AWS Local Emulator alternative
fix(cognito): omit AvailableChallenges from RespondToAuthChallenge responses (#4025)
Matheus Andrédb2e0ab8d731
diff --git a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoAuthFlowHandler.java b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoAuthFlowHandler.javaindex f69d1eb00a..0d5e42dd26 100644--- a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoAuthFlowHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoAuthFlowHandler.java@@ -498,7 +498,7 @@ final class CognitoAuthFlowHandler {return userAuthChallengeResponse(pool, client, user, "SELECT_CHALLENGE", available,Map.of("USERNAME", user.getUsername()));}- return startUserAuthChallenge(pool, client, user, preferred, available, params, clientMetadata);+ return startUserAuthChallenge(pool, client, user, preferred, available, true, params, clientMetadata);}/**@@ -519,29 +519,37 @@ final class CognitoAuthFlowHandler {CognitoUser user = service.adminGetUser(pool.getId(), username);requireSignInEligible(user);List<String> available = availableUserAuthChallenges(user);- return startUserAuthChallenge(pool, client, user, answer, available, responses, clientMetadata);+ return startUserAuthChallenge(pool, client, user, answer, available, false, responses, clientMetadata);}+ /**+ * Starts {@code challenge} for {@code user}. {@code advertiseAvailable} says whether the+ * challenge response may carry {@code AvailableChallenges}: only {@code InitiateAuthResponse}+ * and {@code AdminInitiateAuthResponse} declare that member, so the {@code SELECT_CHALLENGE}+ * path (a RespondToAuthChallenge response) passes {@code false}.+ */private Map<String, Object> startUserAuthChallenge(UserPool pool, UserPoolClient client, CognitoUser user,String challenge, List<String> available,+ boolean advertiseAvailable,Map<String, String> params, Map<String, String> clientMetadata) {if (!available.contains(challenge)) {throw new AwsException("InvalidParameterException",challenge + " is not an available challenge for this user", 400);}+ List<String> advertised = advertiseAvailable ? available : null;return switch (challenge) {case "PASSWORD" -> params.containsKey("PASSWORD")? authenticateWithPassword(pool, client, params, clientMetadata)- : userAuthChallengeResponse(pool, client, user, "PASSWORD", available,+ : userAuthChallengeResponse(pool, client, user, "PASSWORD", advertised,Map.of("USERNAME", user.getUsername()));case "PASSWORD_SRP" -> params.containsKey("SRP_A")? handleUserSrpAuth(pool, client, params, clientMetadata)- : userAuthChallengeResponse(pool, client, user, "PASSWORD_SRP", available,+ : userAuthChallengeResponse(pool, client, user, "PASSWORD_SRP", advertised,Map.of("USERNAME", user.getUsername()));case "EMAIL_OTP" -> issueOtpChallenge(pool, client, user, "EMAIL_OTP", "email", "EMAIL",- available, clientMetadata);+ advertised, clientMetadata);case "SMS_OTP" -> issueOtpChallenge(pool, client, user, "SMS_OTP", "phone_number", "SMS",- available, clientMetadata);+ advertised, clientMetadata);default -> throw new AwsException("InvalidParameterException",challenge + " is not a supported challenge", 400);};@@ -585,6 +593,11 @@ final class CognitoAuthFlowHandler {return result;}+ /**+ * Builds a USER_AUTH challenge response and tracks its session. {@code available} is the+ * {@code AvailableChallenges} list to emit, or {@code null} to leave the member out, which+ * RespondToAuthChallenge responses must do because their shape does not declare it.+ */private Map<String, Object> userAuthChallengeResponse(UserPool pool, UserPoolClient client, CognitoUser user,String challengeName, List<String> available,Map<String, String> challengeParameters) {@@ -595,7 +608,9 @@ final class CognitoAuthFlowHandler {result.put("ChallengeName", challengeName);result.put("Session", session);result.put("ChallengeParameters", challengeParameters);- result.put("AvailableChallenges", available);+ if (available != null) {+ result.put("AvailableChallenges", available);+ }return result;}diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoIntegrationTest.javaindex e2239ee1e0..7ed20d3774 100644--- a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoIntegrationTest.java@@ -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": "[email protected]" }] }+ """.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 userdiff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoServiceTest.javaindex 695f9602d5..396bfa299a 100644--- a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoServiceTest.java@@ -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());+ }+@Testvoid selectChallengeWithPasswordSrpAnswerAndSrpAReturnsPasswordVerifier() {UserPool pool = createPoolAndUser();