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

@@ -253,6 +253,53 @@ class CognitoCustomSrpAuthTest {
}
}
@ParameterizedTest
@CsvSource({"false,false", "false,true", "true,false", "true,true"})
void undeclaredNewPasswordAttributesPreservePasswordAndSession(boolean custom, boolean admin) throws Exception {
temporaryPassword = true;
service.adminSetUserPassword(pool.getId(), USERNAME, PASSWORD, false);
service.addCustomAttributes(pool.getId(), List.of(
Map.of("Name", "declared", "AttributeDataType", "String", "Mutable", true)));
Map<String, Object> challenge;
if (custom) {
Map<String, Object> started = start(admin);
challenge = respond(admin, client.getClientId(), "PASSWORD_VERIFIER", started, proof(started));
} else {
client.setExplicitAuthFlows(List.of("ALLOW_USER_PASSWORD_AUTH", "ALLOW_ADMIN_USER_PASSWORD_AUTH"));
Map<String, String> parameters = Map.of("USERNAME", USERNAME, "PASSWORD", PASSWORD);
challenge = admin
? service.adminInitiateAuth(pool.getId(), client.getClientId(), "ADMIN_USER_PASSWORD_AUTH",
parameters, Map.of())
: service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH", parameters);
}
assertEquals("NEW_PASSWORD_REQUIRED", challenge.get("ChallengeName"));
String originalHash = service.adminGetUser(pool.getId(), USERNAME).getPasswordHash();
Map<String, String> responses = new HashMap<>(Map.of("USERNAME", USERNAME, "NEW_PASSWORD", "Changed1234!",
"userAttributes.custom:undeclared", "invalid", "userAttributes.custom:declared", "valid"));
AwsException error = assertThrows(AwsException.class, () -> respond(admin, client.getClientId(),
"NEW_PASSWORD_REQUIRED", challenge, responses));
assertEquals("InvalidParameterException", error.getErrorCode());
assertTrue(error.getMessage().contains("Attribute does not exist in the schema"));
assertEquals(originalHash, service.adminGetUser(pool.getId(), USERNAME).getPasswordHash());
assertEquals("FORCE_CHANGE_PASSWORD", service.adminGetUser(pool.getId(), USERNAME).getUserStatus());
assertFalse(service.adminGetUser(pool.getId(), USERNAME).getAttributes().containsKey("custom:undeclared"));
assertFalse(service.adminGetUser(pool.getId(), USERNAME).getAttributes().containsKey("custom:declared"));
responses.remove("userAttributes.custom:undeclared");
Map<String, Object> next = respond(admin, client.getClientId(), "NEW_PASSWORD_REQUIRED", challenge, responses);
if (custom) {
assertEquals("CUSTOM_CHALLENGE", next.get("ChallengeName"));
} else {
assertNotNull(next.get("AuthenticationResult"));
}
assertEquals("valid", service.adminGetUser(pool.getId(), USERNAME).getAttributes().get("custom:declared"));
assertEquals("CONFIRMED", service.adminGetUser(pool.getId(), USERNAME).getUserStatus());
assertNotEquals(originalHash, service.adminGetUser(pool.getId(), USERNAME).getPasswordHash());
assertDenied(() -> respond(admin, client.getClientId(), "NEW_PASSWORD_REQUIRED", challenge, responses));
}
@Test
void rejectedCustomResponseDoesNotReplaceClientMetadata() throws Exception {
Map<String, Object> started = start(false);
@@ -94,7 +94,8 @@ class CognitoFederationServiceTest {
stateStore = new CognitoFederationStateStore(CLOCK);
federationService = new CognitoFederationService(
cognitoService, stateStore, new CognitoOidcClient(MAPPER), CLOCK);
pool = cognitoService.createUserPool(Map.of("PoolName", "FederationPool"), "us-east-1");
pool = cognitoService.createUserPool(Map.of("PoolName", "FederationPool", "Schema", List.of(
Map.of("Name", "note", "AttributeDataType", "String", "Mutable", true))), "us-east-1");
tokenStatus = 200;
tokenResponse = "{\"access_token\":\"provider-access-token\"}";
claimsStatus = 200;
@@ -2506,6 +2506,16 @@ class CognitoIntegrationTest {
""");
String delPoolId = poolResponse.path("UserPool").path("Id").asText();
cognitoAction("AddCustomAttributes", """
{
"UserPoolId": "%s",
"CustomAttributes": [
{ "Name": "age", "AttributeDataType": "String", "Mutable": true },
{ "Name": "gender", "AttributeDataType": "String", "Mutable": true }
]
}
""".formatted(delPoolId)).then().statusCode(200);
JsonNode clientResponse = cognitoJson("CreateUserPoolClient", """
{
"UserPoolId": "%s",
@@ -79,6 +79,19 @@ import static org.mockito.Mockito.*;
@Execution(ExecutionMode.CONCURRENT)
class CognitoServiceTest {
@Test
void adminUpdateUserAttributesRejectsUndeclaredCustomAttribute() {
UserPool pool = service.createUserPool(Map.of("PoolName", "CustomAttributeValidation"), "us-east-1");
service.adminCreateUser(pool.getId(), "alice", Map.of(), "TempPass1!");
AwsException error = assertThrows(AwsException.class, () -> service.adminUpdateUserAttributes(
pool.getId(), "alice", Map.of("custom:undeclared", "x")));
assertEquals("InvalidParameterException", error.getErrorCode());
assertTrue(error.getMessage().contains("Attribute does not exist in the schema"));
assertFalse(service.adminGetUser(pool.getId(), "alice").getAttributes().containsKey("custom:undeclared"));
}
private static final ObjectMapper MAPPER = new ObjectMapper();
/** What AWS's DescribeUserPool reports for a pool created without a SignInPolicy. */
private static final Map<String, Object> DEFAULT_SIGN_IN_POLICY =
@@ -2750,6 +2763,7 @@ class CognitoServiceTest {
mock(TlsCertificateManager.class));
UserPool pool = serviceWithVerification.createUserPool(Map.of(
"PoolName", "AdminPendingContactPool",
"Schema", List.of(Map.of("Name", "note", "AttributeDataType", "String", "Mutable", true)),
"AutoVerifiedAttributes", List.of("email", "phone_number"),
"UserAttributeUpdateSettings", Map.of(
"AttributesRequireVerificationBeforeUpdate", List.of("email", "phone_number"))),
@@ -42,7 +42,10 @@ class CognitoUserInfoIntegrationTest {
void createPoolClientAndUser() throws Exception {
JsonNode poolResponse = cognitoJson("CreateUserPool", """
{
"PoolName": "UserInfoPool"
"PoolName": "UserInfoPool",
"Schema": [
{ "Name": "tenant_id", "AttributeDataType": "String", "Mutable": true }
]
}
""");
poolId = poolResponse.path("UserPool").path("Id").asText();