Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

What Is Cross Site Request Forgery? Explained

· 7 min read · Home Tech

In this article
  1. Background: How CSRF Became a Known Threat
  2. How CSRF Works: The Mechanics
  3. Common CSRF Vulnerabilities and Misconceptions
  4. How to Prevent CSRF: Effective Defenses
  5. CSRF vs. XSS: Key Differences
  6. Tools and Testing for CSRF
  7. Frequently Asked Questions

Cross-site request forgery (CSRF, pronounced "see-surf") is a web security vulnerability where an attacker tricks a logged-in user's browser into sending an unauthorized request to a trusted site. The attack works because the browser automatically attaches cookies (or other authentication credentials) to every request sent to that site, so the server cannot tell whether the request came from the user's own action or from a malicious page. The standard defense is a CSRF token: a unique, secret value embedded in forms and verified by the server on every state-changing request.

Background: How CSRF Became a Known Threat

The term "cross-site request forgery" was coined in the early 2000s by security researcher Peter Watkins, who documented the attack in a 2001 paper. The vulnerability predates that name—older references called it "session riding" or "one-click attack." It became widely recognized after several high-profile incidents, including a 2008 attack on Netflix that let attackers change a victim's shipping address and add movies to their queue.

CSRF is distinct from cross-site scripting (XSS). In XSS, the attacker injects malicious script into a trusted page; in CSRF, the attacker forges a request from the user's browser without injecting any code into the target site. The attack exploits the browser's automatic inclusion of credentials (cookies, HTTP basic auth, client certificates) rather than a flaw in the target site's input validation.

How CSRF Works: The Mechanics

A typical CSRF attack has three parts: the victim's active session on a trusted site, a malicious page controlled by the attacker, and a state-changing request that the trusted site accepts without verifying the user's intent.

  1. Victim logs in to a trusted site (e.g., a bank or email provider). The site sets an authentication cookie in the browser.
  2. Victim visits the attacker's page (via a phishing email, malicious ad, or compromised forum). The page contains an HTML form, image tag, or JavaScript that automatically submits a request to the trusted site.
  3. Browser sends the request with the victim's cookies attached, because the request is made to the trusted site's domain. The server processes it as if the victim intended it.

Example: A bank's transfer form at bank.com/transfer accepts a POST request with parameters amount=1000 and to=attacker. The attacker crafts a page with a hidden form that auto-submits to that URL. When the victim loads the page, the browser sends the POST with the victim's session cookie, and the bank transfers the money.

GET requests are especially dangerous because they can be triggered by a simple <img src="bank.com/transfer?amount=1000&to=attacker"> tag. Modern web frameworks often require POST, PUT, or DELETE for state-changing actions, but CSRF can still target those if the server doesn't validate a token.

Common CSRF Vulnerabilities and Misconceptions

One common misconception is that CSRF only affects sites that use cookies. In reality, any site that relies on automatic credential attachment—including HTTP basic authentication, client-side TLS certificates, or IP-based sessions—is vulnerable. Another misconception is that CSRF is only a problem for banks or social media; any site that lets users change their profile, password, or settings is a target.

Real-world examples of CSRF attacks include:

  • Changing a user's email address or password on a webmail service.
  • Posting unwanted content on a social media account (the 2008 Netflix attack is a classic).
  • Transferring funds or making purchases on an e-commerce site.
  • Modifying router or IoT device settings if the admin panel is accessible over the internet and uses cookie-based auth.

A common related question is whether CSRF can be prevented by using HTTPS. HTTPS encrypts the traffic but does not stop CSRF—the attacker's page can still send a request over HTTPS, and the browser will attach the cookies. HTTPS protects against man-in-the-middle eavesdropping, not against forged requests.

How to Prevent CSRF: Effective Defenses

The primary defense is the synchronizer token pattern (CSRF token). The server generates a random, unpredictable token for each session (or each form) and embeds it as a hidden field in HTML forms. When the form is submitted, the server compares the submitted token with the one stored in the session. If they don't match, the request is rejected. The token must be tied to the user's session and not guessable by an attacker.

