mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cognito): stop defaulting unset password requirements to enabled (#4250)
* fix(cognito): stop defaulting unset password requirements to enabled
CreateUserPool filled in RequireUppercase, RequireLowercase, RequireNumbers
and RequireSymbols as true whenever a PasswordPolicy left them out. The four
members are unboxed booleans in the Cognito model, so a client that wants one
off cannot say so on the wire: aws-sdk-go-v2 emits each under
`if v.RequireLowercase != false`. A request built for require_* = false
carries only MinimumLength, and Floci answered it with all four enabled.
That made every Terraform plan after a create report drift on all four flags,
converging only because UpdateUserPool does not normalize, and it over-enforced
the policy on SignUp and AdminSetUserPassword: a pool created with
PasswordPolicy { MinimumLength: 6 } rejected passwords real Cognito accepts.
The API reference documents no default for any of the four. It documents one
for TemporaryPasswordValidityDays ("Defaults to 7"), which is kept, along with
MinimumLength 8. The all-enabled policy is the console's "Cognito defaults"
mode, not an API default; terraform-provider-aws asserts require_numbers and
require_uppercase read back as false immediately after a create that set them
to false, against the live service.
Absence now means "not required", which validatePasswordAgainstPolicy already
resolved that way through policyBoolean, so enforcement follows with no change.
Pools created with no PasswordPolicy at all are untouched, as before.
* test(cognito): pin the unset password requirement contract at the wire
Adds the JSON 1.1 half of the CreateUserPool password policy fix: a request
carrying only MinimumLength must come back without the four Require* members,
and a request that sets them to false explicitly must come back with false.
Verified non-vacuously - with the RequireUppercase default restored, the first
test fails on "RequireUppercase must not be defaulted on".
This commit is contained in:
1 parent
f8f1b56ce9
commit
4e42f47740
4 files changed
+181
-19
No files matched your search
@@ -479,6 +479,66 @@ class CognitoIntegrationTest {
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createUserPoolDoesNotEnablePasswordRequirementsTheRequestOmitted() throws Exception {
|
||||
// RequireUppercase and its three siblings are unboxed booleans in the Cognito model, so a
|
||||
// client that wants them off omits them: this body is what aws-sdk-go-v2 puts on the wire
|
||||
// for a policy with every require_* set to false. Answering it with the members enabled
|
||||
// made Terraform report drift on all four in the first plan after a create.
|
||||
JsonNode created = cognitoJson("CreateUserPool", """
|
||||
{
|
||||
"PoolName": "NoPasswordRequirementsPool",
|
||||
"Policies": {
|
||||
"PasswordPolicy": {
|
||||
"MinimumLength": 7
|
||||
}
|
||||
}
|
||||
}
|
||||
""");
|
||||
String pool = created.path("UserPool").path("Id").asText();
|
||||
|
||||
JsonNode described = cognitoJson("DescribeUserPool", """
|
||||
{
|
||||
"UserPoolId": "%s"
|
||||
}
|
||||
""".formatted(pool));
|
||||
|
||||
JsonNode policy = described.path("UserPool").path("Policies").path("PasswordPolicy");
|
||||
assertEquals(7, policy.path("MinimumLength").asInt());
|
||||
assertEquals(7, policy.path("TemporaryPasswordValidityDays").asInt());
|
||||
assertFalse(policy.has("RequireUppercase"), "RequireUppercase must not be defaulted on");
|
||||
assertFalse(policy.has("RequireLowercase"), "RequireLowercase must not be defaulted on");
|
||||
assertFalse(policy.has("RequireNumbers"), "RequireNumbers must not be defaulted on");
|
||||
assertFalse(policy.has("RequireSymbols"), "RequireSymbols must not be defaulted on");
|
||||
}
|
||||
|
||||
@Test
|
||||
void createUserPoolKeepsPasswordRequirementsTheRequestDisabledExplicitly() throws Exception {
|
||||
// The Java SDK boxes these members, so it can say false where the Go SDK cannot. That
|
||||
// false must be stored and echoed rather than replaced.
|
||||
JsonNode created = cognitoJson("CreateUserPool", """
|
||||
{
|
||||
"PoolName": "ExplicitlyDisabledRequirementsPool",
|
||||
"Policies": {
|
||||
"PasswordPolicy": {
|
||||
"MinimumLength": 10,
|
||||
"RequireUppercase": false,
|
||||
"RequireLowercase": true,
|
||||
"RequireNumbers": false,
|
||||
"RequireSymbols": false
|
||||
}
|
||||
}
|
||||
}
|
||||
""");
|
||||
|
||||
JsonNode policy = created.path("UserPool").path("Policies").path("PasswordPolicy");
|
||||
assertEquals(10, policy.path("MinimumLength").asInt());
|
||||
assertFalse(policy.path("RequireUppercase").asBoolean());
|
||||
assertTrue(policy.path("RequireLowercase").asBoolean());
|
||||
assertFalse(policy.path("RequireNumbers").asBoolean());
|
||||
assertFalse(policy.path("RequireSymbols").asBoolean());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
void confirmSignUpRequiresValidConfirmationCode() throws Exception {
|
||||
|
||||
@@ -136,15 +136,14 @@ class CognitoServiceTest {
|
||||
List.of(Map.of("Name", "custom:my-attr", "AttributeDataType", "String")),
|
||||
pool.getSchemaAttributes(),
|
||||
"a non-standard Schema attribute is stored under the custom: namespace");
|
||||
// A supplied PasswordPolicy is normalized with AWS's defaults for the fields left unset
|
||||
// (MinimumLength here was explicit; the rest were not), matching what DescribeUserPool
|
||||
// returns on real Cognito for a policy submitted this way.
|
||||
// A supplied PasswordPolicy only picks up the two defaults the API reference documents,
|
||||
// TemporaryPasswordValidityDays 7 and MinimumLength (explicit here). The four Require*
|
||||
// members carry no documented default and must stay unset: they are unboxed booleans in
|
||||
// the Cognito model, so an SDK asked for false omits them from the wire entirely, and
|
||||
// filling them in with true is what made a pool created with require_symbols = false
|
||||
// read back as true.
|
||||
Map<String, Object> expectedPasswordPolicy = new HashMap<>();
|
||||
expectedPasswordPolicy.put("MinimumLength", 12);
|
||||
expectedPasswordPolicy.put("RequireUppercase", true);
|
||||
expectedPasswordPolicy.put("RequireLowercase", true);
|
||||
expectedPasswordPolicy.put("RequireNumbers", true);
|
||||
expectedPasswordPolicy.put("RequireSymbols", true);
|
||||
expectedPasswordPolicy.put("TemporaryPasswordValidityDays", 7);
|
||||
assertEquals(Map.of("PasswordPolicy", expectedPasswordPolicy), pool.getPolicies());
|
||||
assertEquals(List.of("email"), pool.getUsernameAttributes());
|
||||
@@ -499,8 +498,9 @@ class CognitoServiceTest {
|
||||
@Test
|
||||
void createUserPoolDefaultsAnUnsetMinimumLengthToEight() {
|
||||
// A policy present but silent on MinimumLength gets AWS's default (8), not policyInt's
|
||||
// fallback of 0 for an absent key — and the unset RequireUppercase/Lowercase/Numbers
|
||||
// default to enabled too, the same "Cognito defaults" a console-created pool gets.
|
||||
// fallback of 0 for an absent key. The character classes the policy did not ask for are
|
||||
// not enforced: only RequireSymbols was set, so a password with no uppercase and no
|
||||
// digit is accepted once it is long enough.
|
||||
UserPool pool = service.createUserPool(Map.of(
|
||||
"PoolName", "SymbolsOnlyPool",
|
||||
"Policies", Map.of("PasswordPolicy", Map.of("RequireSymbols", true))
|
||||
@@ -514,10 +514,59 @@ class CognitoServiceTest {
|
||||
assertEquals("InvalidPasswordException", exception.getErrorCode());
|
||||
|
||||
assertDoesNotThrow(() -> service.signUp(
|
||||
client.getClientId(), "bob@example.com", "Eightplus1!", Map.of(
|
||||
client.getClientId(), "bob@example.com", "lowercase!", Map.of(
|
||||
"email", "bob@example.com", "phone_number", "+4915112345679")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createUserPoolLeavesPasswordRequirementsTheRequestOmittedUnset() {
|
||||
// The four Require* members are unboxed booleans in the Cognito model, so an SDK told to
|
||||
// require nothing sends a PasswordPolicy carrying only the members it can express -
|
||||
// aws-sdk-go-v2 emits `if v.RequireLowercase != false`. Treating their absence as
|
||||
// "enabled" turned Terraform's require_lowercase = false into permanent drift on the
|
||||
// first plan after create, because UpdateUserPool (which does not normalize) reported
|
||||
// them absent while CreateUserPool reported them true.
|
||||
UserPool pool = service.createUserPool(Map.of(
|
||||
"PoolName", "NoRequirementsPool",
|
||||
"Policies", Map.of("PasswordPolicy", Map.of("MinimumLength", 7))
|
||||
), "us-east-1");
|
||||
|
||||
Map<String, Object> expectedPasswordPolicy = new HashMap<>();
|
||||
expectedPasswordPolicy.put("MinimumLength", 7);
|
||||
expectedPasswordPolicy.put("TemporaryPasswordValidityDays", 7);
|
||||
assertEquals(Map.of("PasswordPolicy", expectedPasswordPolicy), pool.getPolicies());
|
||||
|
||||
UserPoolClient client = service.createUserPoolClient(
|
||||
pool.getId(), "no-requirements-client", false, false, List.of(), List.of());
|
||||
assertDoesNotThrow(() -> service.signUp(
|
||||
client.getClientId(), "alice@example.com", "abcdefg", Map.of(
|
||||
"email", "alice@example.com", "phone_number", "+4915112345678")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createUserPoolKeepsPasswordRequirementsTheRequestDisabledExplicitly() {
|
||||
// A caller that can express false - the Java SDK boxes these members, and so does a
|
||||
// hand-written JSON 1.1 request - must have it stored as false, not overwritten.
|
||||
UserPool pool = service.createUserPool(Map.of(
|
||||
"PoolName", "ExplicitFalsePool",
|
||||
"Policies", Map.of("PasswordPolicy", Map.of(
|
||||
"MinimumLength", 10,
|
||||
"RequireUppercase", false,
|
||||
"RequireLowercase", true,
|
||||
"RequireNumbers", false,
|
||||
"RequireSymbols", false))
|
||||
), "us-east-1");
|
||||
|
||||
Map<String, Object> expectedPasswordPolicy = new HashMap<>();
|
||||
expectedPasswordPolicy.put("MinimumLength", 10);
|
||||
expectedPasswordPolicy.put("RequireUppercase", false);
|
||||
expectedPasswordPolicy.put("RequireLowercase", true);
|
||||
expectedPasswordPolicy.put("RequireNumbers", false);
|
||||
expectedPasswordPolicy.put("RequireSymbols", false);
|
||||
expectedPasswordPolicy.put("TemporaryPasswordValidityDays", 7);
|
||||
assertEquals(Map.of("PasswordPolicy", expectedPasswordPolicy), pool.getPolicies());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createUserPoolNamespacesNonStandardSchemaAttributes() {
|
||||
// The Schema property carries custom attributes unprefixed - it is what CloudFormation
|
||||
|
||||
Reference in new issue
Block a user