Skip to content

Preserve configured order when evaluating password policies - #51840

Merged
rmartinc merged 2 commits into
keycloak:mainfrom
adityaanikam:fix-password-policy-order-51674
Aug 28, 2026
Merged

rmartinc merged 2 commits into
keycloak:mainfrom
adityaanikam:fix-password-policy-order-51674

Conversation

@adityaanikam

@adityaanikam adityaanikam commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

PasswordPolicy.Builder#build parses the configured policy string into an order preserving LinkedHashMap, but then copies those entries into a plain HashMap to build the PasswordPolicy it returns. PasswordPolicy#getPolicies() reads from that HashMap, so the order policies are evaluated in becomes hash-bucket order instead of the order they were actually configured in.

DefaultPasswordPolicyManagerProvider#validate evaluates policies in whatever order getPolicies() 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 throws FipsUnapprovedOperationError for passwords under 112 bits, which surfaces as an uncaught 500 instead of the expected "password too short" validation error.

Fix is to use a LinkedHashMap for the copy in Builder#build as 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, asserting PasswordPolicy.parse(...).getPolicies() returns policy ids in the same order they appear in the policy string. Follows this module's existing java.lang.reflect.Proxy pattern (see RoleProviderCompositeDefaultTest) for the KeycloakSession/PasswordPolicyProvider test 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.

Copilot AI balanced review requested due to automatic review settings August 19, 2026 08:15
@adityaanikam
adityaanikam requested a review from a team as a code owner August 19, 2026 08:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 LinkedHashMap when 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>
Copilot AI review requested due to automatic review settings August 19, 2026 18:47
@adityaanikam
adityaanikam force-pushed the fix-password-policy-order-51674 branch from fade3c7 to 3ecfdee Compare August 19, 2026 18:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 UnsupportedOperationException for all methods except getProvider, including toString/hashCode/equals which may be called implicitly by logging/debugging/test frameworks, leading to brittle failures with little diagnostic context. Consider explicitly handling Object methods 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 config is populated from a known source (map.entrySet()), consider initializing LinkedHashMap with an expected size (e.g., new LinkedHashMap<>(map.size())) to avoid resizing/rehashing during the copy.
            Map<String, Object> config = new LinkedHashMap<>();

@rmartinc rmartinc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Copilot AI review requested due to automatic review settings August 27, 2026 06:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

@rmartinc rmartinc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @adityaanikam for the changes! LGTM! I'm running the tests now.

@rmartinc
rmartinc merged commit 11dfd45 into keycloak:main Aug 28, 2026
150 of 153 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Keycloak's password length policy isn't triggered when changing password in FIPS mode

3 participants