state and nonce: stopping CSRF and ID token replay
In one line: Where to store and compare state, where nonce is verified in an ID token, length and expiry, and the multi-tab and SPA failure modes.
state and nonce are both random values, but they stop different attacks. state proves a callback came from a login this client started; nonce proves an ID token was issued for this login.
state
Create a fresh random value per authorization request, store it in the session (server session or SPA sessionStorage), and compare it with the callback. Reject the callback on mismatch. Without state, an attacker can plant their own authorization code in a victim's browser and log them into the attacker's account (login CSRF).
// authorization request
state = BASE64URL(random(32))
sessionStorage.setItem('oauth_state', state)
// callback
if (params.get('state') !== sessionStorage.getItem('oauth_state')) reject()
sessionStorage.removeItem('oauth_state')
Use at least 128 bits (16 bytes) of randomness. 32 bytes becomes 43 Base64URL characters. Short values such as six digits are guessable and add no protection.
nonce
With openid in scope, send a nonce and require the ID token's nonce claim to match. Verify it together with the signature, iss, aud and exp before trusting or storing the token.
Common failures
- state only in a cookie — third-party cookie blocking or SameSite settings can drop it before the callback. Use a session store or sessionStorage as well.
- Overwriting state — starting a second login in the same tab makes the first callback fail. Use per-tab keys or keep the previous value briefly.
- Comparing re-decoded values — URLSearchParams decodes automatically. Compare the decoded strings, not raw slices of the URL.
- Generating a nonce but not verifying it — sending it alone adds nothing.
- Putting data in state — state must be a random value. Emails or return URLs leak through logs and referrers.
Checking it in OAuthLab
Panel ② generates state and nonce; panel ③ places them in the URL. Panel ④ takes a callback URL and an expected state and reports match or mismatch, and panel ⑤ flags short state values and missing nonce.
PKCE and URL checks run right here in your browser. Nothing you type is stored.
Open OAuthLabFrequently asked questions
Do I need both state and PKCE?
They defend different things, so use both: state against login CSRF, PKCE against code interception.
When is nonce verified?
Right after receiving the ID token, together with signature, iss, aud and exp. Reject before storing anything in the session.
Can I store a hash of state instead?
It is possible but unusual. state is not a secret; being unguessable is enough.
Sources
- RFC 6749 — The OAuth 2.0 Authorization Framework
- RFC 9700 — Best Current Practice for OAuth 2.0 Security
- OpenID Connect Core 1.0
Read next
- PKCE S256: computing code_verifier and code_challenge correctlyThe RFC 7636 character set and length rules, the SHA-256 to Base64URL step, and where each value travels in the authorization and token requests.
- Authorization URL parameters: what to send and whyauthorization_endpoint, client_id, redirect_uri, response_type, scope, state, nonce, prompt and code_challenge, with a complete example URL.
- redirect_uri registration rules and open redirect riskWhy exact matching matters, and how wildcards, subdomains, path traversal and userinfo (@) confusion turn into account takeover.