About this tool
Cross-site request forgery (CSRF) tricks a logged-in user’s browser into sending a request they didn’t intend, such as a form on another site that silently posts to /transfer on yours. The browser attaches your session cookie, so without protection the request succeeds. A CSRF token is a random value your server puts in its own pages and checks on every state-changing request; another site can’t read it, so it can’t forge it.
Use this generator for test fixtures, API tests, static configuration and learning. In a live app, your server must create a fresh token for each session.
How CSRF tokens work
- When a session starts, the server creates a random token (at least 128 bits) and stores it in the session.
- Every HTML form includes it as a hidden field; JavaScript sends it in a header such as
X-CSRF-Token. - On each
POST,PUT,PATCHorDELETE, the server compares the submitted token with the stored one using a constant-time comparison, and rejects the request if they differ.
Do I still need CSRF tokens?
- SameSite cookies (
Laxis the default in modern browsers) block most cross-site form posts, but not all: older browsers, some subdomain set-ups and top-levelGETrequests with side effects remain at risk. OWASP recommends tokens as well. - APIs that use a bearer token in the
Authorizationheader, not cookies, don’t need CSRF tokens, because browsers never attach that header automatically. - Most frameworks do this for you: Django, Rails, Laravel and ASP.NET include CSRF protection; in Express, add a maintained middleware. Prefer the built-in protection over your own.
FAQ
What is a CSRF token?
A random, unpredictable value that the server embeds in its forms and checks on each state-changing request, proving the request came from the site’s own pages.
How long should a CSRF token be?
At least 128 bits of randomness; 256 bits is common. It must come from a cryptographically secure generator.
Should CSRF tokens be generated in the browser?
No: in a real app the server creates the token and stores it in the session. These tokens are for tests, fixtures and learning.
Do SameSite cookies replace CSRF tokens?
They block many attacks, but not every case. OWASP recommends using CSRF tokens together with SameSite cookies.