Skip to main content
Cross-Site Request Forgery (CSRF) is an attack that forces authenticated users to execute unwanted actions on a web application in which they’re currently authenticated. CSRF attacks specifically target state-changing requests, not theft of data, since the attacker has no way to see the response to the forged request.CSRF vulnerabilities exploit the trust that a web application has in a user’s browser, leveraging the authenticated session to perform actions without the user’s consent or knowledge.Preventing CSRF typically involves implementing anti-CSRF tokens, same-site cookies, and other security measures that verify the legitimacy of requests.
Missing CSRF protection allows attackers to trick users into performing unwanted actions on websites where they are authenticated.To implement CSRF protection:
  • Use anti-CSRF tokens in forms and AJAX requests
  • Implement the Synchronizer Token Pattern
  • Validate the token on the server for all state-changing requests
  • Consider using a CSRF protection middleware
  • Ensure tokens are unique per user session
  • Regenerate tokens after authentication
  • Use SameSite cookie attribute as an additional layer of protection
Relying only on the Origin or Referer headers for CSRF protection is insufficient, as these headers can be spoofed or might be missing in some requests.To implement robust CSRF protection:
  • Use anti-CSRF tokens as the primary protection mechanism
  • Consider Origin/Referer checks as an additional layer of defense
  • Implement SameSite cookie attributes
  • Be aware that Referer headers might be stripped by privacy settings
  • Use a defense-in-depth approach with multiple protection mechanisms
  • Regularly test CSRF protections
  • Keep security libraries and frameworks updated
REST APIs can be vulnerable to CSRF attacks if they rely solely on cookies for authentication and don’t implement proper CSRF protections.To protect REST APIs from CSRF:
  • Use token-based authentication with custom headers (e.g., JWT in Authorization header)
  • Implement the Double Submit Cookie pattern
  • Use SameSite cookie attributes
  • Consider requiring a custom X-Requested-With header for AJAX requests
  • Implement proper CORS configuration
  • Use anti-CSRF tokens for endpoints that must use cookie-based authentication
  • Regularly test API endpoints for CSRF vulnerabilities
Multi-step operations can be vulnerable to CSRF attacks, especially at the final step if proper protections are not implemented throughout the process.To protect multi-step operations from CSRF:
  • Implement CSRF protection at each step
  • Use operation-specific tokens that are tied to the particular transaction
  • Implement proper session management
  • Set appropriate expiration times for operation tokens
  • Validate the entire operation context, not just individual steps
  • Clear sensitive data from the session after completion
  • Implement proper error handling for token validation failures
Login CSRF is a specific type of attack where an attacker forces a victim to log in to the attacker’s account, potentially leading to sensitive information disclosure or other attacks.To prevent Login CSRF:
  • Implement CSRF protection for login forms
  • Regenerate the session after login
  • Use SameSite cookie attributes
  • Consider implementing login attempt rate limiting
  • Use multi-factor authentication for sensitive accounts
  • Implement proper logging of login attempts
  • Consider using captchas for login forms
Single Page Applications (SPAs) require special consideration for CSRF protection, as they often use APIs and may not use traditional form submissions.To implement CSRF protection in SPAs:
  • Use token-based authentication (e.g., JWT) with proper storage
  • Implement the Double Submit Cookie pattern
  • Use custom headers for API requests
  • Configure proper CORS settings
  • Use SameSite cookie attributes
  • Implement proper session management
  • Consider using a stateless approach with JWTs in Authorization headers
A comprehensive approach to CSRF prevention involves multiple layers of defense, including tokens, secure cookies, and proper request validation.Key CSRF prevention strategies:
  • Implement proper anti-CSRF tokens
  • Use SameSite cookie attributes
  • Validate the origin of requests
  • Implement proper session management
  • Require re-authentication for sensitive operations
  • Keep security libraries and frameworks updated
  • Follow the principle of defense in depth