How does login CSRF work in an OAuth callback?
Company tags are compiled from public interview reports and are signals, not guarantees.
What interviewers are testing
Interviewers use this to separate "I add CSRF tokens" from "I understand binding". Classic CSRF abuses a cookie the browser attaches; login CSRF abuses a session identifier the attacker already knows. Strong answers explain why a signed callback URL, PKCE, and SameSite each miss the problem, then name the fix: a browser-bound, single-use state value and session rotation.
Mental model
Think of a coat-check ticket. The attacker asks the clerk for ticket #123 for their own hook, then hands the ticket to the victim. The victim gives the clerk their coat; the clerk hangs it on hook #123 — the attacker’s hook. The ticket was genuine. The clerk never checked who owned the hook.
Step-by-step solution
Step 1: A signed link is a claim ticket
The attacker starts a login in their own session. The server returns /authorize?session_id=ATTACKER with an HMAC over the session ID and timestamp. The signature proves the server generated the link. It does not prove who will open it. The attacker now holds a valid claim ticket for their own hook.
Step 2: The ticket travels to the victim
The link points at the real service, so it looks trustworthy. The attacker sends it to the victim. The victim sees a legitimate domain, clicks, and starts a normal login. Nothing in the URL says the session belongs to the attacker.
Step 3: The victim completes the login
The victim authenticates with the identity provider. The provider redirects to /callback with a one-time request_token and the session_id that was forwarded as redirect_params. The app exchanges the request token and receives the victim’s access token.
Step 4: The token lands on the attacker’s hook
The callback verifies the HMAC, then attaches the victim’s access token to the session named in the URL. That session is the attacker’s. The server never checks that the browser finishing the login is the browser that started it. The attacker can now call every tool the app exposes.
Step 5: Fix: bind state to the browser
On /authorize, generate a random state, set it in a short-lived HttpOnly, Secure, SameSite=Lax cookie, and send the same value to the provider. On /callback, compare the returned state with the cookie before exchanging the code; reject on mismatch, make it single-use, and rotate the session ID after login. PKCE protects the code exchange, not the browser-to-session binding.
javascript// /authorize res.cookie('oauth_state', state, { httpOnly: true, secure: true, sameSite: 'lax', maxAge: 300000, }); res.redirect('https://idp.example/authorize?...&state=' + encodeURIComponent(state)); // /callback if (req.query.state !== req.cookies.oauth_state) { return res.status(403).end(); }
Edge cases & traps
- A signed session_id is not a CSRF token: the attacker can legitimately obtain a signed URL for their own session. The signature proves integrity, not intent.
- PKCE does not stop login CSRF: it protects the authorization code exchange, but the attacker can still start the flow and hand the authorize URL to a victim.
- State stored only in server memory can still be fixed if the attacker initiates the flow; bind it to a browser cookie and make it single-use.
- Magic links are bearer tokens too: a forwarded sign-in link logs the recipient into the sender’s account. Bind or confirm intent where the impact matters.