mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(cognito): keep attribute updates after sender failures (#6493)
* fix(cognito): keep attribute updates after sender failures * fix(cognito): withdraw stale codes after sender failure Invalidate prior verification codes when a failed sender handover retains a different active or pending contact destination. Preserve codes for unchanged contacts the send loop never reached.
This commit is contained in:
1 parent
b15bc708fa
commit
da9f7e93b1
3 files changed
+170
-28
No files matched your search
@@ -592,6 +592,13 @@ attribute to `true`.
|
||||
|
||||
Without the setting, `UpdateUserAttributes` replaces the value immediately and
|
||||
sets the matching `*_verified` attribute to `false` until verification succeeds.
|
||||
A failed hand-over of its code to `CustomEmailSender` still keeps the complete
|
||||
attribute update, including other attributes in the same request. The request
|
||||
returns the delivery error, and the undelivered code is withdrawn so another
|
||||
verification code can be requested after the sender is working again.
|
||||
Codes issued for earlier destinations of changed contacts are also withdrawn,
|
||||
including when the replacement is pending. Codes for unchanged contacts that
|
||||
the failed delivery did not reach remain usable.
|
||||
A verification code is still sent to the replacement value, but Cognito no
|
||||
longer retains or exposes the old value. For alias attributes, the old alias is
|
||||
removed immediately and the replacement becomes usable for sign-in only after
|
||||
|
||||
@@ -4245,34 +4245,19 @@ public class CognitoService implements ResourceProvider {
|
||||
|
||||
CognitoUser updatedUser = MAPPER.convertValue(currentUser, CognitoUser.class);
|
||||
List<Map<String, Object>> deliveryDetails = new ArrayList<>();
|
||||
Map<String, String> contacts = new LinkedHashMap<>();
|
||||
|
||||
boolean issued = false;
|
||||
try {
|
||||
for (Map.Entry<String, String> attribute : attributes.entrySet()) {
|
||||
String attributeName = attribute.getKey();
|
||||
String value = attribute.getValue();
|
||||
|
||||
if (blankToNull(value) == null) {
|
||||
// AWS: "To delete an attribute from the user, submit the attribute in
|
||||
// your API request with a blank value."
|
||||
updatedUser.getAttributes().remove(attributeName);
|
||||
updatedUser.getAttributes().remove(attributeName + "_verified");
|
||||
updatedUser.getPendingAttributes().remove(attributeName);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!isVerifiableContactAttribute(attributeName)) {
|
||||
updatedUser.getAttributes().put(attributeName, value);
|
||||
continue;
|
||||
}
|
||||
|
||||
VerificationCode.Purpose purpose = verificationPurpose(attributeName);
|
||||
ensureVerificationWiring();
|
||||
String verificationCode = verificationCodeService.issue(
|
||||
poolId, currentUser.getUsername(), purpose, Duration.ofHours(24));
|
||||
dispatchAttributeVerificationCode(pool, clientId, currentUser, attributeName, value,
|
||||
purpose, verificationCode, "CustomEmailSender_UpdateUserAttribute", clientMetadata, messages);
|
||||
|
||||
for (Map.Entry<String, String> attribute : attributes.entrySet()) {
|
||||
String attributeName = attribute.getKey();
|
||||
String value = attribute.getValue();
|
||||
if (blankToNull(value) == null) {
|
||||
updatedUser.getAttributes().remove(attributeName);
|
||||
updatedUser.getAttributes().remove(attributeName + "_verified");
|
||||
updatedUser.getPendingAttributes().remove(attributeName);
|
||||
} else if (!isVerifiableContactAttribute(attributeName)) {
|
||||
updatedUser.getAttributes().put(attributeName, value);
|
||||
} else {
|
||||
contacts.put(attributeName, value);
|
||||
if (requiresVerificationBeforeUpdate(pool, attributeName)) {
|
||||
updatedUser.getPendingAttributes().put(attributeName, value);
|
||||
} else {
|
||||
@@ -4280,6 +4265,20 @@ public class CognitoService implements ResourceProvider {
|
||||
updatedUser.getAttributes().put(attributeName + "_verified", "false");
|
||||
updatedUser.getPendingAttributes().remove(attributeName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
boolean issued = false;
|
||||
try {
|
||||
for (Map.Entry<String, String> attribute : contacts.entrySet()) {
|
||||
String attributeName = attribute.getKey();
|
||||
String value = attribute.getValue();
|
||||
VerificationCode.Purpose purpose = verificationPurpose(attributeName);
|
||||
ensureVerificationWiring();
|
||||
String verificationCode = verificationCodeService.issue(
|
||||
poolId, currentUser.getUsername(), purpose, Duration.ofHours(24));
|
||||
dispatchAttributeVerificationCode(pool, clientId, currentUser, attributeName, value,
|
||||
purpose, verificationCode, "CustomEmailSender_UpdateUserAttribute", clientMetadata, messages);
|
||||
|
||||
Map<String, Object> detail = new LinkedHashMap<>();
|
||||
detail.put("AttributeName", attributeName);
|
||||
@@ -4290,13 +4289,25 @@ public class CognitoService implements ResourceProvider {
|
||||
issued = true;
|
||||
} catch (VerificationCodeException e) {
|
||||
throw mapVerificationCodeException(e);
|
||||
} catch (CustomEmailSenderException e) {
|
||||
for (String attributeName : attributes.keySet()) {
|
||||
if (isVerifiableContactAttribute(attributeName)
|
||||
&& !Objects.equals(attributeVerificationDestination(currentUser, attributeName),
|
||||
attributeVerificationDestination(updatedUser, attributeName))) {
|
||||
verificationCodeService.invalidatePrevious(
|
||||
poolId, currentUser.getUsername(), verificationPurpose(attributeName));
|
||||
}
|
||||
}
|
||||
updatedUser.setLastModifiedDate(System.currentTimeMillis() / 1000L);
|
||||
userStore.put(userKey(poolId, updatedUser.getUsername()), updatedUser);
|
||||
throw e;
|
||||
} catch (AwsException e) {
|
||||
throw e;
|
||||
} catch (RuntimeException e) {
|
||||
throw codeDeliveryFailure();
|
||||
} finally {
|
||||
if (!issued) {
|
||||
// Nothing is stored, so the codes already issued for messages still to send go with it.
|
||||
// A failed sender hand-over must not leave earlier queued codes live but unsent.
|
||||
messages.forEach(message -> verificationCodeService.invalidatePrevious(
|
||||
poolId, currentUser.getUsername(), message.purpose()));
|
||||
}
|
||||
|
||||
+124
@@ -21,6 +21,9 @@ import io.github.hectorvent.floci.services.ses.SesService;
|
||||
import io.github.hectorvent.floci.services.sns.SnsService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
@@ -47,6 +50,7 @@ import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertAll;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
@@ -57,6 +61,7 @@ import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.atLeastOnce;
|
||||
import static org.mockito.Mockito.clearInvocations;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
@@ -336,6 +341,125 @@ class CognitoCustomEmailSenderTest {
|
||||
verify(lambda, never()).invoke(anyString(), anyString(), any(byte[].class), any());
|
||||
}
|
||||
|
||||
/** The disabled-key capture on #6207 keeps the whole update, in either attribute order. */
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void aKeyThatCannotEncryptKeepsTheWholeAttributeUpdate(boolean emailFirst) {
|
||||
UserPool pool = pool(Map.of("KMSKeyID", keyArn, "CustomEmailSender", sender()), List.of("email"));
|
||||
UserPoolClient client = client(pool);
|
||||
service.adminCreateUser(pool.getId(), "alice",
|
||||
Map.of("email", "alice@example.com", "email_verified", "true", "name", "Original"), null);
|
||||
service.adminSetUserPassword(pool.getId(), "alice", "Passw0rd!", true);
|
||||
String token = signIn(client, "alice", "Passw0rd!");
|
||||
kms.disableKey(keyArn, REGION);
|
||||
Map<String, String> attributes = new LinkedHashMap<>();
|
||||
if (emailFirst) {
|
||||
attributes.put("email", "alice@example.org");
|
||||
}
|
||||
attributes.put("name", "Renamed");
|
||||
if (!emailFirst) {
|
||||
attributes.put("email", "alice@example.org");
|
||||
}
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class,
|
||||
() -> service.updateUserAttributes(token, attributes));
|
||||
|
||||
Map<String, String> stored = service.adminGetUser(pool.getId(), "alice").getAttributes();
|
||||
assertAll(
|
||||
() -> assertEquals("CodeDeliveryFailureException", failure.getErrorCode()),
|
||||
() -> assertEquals("Caller needs to enable the configured KMS Key", failure.getMessage()),
|
||||
() -> assertEquals("alice@example.org", stored.get("email")),
|
||||
() -> assertEquals("false", stored.get("email_verified")),
|
||||
() -> assertEquals("Renamed", stored.get("name")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUndeliveredAttributeUpdateCodeCanBeRequestedAgain() throws Exception {
|
||||
UserPool pool = pool(Map.of("KMSKeyID", keyArn, "CustomEmailSender", sender()), List.of("email"));
|
||||
UserPoolClient client = client(pool);
|
||||
service.adminCreateUser(pool.getId(), "alice",
|
||||
Map.of("email", "alice@example.com", "email_verified", "true"), null);
|
||||
service.adminSetUserPassword(pool.getId(), "alice", "Passw0rd!", true);
|
||||
String token = signIn(client, "alice", "Passw0rd!");
|
||||
kms.disableKey(keyArn, REGION);
|
||||
assertThrows(AwsException.class,
|
||||
() -> service.updateUserAttributes(token, Map.of("email", "alice@example.org")));
|
||||
kms.enableKey(keyArn, REGION);
|
||||
|
||||
service.getUserAttributeVerificationCode(token, "email");
|
||||
|
||||
Map<String, Object> verification = senderEvent();
|
||||
assertEquals("CustomEmailSender_VerifyUserAttribute", verification.get("triggerSource"));
|
||||
assertEquals("alice@example.org",
|
||||
((Map<?, ?>) request(verification).get("userAttributes")).get("email"));
|
||||
service.verifyUserAttribute(token, "email", decrypt(request(verification)).plaintext());
|
||||
assertEquals("true", service.adminGetUser(pool.getId(), "alice").getAttributes().get("email_verified"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"OMIT, false", "SAME, false", "REPLACE, false", "REPLACE, true"})
|
||||
void anEmailSenderFailureOnlyWithdrawsCodesForChangedDestinations(String phoneUpdate, boolean requiresVerification) {
|
||||
UserPool pool = pool(Map.of("KMSKeyID", keyArn, "CustomEmailSender", sender()),
|
||||
List.of("email", "phone_number"));
|
||||
if (requiresVerification) {
|
||||
service.updateUserPool(Map.of("UserPoolId", pool.getId(), "UserAttributeUpdateSettings",
|
||||
Map.of("AttributesRequireVerificationBeforeUpdate", List.of("phone_number"))), REGION);
|
||||
}
|
||||
UserPoolClient client = client(pool);
|
||||
service.adminCreateUser(pool.getId(), "alice", Map.of(
|
||||
"email", "alice@example.com", "email_verified", "true",
|
||||
"phone_number", "+15555550100", "phone_number_verified", "false"), null);
|
||||
service.adminSetUserPassword(pool.getId(), "alice", "Passw0rd!", true);
|
||||
String token = signIn(client, "alice", "Passw0rd!");
|
||||
clearInvocations(sns);
|
||||
service.getUserAttributeVerificationCode(token, "phone_number");
|
||||
ArgumentCaptor<String> oldMessage = ArgumentCaptor.forClass(String.class);
|
||||
verify(sns).publish(any(), any(), eq("+15555550100"), oldMessage.capture(), any(), any(), eq(REGION));
|
||||
String oldCode = oldMessage.getValue().replaceAll("\\D", "");
|
||||
assertTrue(oldCode.matches("[0-9]{6}"));
|
||||
kms.disableKey(keyArn, REGION);
|
||||
Map<String, String> attributes = new LinkedHashMap<>();
|
||||
attributes.put("email", "alice@example.org");
|
||||
attributes.put("name", "Renamed");
|
||||
boolean replacePhone = "REPLACE".equals(phoneUpdate);
|
||||
if (replacePhone) {
|
||||
attributes.put("phone_number", "+15555550200");
|
||||
} else if ("SAME".equals(phoneUpdate)) {
|
||||
attributes.put("phone_number", "+15555550100");
|
||||
}
|
||||
|
||||
AwsException delivery = assertThrows(AwsException.class,
|
||||
() -> service.updateUserAttributes(token, attributes));
|
||||
|
||||
assertEquals("CodeDeliveryFailureException", delivery.getErrorCode());
|
||||
if (!replacePhone) {
|
||||
assertEquals("+15555550100", service.adminGetUser(pool.getId(), "alice").getAttributes().get("phone_number"));
|
||||
service.verifyUserAttribute(token, "phone_number", oldCode);
|
||||
assertEquals("true", service.adminGetUser(pool.getId(), "alice").getAttributes().get("phone_number_verified"));
|
||||
return;
|
||||
}
|
||||
assertEquals(requiresVerification ? "+15555550100" : "+15555550200",
|
||||
service.adminGetUser(pool.getId(), "alice").getAttributes().get("phone_number"));
|
||||
if (requiresVerification) {
|
||||
assertEquals("+15555550200",
|
||||
service.adminGetUser(pool.getId(), "alice").getPendingAttributes().get("phone_number"));
|
||||
}
|
||||
AwsException staleCode = assertThrows(AwsException.class,
|
||||
() -> service.verifyUserAttribute(token, "phone_number", oldCode));
|
||||
assertEquals("CodeMismatchException", staleCode.getErrorCode());
|
||||
assertEquals("false", service.adminGetUser(pool.getId(), "alice").getAttributes().get("phone_number_verified"));
|
||||
|
||||
service.getUserAttributeVerificationCode(token, "phone_number");
|
||||
ArgumentCaptor<String> newMessage = ArgumentCaptor.forClass(String.class);
|
||||
verify(sns).publish(any(), any(), eq("+15555550200"), newMessage.capture(), any(), any(), eq(REGION));
|
||||
String newCode = newMessage.getValue().replaceAll("\\D", "");
|
||||
assertTrue(newCode.matches("[0-9]{6}"));
|
||||
service.verifyUserAttribute(token, "phone_number", newCode);
|
||||
assertEquals("+15555550200", service.adminGetUser(pool.getId(), "alice").getAttributes().get("phone_number"));
|
||||
assertEquals("true", service.adminGetUser(pool.getId(), "alice").getAttributes().get("phone_number_verified"));
|
||||
assertFalse(service.adminGetUser(pool.getId(), "alice").getPendingAttributes().containsKey("phone_number"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Measured (eu-west-1, 2026-10-20): Cognito hands out a code again at once, on a healthy pool
|
||||
* and once a key that could not encrypt is enabled again. The code a failed delivery issued is
|
||||
|
||||
Reference in new issue
Block a user