Skip to content

fix(csrf): add per-session CSRF token to state-mutating auth requests#694

Open
anshul23102 wants to merge 1 commit into
GitMetricsLab:mainfrom
anshul23102:fix/689-csrf-state-token
Open

fix(csrf): add per-session CSRF token to state-mutating auth requests#694
anshul23102 wants to merge 1 commit into
GitMetricsLab:mainfrom
anshul23102:fix/689-csrf-state-token

Conversation

@anshul23102
Copy link
Copy Markdown
Contributor

Description

The POST /api/auth/login form submission carried no CSRF protection. A malicious third-party page could submit the login form cross-origin using the victim's active browser session, since the browser automatically attaches session cookies to cross-origin requests.

Root Cause

No CSRF header was included in the axios.post call. The backend had no way to distinguish a legitimate user submission from a forged cross-site request.

Related Issue

Closes #689

Type of Change

  • Bug fix (security)

Changes Made

  • src/utils/csrf.ts: New utility that generates a cryptographically random 32-byte token via window.crypto.getRandomValues(), persists it in sessionStorage, and exposes a csrfHeaders() helper that returns the X-CSRF-Token header object.
  • src/pages/Login/Login.tsx: Applied csrfHeaders() to the login POST request headers.

The backend must validate the X-CSRF-Token header against the server-side session value to complete the protection chain.

Testing Done

  1. Login form submission includes X-CSRF-Token header with a random value.
  2. Subsequent requests within the same session use the same token.
  3. A new tab generates a fresh token (sessionStorage is tab-scoped).

Checklist

  • My code follows the style and formatting of this project
  • I have tested my changes locally and they work as expected
  • There are no merge conflicts with the base branch
  • This PR is linked to the correct issue

POST /api/auth/login was submitted with no CSRF protection. An attacker
could craft a page that silently submits the form from a third-party
origin using the victim's browser session, since the browser automatically
attaches session cookies to cross-origin requests.

Add a csrf.ts utility that generates a cryptographically random 32-byte
token via window.crypto.getRandomValues(), stores it in sessionStorage
for the lifetime of the tab, and exposes csrfHeaders() for use with axios.
Apply csrfHeaders() to the login POST request. The backend must validate
the X-CSRF-Token header against the server-side session to complete the
protection chain.

Closes GitMetricsLab#689
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 3, 2026

Warning

Review limit reached

@anshul23102, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 54 minutes and 12 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: eeb33b82-0f2a-4fe3-b61d-8a004199b46c

📥 Commits

Reviewing files that changed from the base of the PR and between 53f820b and 2f6ad5a.

📒 Files selected for processing (2)
  • src/pages/Login/Login.tsx
  • src/utils/csrf.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@netlify
Copy link
Copy Markdown

netlify Bot commented Jun 3, 2026

Deploy Preview for github-spy ready!

Name Link
🔨 Latest commit 2f6ad5a
🔍 Latest deploy log https://app.netlify.com/projects/github-spy/deploys/6a2029008946b400080f3f6e
😎 Deploy Preview https://deploy-preview-694--github-spy.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

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] Missing CSRF Protection - Forms Vulnerable to Cross-Site Request Forgery

1 participant