Cross-Site Request Forgery (CSRF)
Learn how Cross-Site Request Forgery (CSRF) attacks exploit user authentication to perform unauthorized actions, and explore effective prevention strategies using CSRF tokens, SameSite cookies, and secure request validation.
What is Cross-Site Request Forgery (CSRF)?
Cross-Site Request Forgery (CSRF) is an attack that tricks a logged-in user’s browser into sending a malicious request to a trusted website without their consent. This can result in unwanted actions being performed on behalf of the user, such as changing account details or transferring funds.
How Does CSRF Work?
- Victim logs in to a trusted website.
- Browser stores session cookies for that site.
- Attacker tricks the victim into clicking a link or visiting a page containing a malicious request.
- Browser sends request with stored cookies, and the server believes it’s from the legitimate user.
Example:
- A user is logged into
bank.comin one browser tab and has an active session cookie. - The user visits a malicious site
hack.comthat contains hidden form or image that triggers a POST request:
- Because the user is authenticated, their browser automatically sends request with the session cookies, causing the bank to process the transaction unknowingly.
Common Attack Vectors
- Hidden image or iframe requests.
- Malicious forms auto-submitted with JavaScript.
- GET or POST requests exploiting stored authentication cookies.
Common Targets and Impacts
- Changing account settings
- Performing transactions
- Changing passwords or email forwarding
Defenses against CSRF
- SameSite Cookies (Lax, Strict or None) — reduce cookie sending on cross-site requests.
- Strict — The browser will never send the cookie on cross-site requests. The cookie is only sent when the navigation originates from the same site. This offers the strongest CSRF protection but can break legitimate cross-site flows. For example, external links that open a site and expect the user to stay logged in.
- Lax — A balance between security and usability. Cookies are not sent on most cross-site subrequests (like images or iframes), but are sent when the user performs a top-level navigation using a safe HTTP method (typically GET). For example, following a link. Lax prevents many CSRF scenarios while still supporting common cross-site link navigation flows.
- Anti-CSRF Tokens (synchronizer tokens) — include a per-session (or per-form) unpredictable token in forms and validate on server.
- Double Submit Cookie — send token both as cookie and request param and validate they match.
- Use Safe HTTP Methods — require state-changing actions to use POST/PUT/PATCH/DELETE and verify CSRF protection.
- Check Origin / Referer Headers — validate requests come from allowed origins
- Referer can be absent in some cases
Example: SameSite Cookie (Set-Cookie Header)
Set cookies with the SameSite attribute to prevent cross-site requests.
Example: Synchronizer Token with Node/Express
Generate a unique, unpredictable token for each session or request, and validate it server-side.
Conclusion
CSRF exploits a website’s trust in a user’s browser. Preventing it requires verifying the intent of the request using CSRF tokens, secure cookies, and strict validation measures.