IAM interview questions · Protocols
What are the state and nonce parameters for?
In plain wordsState is the receipt you keep to check the reply is for your request. Nonce is a unique word written inside the ID token so it cannot be reused.
How it works
state is an unguessable value the client stores (for example in a cookie) before redirecting, and expects back unchanged on the callback. If it does not match, the response was not started by this browser: it blocks login CSRF, where an attacker makes the victim log into the attacker’s account.
nonce is sent in the authorization request and the IdP copies it into the ID token. The client checks that the ID token contains the nonce of this session, which prevents replay of an ID token stolen from another session.
A concrete example
An attacker starts a login with their own account, stops before the callback and sends the victim the link …/callback?code=ATTACKER_CODE. Without state check the victim ends up logged in as the attacker and saves their credit card into the attacker’s account. With state, the victim’s browser has no matching value and the callback is rejected.
See it in action: is this login yours
Mistakes to avoid
- Using a constant or predictable state.
- Sending nonce but never checking it in the ID token.
They may also ask you
- Does PKCE make state unnecessary?
- Where do you store state in a single-page app?