Cross-Site Request Forgery (CSRF) Overview
Cross-Site Request Forgery (CSRF) Overview
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
Missing 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
Insecure Cookie Configuration
Insecure Cookie Configuration
Relying Only on Request Origin
Relying Only on Request Origin
- 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
CSRF in REST APIs
CSRF in REST APIs
- 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
Double Submit Cookie Pattern
Double Submit Cookie Pattern
SameSite Cookie Attribute
SameSite Cookie Attribute
CSRF in Multi-Step Operations
CSRF in Multi-Step Operations
- 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
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
CSRF Protection in Single Page Applications
CSRF Protection in Single Page Applications
- 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
CSRF Prevention Checklist
CSRF Prevention Checklist
- 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