Repository navigation
Preserve configured order when evaluating password policies - #51840
Conversation
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds regression coverage and adjusts password policy building to preserve the configured policy order.
Changes:
- Use
LinkedHashMapwhen building the password policy config to retain insertion order. - Add a unit test asserting that parsed policies are preserved in configured order.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| server-spi/src/main/java/org/keycloak/models/PasswordPolicy.java | Switches config map to LinkedHashMap so iteration preserves configured policy order. |
| server-spi/src/test/java/org/keycloak/models/PasswordPolicyTest.java | Adds a test and lightweight session/provider stubs to verify policy order preservation. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
PasswordPolicy.Builder#build copied the parsed policy entries into a plain HashMap, discarding the order the policies were configured in. DefaultPasswordPolicyManagerProvider#validate evaluates policies in that order and returns on the first failure, so once the order became effectively random, a length-policy failure could run after a hash-based policy like password history, which throws a FIPS crypto error instead of a clean validation failure in FIPS mode. Use a LinkedHashMap so evaluation follows the order the policies were actually configured in. Closes keycloak#51674 Signed-off-by: adityaanikam <adityanikam9502@gmail.com>
fade3c7 to
3ecfdee
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.
Suppressed comments (3)
server-spi/src/test/java/org/keycloak/models/PasswordPolicyTest.java:26
- The proxy throws
UnsupportedOperationExceptionfor all methods exceptgetProvider, includingtoString/hashCode/equalswhich may be called implicitly by logging/debugging/test frameworks, leading to brittle failures with little diagnostic context. Consider explicitly handlingObjectmethods and including method + args details in the exception message so failures are actionable.
private static final KeycloakSession SESSION = (KeycloakSession) Proxy.newProxyInstance(
PasswordPolicyTest.class.getClassLoader(),
new Class<?>[]{KeycloakSession.class},
(proxy, method, args) -> {
if ("getProvider".equals(method.getName())
&& args != null && args.length == 2 && args[0] == PasswordPolicyProvider.class) {
return new NoopPasswordPolicyProvider();
}
throw new UnsupportedOperationException(method.getName());
});
server-spi/src/test/java/org/keycloak/models/PasswordPolicyTest.java:41
- These
UnsupportedOperationExceptions have no message, which makes failures harder to diagnose if these methods are ever invoked (e.g., due to a refactor). Consider providing a short message like "not used in this test" or the method name.
@Override public PolicyError validate(RealmModel realm, UserModel user, String password) { throw new UnsupportedOperationException(); }
@Override public PolicyError validate(String user, String password) { throw new UnsupportedOperationException(); }
server-spi/src/main/java/org/keycloak/models/PasswordPolicy.java:217
- Minor: since
configis populated from a known source (map.entrySet()), consider initializingLinkedHashMapwith an expected size (e.g.,new LinkedHashMap<>(map.size())) to avoid resizing/rehashing during the copy.
Map<String, Object> config = new LinkedHashMap<>();
rmartinc
left a comment
There was a problem hiding this comment.
Thanks @adityaanikam! The change LGTM but I have re-checked the tests and I think we can just check the order in the testComplex of the existing PasswordPolicyTest class. It's much easier. You can chcek my little diff. WDYT?
Per review: the existing PasswordPolicyManagerProvider-based PasswordPolicyTest already exercises this configuration end-to-end. Add a single order assertion there rather than a new unit test with a hand-rolled KeycloakSession proxy. Signed-off-by: adityaanikam <adityanikam9502@gmail.com>
rmartinc
left a comment
There was a problem hiding this comment.
Thanks @adityaanikam for the changes! LGTM! I'm running the tests now.
PasswordPolicy.Builder#buildparses the configured policy string into an order preservingLinkedHashMap, but then copies those entries into a plainHashMapto build thePasswordPolicyit returns.PasswordPolicy#getPolicies()reads from thatHashMap, so the order policies are evaluated in becomes hash-bucket order instead of the order they were actually configured in.DefaultPasswordPolicyManagerProvider#validateevaluates policies in whatever ordergetPolicies()returns and stops at the first failure. Once that order is effectively random, a short password can end up being checked against the password history policy before the length policy runs. History's check hashes the candidate password, and in FIPS mode that hash call throwsFipsUnapprovedOperationErrorfor passwords under 112 bits, which surfaces as an uncaught 500 instead of the expected "password too short" validation error.Fix is to use a
LinkedHashMapfor the copy inBuilder#buildas well, so evaluation order matches configured order. With the length policy running first as configured, it rejects the short password before the history policy's hashing code ever runs.Added
PasswordPolicyTest#policiesArePreservedInConfiguredOrder, assertingPasswordPolicy.parse(...).getPolicies()returns policy ids in the same order they appear in the policy string. Follows this module's existingjava.lang.reflect.Proxypattern (seeRoleProviderCompositeDefaultTest) for theKeycloakSession/PasswordPolicyProvidertest doubles rather than introducing a mocking framework.Confirmed with @rmartinc in #51674 that the ordering fix alone is sufficient, since the fail-fast evaluation means the history policy's hashing code never runs once length has already rejected the password.
Closes #51674
Used an AI coding agent (Claude) to investigate the root cause, implement the fix, and write the test, per the disclosure requirement in CONTRIBUTING.md.