Change This answer is at least partially erroneous. Using a session identifier as a CSRF token can lead to session hijacking if, for example, links are copied + pasted. See Reply and comments by ircmaxell.
Yes, since the session identifier is random and associated with the user, it will be an acceptable form of CSRF protection.
However, it would be even safer to use a different random number, in order to prevent malicious JavaScript from stealing the session cookie (and session ID) ... But if I had to choose between โno CSRF tokenโ and โsession ID as CSRF token, I always select a session as a CSRF token.
The only potential problem with using session identifiers as CSRF tokens: if someone could steal a CSRF token, they could also capture a connected session ... But I can't think of a reasonable scenario where this could be a problem.
Now, from a discussion of Marc Bโs answer below: using nonce , other benefits will be provided (e.g. preventing form re-submission) ... But this is no more secure for CSRF attacks than the session identifier (with one caveat, which I mention in first paragraph of the second).
See also: CSRF Verifier: Is Session ID Safe?
David wolever
source share