OAuth state parameter: bind the login response to its original request
Learn how to generate, store and verify OAuth state for each login, when PKCE or OIDC nonce can provide CSRF protection, and how to handle return destinations safely.
OAuth state parameter: bind the login response to its original request
OAuth state is a value the client includes in an authorization request and the authorization server returns in its response. The client uses it to confirm that the response belongs to a login request it started, helping prevent CSRF attacks that inject a forged response. Generate an unpredictable, one-time value for each request and bind it securely to the initiating browser session.
Basic flow
When a client starts login, generate a fresh `state` with a cryptographically secure random generator.
Store the original value securely with the current browser session or authentication transaction.
Send `state` to the authorization server in the authorization request.
The authorization server returns `state` in the callback response.
Compare the returned value with the stored value. Do not process the authorization response if it is missing, mismatched or already consumed.
Use a different state for every request. Do not use a fixed value, username, email address or predictable counter. Even if state is represented in a URL, confirm that the response is bound to the app and browser session that initiated the request.
When PKCE or OIDC nonce is used
PKCE binds an authorization code to the client that initiated the request. OpenID Connect nonce checks that an ID Token came from a particular login request. RFC 9700 explains that PKCE or OIDC nonce can provide protection equivalent to the CSRF role of state. The client must actually verify that PKCE is supported and securely bind a unique value to each request. If server support for PKCE is unknown, use state or nonce for CSRF protection.
If state also carries application data, such as where to return after login, do not treat that data as a CSRF token by itself. Protect it from tampering and substitution across users, for example by binding it to the browser session or signing/encrypting it. Embedding an external URL and redirecting there after callback can create an open redirect; map only to allowed internal paths.
A safe pattern is for the client to generate random state and store it server-side with the login session and an allowed internal path such as /billing. At callback, validate state, consume the transaction once, then return to the stored allowed path. This avoids executing an externally supplied URL as the destination.
Implementation checklist
| Item | Safe handling |
|---|---|
| Generation | Create a fresh value for each request with a secure random generator. |
| Session binding | Bind it to the browser and login transaction so values cannot cross tabs or users. |
| Response check | Process a code or token response only after the callback value exactly matches the stored value. |
| One-time use | Consume or discard the value after success or failure to prevent reuse. |
| Application data | Protect destinations or other data inside state from tampering; do not allow arbitrary external redirects. |
| Failure handling | Treat missing, mismatched or expired state as failure; do not log sensitive values. |
In a test environment, try a state from another login session, a modified value and a reused value. Confirm that authorization processing is rejected. Do not disrupt real users' production login flow or put real authorization codes and tokens in test materials.
Summary
state is a per-request value that connects an OAuth response to the client session that started it. Generate it randomly, bind it to the browser session, verify the returned value and use it once. PKCE or OIDC nonce can provide CSRF protection under the right conditions, but application data inside state still needs integrity protection and safeguards against arbitrary external redirects.
References
[RFC 6749 §4.1.1 — Authorization Request](https://www.rfc-editor.org/rfc/rfc6749.html#section-4.1.1)
[RFC 6749 §4.1.2 — Authorization Response](https://www.rfc-editor.org/rfc/rfc6749.html#section-4.1.2)
[RFC 9700 §2.1 — Protecting Redirect-Based Flows](https://www.rfc-editor.org/rfc/rfc9700.html#section-2.1)
[RFC 9700 §4.7.1 — CSRF Countermeasures](https://www.rfc-editor.org/rfc/rfc9700.html#section-4.7.1)