Skip to content

Fix: browser OAuth flow never sets session cookies (redirect format)#271

Open
carlosplanchon wants to merge 1 commit into
benavlabs:mainfrom
carlosplanchon:fix/oauth-redirect-session-cookies
Open

Fix: browser OAuth flow never sets session cookies (redirect format)#271
carlosplanchon wants to merge 1 commit into
benavlabs:mainfrom
carlosplanchon:fix/oauth-redirect-session-cookies

Conversation

@carlosplanchon

Copy link
Copy Markdown
Collaborator

The OAuth callback sets session cookies on the injected response parameter but returns a RedirectResponse directly; FastAPI drops the injected response's headers in that path, so the browser flow (response_format=redirect, the default) completes the dance but the user lands logged out. Only the json flow worked, which is why tests never caught it. Fix: set the cookies on the RedirectResponse itself. Includes a regression test that fails without the fix. Found live while integrating a self-hosted OIDC provider (Zitadel); reproduces with the stock Google flow.

The Google OAuth callback set the session cookies on the injected
response parameter and then returned a RedirectResponse. FastAPI only
merges the injected response's headers into the final response on the
serialization path; when an endpoint returns a Response directly they
are dropped. Net effect: with response_format=redirect (the default,
i.e. the real browser flow) the OAuth dance completed and the session
was created server-side, but the browser never received
session_id/csrf_token and the user landed logged out. The json flow
worked, which is why the test suite (json-only for the success case)
never caught it.

Build the RedirectResponse first, set the cookies on it, and return it.
Adds a regression test asserting the 302 carries both Set-Cookie
headers (fails without the fix).

Found live while integrating a self-hosted OIDC provider; reproduced
with the stock Google flow.

Signed-off-by: Carlos Andrés Planchón Prestes <carlosandresplanchonprestes@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant