Take the 2-minute tour ×
Information Security Stack Exchange is a question and answer site for Information security professionals. It's 100% free, no registration required.

In many tutorials and guides I see that an CSRF token should be refreshed per request, my question is why do I have to do this? Isn't a single CSRF token per session much easier than regenerating one per request and keeping track of the ones requested?

Regenerating the token on a per-request basis doesn't seem to add anything to security, that a per-session token doesn't already do. The only argument seems to be XSS protection, but this doesn't apply as when you have a XSS vulnerability the script could read out the new tokens anyway.

What are the benefits of generating new tokens per request?

share|improve this question

4 Answers 4

up vote 16 down vote accepted

For the reasons already discussed, it is not necessary to generate a new token per request. It brings almost zero security advantage, and it costs you in terms of usability: with only one token valid at once, the user will not be able to navigate the webapp normally. For example if they hit the 'back' button and submit the form with new values, the submission will fail, and likely greet them with some hostile error message. If they try to open a resource in a second tab, they'll find the session randomly breaks in one or both tabs. It is usually not worth maiming your application's usability to satisfy this pointless requirement.

There is one place where it is worth issuing a new CSRF token, though: on principal-change inside a session. That is, primarily, at login. This is to prevent a session fixation attack leading to a CSRF attack possibility.

For example: attacker accesses the site and generates a new session. They take the session ID and inject it into a victim's browser (eg via writing cookie from a vulnerable neighbour domain, or using another vulnerability like jsessionid URLs), and also inject the CSRF token into a form in the victim's browser. They wait for the victim to log in with that form, and then use another form post to get the victim to perform an action with the still-live CSRF token.

To prevent this, invalidate the CSRF token and issue a new one in the places (like login) that you're already doing the same to the session ID to prevent session fixation attacks.

share|improve this answer

XSS can be used to read a CSRF token, even if it is a single submit token, that is child's play. Its likely that this recommendation of a single submit token came from someone who doesn't understand CSRF.

The only reason to use a "single submit token" is if you want to prevent the user from accidentally clicking submit twice. A good use of this is to prevent the user from clicking "checkout" twice and accidentally charging the customer twice.

A CAPTCHA or asking for the users current password can be used as an anti-csrf measure that cannot be bypassed by XSS.

I recommend reading the CSRF Prevention Cheat Sheet.

share|improve this answer
I don't see how a CAPTCHA or entering the password would be secure in the face of XSS. It just forces the attacker to jump through a few additional hoops. –  CodesInChaos Oct 20 '12 at 19:00
@CodesInChaos the attacker would have to know the password, and then its a moot point. –  rook Oct 20 '12 at 19:53
If the user enters the password at some point after the xss the attacker can probably steal it. So re-entering the password helps a bit, but not much. –  CodesInChaos Oct 20 '12 at 20:12
@CodesInChaos That works fine on paper, but this proposed attack is considerably less likely to succeed in a real world scenario. –  rook Oct 20 '12 at 20:28
@CodesInChaos you are correct that a CAPTCHA would be irrelevant to this. On the other hand, reauthentication (selectively on sensitive actions) is one of the best solutions for CSRF, and should be used more. Also see Schneier's discussion about "transaction authentication", which is similar, though not identical. –  AviD Oct 21 '12 at 11:12

Overview. The standard advice is to use a unique CSRF token that is unique for each request. Why? Because a per-request token is a bit more resilient to certain kinds of implementation errors than a per-session token. This makes per-request tokens arguably the best choice for new web application development. Also, no security auditor is going to hassle you about using a per-request CSRF token.

If you're a web application developer, this is all you need to know, and you can stop reading here. But if you're a security expert wondering about the detailed rationale behind this advice, or wondering about just how great the risk is if you do use a per-session token, read on....

Digging in a bit deeper. The truth is that, if you don't have any other vulnerabilities in your web site, a single CSRF token per session is OK. There's no reason why you necessarily have to generate a fresh CSRF token per request.

