A simple diagram of the Cross-Site Request Forgery (CSRF) risk path
Risk path: an input or context crosses a security boundary.
A simple three-step test path for Cross-Site Request Forgery (CSRF)
Testing path: establish a baseline, make one controlled change, and verify the effect.

Issue

Cross-Site Request Forgery (CSRF) is a flaw that lets an attacker cause a user’s browser to send an unwanted request to a website where the user is signed in. A browser may automatically attach the site’s session cookie to that request. If the server accepts the cookie as proof of the user’s identity without checking that the request was intentionally made through the site, it may perform the action using the user’s privileges. OWASP’s CSRF guidance explains this browser behavior and the need for server-side defenses.

A CSRF attack generally requires:

  • A user who is authenticated to the target application.
  • A state-changing function the user is allowed to perform.
  • A request the attacker can cause the user’s browser to send.
  • Missing or ineffective CSRF protections.

A state-changing request changes server-side data or settings, such as updating an email address or placing an order. These actions should use methods such as POST, PUT, PATCH, or DELETE. GET requests should not change state: they can be triggered by links, images, and other ordinary browser behavior. OWASP and MDN recommend keeping state changes out of GET requests.

CSRF is most relevant when authentication relies on credentials the browser sends automatically, especially session cookies. It is not established simply because an endpoint accepts a request without a token: the application may use another effective defense, or the request may not rely on a user’s browser session.

A common cause is an endpoint that trusts the session cookie but does not require a valid CSRF token or apply another suitable check. Other contributing causes include state changes through GET, incomplete protection of some endpoints, and a cookie policy that allows cross-site requests when the application assumes it blocks them.

The browser’s same-origin policy restricts scripts from reading certain cross-origin responses. It does not prevent all cross-origin requests: for example, HTML forms can submit requests to another site. Therefore, the same-origin policy alone is not CSRF protection. MDN’s explanation of CSRF and browser behavior describes this distinction.

Examples

Illustrative vulnerable pattern

A site changes a user’s notification setting through a request authenticated only by the session cookie:

POST /settings/notifications HTTP/1.1Host: bank.example.testCookie: session=dummy-session-valueContent-Type: application/x-www-form-urlencoded email_alerts=off

If the application accepts this request without validating a CSRF token or applying another effective defense, an attacker may be able to cause a signed-in user’s browser to submit it. The request’s presence alone does not prove the attack works; testing must confirm the browser sends the user’s credentials and that the server changes the setting.

Minimal illustrative cross-site form

<!-- Hypothetical test page on attacker.example.test --><form action="https://bank.example.test/settings/notifications"      method="post">  <input type="hidden" name="email_alerts" value="off">  <button type="submit">Submit test request</button></form>

This demonstrates how a browser form can submit a cross-origin request. It does not guarantee the browser will attach the target site’s cookie, that the server will accept the request, or that a real application has a vulnerability. Those depend on cookie policy, server-side checks, and the endpoint’s behavior.

Illustrative protected request

A server can issue an unpredictable CSRF token and require it on a state-changing request:

POST /settings/notifications HTTP/1.1Host: bank.example.testCookie: session=dummy-session-valueContent-Type: application/x-www-form-urlencoded email_alerts=off&csrf_token=dummy-unpredictable-token

The server must validate the token against the user’s session or another securely implemented token scheme. Merely placing a token field in a page or accepting a token parameter without checking it does not provide protection. OWASP recommends that synchronizer tokens be secret, unpredictable, and validated on the server; tokens should not be placed in URLs or exposed in logs. OWASP CSRF Prevention Cheat Sheet

Impact

A successful CSRF attack can perform actions available to the signed-in user. Depending on the endpoint, that might mean changing an account setting, posting content, or placing an order. The impact is limited by the user’s permissions and the application’s exposed functions. OWASP Web Security Testing Guide describes these prerequisites.

