Skip to content

fix(security): validate GitHub OAuth state nonce to prevent login CSRF - #1307

Merged
RUKAYAT-CODER merged 1 commit into
rinafcode:mainfrom
Olumide-01:fix/github-oauth-state-csrf-1158
Aug 30, 2026
Merged

fix(security): validate GitHub OAuth state nonce to prevent login CSRF#1307
RUKAYAT-CODER merged 1 commit into
rinafcode:mainfrom
Olumide-01:fix/github-oauth-state-csrf-1158

Conversation

@Olumide-01

Copy link
Copy Markdown
Contributor

Summary

The GitHub OAuth callback did not validate a cryptographically secure state nonce, leaving the login flow open to login CSRF. The state value was generated with Math.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 — adds generateOAuthState() (CSPRNG-backed, 256-bit entropy via crypto.getRandomValues) and validateOAuthState() (constant-time comparison).
  • src/lib/github/oauth.tsgenerateState() now delegates to the CSPRNG and a new validateState(actual, expected) helper is exposed for the callback.
  • src/app/api/auth/github/callback/route.ts — verifies the state parameter against the stored cookie through validateState() and rejects mismatched/missing values before token exchange.

Tests

  • src/middleware/__tests__/security.test.ts — coverage for generateOAuthState / 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 --noEmit and Prettier checks are clean.

Closes #1158

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
@drips-wave

drips-wave Bot commented Aug 30, 2026

Copy link
Copy Markdown

@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! 🚀

Learn more about application limits

@RUKAYAT-CODER

Copy link
Copy Markdown
Contributor

Thank you for contributing to the project.

@RUKAYAT-CODER
RUKAYAT-CODER merged commit d0dc3d9 into rinafcode:main Aug 30, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Validate OAuth state parameter on GitHub login callback

2 participants