fix: Add CSRF token validation to prevent CSRF attacks (fixes #22)#45
Open
Vivekk0712 wants to merge 3 commits intotarinagarwal:mainfrom
Open
fix: Add CSRF token validation to prevent CSRF attacks (fixes #22)#45Vivekk0712 wants to merge 3 commits intotarinagarwal:mainfrom
Vivekk0712 wants to merge 3 commits intotarinagarwal:mainfrom
Conversation
- Add csrf-csrf middleware to protect state-changing requests - Create /api/csrf-token endpoint for token generation - Configure secure cookies (HttpOnly, SameSite=Lax) - Add frontend CSRF token management - Exempt public endpoints (auth, health check) - Add automated test suite (15 tests, 100% pass rate) Fixes tarinagarwal#22
tarinagarwal
requested changes
Jan 2, 2026
Owner
tarinagarwal
left a comment
There was a problem hiding this comment.
Nice work on the CSRF setup. One thing - in main.tsx you're using .then() which means if the CSRF fetch fails (backend down, network issue), the app won't render at all. Use .finally() instead so the app still loads. Also remove the hardcoded fallback secret in index.js - better to throw an error if CSRF_SECRET isn't configured.
Contributor
Author
|
Hi @tarinagarwal Switched CSRF init to use .finally() so the app renders even if CSRF fetch fails Please let me know if anything else is needed. Thanks! |
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.
📝 Description
Fixed missing CSRF token validation detected by CodeQL in issue #22. The application was vulnerable to Cross-Site Request Forgery (CSRF) attacks as state-changing requests (POST, PUT, DELETE) were not validating CSRF tokens.
Changes made:
csrf-csrfpackage (v4.0.3) for Double Submit Cookie CSRF protectionGET /api/csrf-tokenSecurity Impact:
Technical Details:
csrf-csrfpackage with Double Submit Cookie patternx-csrf-token(httpOnly, sameSite, secure in production)x-csrf-token(must match cookie value)🔗 Related Issue
Closes #22
🏷️ Type of Change
📸 Screenshots
Backend Implementation:
Frontend Implementation:
Test Results:
Browser DevTools Evidence:
x-csrf-token=aa0b643d1f5abd192ac696e6565d1855ee8897f52539...x-csrf-token: aa0b643d1f5abd192ac696e6565d1855ee8897f52539...✅ Checklist
🧪 Testing
Test Script Created:
test-csrf-implementation.js- Comprehensive CSRF protection test suiteTesting Performed:
Manual Testing:
x-csrf-tokenx-csrf-tokenheaderFiles Modified:
Backend:
Edulume/server/index.js- Added CSRF middleware and configurationEdulume/server/.env.example- Added CSRF_SECRET templateEdulume/server/package.json- Added csrf-csrf dependencyFrontend:
Edulume/client/src/utils/api.ts- Added CSRF token managementEdulume/client/src/main.tsx- Initialize CSRF on app startDocumentation Added:
test-csrf-implementation.js- Comprehensive test suite📋 Additional Notes
This is a critical security fix that addresses CodeQL warnings about missing CSRF token validation. The implementation follows industry best practices and uses the Double Submit Cookie pattern.
Why this matters:
Implementation Details:
Protected Endpoints:
All POST/PUT/DELETE requests except:
/api/csrf-token(token generation)/api/auth/login(public)/api/auth/signup(public)/api/auth/send-otp(public)/api/auth/verify-otp(public)/api/auth/forgot-password(public)/api/auth/reset-password(public)/api/auth/google(OAuth)/api/auth/google/callback(OAuth)/api/health(health check)Performance Impact:
Browser Compatibility:
References:
SWOC 2026 Participant? Yes!