Cross Site Request Forgery (CSRF)
1 minute read
Cross Site Request Forgery (CSRF)
Cross-Site Request Forgery (CSRF) is a vulnerability that allows an attacker to trick a user’s browser into performing unintended actions (eg. go to a website that can transfer money from your bank account,change password of your social media account). This happens when user is already logged into a trusted website beforehand.
The core issue is excessive trust in the browser:
- The browser automatically includes session cookies, authorization headers, or credentials with every request to a site.
- If the server does not properly verify who initiated the request, it cannot distinguish a legitimate user action from a forged one.
What Can CSRF Do?
- Account Takeover: Change passwords, emails, or security settings
- Financial Fraud: Transfer funds, make purchases
- Data Manipulation: Delete/modify sensitive data
- Privilege Escalation: Grant admin rights to attackers
Why CSRF Happens
- No Server-Side Request Validation: The server does not verify whether the request was intentionally generated by the user and accepts any request that includes valid session credentials
- Flawed Session Handling: Session tokens are valid but not tied to user intent and emain usable across sites and requests
- Weak Cookie Policies: Cookies lack SameSite attributes which allows browsers to freely send them on cross-site requests
- Reliance on Untrusted Headers: Using Referer or Origin headers alone. These headers can be missing or stripped
Mitigation
- Use anti-CSRF tokens (unique token generated at server side) to validate user intent and embed it in forms or headers. Token must be tied to the user session
- Use SameSite attribute for authentication cookies. it can be set to Strict (cookies sent only for same-site navigation) or Lax (Allows top-level navigation but blocks most CSRF)
- Require re-authentication for sensitive actions like changing passwords, updating email or disabling MFA.
- Use only POST requests for state changes. CSRF is easier to exploit when actions are exposed via GET req.
- Avoid weak Referer validation like checking Referer and Origin headers. It can be helpful but should never be the sole protection.
- Using a well-defined Content-Security Policy header is a supplementary security measure that can reduce attack surface significantly.
Let me know what you think of this article in the comment section below!
comments powered by Disqus