Cross-Site Request Forgery (CSRF)

Cross-site request forgery (CSRF) tricks a logged-in user's browser into sending an unwanted request to a site they trust, such as changing an email address or making a transfer, without the user realizing it.

AdvancedCategory: Website, application and API security

Also called: CSRF, XSRF, session riding, one-click attack

What is cross-site request forgery?

When you're logged in to a website, your browser automatically sends your session cookie with every request to that site. CSRF abuses this. MITRE's weakness catalog describes it as an application failing to check whether a request was intentionally made by the user. If a site doesn't check, a malicious page can quietly make your browser send a request, and the site will treat it as yours.

How does a CSRF attack work?

  1. You're signed in to a web app, say your company's payroll portal.
  2. In another tab, you visit a malicious page or open a crafted email.
  3. That page contains a hidden form or image that submits a request to the payroll site, such as "change bank account to X".
  4. Your browser sends it with your session cookie, and the site processes it.

The attacker never sees your password or the response; they just get the action done.

How to prevent CSRF

OWASP recommends:

  • Anti-CSRF tokens: a secret, unpredictable value included in each form and checked by the server.
  • SameSite cookies: stop browsers sending session cookies with requests from other sites; modern browsers default to a partial version of this.
  • Checking the Origin or Referer header for sensitive actions.
  • Re-authentication for high-risk actions like changing bank details or passwords.

Most modern web frameworks include CSRF protection; make sure it's turned on.

Related terms

Sources

  1. Cross-Site Request Forgery Prevention Cheat Sheet (OWASP)
  2. CWE-352: Cross-Site Request Forgery (CSRF) (MITRE CWE)

Drafted with AI assistance, then edited and fact-checked by a person. How we use AI.