Vulnerabilities

What is Cross-Site Request Forgery (CSRF)?

CSRF is an attack that tricks authenticated users into unknowingly submitting requests to a web application — executing actions they did not intend using their existing session credentials.

By the Hyrax team·4 min read·May 1, 2026
TL;DR
  1. 1.Definition
  2. 2.How CSRF Works
  3. 3.CSRF vs. XSS
  4. 4.Prevention
  5. 5.CSRF and Autonomous Code Governance

Definition

Cross-site request forgery (CSRF) is an attack that tricks an authenticated user into submitting a request to a web application they are logged into, without their knowledge or intent. Because the browser automatically includes session cookies with every request to a domain, the forged request carries the victim's credentials and the server has no way to distinguish it from a legitimate action.

CSRF exploits the trust a web application has in its users' browsers. The attack does not steal credentials — it uses existing authentication to perform unauthorized actions on behalf of the victim.

How CSRF Works

The attack follows a simple pattern:

  1. The victim is authenticated to a target application (their browser holds a valid session cookie).
  2. The victim visits a malicious page (via a phishing link, a compromised ad, or an attacker-controlled site).
  3. The malicious page contains a hidden form or image tag targeting the vulnerable application.
  4. The browser automatically submits the request, including the session cookie.
  5. The target application processes the request as if the authenticated user initiated it.

Classic examples: transferring funds from a banking application, changing an email address, adding an admin user, or deleting data — any state-changing operation accessible to the victim.

CSRF vs. XSS

PropertyCSRFXSS
Attack originAttacker-controlled third-party siteInjected script on the target site itself
Requires authenticationYes — exploits existing sessionNo — targets any visitor
What attacker controlsWhich requests are madeArbitrary code in the browser
Primary defenseCSRF tokens, SameSite cookiesOutput encoding, CSP

Prevention

CSRF tokens

The application generates a unique, unpredictable token per session (or per form submission) and includes it as a hidden field in every state-changing form. On submission, the server validates the token. An attacker on a different origin cannot read the token and therefore cannot forge a valid request.

SameSite cookie attribute

Setting cookies with SameSite=Strict or SameSite=Lax prevents browsers from sending them with cross-site requests. SameSite=Strict blocks the cookie on all cross-origin requests; Lax blocks it on cross-origin non-safe methods (POST, PUT, DELETE). This is now the most effective and easiest-to-implement defense.

Origin and Referer header validation

The server checks that the Origin or Referer header of state-changing requests matches the expected application origin. This is a weaker defense than tokens because headers can be absent, but it adds a layer of defense-in-depth.

Custom request headers

Requiring a custom header (X-Requested-With: XMLHttpRequest) that cannot be set by cross-origin forms. Browsers enforce the same-origin policy on custom headers in cross-origin requests. Only valid for AJAX requests, not form submissions.

CSRF and Autonomous Code Governance

Hydra detects missing CSRF protection on state-changing endpoints by analyzing route definitions, HTTP method usage, and the presence of CSRF token validation middleware. For frameworks with built-in CSRF protection (Rails, Django, Laravel), Hydra identifies endpoints where protection has been explicitly disabled or bypassed. Fixes add the appropriate token validation or SameSite cookie configuration for the framework in use.

Frequently Asked Questions

Is CSRF still relevant with modern browsers?

Yes, though modern browsers' default SameSite=Lax cookie behavior reduces the attack surface significantly. However, SameSite=Lax still allows GET-based CSRF, many sites opt out of SameSite protection for compatibility, and older browser versions lack the default. CSRF tokens remain a required defense for any state-changing operation.

Do SPAs need CSRF protection?

SPAs using token-based authentication (JWT in Authorization headers) are naturally CSRF-resistant because browsers do not automatically include Authorization headers in cross-site requests. SPAs that rely on session cookies for authentication need CSRF protection like any other cookie-authenticated application.

Can HTTPS prevent CSRF?

No. CSRF exploits the browser's automatic cookie inclusion, which happens regardless of whether the connection is encrypted. HTTPS protects data in transit; it does not protect against requests that the browser makes willingly on the user's behalf.

Stop flagging. Start fixing.

Hyrax reviews your pull requests, remediates issues autonomously, and closes the ticket.

Join the waitlist