fix: Add CSRF token validation to prevent CSRF attacks (fixes #22) #45
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!