This is demonstrated by the fact that you'll also find reputable security experts who say that another reasonable way to defend against CSRF is to use cookie double-submission: in other words, you use some client-side Javascript which computes a hash of the session cookie and adds that to each POST request, treating the hash as the CSRF token. You can see that this essentially generates on-the-fly a CSRF token that is the same for the entire session.

Of course, I know the argument why some people might recommend generating a new CSRF token for every request. They are thinking, if you also have a XSS vulnerability on your website, then if you use a single CSRF token per session it will be easy to use XSS to recover the CSRF token, whereas if you generate a new CSRF token per request, it will take more work to recover the CSRF token. Personally, I don't find this a terribly compelling argument. If you have a XSS vulnerability on your site, it's still possible to recover CSRF tokens even if you generate a new CSRF token for every request, it just takes a few extra lines of malicious Javascript. Either way, if you have a XSS vulnerability on your site and you face a serious, knowledgeable attacker, it's hard to guarantee security, no matter how you generate your CSRF tokens.

Overall, it can't hurt to generate a new CSRF token for every request. And maybe it's better to do it that way, just to get security auditors off your back. But if you already have a legacy application that uses a single CSRF token for the entire session, spending the money to convert it to generate a new CSRF token for each request probably wouldn't be super-high on my priority list: I bet I could find some other uses for that money and developer energy that would improve security even more.

share|improve this answer
I wouldnt call them "reputable security experts" if they recommend double cookie submission... XSS is not the only issue at hand for that. OWASP has a good rundown on this... –  AviD Oct 21 '12 at 11:15
@AviD, double submit cookies is perfectly fine. I've yet to see any proof that this approach is substantially more vulnerable than any other approach. Every technique has its vulnerabilities. This one is no different. –  Gili Jun 27 '14 at 3:35
@Gili the problem with double-submit cookies is that this typically is referring to the normal, already-existing session cookie - and this should not be accessible to JavaScript (e.g. using the HttpOnly flag). Using this technique may prevent the CSRF attack, but would open the session cookie to other issues, such as making it more vulnerable to possible XSS vulnerabilities. There are techniques which work just fine, without causing other, separate vulnerabilities. –  AviD Jun 27 '14 at 8:14
@AviD, as mentioned in security.stackexchange.com/q/61110/5002 OWASP instructs developers to use a separate cookie for the CSRF token precisely for this reason. You can keep the HttpOnly flag on the session cookie. –  Gili Jun 27 '14 at 8:24
@Gili, re-read AviD's comments. Don't miss his use of the word "typically". Double-cookie submission is typically understood to mean re-using the session cookie. If someone is recommending double-cookie submission, and they don't have some explicit disclaimers to the contrary, that means they are recommending re-using the session cookie. I think AviD covered this with his comments above. –  D.W. Jun 27 '14 at 17:15

If the token is the same across the entire session, it may be possible for an attacker to leak a token from one page and use it for a different action.

For example, you could use an iframe to load a page, then extract the token using an XSS vulnerability. From there, you could use that token to submit a password change form

Using a per-request token instead of a session-wide one makes it more difficult, but it doesn't prevent CSRF. An attacker can simply leverage an XSS to read the token from the page, then fire it off. However, if the token is global rather than restricted to that individual page, an attacker can target any page to steal the token. Using separate tokens for each request makes this much harder.

share|improve this answer
Theres no way someone can extract a anything from iframe, this would break the same origin policy –  Philipp Oct 20 '12 at 14:46
@SkPhilipp Sorry, I meant to say via XSS. –  Polynomial Oct 20 '12 at 15:18
Yes, via XSS you could then load an iframe to a password change form, and you could extract that token, generating new tokens wouldn't make that any secure if you have an XSS vulnerability. –  Philipp Oct 20 '12 at 17:41
@Rook Huh? I never said using a single token was good... –  Polynomial Oct 21 '12 at 11:56
If you have an XSS vulnerability you can use an XHR to read any page and submit any request. Having a different token on each page doesn't help. –  rook Oct 21 '12 at 17:37

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.