Understanding state, nonce, and PKCE
Blog post from WorkOS
OAuth 2.0 and OpenID Connect utilize three critical mechanisms—state, nonce, and PKCE—to ensure secure authentication flows by countering distinct attacks at different stages of the protocol. Each mechanism addresses specific vulnerabilities: the state parameter prevents cross-site request forgery during browser redirects, nonce protects against ID token replay attacks by verifying the token's issuance for a specific session, and PKCE ensures the authorization code exchange at the token endpoint is secure, especially for public clients without a client secret. These mechanisms do not overlap and are not redundant, as each neutralizes a unique class of attack, with state verified at the callback, nonce checked within the ID token, and PKCE confirmed at the token exchange. Employing all three mechanisms is crucial to maintaining a robust security posture, as omitting any one creates exploitable gaps, and common mistakes such as using predictable state values or treating PKCE as optional for confidential clients can undermine their effectiveness.