Repository navigation
Description
Before reporting an issue
- I have read and understood the above terms for submitting issues, and I understand that my issue may be closed without action if I do not follow them.
Area
authentication
Describe the bug
This is a follow-up to #53633, which covers several problems around the deferred password re-hash. This issue only covers the update of a credential that has become stale between the password validation and the re-hash.
Since Keycloak 26.3 (and 26.2.6), PasswordCredentialProvider.isValid() no longer re-hashes an outdated password inline. It queues a job with enlistAfterCompletion. After the request's transaction has committed, that job re-hashes the validated password in a new transaction and writes it to the same credential id (refreshPassword). The job does not check whether the stored credential changed in the meantime, and it writes the full credential, so every change made after the validation is lost.
If the user's password is changed while the re-hash is pending, the job writes the old password back. The change reports success, the old password keeps working, and the new one is rejected. This happens when the change comes from another request committing between the login's password check and the end of its commit, or from code that calls isValid(current) followed by updateCredential(new) in the same transaction (in that case the overwrite happens every time).
The re-hash also overwrites other changes to the stored credential, for example its user label or created date.
When the credential is changed concurrently, the write may also fail with a Hibernate StaleStateException. A failed admin password reset then returns HTTP 500.
Federated user credentials have the same problem. FederatedUserCredentialEntity has no @Version, so it is not possible to detect that the credential changed after the job read it.
Version
26.8.0, 26.6.0
Regression
- The issue is a regression
Expected behavior
- A password set by an admin reset (or any other password update) stays in effect when a login for the same user is running at the same time.
- A password checked and then updated in the same transaction (
isValid(current)followed byupdateCredential(new)) keeps the new password after commit. - The re-hash does not overwrite other changes to the credential, like the user label or the created date.
- If the credential changed after it was validated, the re-hash is skipped, or fails without affecting the request that validated the password.
Actual behavior
- The admin reset returns 204, but afterwards the previous password is valid again and the new one is rejected.
- A password updated in the same transaction after
isValid(current)is replaced by the old password after commit. - Metadata changes to the credential made in between are lost.
How to Reproduce?
- Create a user with a password hashed with outdated parameters, for example by creating the password with the realm password policy
hashIterations(1)and then changing the policy tohashIterations(2). - In a custom authenticator or extension, call
user.credentialManager().isValid(UserCredentialModel.password(currentPassword))and thenuser.credentialManager().updateCredential(UserCredentialModel.password(newPassword))in the same transaction. - After the transaction has committed, validate the new password.
Expected: the new password is valid. Actual: the old password is valid again.
The same happens without custom code when an admin resets the password while a login with the old password is running. See #53633 for a reproducer script and results.
Anything else?
The first part of #53742 fixes the Argon2 policyCheck that caused a re-hash on every login. This issue is the part that remains for the re-hash itself.
#53742 proposes to compute the hash first, then read the credential from local storage (not through the user cache), skip the update if the credential is gone or its secret data changed, and rely on the @Version of CredentialEntity for changes between the read and the write. For federated credentials this needs a VERSION column on FED_USER_CREDENTIAL, which requires a database migration.
Metadata
Metadata
Assignees
Labels
Type
Fields
Projects
- StatusShow more project fieldsDone
Activity
Test stale persistence context in conditional credential update
4 remaining items
Replace a credential only if unchanged when re-hashing a password
Replace a credential only if unchanged when re-hashing a password
Replace a credential only if unchanged when re-hashing a password
Replace a credential only if unchanged when re-hashing a password
Replace a credential only if unchanged when re-hashing a password