fix(security): validate GitHub OAuth state nonce to prevent login CSRF - #1307
Merged
RUKAYAT-CODER merged 1 commit intoAug 30, 2026
Merged
Conversation
Generate the OAuth state value from the CSPRNG instead of Math.random() and validate it in constant time on the callback, so an attacker can no longer predict or reuse the nonce to mount a login CSRF attack. - security: add generateOAuthState/validateOAuthState helpers - github/oauth: generateState now uses the CSPRNG; add validateState - github callback: verify state via validateState before exchanging code - tests for the security helpers, GitHub OAuth utils and callback route Closes rinafcode#1158
|
@Olumide-01 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
Contributor
|
Thank you for contributing to the project. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The GitHub OAuth callback did not validate a cryptographically secure
statenonce, leaving the login flow open to login CSRF. The state value was generated withMath.random()(predictable) and compared with a plain string comparison.This PR makes the nonce unpredictable and checks it in constant time before exchanging the authorization code:
src/middleware/security.ts— addsgenerateOAuthState()(CSPRNG-backed, 256-bit entropy viacrypto.getRandomValues) andvalidateOAuthState()(constant-time comparison).src/lib/github/oauth.ts—generateState()now delegates to the CSPRNG and a newvalidateState(actual, expected)helper is exposed for the callback.src/app/api/auth/github/callback/route.ts— verifies the state parameter against the stored cookie throughvalidateState()and rejects mismatched/missing values before token exchange.Tests
src/middleware/__tests__/security.test.ts— coverage forgenerateOAuthState/validateOAuthState.src/lib/github/__tests__/oauth.test.ts— new test file for the GitHub OAuth utils.src/app/api/auth/github/callback/__tests__/route.test.ts— new route tests covering success, invalid/missing state, OAuth error, missing code, and missing email.All added/updated tests pass;
tsc --noEmitand Prettier checks are clean.Closes #1158