Repository navigation
Add a cooldown for reset password emails - #55308
Conversation
c405a3e to
d33371d
Compare
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues were identified for the configurable password-reset email cooldown change.
No open issues 🎉
What changed in this PR
Adds a configurable per-user cooldown to Keycloak’s password-reset email flow, addressing #55244 by limiting inbox flooding while preserving the generic response that avoids revealing whether an account exists.
Changes:
- Enforces a default 30-second cooldown with an atomic check, configurable duration, and an option to disable it.
- Adds tests for suppression, expiration, disabled cooldowns, and invalid configuration; clears cooldown state in existing tests.
- Documents the new behavior and configuration.
| File | Description |
|---|---|
testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/forms/ResetPasswordTest.java |
Clears cooldowns before repeated reset requests. |
testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/federation/ldap/LDAPNoCacheTest.java |
Clears cooldowns between LDAP reset-email checks. |
tests/base/src/test/java/org/keycloak/tests/sessionlimits/UserSessionLimitsTest.java |
Resets cooldown state during test setup. |
tests/base/src/test/java/org/keycloak/tests/i18n/EmailTest.java |
Clears cooldowns for repeated email localization checks. |
tests/base/src/test/java/org/keycloak/tests/forms/ResetPasswordUserEnumerationTest.java |
Isolates cooldown state between enumeration test cases. |
tests/base/src/test/java/org/keycloak/tests/forms/ResetPasswordTest.java |
Tests cooldown behavior and updates email-count expectations. |
tests/base/src/test/java/org/keycloak/tests/forms/AbstractResetCredentialsAlternativeFlowsTest.java |
Clears shared-user cooldowns before alternative-flow tests. |
services/src/main/java/org/keycloak/authentication/requiredactions/util/EmailCooldownManager.java |
Adds atomic cooldown acquisition and authenticator configuration parsing. |
services/src/main/java/org/keycloak/authentication/authenticators/resetcred/ResetCredentialEmail.java |
Suppresses emails during cooldown and exposes configuration. |
docs/documentation/upgrading/topics/changes/changes-27_0_0.adoc |
Describes the default cooldown and related error event. |
docs/documentation/server_admin/topics/login-settings/forgot-password.adoc |
Explains cooldown settings for administrators. |
Review effort: Balanced
d33371d to
4eaea57
Compare
4eaea57 to
dd67795
Compare
mabartos
left a comment
There was a problem hiding this comment.
@gaoyikeshuer Thanks! Just one thing, otherwise LGTM.
dd67795 to
d1ee405
Compare
mabartos
left a comment
There was a problem hiding this comment.
@gaoyikeshuer Very nice job! LGTM.
I agree to go with this approach, but maybe for the future, we could provide some kind of a visible countdown timer in the login page, that the user could resend the email. But it's out of the scope, as it'd change the handling of the Forgot password, as we always return to the initial page.
rmartinc
left a comment
There was a problem hiding this comment.
Thanks @gaoyikeshuer ! Looks good to me. Just one comment, I don't like the idea of adding cache entries for unknown users. If you want to add something because of the enumeration just add always the same entry for all the unknown users.
| .error(Errors.INVALID_EMAIL); | ||
| } | ||
| UserModel recipient = deliverable ? user : dummyUser(context, username); | ||
| boolean inCooldown = !startEmailCooldown(context, user, username); |
There was a problem hiding this comment.
Why are you adding a cache entry for the dummy user? Is it for more or less doing the same things and use the same time?
If you detect the time is very different and can be used to enumerate users, then just add always the same entry in the cache with the same value (and differentiate the cache name completely reset-credential-email-cooldown-known- and reset-credential-email-cooldown-unknown-). You can pass always the same id for the user (like unknown). I don't want a lot of entries for whatever username that can be abused.
If it's because the hash (that can be detected as different time), just hash a generated keycloak id but not add any entry in the cache.
There was a problem hiding this comment.
Thanks @rmartinc Yes, it was only to make known and unknown users do the same work. I used the typed username as the key. the time difference is small and probably not worthy. I have fixed it in the way you suggested now: all unknown users now share one entry with the id unknown, and the prefixes are reset-credential-email-cooldown-known- and reset-credential-email-cooldown-unknown-.
d1ee405 to
04e18ef
Compare
04e18ef to
7e514a8
Compare
There was a problem hiding this comment.
🟡 Changes recommended
The added cooldown tests cover only sequential requests, leaving concurrent resets across independent authentication sessions untested for exactly one accepted email, slow_down events, and generic responses.
1 open issue
1 resolved since last review
Review effort: Balanced
Closes keycloak#55244 Signed-off-by: Yike Gao <yikegao8@gmail.com>
7e514a8 to
421c26b
Compare
rmartinc
left a comment
There was a problem hiding this comment.
Thanks @gaoyikeshuer! LGTM!
@mabartos please check if it's OK to you now.
The form shows the same page during the cooldown, with no "please wait" message. The form is anonymous, so a visible message would show whether an account exists. A suppressed request records a
SEND_RESET_PASSWORD_ERRORevent with the errorslow_down, so admins can still see it.The cooldown is checked and started in one
putIfAbsentcall, because anonymous requests can be sent in parallel and the existing check-then-add methods leave a gap. The verify-email and update-email paths are not changed.The default is 30 seconds, the same as verify-email and update-email, and
0in the Send Reset Email step turns it off. Someone who knows a username can keep the cooldown running, but the email that was sent still contains a working link, so the user is not locked out.The key uses a hash of the user ID, so it fits the database store for long federated IDs. Unknown usernames get their own key, so a repeated request does the same work whether or not the user exists (prevent enumeration). The verify-email and update-email keys have the same length limit and are left for a follow-up issue which maybe we need to open later.
After fix: [
Screen.Recording.2026-11-18.at.13.01.06.mov
Screen.Recording.2026-11-18.at.13.04.49.mov
](url)
Closes #55244
reference ticket: #43076