Fix: auto-retry OAuth2 flow on invalid state instead of showing error#8674
Open
addow wants to merge 1 commit into
Open
Fix: auto-retry OAuth2 flow on invalid state instead of showing error#8674addow wants to merge 1 commit into
addow wants to merge 1 commit into
Conversation
When the OAuth2 state cookie is lost between the redirect and callback (e.g. due to session GC, concurrent requests, or cache/session mismatch), the user gets a confusing "Invalid state parameter" error. Fix by transparently restarting the auth flow on the first failure; a second consecutive failure still shows the error to avoid infinite loops.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When the OAuth2 state stored in the PHP session is lost between the
/connect/{provider}redirect and the/connect/{provider}/checkcallback, the
KnpU\OAuth2ClientBundlethrows anInvalidStateAuthenticationException. The user sees a confusing redflash message — "Invalid state parameter passed in callback URL." —
and is redirected to the home page, where they must manually navigate
back to the login page and click the SSO button again.
This can happen due to:
The scenario we observed: admin logs in via SSO → uses "Login as" to
impersonate another user → logs out → tries to re-authenticate via SSO →
hits the invalid-state error because the session was invalidated during
logout and the new session state was lost.
Solution
Catch
InvalidStateAuthenticationExceptioninAbstractAuthenticator::onAuthenticationFailure()and transparentlyrestart the auth flow by redirecting to the provider's start URL
(
/connect/{provider}). A session flag (_oauth2_state_retry_{provider})limits this to one automatic retry per provider; a second consecutive
failure still shows the error, preventing an infinite redirect loop.
The change is a one-file patch affecting all OAuth2 providers
(generic, Facebook, Keycloak, Azure) equally.
Test plan
/connect/{provider}, clear the session cookie, follow the callback URL → should transparently retry and land on the login page (or complete auth), not show an error