What is a CSRF token? What is its importance and how does it work?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Cross-Site Request Forgery (CSRF) is a type of security attack that tricks a user into executing unwanted actions on a web application in which they're currently authenticated. CSRF attacks specifically target state-changing requests, not theft of data, since the attacker has no way to see the response to the forged request.
Understanding CSRF
To understand CSRF, consider a simple scenario where a user is logged into a bank account, and a malicious website tries to submit a fund transfer form to the bank's server from the user’s browser without their knowledge. If the bank's website does not have protections against CSRF, the server might process the request as legitimate.
Role of a CSRF Token
A CSRF token is a unique, secret, unpredictable value that is generated by the server-side application and transmitted to the client in such a way that it is included in a subsequent HTTP request made by the client. When the later request is made, the server can then determine if the request is legitimate or not.
How CSRF Tokens Work
- Token Generation: When a form is generated, the server creates a CSRF token and embeds it in the form as a hidden field. The token is also saved somewhere on the server, often in the user session.
- Token Submission: When the user submits the form, the token is sent as part of the request.
- Token Verification: The server then compares the token received from the client with the one stored in the session. If they match, the request is considered valid. If not, the request is rejected because it may be a CSRF attack.
CSRF Token Attributes
- Uniqueness: Each token must be unique to each user session, ensuring that a token generated for one user’s session is not valid for another.
- Predictability: Tokens should be sufficiently random and protected from prediction attacks.
- Mapping: The token should be properly mapped to the user session to ensure proper verification.
Implementation and Considerations
- Statelessness Issue: In stateless application architectures, such as those using RESTful APIs, CSRF tokens need to be handled differently, typically using other strategies such as Double Submit Cookies or custom headers.
- Token Storage: It's essential to securely store the CSRF token on the server using user sessions or other mechanisms.
- Site Architecture: Proper understanding and mapping of which parts of the application are vulnerable to CSRF are crucial for implementing CSRF tokens effectively.
Example
Here is a simple example of what the HTML of a CSRF-protected form might look like:
Table: Summary of Key Points about CSRF Tokens
| Feature | Description |
| Purpose | To prevent CSRF attacks by verifying the authenticity of the request origin. |
| Functionality | Token is tied to the user's session and must match server and client tokens. |
| Placement | Embedded in forms as hidden inputs. |
| Security | Tokens should be randomly generated and hard to guess. |
| Usability | Tokens should not affect the user's interaction with the application. |
Additional Details
Double Submit Cookies
Double Submit Cookies is another method used to mitigate CSRF where cookies are used to store the CSRF tokens. A cookie with the CSRF token is sent to the client, and in subsequent requests, the browser sends the cookie back, and the token is also sent via other means (like a hidden form field). The server then compares both tokens.
Custom Headers
Using custom headers, such as X-CSRF-Token, is another method to mitigate CSRF in API-driven applications. The client must include this header with the token in its HTTP request, which an attacker cannot usually mock in a CSRF attack because of the same-origin policy.
The CSRF token is a vital security measure for modern web applications. Implementing it correctly ensures that actions performed on the site are genuinely initiated by the authenticated user, thus safeguarding against potential CSRF attacks.