Additional defenses include:

  • SameSite cookies: Set the SameSite attribute on session cookies to Lax or Strict. Lax blocks cookies on cross-site POST requests but allows them on top-level GET navigations; Strict blocks all cross-site requests. This is a strong modern defense but not universal—older browsers may ignore it.
  • Custom request headers: Require a custom header (e.g., X-Requested-With) for AJAX requests. Since cross-site requests cannot set custom headers without CORS permission, this blocks many CSRF attempts. However, it does not protect against form-based attacks.
  • Double-submit cookie: Send a random value both as a cookie and as a request parameter; the server checks they match. This works without server-side session state but is weaker if the attacker can set cookies (e.g., via a subdomain vulnerability).
  • Origin/Referer header validation: Check the Origin or Referer header to ensure the request came from the same site. This is less reliable because headers can be stripped or spoofed in some scenarios.

For users, the practical defense is to log out of sensitive sites when not using them, avoid clicking suspicious links while logged in, and use browser extensions that block third-party requests. However, the primary responsibility lies with the website developer.

CSRF vs. XSS: Key Differences

CSRF and XSS are often confused, but they are fundamentally different:

  • CSRF forges a request using the victim's credentials; the attacker does not see the response. It targets state-changing actions (transfer, delete, change password).
  • XSS injects executable script into the trusted page; the attacker can read data, steal cookies, and perform actions on behalf of the user. XSS can be used to bypass CSRF defenses because the injected script can read the CSRF token from the page.

In practice, XSS is more severe because it gives the attacker full control over the victim's session. CSRF is limited to the specific actions the attacker can forge. However, CSRF is easier to exploit because it doesn't require finding an injection point—just a logged-in user who visits a malicious page.

Tools and Testing for CSRF

Developers can test for CSRF using browser developer tools or automated scanners. The simplest manual test is to log into a site, open the developer console, and try to submit a state-changing request without a CSRF token (e.g., by crafting a fetch request). If the server accepts it, the site is vulnerable.

Common automated tools include OWASP ZAP and Burp Suite, which have CSRF scanner plugins. These tools send a request without a token and check if the server responds with a success status. However, they may produce false positives if the site uses custom token validation that the scanner doesn't recognize.

For home users, there's no practical way to test a site's CSRF defenses—it's the site's responsibility. But you can reduce your risk by using a modern browser (which enforces SameSite cookies by default) and by logging out of sites when you're done.

Frequently Asked Questions

Can CSRF happen through an email link?

Yes, if you're logged into a site and click a link in an email that points to a state-changing URL (e.g., bank.com/transfer?amount=1000&to=attacker), the browser will send the request with your cookies. This is why many sites now use POST for state changes and require a token.

Does using a VPN or incognito mode protect against CSRF?

No. VPNs change your IP address, and incognito mode starts a fresh session, but neither stops the browser from attaching cookies to requests. If you're logged into a site in incognito mode and visit a malicious page, the CSRF attack still works.

Is CSRF the same as a man-in-the-middle attack?

No. A man-in-the-middle (MITM) attack intercepts and modifies traffic between the user and server. CSRF doesn't intercept anything—it uses the user's own browser to send a forged request. MITM can steal credentials, while CSRF abuses an existing session.

Can a CSRF token be stolen?

Yes, if the site has an XSS vulnerability, the attacker can run script in the user's browser and read the token from the page. That's why CSRF tokens must be kept secret and why XSS is a higher-priority fix. Also, tokens should be unpredictable and tied to the session, not static.

Do mobile apps need CSRF protection?

Native mobile apps typically don't use cookies; they use API tokens or OAuth, which are not automatically attached by the browser. However, if a mobile app uses a webview that loads a web page, CSRF can apply. Most modern APIs require custom headers or tokens, which already provide CSRF protection.

Is CSRF a thing of the past with modern frameworks?

No. Modern frameworks like Django, Rails, and Spring include built-in CSRF protection, but it's only effective if the developer enables it and uses it correctly. Many sites still have CSRF vulnerabilities due to misconfiguration, such as disabling the token for API endpoints or using GET for state changes.