Light, fluffy, and always free - The AWS Local Emulator alternative
fix(cognito): omit absent authentication message metadata (#6440)
Aniket Shuklac5ab54d49c9f
diff --git a/docs/services/cognito.md b/docs/services/cognito.mdindex c5e0588c1d..135765c71c 100644--- a/docs/services/cognito.md+++ b/docs/services/cognito.md@@ -524,6 +524,13 @@ empty list, matching AWS. It is enforced as if it had `ALLOW_REFRESH_TOKEN_AUTH`the stored or returned value. A client that signs in with `USER_PASSWORD_AUTH`, `ADMIN_USER_PASSWORD_AUTH` or`USER_AUTH` must list the matching `ALLOW_` value.+## Custom Message Trigger++`CustomMessage` events omit `request.clientMetadata` when metadata is absent or empty,+including `CustomMessage_Authentication`, which does not forward `InitiateAuth`+metadata. Custom Email Sender events retain the key with `null` when+there is no metadata; their event shape is distinct.+## Custom Email Sender TriggerA pool whose `LambdaConfig` names a `CustomEmailSender` function (`{"LambdaArn": ..., "LambdaVersion": "V1_0"}`)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 5ab5275c04..bba576e05e 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@@ -1878,17 +1878,14 @@ final class CognitoAuthFlowHandler {* subject/body Cognito would otherwise deliver. Non-blocking: an unconfigured or* failing trigger falls back to the pool's default templated message, mirroring how* {@link #firePostConfirmation} tolerates errors. {@code clientMetadata} is the request's- * ClientMetadata; a sign-in code's event carries none (measured, eu-west-1, 2026-10-20).+ * ClientMetadata; a sign-in code's event omits the member when it carries none.*/Map<String, Object> fireCustomMessage(UserPool pool, UserPoolClient client, CognitoUser user,String triggerSource, Map<String, String> clientMetadata) {Map<String, Object> req = new HashMap<>();req.put("codeParameter", CUSTOM_MESSAGE_CODE_PARAMETER);req.put("usernameParameter", user.getUsername());- Map<String, String> metadata = requestClientMetadata(clientMetadata);- if (metadata != null || "CustomMessage_Authentication".equals(triggerSource)) {- req.put("clientMetadata", metadata);- }+ putCustomMessageClientMetadata(req, clientMetadata);return customMessageResponse(pool, client, user, triggerSource, req);}@@ -1902,16 +1899,20 @@ final class CognitoAuthFlowHandler {Map<String, Object> req = new HashMap<>();req.put("codeParameter", CUSTOM_MESSAGE_CODE_PARAMETER);req.put("usernameParameter", CUSTOM_MESSAGE_USERNAME_PARAMETER);+ putCustomMessageClientMetadata(req, clientMetadata);+ return customMessageResponse(pool, null, user, "CustomMessage_AdminCreateUser", req);+ }++ private static void putCustomMessageClientMetadata(Map<String, Object> request, Map<String, String> clientMetadata) {Map<String, String> metadata = requestClientMetadata(clientMetadata);if (metadata != null) {- req.put("clientMetadata", metadata);+ request.put("clientMetadata", metadata);}- return customMessageResponse(pool, null, user, "CustomMessage_AdminCreateUser", req);}/*** Normalizes absent or empty ClientMetadata for each trigger's event shape. CustomMessage- * omits the member for measured sources; CustomEmailSender includes it with null.+ * omits the member; CustomEmailSender includes it with null.*/private static Map<String, String> requestClientMetadata(Map<String, String> clientMetadata) {return clientMetadata == null || clientMetadata.isEmpty() ? null : clientMetadata;diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoCustomEmailSenderTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoCustomEmailSenderTest.javaindex 35e98ceb77..7a1af4cb9e 100644--- a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoCustomEmailSenderTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoCustomEmailSenderTest.java@@ -250,6 +250,7 @@ class CognitoCustomEmailSenderTest {assertEquals("CustomEmailSender_Authentication", event.get("triggerSource"));// Measured (eu-west-1, 2026-10-20): a sign-in code's event carries no ClientMetadata, though// InitiateAuth sends one.+ assertTrue(request(event).containsKey("clientMetadata"));assertNull(request(event).get("clientMetadata"));Map<String, Object> result = service.respondToAuthChallenge(client.getClientId(), "EMAIL_OTP",diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoLambdaTriggersTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoLambdaTriggersTest.javaindex f059ff7e3a..59e440e0fd 100644--- a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoLambdaTriggersTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoLambdaTriggersTest.java@@ -2806,9 +2806,11 @@ class CognitoLambdaTriggersTest {assertFalse(event.path("request").has("clientMetadata"));}- /** Measured (eu-west-1, 2026-10-20): a sign-in code's event carries no ClientMetadata, though InitiateAuth sends one. */- @Test- void customMessageForASignInCodeCarriesANullClientMetadata() throws Exception {+ /** The raw-event measurement in #6190 distinguishes an absent key from a null value. */+ @ParameterizedTest+ @NullAndEmptySource+ @ValueSource(strings = "web")+ void customMessageForASignInCodeOmitsClientMetadata(String metadataValue) throws Exception {CognitoService svc = customMessageService(mock(SesService.class), VerificationCode.Purpose.EMAIL_OTP, "555666");UserPool pool = customMessagePool(svc, Map.of("Policies", Map.of("SignInPolicy",Map.of("AllowedFirstAuthFactors", List.of("PASSWORD", "EMAIL_OTP")))));@@ -2819,12 +2821,14 @@ class CognitoLambdaTriggersTest {svc.adminSetUserPassword(pool.getId(), "spike", "Spike@Test1", true);ArgumentCaptor<byte[]> events = customMessageAnswers(Map.of());+ Map<String, String> metadata = metadataValue == null ? null+ : metadataValue.isEmpty() ? Map.of() : Map.of("app", metadataValue);svc.initiateAuth(client.getClientId(), "USER_AUTH",- Map.of("USERNAME", "spike", "PREFERRED_CHALLENGE", "EMAIL_OTP"), Map.of("app", "web"));+ Map.of("USERNAME", "spike", "PREFERRED_CHALLENGE", "EMAIL_OTP"), metadata);JsonNode event = MAPPER.readTree(events.getValue());assertEquals("CustomMessage_Authentication", event.path("triggerSource").asText());- assertTrue(event.path("request").path("clientMetadata").isNull());+ assertFalse(event.path("request").has("clientMetadata"));}@Test