OAuth state parameter is validated
Why it matters
The OAuth state parameter is the CSRF token for the OAuth flow. Without it, an attacker can initiate an OAuth authorization on behalf of a victim and then complete the flow with the attacker's authorization code, linking the victim's account to the attacker's identity (account linking attack). CWE-352 (CSRF) and CWE-601 (Open Redirect) are both implicated in OAuth flow manipulation; CAPEC-62 documents the OAuth CSRF attack specifically. OWASP A01 (Broken Access Control) covers this when an attacker can substitute their own authorization code. State validation is a mandatory requirement of RFC 6749.
Severity rationale
High because OAuth CSRF without state validation can silently link a victim's account to an attacker's social identity or trigger unauthorized account access during the callback.
Remediation
Generate a per-flow cryptographically random state, store it in an HttpOnly cookie before redirecting, and verify it matches on callback:
// Initiation — generate and store state
const state = crypto.randomBytes(16).toString('hex')
response.cookies.set('oauth_state', state, { httpOnly: true, secure: true, sameSite: 'lax' })
return redirect(provider.buildAuthUrl({ state, redirect_uri: callbackUrl }))
// Callback — verify state before processing code
const storedState = request.cookies.get('oauth_state')?.value
const returnedState = searchParams.get('state')
if (!storedState || storedState !== returnedState) {
return Response.json({ error: 'State mismatch — possible CSRF' }, { status: 400 })
}
NextAuth and Supabase Auth handle state generation and verification automatically. If you've written a custom OAuth callback handler, do not skip this step.
DetectionPro
The detection method for this pattern is part of AuditBuffet Pro.
The full method (what to look for, pass and fail conditions, and how results get reported) ships inside the Authentication Audit. The pattern definition, severity rationale, and remediation remain openly licensed under CC-BY-4.0.
External references
- cwe · CWE-352 — Cross-Site Request Forgery (CSRF)
- cwe · CWE-601 — URL Redirection to Untrusted Site ('Open Redirect')
- owasp:2021 · A01
- capec · CAPEC-62 — Cross-Site Request Forgery
Taxons
History
- 2026-04-18·v1.0.0·Initial import from saas-authentication·automated