Repository navigation
Conversation
Closes keycloak#55405 Signed-off-by: Tochukwu Chikezie <95018735+Praiztl@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
Cooldown TTL renewal can reject retries beyond the advertised expiry, while rejected admissions leave sessions unprotected against unintended refresh-triggered emails.
3 open issues
What changed in this PR
Addresses #55405 by using atomic per-user cooldown admission to prevent concurrent verification-email requests from each sending an email.
Changes:
- Gates automatic sends and explicit resends through
putIfAbsent. - Handles cooldown entries whose expiry metadata is not yet available.
- Retains the cooldown after an email-send failure.
| File | Description |
|---|---|
services/src/main/java/org/keycloak/authentication/requiredactions/VerifyEmail.java |
Requires successful cooldown admission before sending; otherwise re-renders the verification page. |
services/src/main/java/org/keycloak/authentication/requiredactions/util/EmailCooldownManager.java |
Adds atomic admission and a fallback for missing expiry metadata. |
Review effort: Balanced
There was a problem hiding this comment.
🔵 Needs a closer look
The deferred cache write extends the admission TTL beyond the recorded cooldown expiry, silently rejecting valid retries, and concurrent-send regression coverage is missing.
2 open issues
1 resolved since last review
Review effort: Balanced
3229b51 to
f3550be
Compare
|
Thanks for the review. Responses to the open points: Cooldown TTL: you're right here and my first take was wrong, see the thread below. putIfAbsent hits the cache immediately but put() is deferred to transaction commit (after the send), so the TTL is re-anchored past the recorded expiry; replace() isn't a clean swap either since it drops the lifespan. I've left that thread open for a maintainer's steer on the fix. Verification note on a lost claim: the note is set on the request that wins the claim and sends. A request that loses the claim does not send, and the cooldown entry the winner created already blocks a resend-on-refresh for the rest of the window. A resend after the cooldown has expired is the intended behaviour, so there is no unintended refresh-triggered send. Concurrent-admission test: admission goes through SingleUseObjectProvider.putIfAbsent, the same atomic primitive ResetCredentialEmail relies on, which the single-use provider tests already cover. A deterministic two-request race test is not straightforward in the arquillian harness (requests serialise through one driver with no hook to pause between the read and the write), so I have leaned on the existing cooldown coverage. Happy to add one if there is a preferred approach. |
putIfAbsent is applied immediately, but put() is deferred to transaction commit after the email send, which extended the key's TTL past the recorded expiry and rejected valid retries in that gap. Rely on the putIfAbsent lifespan alone; retrieveCooldownEntry reports the full configured window when no expiry is recorded. Signed-off-by: Tochukwu Chikezie <95018735+Praiztl@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
The cache-clock mismatch breaks an existing resend test, and denied authentication sessions can trigger an unintended email on refresh after cooldown because refresh suppression remains unset.
2 open issues
1 resolved since last review
Review effort: Balanced
…ldown Advance the cache clock via enableForCaches, and wait out the per-user cooldown in the forget-password flow before a second automatic verification email is expected, matching the cross-session throttle this change enforces. Signed-off-by: Tochukwu Chikezie <95018735+Praiztl@users.noreply.github.com>
|
One behavioural edge for maintainers to weigh. Because the cooldown is per user and this change gates the automatic challenge send (not only the explicit resend), a user who registers and then immediately starts "forgot password" will not get a fresh verification email until the cooldown (default 30s) elapses, since the registration send already claimed the window. That is consistent with issue 55405's expected behaviour (one admitted send per user per window, including concurrent automatic sends and across sessions and nodes) and with the #54821/#54947 goal of not sending on every new authentication session, so I have kept it and updated RegisterWithEmailVerificationTest to advance past the cooldown in that flow. Flagging it in case you would prefer the password-reset flow to be exempt, which would be a scope change to the admission key rather than a test change. |
There was a problem hiding this comment.
🔵 Needs a closer look
The tests only advance time in sequential flows and would pass with the original check-then-write logic, leaving concurrent resends and automatic-send-versus-resend races without deterministic regression coverage.
1 resolved since last review
Review effort: Balanced
Spawns concurrent putIfAbsent calls for one key against the single-use store and asserts exactly one is admitted, so a non-atomic check-then-write would fail, and checks that a fresh request is admitted once the cooldown expires. Runs against embedded and remote Infinispan through the model suite. Signed-off-by: Tochukwu Chikezie <95018735+Praiztl@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
The added test only exercises putIfAbsent() and would pass if the fix were reverted, leaving the race between automatic verification emails and explicit resends without effective regression coverage through VerifyEmail.
1 open issue
1 resolved since last review
Review effort: Balanced
| EventBuilder event = context.getEvent().clone().event(EventType.SEND_VERIFY_EMAIL).detail(Details.EMAIL, email); | ||
| challenge = sendVerifyEmail(context, event); | ||
| // Atomically claim the cooldown slot so that concurrent requests cannot each send a verification email | ||
| if (EmailCooldownManager.addCooldownEntryIfAbsent(context, EMAIL_RESEND_COOLDOWN_KEY_PREFIX)) { |
There was a problem hiding this comment.
Fair point. The model test guards the putIfAbsent primitive rather than VerifyEmail's use of it, so it would not catch a revert of the gating change here. A deterministic test that races an automatic send against an explicit resend through VerifyEmail needs a way to hold one request between the cooldown read and the atomic claim, and the tests/ harness drives requests through a single browser session with no such hook, so it cannot be made reliable without a small test seam in the challenge path. I would rather not add that seam unilaterally. If a maintainer has a preferred approach (a seam in VerifyEmail, or a different level at which to assert the single send), I am happy to add it. Leaving this open for that steer.



Closes #55405
Decisions and assumptions:
This pull request was created with the assistance of an AI agent.