fix(cognito): reject undeclared custom user attributes (#7994) (#8124)

This commit is contained in:
jwoa95 authored and GitHub committed 2026-11-12 14:04:51 -04:00
1 parent 021e580a60
commit 5da8045bb2
9 files changed
+241 -6

No files matched your search

@@ -572,21 +572,27 @@ final class CognitoAuthFlowHandler {
if (username == null || newPassword == null) {
throw new AwsException("InvalidParameterException", "USERNAME and NEW_PASSWORD are required", 400);
}
CognitoService.validateCustomAttributes(pool, newPasswordAttributes(responses));
service.adminSetUserPassword(pool.getId(), username, newPassword, true);
}
private CognitoUser applyNewPasswordAttributes(UserPool pool, Map<String, String> responses) {
String username = responses.get("USERNAME");
Map<String, String> attrUpdates = newPasswordAttributes(responses);
if (!attrUpdates.isEmpty()) {
service.adminUpdateUserAttributes(pool.getId(), username, attrUpdates);
}
return service.adminGetUser(pool.getId(), username);
}
private static Map<String, String> newPasswordAttributes(Map<String, String> responses) {
Map<String, String> attrUpdates = new HashMap<>();
for (Map.Entry<String, String> entry : responses.entrySet()) {
if (entry.getKey() != null && entry.getKey().startsWith("userAttributes.")) {
attrUpdates.put(entry.getKey().substring("userAttributes.".length()), entry.getValue());
}
}
if (!attrUpdates.isEmpty()) {
service.adminUpdateUserAttributes(pool.getId(), username, attrUpdates);
}
return service.adminGetUser(pool.getId(), username);
return attrUpdates;
}
// ──────────────────────────── USER_PASSWORD / REFRESH ────────────────────────────
@@ -2505,6 +2505,7 @@ public class CognitoService implements ResourceProvider {
resolvedAttributes.put("sub", canonicalUsername);
}
validateCustomAttributes(pool, resolvedAttributes);
validateEmailAttribute(resolvedAttributes, false);
CognitoUser user = new CognitoUser();
@@ -2822,6 +2823,7 @@ public class CognitoService implements ResourceProvider {
Map<String, String> clientMetadata, List<AttributeCodeMessage> messages) {
CognitoUser currentUser = adminGetUser(userPoolId, username);
UserPool pool = describeUserPool(userPoolId);
validateCustomAttributes(pool, attributes);
boolean stagesContactUpdate = attributes.entrySet().stream()
.anyMatch(attribute -> isVerifiableContactAttribute(attribute.getKey())
&& shouldVerifyAttribute(pool, attribute.getKey())
@@ -3566,6 +3568,7 @@ public class CognitoService implements ResourceProvider {
resolvedAttributes.put("sub", canonicalUsername);
}
validateCustomAttributes(pool, resolvedAttributes);
validateEmailAttribute(resolvedAttributes, false);
String key = userKey(userPoolId, canonicalUsername);
@@ -4630,6 +4633,7 @@ public class CognitoService implements ResourceProvider {
Map<String, String> clientMetadata, List<AttributeCodeMessage> messages) {
UserPool pool = describeUserPool(poolId);
CognitoUser currentUser = adminGetUser(poolId, username);
validateCustomAttributes(pool, attributes);
validateEmailAttribute(attributes, true);
// Validate every entry (alias availability) before mutating the user or issuing any
@@ -4729,6 +4733,17 @@ public class CognitoService implements ResourceProvider {
return deliveryDetails;
}
static void validateCustomAttributes(UserPool pool, Map<String, String> attributes) {
List<Map<String, Object>> schema = pool.getSchemaAttributes();
for (String name : attributes.keySet()) {
if (name != null && name.startsWith("custom:")
&& (schema == null || schema.stream().noneMatch(attribute -> name.equals(attribute.get("Name"))))) {
throw new AwsException("InvalidParameterException", "Attributes did not conform to the schema: "
+ name + ": Attribute does not exist in the schema.", 400);
}
}
}
private static void validateEmailAttribute(Map<String, String> attributes, boolean allowDeletion) {
if (attributes == null || !attributes.containsKey("email")) {
return;