Skip to content

fix(security): strengthen CSRF protection and enforcement - #8150

Merged
TheSkylancer merged 4 commits into
SandeepVashishtha:masterfrom
Namraa310806:fix/csrf-protection-hardening
Jun 11, 2026
Merged

TheSkylancer merged 4 commits into
SandeepVashishtha:masterfrom
Namraa310806:fix/csrf-protection-hardening

Conversation

@Namraa310806

Copy link
Copy Markdown
Contributor

Description

This PR strengthens the frontend CSRF protection implementation by introducing configurable enforcement modes, structured security logging, dedicated CSRF error handling, centralized validation helpers, and comprehensive test coverage.

Changes Made

  • Added CSRFError class for CSRF-specific request failures

  • Added structured security logging for CSRF-related events

  • Introduced configurable CSRF enforcement modes:

    • warning (default)
    • strict
    • disabled
  • Centralized CSRF validation logic in csrfToken.js

  • Added requiresCSRF() helper for consistent mutating-request detection

  • Added validateCSRFToken() helper for reusable validation logic

  • Updated API request interceptors to enforce CSRF rules consistently

  • Added environment configuration through VITE_CSRF_ENFORCEMENT_MODE

  • Expanded security documentation with CSRF architecture and operational guidance

  • Added comprehensive test coverage for:

    • Meta tag token retrieval
    • Cookie token retrieval
    • Token source prioritization
    • Enforcement mode behavior
    • Request blocking in strict mode
    • Header attachment and merging behavior

Security Improvements

  • Improves visibility of missing CSRF tokens through structured logging
  • Allows stricter deployment configurations without breaking existing environments
  • Provides explicit error handling for CSRF failures
  • Reduces duplicated CSRF validation logic across the codebase
  • Improves maintainability and auditability of CSRF-related security controls

Testing

  • Added 21 CSRF-focused test cases
  • All tests passing
  • No new lint errors introduced

Related Issue

Fixes: #8149

Suggested GSSoC Labels:

level:critical quality:exceptional type:security type:testing type:documentation type:bug

@vercel

vercel Bot commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

@Namraa310806 is attempting to deploy a commit to the sandeepvashishtha's projects Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions Bot added level:critical bug Something isn't working labels Jun 10, 2026
@TheSkylancer
TheSkylancer merged commit b7c4454 into SandeepVashishtha:master Jun 11, 2026
1 check failed
@Namraa310806
Namraa310806 deleted the fix/csrf-protection-hardening branch June 13, 2026 18:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CSRF-Protected Requests Proceed Even When CSRF Token Is Missing

2 participants