Skip to content

Implement CSRF protection middleware for API routes#380

Open
nancy-verma780 wants to merge 2 commits into
devpathindcommunity-india:masterfrom
nancy-verma780:feat-csrf-protection
Open

Implement CSRF protection middleware for API routes#380
nancy-verma780 wants to merge 2 commits into
devpathindcommunity-india:masterfrom
nancy-verma780:feat-csrf-protection

Conversation

@nancy-verma780
Copy link
Copy Markdown
Contributor

Description

Implemented a centralized request-intercepting middleware inside the src/ directory to block unauthorized state-mutating API calls missing valid CSRF tokens.

Closes #359

This middleware checks for CSRF tokens on state-mutating requests and blocks requests if validation fails.
@nancy-verma780
Copy link
Copy Markdown
Contributor Author

Hi @Aditya948351, I have successfully integrated a global CSRF mitigation wrapper inside the src/middleware.ts file to intercept unverified state-mutating request vectors. My Pull Request has been opened! Please review and merge it. Thank you!

@Aditya948351 Aditya948351 self-requested a review May 29, 2026 09:28
Copy link
Copy Markdown
Collaborator

@Aditya948351 Aditya948351 left a comment

Choose a reason for hiding this comment

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

Some changes needed @nancy-verma780 , while this correctly implements the backend checking mechanism for CSRF, it currently lacks the frontend implementation.

If this PR is merged as-is, it will immediately break all forms, authentications, and state-mutating API requests across the entire application because the frontend does not currently generate, store, or send the x-csrf-token header or csrfToken cookie. All mutating requests will fail with a 403 Forbidden error.

To get this merged, please update the PR to include:

  1. A mechanism to securely generate and set the csrfToken cookie.
  2. Updates to our frontend API utilities or fetch wrappers to ensure they read this token and attach the x-csrf-token header to all outgoing POST, PUT, DELETE, and PATCH requests.

This file provides utility functions for handling CSRF tokens and a custom fetch wrapper to include CSRF headers in mutating requests.
@nancy-verma780
Copy link
Copy Markdown
Contributor Author

Hi @Aditya948351, thank you for the feedback! I completely agree that blocking mutating requests without frontend support would halt form submissions.I have updated the PR by implementing a custom frontend helper client inside src/lib/apiClient.ts. It securely structures a client-side csrfToken cookie context configuration layer and builds a custom secureFetch wrapper that reads this cookie token and automatically appends the matching x-csrf-token header to all outgoing POST, PUT, DELETE, and PATCH methods.The code changes are committed directly to this branch. Could you please check out the update? Thank you!

@nancy-verma780
Copy link
Copy Markdown
Contributor Author

Hi @Aditya948351, thanks for assigning me! I have already implemented the frontend apiClient.ts wrapper to automatically handle the x-csrf-token header.

Are there any specific frontend forms or components you want me to explicitly update with this new wrapper before you approve, or should we verify the current setup? Let me know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Security] Implement CSRF protection for state-mutating requests

2 participants