What is xsrf token

Last updated: April 1, 2026

Quick Answer: An XSRF token (Cross-Site Request Forgery token) is a security measure that prevents unauthorized requests to web applications by verifying that form submissions come from legitimate users.

Key Facts

Understanding XSRF Attacks

A Cross-Site Request Forgery (XSRF or CSRF) attack occurs when an attacker tricks an authenticated user into performing unwanted actions on a website. For example, if you're logged into your bank account and visit a malicious website simultaneously, that site could potentially make unauthorized transfers using your authenticated session. XSRF tokens prevent this vulnerability.

How XSRF Tokens Work

The XSRF token mechanism is straightforward:

This process ensures that the form was actually generated by the legitimate website, not by an attacker's malicious page.

Token Storage and Validation

The server typically stores XSRF tokens in the user's session or as a secure cookie. Each form request receives a fresh token, and tokens are usually single-use or have a limited lifespan. When a form is submitted, the server compares the submitted token against its stored value. This comparison is case-sensitive and exact to prevent manipulation.

Implementation in Web Applications

Modern web frameworks typically provide built-in XSRF protection. Developers can enable this with minimal code. For example, Django, Flask, and ASP.NET all include automatic XSRF token generation and validation. When building custom applications, developers must manually implement token generation, storage, and validation.

XSRF vs CORS vs Authentication

XSRF tokens are distinct from other security measures. Authentication tokens verify who a user is, while XSRF tokens verify that a request originated from your own website. CORS (Cross-Origin Resource Sharing) policies control which external websites can access your resources. A complete security strategy uses all three mechanisms together.

Related Questions

Why can't XSRF attacks work on read-only requests?

XSRF attacks typically target state-changing actions like password changes or money transfers. Read-only requests (like viewing a page) don't modify data, so attackers gain no benefit from forging these requests, making XSRF tokens unnecessary for GET requests.

What happens if an XSRF token is missing or invalid?

The web server rejects the request and typically displays an error message. The action is not executed, and the user may need to reload the page and resubmit the form with a valid token to complete their request.

Can attackers steal or predict XSRF tokens?

XSRF tokens are generated using cryptographically secure random values, making them virtually impossible to predict. However, if an attacker gains access to the token through XSS vulnerability, they could use it. This is why XSRF protection is combined with XSS prevention measures.

Sources

  1. OWASP - Cross-Site Request Forgery CC-BY-SA-4.0
  2. Wikipedia - Cross-Site Request Forgery CC-BY-SA-4.0