Skip to content

Recover correct user session when auth session expired - #55269

Open
rmartinc wants to merge 1 commit into
keycloak:mainfrom
rmartinc:issue-55149
Open

rmartinc wants to merge 1 commit into
keycloak:mainfrom
rmartinc:issue-55149

Conversation

@rmartinc

Copy link
Copy Markdown
Contributor

Closes #55149

There was a corner case regression in #54688 when auth session is expired. The user session was not correctly selected and the login failed. I can only detect this in update email and using a very specific authentication flow that uses cookie and after that another authenticator (password for example). Tests added for both cases.

@rmartinc
rmartinc marked this pull request as ready for review November 17, 2026 14:15
@rmartinc
rmartinc requested review from a team as code owners November 17, 2026 14:15
Copilot AI balanced review requested due to automatic review settings November 17, 2026 14:15

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 session-recovery change can reject valid browser logins when the identity cookie is missing and cause password grants to fail when a browser cookie belongs to another user.

2 open issues
What changed in this PR

This PR addresses #55149 by recovering the browser’s user session from the identity cookie when the authentication-session root no longer matches it.

Changes:

  • Uses identity-cookie validation to recover an unattached user session.
  • Adds regression coverage for same-browser email verification followed by a password update, and for expired cookie-and-password login flows.
  • Extends test helpers to select required actions and configure login timeouts.
File Description
testsuite/​integration-arquillian/​tests/​base/​src/​test/​java/​org/​keycloak/​testsuite/​actions/​AppInitiatedActionUpdateEmailWithVerificationTest.java Tests password updates after same-browser email verification.
testsuite/​integration-arquillian/​tests/​base/​src/​test/​java/​org/​keycloak/​testsuite/​actions/​AbstractAppInitiatedActionTest.java Allows tests to request a specific required action.
tests/​base/​src/​test/​java/​org/​keycloak/​tests/​forms/​AuthCookieAndPasswordLoginTest.java Tests recovery after a cookie-and-password login expires.
test-framework/​builders/​src/​main/​java/​org/​keycloak/​testframework/​realm/​RealmBuilder.java Adds a login-timeout configuration helper.
services/​src/​main/​java/​org/​keycloak/​authentication/​AuthenticationProcessor.java Replaces root-ID session lookup with identity-cookie recovery.

Review effort: Balanced

Closes keycloak#55149

Signed-off-by: rmartinc <rmartinc@redhat.com>
Copilot AI balanced review requested due to automatic review settings November 17, 2026 16:11

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

A critical session-handling flaw can reuse a deleted authentication root’s ID when SSO expires during reauthentication, bypassing fresh-ID protection and requiring a fix with regression coverage.

1 open issue
2 resolved since last review

Review effort: Balanced

@keycloak-github-bot keycloak-github-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Unreported flaky test detected, please review

@keycloak-github-bot

Copy link
Copy Markdown

Unreported flaky test detected

If the flaky tests below are affected by the changes, please review and update the changes accordingly. Otherwise, a maintainer should report the flaky tests prior to merging the PR.

org.keycloak.testsuite.forms.MultipleTabsLoginTest#testInjectRedirectUriInClientDataAfterAuthSessionExpiration

Keycloak CI - Forms IT (chrome)

org.opentest4j.MultipleFailuresError: 
Multiple Failures (2 failures)
	org.openqa.selenium.TimeoutException: timeout: Timed out receiving message from renderer: 10.000
  (Session info: chrome=157.0.8103.50)
Build info: version: '4.46.0', revision: 'df5a634 df5a6341cbb5f8f06b836bd595752775627c80dc'
...

Report flaky test

@rmartinc
rmartinc requested a review from pedroigor November 18, 2026 15:55
userSession = session.sessions().getUserSession(realm, authSession.getParentSession().getId());
// check if user session can be retrieved using cookie
if (isCookie) {
AuthenticationManager.AuthResult result = AuthenticationManager.authenticateIdentityCookie(session, realm, true);

@pedroigor pedroigor Nov 18, 2026 •

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.

Now we are attempting a session from the cookie, we should consider checking whether its state is LOGGED_IN.

We have a similar check in TokenManager that throws an exception if it is not the case.

The scenario I have in mind is a session in a state other than LOGGED_IN, for instance, LOGGING_OUT, and this change changes its state back to LOGGED_IN, whereas it should just stop and not accept the session.

Wdyt?

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.

26.8.2 regression: required actions fail with "already authenticated as different user" after confirming UPDATE_EMAIL in the same browser

3 participants