Skip to content

Conversation

@ProgrammerSONGBIT
Copy link
Contributor

@ProgrammerSONGBIT ProgrammerSONGBIT commented Dec 28, 2025

Changes

  • Added generateRandomState() using crypto.getRandomValues() for cryptographically secure random OAuth state
  • Modified handleLogin() to append unique &state=${randomState} to GITHUB_OAUTH_URL each login
  • Each "Student/Mentor Sign-up" click now generates different 43-char state parameter

Why this matters

Prevents OAuth CSRF attacks by ensuring state parameter is unique per login attempt [web:162]

Testing

  • Click "Student Sign-up" → URL gets &state=abcXYZ123random...
  • Refresh → Click again → NEW &state=def456different...

Fixes kossiitkgp#216

## Changes
- Added `generateRandomState()` using `crypto.getRandomValues()` for cryptographically secure random OAuth state
- Modified `handleLogin()` to append unique `&state=${randomState}` to `GITHUB_OAUTH_URL` each login
- Each "Student/Mentor Sign-up" click now generates **different 43-char state parameter**

## Why this matters
Prevents OAuth CSRF attacks by ensuring `state` parameter is unique per login attempt [web:162]

## Testing
- Click "Student Sign-up" → URL gets `&state=abcXYZ123random...`
- Refresh → Click again → **NEW** `&state=def456different...`
@vercel
Copy link

vercel bot commented Dec 28, 2025

@ProgrammerSONGBIT is attempting to deploy a commit to the jai's projects Team on Vercel.

A member of the Team first needs to authorize it.

@netlify
Copy link

netlify bot commented Dec 28, 2025

👷 Deploy request for kwoc-2025 pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 6be934e

Copy link
Member

@dipamsen dipamsen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR does not completely implement #246. It generates a random state for logging in, but does not verify whether the state is correct after the redirect. See the description in the issue for more details.

Solution:

  1. Generate a random string when clicking on the login button
  2. Store the string in localstorage and redirect to the oauth URL with this state
  3. When the oauth login is complete and the user is redirected to /oauth on the frontend, match these strings and only proceed to log in if they match
  4. It would also be good to make the state a limited-time string so that a string generated previously cannot be used

Added localStorage handling for OAuth state and timestamp.
Added OAuth state validation to prevent CSRF attacks and handle state expiration.
@ProgrammerSONGBIT
Copy link
Contributor Author

Changed the files =

  • HeroSection.tsx: Store oauthState + oauthStateTimestamp in localStorage
  • oauth.tsx: Validate state match + 10min TTL before backend call

Tested = Tested:

  • Redirects to home
  • CSRF attack blocked

Comment on lines 18 to 32
// Check 1: State received from URL
if (!receivedState) {
setErr("No state parameter in OAuth callback. Please try logging in again.");
return false;
}

// Check 2: State was stored in localStorage
if (!storedState) {
setErr("No stored OAuth state found. Your session may have expired.");
return false;
}

// Check 3: States match (prevents CSRF attacks)
if (receivedState !== storedState) {
console.error("CSRF ATTACK DETECTED: OAuth state mismatch!", {
Copy link
Member

@dipamsen dipamsen Jan 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to provide such descriptive error messages to the user (specifically in setErr) -- the error shown to the user can simply be 'Something went wrong. Please try logging in again.'

return;
}

// Validate OAuth state (NEW - prevents CSRF attacks)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove the comments

1. Added simple error message
2. Comments are removed
@ProgrammerSONGBIT
Copy link
Contributor Author

Removed comments as per your feedback and added simple error messages.

auth.setUserType(userType as UserType);
window.location.href = GITHUB_OAUTH_URL;
};
const handleLogin = (userType: string) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of a separate function, can this be part of the auth context?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of a separate function, can this be part of the auth context?

Changed the auth tsx file

Removed GitHub OAuth logic and replaced with auth context method.
Added handleOAuthLogin method to AuthContext.
Added OAuth login handler and random state generation for CSRF protection in the auth file .
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.

3 participants