Feature: Cross-Site Request Forgery (CSRF) Protection - #1286
Open
vivek0028 wants to merge 2 commits into
Open
Conversation
|
@vivek0028 is attempting to deploy a commit to the Nitya Gosain's projects Team on Vercel. A member of the Team first needs to authorize it. |
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
This pull request implements Cross-Site Request Forgery (CSRF) protection for the CropChain API backend.
Most API endpoints use
Authorization: Bearertokens, which are generally not vulnerable to CSRF because browsers do not automatically attach Authorization headers to cross-origin requests. However, the authentication refresh token used by/api/auth/refreshis stored in an HttpOnly cookie.Without CSRF protection, cookie-authenticated endpoints can potentially be abused through forged cross-origin requests. This implementation adds an additional security layer to protect the API against such attacks.
🚀 Key Changes
📦 Dependencies
Added the following dependencies to
backend/package.json:csurfcookie-parser🛡️ CSRF Middleware —
backend/app.jsInitialized
cookie-parser.Added CSRF protection using
csurf.Configured the CSRF cookie with:
httpOnly: truesameSite: "strict"secure: truein productionAdded a dedicated endpoint:
This endpoint allows the frontend to obtain a CSRF token before making state-changing requests.
backend/middleware/errorHandler.jsAdded handling for the
EBADCSRFTOKENerror.Invalid or missing CSRF tokens now return:
403 Forbiddenwith a clear response message:
🔍 Security Impact
This implementation helps protect cookie-authenticated requests against:
The protection complements the existing JWT authentication and Helmet security headers.
🧪 Verification
csurfdependency.cookie-parser.backend/app.js./api/csrf-tokenendpoint.EBADCSRFTOKENerror handling.X-CSRF-Tokenare rejected with403 Forbidden.NODE_ENV === "test"to avoid CI/test regressions.The frontend will need to be updated to work with the new CSRF protection.
Before making state-changing requests such as:
the frontend should:
X-CSRF-Token: <csrf-token>This ensures legitimate frontend requests pass CSRF validation.
Warning
This PR focuses on the backend implementation. Frontend integration is required for clients making protected state-changing requests.
📂 Files Changed
backend/app.jsbackend/package.jsonbackend/middleware/errorHandler.js📌 Related Issue
Closes #1222
🏷️ Labels
enhancementsecuritybackendGSSoC'26