8000 Admit a single concurrent verification email send per cooldown window by Praiztl · Pull Request #55511 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

Admit a single concurrent verification email send per cooldown window - #55511

Open
Praiztl wants to merge 4 commits into
keycloak:mainfrom
Praiztl:fix/55405-verify-email-cooldown-race
Open

Praiztl wants to merge 4 commits into
keycloak:mainfrom
Praiztl:fix/55405-verify-email-cooldown-race

Conversation

@Praiztl
@Praiztl Praiztl commented Nov 25, 2026
Copy link
Copy Markdown

Closes #55405
Decisions and assumptions:

  • Admission is now a single putIfAbsent on the cooldown key, matching ResetCredentialEmail, so only one of several concurrent requests sends.
  • The cooldown is kept after a failed send, so a failing mail server cannot be used to bypass it.
    This pull request was created with the assistance of an AI agent.

Closes keycloak#55405

Signed-off-by: Tochukwu Chikezie <95018735+Praiztl@users.noreply.github.com>
@Praiztl
Praiztl marked this pull request as ready for review November 25, 2026 09:37
@Praiztl
Praiztl requested a review from a team as a code owner November 25, 2026 09:37
Copilot AI balanced review requested due to automatic review settings November 25, 2026 09:37
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.

🟡 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

Copilot AI balanced review requested due to automatic review settings November 25, 2026 09:56
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.

🔵 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

Copilot AI balanced review requested due to automatic review settings November 25, 2026 10:12
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.

🔵 Needs a closer look

Deferred cache writes extend resend blocking beyond the reported cooldown, and a missing verification auth note allows plain refreshes to send another email after expiry.

2 open issues

Review effort: Balanced

@Praiztl
Praiztl force-pushed the fix/55405-verify-email-cooldown-race branch from 3229b51 to f3550be Compare November 25, 2026 10:20
@Praiztl
Praiztl commented Nov 25, 2026 •
Copy link
Copy Markdown
8000 Author

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>
Copilot AI balanced review requested due to automatic review settings November 25, 2026 12:56
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.

🟡 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>
Copilot AI balanced review requested due to automatic review settings November 25, 2026 15:13
@Praiztl
Praiztl commented Nov 25, 2026
Copy link
Copy Markdown
Author

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.

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.

🔵 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 open issue
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>
@Praiztl
Praiztl requested review from a team as code owners November 25, 2026 16:47
Copilot AI balanced review requested due to automatic review settings November 25, 2026 16: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.

🟡 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)) {
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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.

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.

VERIFY_EMAIL cooldown allows concurrent requests to send multiple emails

2 participants

0