Information Security Stack Exchange is a question and answer site for information security professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 41 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

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 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

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

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

One reason for wanting to change the CSRF token per request is to mitigate compression leaking of the token. What I mean by that is that if the attacker Eve can inject data on a page containing the token where the page is sent compressed Eve can then guess at the first character of the string receiving a smaller dataset for the page knowing she guessed right then go on to the next character.

This is similar to a timing attack.

At the moment there is no reason (that I know of) to change the token if it is only sent in the HTTP headers.

Another method is to change the token as soon as a failed request is discovered, but this could have problems with the user not being able to submit any form.

share|improve this answer

It isn't very known fact, but normal string comparison is vulnerable to timing attacks (such as this one. Long story short, normal string comparison operations (== or ===) will compare two strings character-by character from left to right and return false once they've encountered any character that's not equal at a given position in both strings. This gives a minuscule difference in timing that's been proven to be detectable.

By using a timing attack to try every possible character for each position, it is possible to work out what the actual token is. By creating a new token every time a request with a token is submitted, this attack can be prevented.

Of course, this only works if you also recreate the token if an invalid token has been submitted, which may be undesirable. Therefore, you're better off fixing this by using a time-insensitive string comparison function such as this one.

share|improve this answer
Okay, but i have to note in the examples and paper you've listed a theoretical attacker could measure the CPU clock time very precisely. The machine was under no other load, and many languages use precalculated hashcodes for string comparison before performing a character by character check. – Philipp Jul 22 '15 at 10:10
Constant time compares can and should be used to mitigate this (loop over all chars and xor a difference flag). – eckes Jan 14 at 21:46

Further to the other answers, it might be wise to refresh the token too if your server is susceptible to the BREACH attack.

This requires the following three conditions:

  • Be served from a server that uses HTTP-level compression
  • Reflect user-input in HTTP response bodies
  • Reflect a secret (such as a CSRF token) in HTTP response bodies

To mitigate BREACH you would need to refresh the CSRF token on the GET request that loads a form to invalidate all previous tokens. This way, a MITM (Man-In-The-Middle) creating additional requests to discover the token in the page will get a different token each time. This does mean that the real user will not be able to submit the form in a MITM situation.

Of course you need the other two conditions to hold true also. It would probably be easier to maintain a CSRF token per session and disable HTTP-level compression for any pages that serve forms.

share|improve this answer

If the same token is used for the entire session then then there is probability of attacker can hijack the token using XSS and use the victim's session to do some malicious activities like changing the password .

They can cange passwords by lodaing an iframe into the site you are using.

So it is better to use one token per request to avoid the attacker to gain access to that token.

share|improve this answer
An attacker using XSS can get access to new tokens, this is what the whole question is about. See also the comments on – Philipp Jul 28 '15 at 9:35

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.