CSRF does not, by itself, let the attacker read the target site’s response or steal the user’s session cookie. Those outcomes require additional weaknesses or conditions, such as a separate cross-origin data exposure or script injection flaw. A response displayed in the victim’s browser is not automatically visible to the attacker.

Testing

1. Check whether a cross-site request changes state

Capture a reversible action in a test account, then submit the same action from a different origin without its CSRF token. Confirm the stored state and whether the browser sent the session cookie.

Example: A test account changes a preference. A successful response alone does not prove the preference changed.

POST /account/preferences HTTP/1.1
Host: app.example.test
Cookie: session=TEST_SESSION
Content-Type: application/x-www-form-urlencoded
 
theme=dark

2. Check token validation and session binding

Repeat the action with a missing, incorrect, and another test session’s token. Compare the resulting account state for each request.

Example: Account A and account B have different sessions. A token from B should not authorize A’s protected request.

POST /account/preferences HTTP/1.1
Host: app.example.test
Cookie: session=SESSION_A
Content-Type: application/x-www-form-urlencoded
 
theme=dark&csrf=TOKEN_FROM_SESSION_B

3. Compare methods, cookie policy, and origin checks

Check state-changing GET requests and supported content types. Repeat the browser flow with the site’s SameSite cookies and any Origin or Referer checks.

Example: An action may accept more than one request format. Test the formats the application actually supports.

Common false positives include testing an endpoint that does not change state, testing while logged out, using a request that does not carry the user’s authentication, and overlooking a separate defense such as origin validation. A missing token in one request is an indicator to investigate, not a confirmed finding.

Mitigation

Primary protections

  • Use the web framework’s built-in CSRF protection. Enable it for every state-changing request that relies on cookie-based authentication. Framework protections are generally safer than custom token code, but must be configured and applied to all relevant routes.
  • Validate a CSRF token on the server. For a synchronizer token, bind an unpredictable token to the user’s session and reject missing or invalid values. For stateless applications, use a well-implemented signed double-submit cookie pattern; do not rely on a plain cookie comparison that an attacker may be able to forge.
  • Keep state changes out of GET. Use appropriate non-safe methods and ensure method-override features do not let an attacker turn a cross-site GET into a state-changing operation.
  • For JavaScript APIs, require a non-simple request and configure CORS narrowly. A custom request header can help because an untrusted site generally cannot send it cross-origin without a successful CORS preflight. This works only if the server’s CORS policy does not grant that untrusted origin credentialed access. CORS configuration is not a replacement for server-side authorization.

Additional defenses

  • Set an explicit SameSite policy on session cookies. Use Strict where the application’s navigation flows allow it, or Lax where cross-site top-level navigation must continue to work. SameSite=None permits cross-site cookie sending and requires Secure in modern browsers. SameSite is defense in depth: it does not distinguish origins within the same site, and its behavior depends on the browser and application flow. MDN’s CSRF guide discusses these limits.
  • Validate Origin or, where appropriate, Referer. Compare the value against an explicit allowlist of trusted origins. Handle missing or null values deliberately; do not accept arbitrary suffix matches or treat an unrecognized value as trusted.
  • Use Fetch Metadata as an additional signal. For example, reject cross-site state-changing requests identified by Sec-Fetch-Site: cross-site, with a documented fallback for clients that omit Fetch Metadata headers. Make exceptions explicit for intended cross-origin flows. OWASP recommends treating this as part of a policy with appropriate fallbacks, not as a replacement for careful endpoint security. OWASP’s Fetch Metadata guidance
  • Require confirmation for high-impact actions. Reauthentication or a user-confirmation step can reduce risk for sensitive actions, but should supplement CSRF protections rather than replace them.

Retesting after a fix

Repeat the authorized tests for every state-changing endpoint. Confirm that valid requests still work, while requests with missing or invalid tokens—or requests rejected by the application’s origin or Fetch Metadata policy—leave the state unchanged. Also test expected login, navigation, mobile, and embedded-browser flows to identify compatibility problems. Review cookie attributes and CORS rules alongside the endpoint checks.