fix(cognito): omit absent authentication message metadata (#6440)

This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-10-24 14:10:26 -04:00
1 parent 9e12547978
commit c5ab54d49c
4 files changed
+26 -13

No files matched your search

+7
View File
@@ -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 Trigger
A pool whose `LambdaConfig` names a `CustomEmailSender` function (`{"LambdaArn": ..., "LambdaVersion": "V1_0"}`)
@@ -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;
@@ -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",
@@ -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