CSRF exploits trust that a website has in the user's browser and existing authentication.
How CSRF works
- Victim is authenticated to vulnerable site (has session cookie).
- Attacker tricks victim into visiting malicious page.
- Malicious page makes request to vulnerable site.
- Browser automatically includes session cookie.
- Vulnerable site processes request as legitimate user action.
Attack examples
- Transfer money from victim's bank account.
- Change account email or password.
- Post content or comments as victim.
- Delete user data or resources.
- Add/remove admin users.
Attack vectors
- Malicious websites with hidden forms.
- Image tags with action URLs:
<img src="bank.com/transfer?to=attacker&amount=1000"> - Iframe embedding vulnerable endpoints.
- Malicious browser extensions.
- XSS combined with CSRF.
Prevention
- CSRF tokens: Random, unique per-session tokens in forms.
- SameSite cookies: Prevent cookie sending on cross-site requests.
SameSite=Strict: No cross-site cookies (most secure).SameSite=Lax: Allow safe methods (GET) only.SameSite=None: Requires Secure flag (HTTPS only).
- Origin/Referer validation: Check request source headers.
- Custom request headers: Require X-Requested-With or similar.
- Double submit cookies: Compare cookie value with form value.
- Re-authentication: Require password for sensitive actions.
Testing
- Remove CSRF token and submit form.
- Change token to invalid value.
- Use token from different session.
- Test cross-origin requests with different SameSite values.
Impact
- Unauthorized state changes.
- Account takeover.
- Financial loss.
- Data modification or deletion.
- Reputation damage.
Explore More Web Security
View all termsCross-Site Scripting (XSS)
A web security vulnerability that allows attackers to inject malicious scripts into web pages viewed by other users.
Read more →HTML Entity Encoding
A method of representing special characters in HTML using named or numeric references to prevent interpretation as code.
Read more →HTTP Cookie
Small pieces of data stored by web browsers, used for session management, personalization, and tracking.
Read more →HTTP Security Headers
Response headers that enable browser security protections against common web attacks.
Read more →JSON Web Token (JWT)
A compact, URL-safe token format used to securely transmit claims between parties in web applications.
Read more →Subresource Integrity (SRI)
A security feature that allows browsers to verify that files from CDNs have not been tampered with.
Read more →