8000 Apply VERIFY_EMAIL resend cooldown on new authentication sessions by julioo19 · Pull Request #54947 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

Apply VERIFY_EMAIL resend cooldown on new authentication sessions - #54947

Open
julioo19 wants to merge 1 commit into
keycloak:mainfrom
julioo19:fix-verify-email-cooldown-54821
Open

julioo19 wants to merge 1 commit into
keycloak:mainfrom
julioo19:fix-verify-email-cooldown-54821

Conversation

@julioo19
@julioo19 julioo19 commented Nov 9, 2026 •
Copy link
Copy Markdown

Closes #54821

VerifyEmail.process() sent the verification e-mail whenever the authentication session had no VERIFY_EMAIL_KEY note, which is the case for every new session. The resend cooldown is stored per user, but it was only checked in processAction() (the resend link).

This change checks the cooldown in process() before sending, as an extra condition of the existing if. While it is active, the page is rendered without sending an e-mail and without setting the note, so a refresh after the cooldown expires sends at most one e-mail (as discussed in the issue).

Tests: added verifyEmailNewSessionRespectsResendCooldown to RequiredActionEmailVerificationTest. It fails without the fix (two e-mails instead of one) and passes with it.

Since the old testsuite is deprecated, I followed testsuite/DEPRECATED.md, which allows a limited number of changes to existing tests, mainly to add test cases when resolving bugs, and does not allow new files. This adds one test method and no new files. Happy to move it to the new Test Framework if you prefer though.

Updated tests

Some tests in the new test framework assumed one verification e-mail per login, which no longer holds with the per-user cooldown:

  • RegisterWithEmailVerificationTest, OrganizationPostBrokerMembershipTest: @InjectTimeOffSet(enableForCaches = true) + timeOffSet.set(40) before the step that expects a new e-mail.
  • AbstractSslEmailTest, EmailPlainAuthTest: per-method user (LifeCycle.METHOD) so each test starts without a cooldown.

CI on my fork is green for this commit: https://github.com/julioo19/keycloak/actions?query=branch%3Aci-check-54821

Docs: no option was added or changed, and I found no hand-written docs describing this behavior.

AI usage: I used Claude to help me understand the code and the project workflow, to review my changes, and for suggestions on the test outline, the test adaptations, some code comments, and the wording of this PR. I wrote the fix and the test logic myself and ran the tests locally.

@julioo19
julioo19 requested review from a team as code owners November 9, 2026 20:58
Copilot AI balanced review requested due to automatic review settings November 9, 2026 20:58
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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

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.

🟢 Approval recommended

No actionable issues were found in the reviewed changes.

No open issues 🎉

Review effort: Balanced

@julioo19
julioo19 commented Dec 5, 2026
Copy link
Copy Markdown
Author

Updated the PR: some existing tests in the new test framework assumed one verification e-mail per login, which no longer holds with the per-user resend cooldown. I adapted them:

  • RegisterWithEmailVerificationTest and OrganizationPostBrokerMembershipTest: @InjectTimeOffSet(enableForCaches = true) + timeOffSet.set(40) before the step that expects a new e-mail.
  • AbstractSslEmailTest and EmailPlainAuthTest: the injected user now uses LifeCycle.METHOD, so each test starts without a cooldown left by the previous one.

I also merged the cooldown check into the main if condition in VerifyEmail.process(); behavior is unchanged. Everything is squashed into a single commit.

Locally (Windows, JDK 21) these classes pass, except EmailPlainAuthTest#testVerifyEmailWithPlainSmtpAuth: the send attempt now happens, but fails with SEND_VERIFY_EMAIL_ERROR on my machine, also when run in isolation. I suspect it's environment-related (the test points SMTP at localhost.localdomain, which may not resolve on Windows), but I couldn't confirm it, and I couldn't check on Linux CI because GitHub Actions is disabled on my fork.

@julioo19
julioo19 commented Dec 5, 2026
Copy link
Copy Markdown
Author

Follow-up: CI on my fork is green for this exact commit (dcc9cdb), including the Base1–Base4 test suites (Base2 and Base3 previously failed): https://github.com/julioo19/keycloak/actions?query=branch%3Aci-check-54821

So EmailPlainAuthTest passes on Linux; the failure I mentioned was specific to my Windows setup.

Closes keycloak#54821

Signed-off-by: Julio Cesar Montalvan <110637361+julioo19@users.noreply.github.com>
@julioo19
julioo19 force-pushed the fix-verify-email-cooldown-54821 branch from dcc9cdb to 1b4ba23 Compare December 5, 2026 05:16
Copilot AI balanced review requested due to automatic review settings December 5, 2026 05:16
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

Concurrent new authentication sessions for the same user can both pass the cooldown check and send verification emails. The cache read does not atomically reserve the cooldown, and the unconditional write is deferred until the Infinispan transaction commits.

1 open issue

Review effort: Balanced

if (!Objects.equals(authSession.getAuthNote(Constants.VERIFY_EMAIL_KEY), email) && !(isCurrentActionTriggeredFromAIA(context) && isChallenge)) {
if (!Objects.equals(authSession.getAuthNote(Constants.VERIFY_EMAIL_KEY), email)
&& !(isCurrentActionTriggeredFromAIA(context) && isChallenge)
&& remaining == null) {
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.

Thanks Copilot, this is correct: two concurrent requests can both read no cooldown before either write is committed (the write is deferred until the Infinispan transaction commits), so both would send an e-mail.

I left it out of this PR on purpose:

That said, it's the maintainers' call: I'm happy to include an atomic reservation here, or to align with #55511 in whichever order you prefer to merge them.

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 resend cooldown is not applied on the challenge path: every new authentication session sends a verification e-mail

2 participants

0