Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle race condition of state and code verifier #144

Merged
merged 2 commits into from
Dec 16, 2024

Conversation

sergiodxa
Copy link
Owner

Fix #67

When triggering multiple times the OAuth2 flow, each redirect to the authorization URL sets the same cookie with the state and codeVerifier values. This can cause a race condition where only the last one will work as the previous states and code verifiers will be overwritten by the last Set-Cookie header.

This PR implements a StateStore class that's used internally to keep track of multiple states and code verifiers.

The way it works is that every redirect to the authorization URL will now set a different cookie name, using the provided cookie name (or oauth2) as the prefix and appending a UUID (e.g. oauth2:b450faeb-1f67-461e-bf4d-004b6f0a6e55 or custom-name:b450faeb-1f67-461e-bf4d-004b6f0a6e55).

Then when the user is back on the application, the StateStore can read all of these cookies to get every state and code verifier, if the state in the URL matches one of the stored ones, it will then use the associated code verifier to get the tokens from the identity provider.

Because each state and code verifier is stored in a separate cookie, each one can expire individually, which allows using short max age of the cookies (the default is 5 minutes but it could probably be reduced to one or two safely).

@sergiodxa sergiodxa added the enhancement New feature or request label Dec 2, 2024
@sergiodxa sergiodxa self-assigned this Dec 2, 2024
src/index.ts Outdated Show resolved Hide resolved
@sergiodxa sergiodxa merged commit 5743d5f into main Dec 16, 2024
5 checks passed
@sergiodxa sergiodxa deleted the solve-state-race-conditions branch December 16, 2024 21:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"State doesn't match" race condition
2 participants