fix(cognito): validate email attributes before storing users (#5460)

* fix(cognito): validate email attributes before storing users

* fix(cognito): reject escaped and quoted email separators

* fix(cognito): preserve blank attribute deletion on updates

* test(cognito): use inline mocks for final verification services
This commit is contained in:
Daniël van Ziel authored and GitHub committed 2026-10-15 09:52:59 -04:00
1 parent 15fb050d91
commit 3ad8c25a4f
4 files changed
+275 -8

No files matched your search

@@ -112,6 +112,10 @@ public class CognitoService implements ResourceProvider {
private static final List<String> DELIVERY_MEDIUMS = List.of("SMS", "EMAIL");
// JVM-local stripes bound lock memory without retaining one lock for every user key.
private static final int USER_LOCK_STRIPES = 512;
// A Cognito email attribute is one address, not an SES display-name or recipient list.
private static final Pattern EMAIL_ATTRIBUTE_PATTERN =
Pattern.compile("[^\\s\\p{Cntrl}@<>,;]+@[^\\s\\p{Cntrl}@<>,;]+");
private static final int EMAIL_ATTRIBUTE_MAX_LENGTH = 2048;
/**
* No configured CloudFront suffix, for the test constructors: the suffix then follows the
* request's partition. CDI wires {@code floci.services.cloudfront.domain-suffix}.
@@ -2328,6 +2332,8 @@ public class CognitoService implements ResourceProvider {
resolvedAttributes.put("sub", canonicalUsername);
}
validateEmailAttribute(resolvedAttributes, false);
CognitoUser user = new CognitoUser();
user.setUsername(canonicalUsername);
user.setUserPoolId(pool.getId());
@@ -2628,10 +2634,11 @@ public class CognitoService implements ResourceProvider {
String userPoolId, String username, Map<String, String> attributes) {
CognitoUser user = adminGetUser(userPoolId, username);
UserPool pool = describeUserPool(userPoolId);
validateEmailAttribute(attributes, true);
if (usesAliasUsernames(pool)) {
for (String aliasAttribute : pool.getUsernameAttributes()) {
String newValue = attributes.get(aliasAttribute);
if (newValue == null || newValue.equals(user.getAttributes().get(aliasAttribute))) {
if (blankToNull(newValue) == null || newValue.equals(user.getAttributes().get(aliasAttribute))) {
continue;
}
CognitoUser other = findUserByAlias(userPoolId, aliasAttribute, newValue);
@@ -2643,7 +2650,16 @@ public class CognitoService implements ResourceProvider {
}
attributes.keySet().forEach(attributeName ->
clearPendingAttributeVerification(userPoolId, user, attributeName));
user.getAttributes().putAll(attributes);
for (Map.Entry<String, String> attribute : attributes.entrySet()) {
String attributeName = attribute.getKey();
String value = attribute.getValue();
if (blankToNull(value) == null) {
user.getAttributes().remove(attributeName);
user.getAttributes().remove(attributeName + "_verified");
} else {
user.getAttributes().put(attributeName, value);
}
}
user.setLastModifiedDate(System.currentTimeMillis() / 1000L);
userStore.put(userKey(userPoolId, user.getUsername()), user);
}
@@ -3262,6 +3278,8 @@ public class CognitoService implements ResourceProvider {
throw new AwsException("UsernameExistsException", "User already exists", 400);
}
validateEmailAttribute(resolvedAttributes, false);
String key = userKey(userPoolId, canonicalUsername);
CognitoUser user = new CognitoUser();
@@ -4043,6 +4061,7 @@ public class CognitoService implements ResourceProvider {
String poolId, String username, Map<String, String> attributes) {
UserPool pool = describeUserPool(poolId);
CognitoUser currentUser = adminGetUser(poolId, username);
validateEmailAttribute(attributes, true);
// Validate every entry (alias availability) before mutating the user or issuing any
// verification code. Otherwise a later entry failing leaves an earlier entry's code
@@ -4113,6 +4132,36 @@ public class CognitoService implements ResourceProvider {
return deliveryDetails;
}
private static void validateEmailAttribute(Map<String, String> attributes, boolean allowDeletion) {
if (attributes == null || !attributes.containsKey("email")) {
return;
}
String email = attributes.get("email");
if (allowDeletion && email != null && email.isBlank()) {
return;
}
// Use Cognito's 2048-character attribute limit, not SES's envelope-address limit.
if (email == null || email.length() > EMAIL_ATTRIBUTE_MAX_LENGTH
|| !EMAIL_ATTRIBUTE_PATTERN.matcher(email).matches() || !hasEmailSeparator(email)) {
throw new AwsException("InvalidParameterException", "Invalid email address format.", 400);
}
}
private static boolean hasEmailSeparator(String email) {
boolean quoted = false;
for (int i = 0; i < email.length(); i++) {
char character = email.charAt(i);
if (character == '\\') {
i++;
} else if (character == '"') {
quoted = !quoted;
} else if (character == '@' && !quoted) {
return true;
}
}
return false;
}
public void deleteUserAttributes(String accessToken, List<String> attributeNames) {
VerifiedAccessToken token = verifyAccessToken(accessToken);
requireScope(accessToken, USER_ADMIN_SCOPE);