You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Failure to complete auth flow when session has SameSite=Strict policy, please store tokens in SameSite=Lax cookies instead #1149
When the session cookie is protected by SameSite=Strict and the SSO is not on the same domain than the application itself, the browser is preventing the callback endpoint to access the session cookie on callbacks. This has been observed with an OpenID Connect SSO.
This is because the state parameter passed in param to the callback does not agree with the state parameter in the session, because the session is empty on the callback.
To avoid depending on the session cookie being SameSite=Lax (which can be a security issue for the rest of the app), the persistent state should be stored in a separate cookie specific to OmniAuth that have SameSite=Lax or even SameSite=None depending on the auth flow in use.
Calls to OmniAuth::Strategy#session should be replaced by either a specific session store that is unique to OmniAuth or directly by cookies.
There is a possible workaround by creating a custom middleware that would create cookies from omniauth values in session and restore those values in session if it detects that the session is empty on the callback URL. However many values probably need saving and restoring this way and I found a solution that does not need this level of knowledge on inner behaviour of OmniAuth by performing a HTML redirect when it detects such situation:
classOmniauthOidcFixStateMiddlewaredefinitialize(app)@app=appendCALLBACK_PATH='/users/auth/openid_connect/callback'# You need to customize thisdefcall(env)session=env['rack.session']# This workd for an OpenID Connect flow because it sets omniauth.state in session, you may need to adjust depending on your auth flowreturnredirectenv['REQUEST_URI']ifenv['REQUEST_PATH'] == CALLBACK_PATH && session['omniauth.state'].blank?# Call the next middleware in the stackstatus,headers,response=@app.call(env)[status,headers,response]enddefredirect(uri)# Perform a HTML redirect to ensure that SameSite=Strict session cookie is# includedr=Rack::Response.newr.write(%( <html> <head> <!-- HTML redirect to include session cookie with SameSite=Strict --> <meta http-equiv="refresh" content="0;URL='#{uri}'"/> </head> </html> ))r.finishendend# Add this middleware before OmniAuth is initialized so the middleware appears before the OmniAuth strategy# check using: rails middlewareRails.application.config.middleware.useOmniauthOidcFixStateMiddleware
Adding this to the v3 milestone. Strategies read and write their state (for example omniauth.state in omniauth-oauth2) directly through session, so moving OmniAuth's state into its own cookie changes the contract between OmniAuth and every strategy. That needs a major version.
I need to correct my earlier comment: strategies get the session through OmniAuth's Strategy#session method, so a different store wouldn't break them or need a major version. Sorry about that.
I still don't think OmniAuth should keep its own cookie, though. It would have to be signed and probably encrypted, since it would hold the state value, PKCE verifiers, SAML request IDs, and omniauth.origin (an open redirect if it can be tampered with). That means OmniAuth would need a secret and would have to handle key rotation and cookie size limits. And a SameSite=Strict session cookie breaks returning from any external identity provider, not just with OmniAuth.
So I've documented it instead, in the README's new Session Cookies and SameSite section (405334f): use Lax for the session cookie, or add a same-site hop before the callback like your middleware does. Thanks for writing up the workaround. Closing as not planned.
Activity
sferik commentedon Oct 20, 2026
Adding this to the v3 milestone. Strategies read and write their state (for example
omniauth.statein omniauth-oauth2) directly throughsession, so moving OmniAuth's state into its own cookie changes the contract between OmniAuth and every strategy. That needs a major version.Document how SameSite session cookies affect the callback
sferik commentedon Oct 20, 2026
I need to correct my earlier comment: strategies get the session through OmniAuth's
Strategy#sessionmethod, so a different store wouldn't break them or need a major version. Sorry about that.I still don't think OmniAuth should keep its own cookie, though. It would have to be signed and probably encrypted, since it would hold the
statevalue, PKCE verifiers, SAML request IDs, andomniauth.origin(an open redirect if it can be tampered with). That means OmniAuth would need a secret and would have to handle key rotation and cookie size limits. And aSameSite=Strictsession cookie breaks returning from any external identity provider, not just with OmniAuth.So I've documented it instead, in the README's new Session Cookies and SameSite section (405334f): use
Laxfor the session cookie, or add a same-site hop before the callback like your middleware does. Thanks for writing up the workaround. Closing as not planned.