Web Development

Cross-Site Request Forgery (CSRF): Technical Guide

Define Cross-Site Request Forgery (CSRF) and learn how to prevent it. Explore the use of CSRF tokens and SameSite cookies to secure web applications.

74.0k
cross-site request forgery
Monthly Search Volume

— ENTITY TRACKING — 1. Cross-Site Request Forgery (CSRF): An attack that tricks an authenticated user's browser into performing unauthorized actions on a trusted website. 2. Same-Origin Policy (SOP): A security standard that prevents a script on one website from accessing data on another website. 3. CSRF Token: A unique, secret value generated by a server and embedded in requests to verify user intent. 4. SameSite Cookie Attribute: A setting in web cookies that dictates whether they are sent with cross-site requests. 5. Confused Deputy: A security term for when a privileged program (the browser) is tricked into misusing its authority. 6. Login CSRF: A variation of the attack where an attacker forces a victim to log into an account controlled by the attacker. 7. Synchronizer Token Pattern (STP): A defensive technique where unpredictable tokens are embedded in forms and verified upon submission.

Cross-Site Request Forgery (CSRF) is a security vulnerability where an attacker tricks a user's browser into sending unauthorized commands to a web application. This attack, also known as XSRF, "Sea Surf," or session riding, exploits the trust a website has in a user’s authenticated session. For marketers managing SEO tool platforms or client sites, a successful attack can result in unauthorized data changes or account takeovers that damage brand reputation.

What is Cross-Site Request Forgery (CSRF)?

CSRF is often described as a "one-click attack" because it requires the victim to be currently logged into the target website. While [CSRF vulnerabilities have been known and exploited since 2001] (Wikipedia), they remains a common threat. Unlike Cross-Site Scripting (XSS), which targets the user's trust in a site, CSRF targets the site's trust in the user's browser.

In these scenarios, the browser automatically includes sensitive credentials like session cookies or IP addresses with every request. Because the request appears to come from a valid, logged-in user, the server cannot distinguish between a legitimate action and a forged one.

Why CSRF matters

CSRF attacks target "state-changing" requests rather than data theft. Because the attacker cannot see the response from the server, they focus on forcing the victim to change something on the back end. * Account Manipulation: Attackers can force users to change their email addresses or passwords to gain full account control. * Financial Risk: On banking or e-commerce sites, forgeries can trigger unauthorized fund transfers or purchases. * Administrative Compromise: If an administrator account is targeted, the attacker could compromise the entire web application. * Data Poisoning: Through Login CSRF, an attacker can trick a user into adding personal info, like credit cards, to an attacker-controlled account.

[Specific vulnerabilities in 2008 allowed YouTube attackers] (Wikipedia) to perform nearly any action on behalf of any user, highlighting the scale of potential impact.

How CSRF works

For an attack to succeed, three conditions must exist: a relevant action (like changing a password), cookie-based session handling, and predictable request parameters.

  1. Preparation: The attacker identifies a reproducible request on the target site, such as bank.com/transfer?amount=100&to=target.
  2. Creation: The attacker builds a malicious link or script. This could be hidden in an image tag <img> or a hidden form.
  3. Social Engineering: The attacker lures the victim to a malicious page or sends an email containing the exploit.
  4. Execution: If the victim is logged into the target site, their browser automatically attaches session cookies and executes the hidden request.

Real-world examples include the [uTorrent exploit from 2008] (Wikipedia), which used simple GET requests to change administrator passwords. Similarly, [Netflix fixed vulnerabilities in 2006] (Wikipedia) that could have allowed attackers to change shipping addresses or rental queues.

Best practices

Effective CSRF defense requires ensuring that the server can verify the user's intent beyond just a session cookie.

  • Implement CSRF Tokens: Use unique, unpredictable tokens for every state-changing request. The server generates these tokens and checks for them upon form submission.
  • Use SameSite Cookie Attributes: Set cookies to "Strict" or "Lax" to prevent the browser from sending them during cross-site navigations. [Chrome began mercury-enforcing Lax restrictions by default in 2021] (PortSwigger).
  • Verify Fetch Metadata: Check headers like Sec-Fetch-Site to determine if a request originated from your own domain or a third party.
  • Avoid GET for State Changes: Only use GET requests for retrieving data. Never use them for actions like deleting records or updating profiles.
  • Require Custom Headers: For JavaScript-heavy sites using APIs, requiring a custom HTTP header (like X-Requested-With) can block simple cross-origin requests.

Common mistakes

Mistake: Relying on HTTPS for protection. Fix: HTTPS encrypts the connection but does nothing to verify if the user actually intended to send the request. Use tokens alongside encryption.

Mistake: Only accepting POST requests. Fix: Attackers can easily forge POST requests using hidden HTML forms and auto-submitting JavaScript. You must still use tokens for POST actions.

Mistake: Using secret cookies. Fix: All cookies associated with a domain are sent automatically by the browser, whether they are "secret" or not.

Mistake: Multi-step transactions. Fix: If the steps of a transaction are predictable, an attacker can script the forgery to follow the entire sequence.

Examples

The Fake Image (GET Request)

An attacker places a transparent, 0x0 pixel image in an email. The image source is actually a command: <img src="http://bank.com/transfer.do?acct=MARIA&amount=100000" width="0" height="0"> When the victim opens the email while logged into their bank, the browser attempts to "load" the image, triggering the transfer.

The Auto-Submitting Form (POST Request)

If a site requires a POST request, an attacker can host a hidden form on their own website:

<body onload="document.forms[0].submit()">
  <form action="http://bank.com/transfer.do" method="POST">
    <input type="hidden" name="acct" value="MARIA">
    <input type="hidden" name="amount" value="100000">
  </form>
</body>

The victim visits the attacker’s site, and the form submits automatically in the background.

FAQ

Can CSRF be used to steal data? Usually, no. CSRF is a "blind" attack. The attacker induces an action but cannot see the response from the server. However, through "Login CSRF," they can trick you into using an account they control, later logging in themselves to see the data or history you generated while using that account.

Is Same-Origin Policy (SOP) enough to stop CSRF? No. SOP prevents a script on site-a.com from reading data from site-b.com, but it does not prevent the browser from sending a request to site-b.com. CSRF exploits the sending of the request, not the reading of the response.

How is CSRF different from XSS? XSS involves injecting malicious script into a trusted website to steal user data (like cookies). CSRF involves a malicious website tricking a user's browser into performing an action on a different, trusted website. Essentially, XSS exploits the user’s trust in a site, while CSRF exploits the site’s trust in the browser.

How do I know if my site is vulnerable? Your site is likely at risk if it performs sensitive actions (like changing passwords or making purchases) using only session cookies for authentication, without requiring an unpredictable token or verifying the request origin.

Does logging out help? Yes. Since CSRF relies on an active, authenticated session, logging out of sensitive sites before visiting other parts of the web reduces the "attack window."

Start Your SEO Research in Seconds

5 free searches/day • No credit card needed • Access all features