Cross-Site Request Forgery Prevention - OWASP Cheat Sheet Series
Skip to content
Cross-Site Request Forgery Prevention Cheat Sheet
Introduction
Cross-Site Request Forgery (CSRF)
attack occurs when a malicious web site, email, blog, instant message, or program tricks an authenticated user's web browser into performing an unwanted action on a trusted site. If a target user is authenticated to the site, unprotected target sites cannot distinguish between legitimate authorized requests and forged authenticated requests.
Since browser requests automatically include all cookies including session cookies, this attack works unless proper authorization is used, which means that the target site's challenge-response mechanism does not verify the identity and authority of the requester. In effect, CSRF attacks make a target system perform attacker-specified functions via the victim's browser without the victim's knowledge (normally until after the unauthorized actions have been committed).
However, successful CSRF attacks can only exploit the capabilities exposed by the vulnerable application and the user's privileges. Depending on the user's credentials, the attacker can transfer funds, change a password, make an unauthorized purchase, elevate privileges for a target account, or take any action that the user is permitted to do.
In short, the following principles should be followed to defend against CSRF:
IMPORTANT: Remember that Cross-Site Scripting (XSS) can defeat all CSRF mitigation techniques!
While Cross-Site Scripting (XSS) vulnerabilities can bypass CSRF protections, CSRF tokens are still essential for web applications that rely on cookies for authentication. Consider the client and authentication method to determine the best approach for CSRF protection in your application.
See the OWASP
XSS Prevention Cheat Sheet
for detailed guidance on how to prevent XSS flaws.
First, check if your framework has
built-in CSRF protection
and use it
If the framework does not have built-in CSRF protection, add
CSRF tokens
to all state-changing requests (requests that cause actions on the site) and validate them on the backend.
If your software targets only modern browsers, you may rely on
Fetch Metadata headers
together with the fallback options described below to block cross-site state-changing requests.
Stateful software should use the
synchronizer token pattern
Stateless software should use
double submit cookies
If an API-driven site can't use
US