Changelog
4.0.3 (2025-05-27)
generateCsrfToken
will now always check if the existing token is valid before returning it. This validation is only derived from the request cookie, this way GET
requests are not expected to include the CSRF token to ensure token reuse, this was a bug and not the intended/expected behavior.
If the CSRF token container in the request is somehow invalid when generateCsrfToken
is called, this will be silently ignored and a new valid CSRF token will be generated and returned. If validateOnReuse
is set to true, an error will be thrown instead.
Changelog
4.0.0 (2025-04-27)
This list may not be an exhaustive list of breaking changes, for more information consult the version 3 -> 4 upgrade guide and the updated configuration documentation in the README.
createHmac
, the format has changed significantly, see the CSRF token format section of the upgrade guide.getSessionIdentifier
is now required and must return a unique identifier per-request (and per-session) - this is an essential part of CSRF token securitygetTokenFromRequest
renamed to getCsrfTokenFromRequest
generateToken
renamed to generateCsrfToken
overwrite
and validateOnReuse
parameters for generateCsrfToken
have been merged into a single object parameter which also accepts cookieOptions
: generateCsrfToken(req, res, options);
validateOnReuse
is now false
cookieOptions.sameSite
is now strict
cookieOptions.signed
is no longer available, CSRF tokens are inherently signed, this is redundantdelimiter
option removed, csrfTokenDelimiter
and messageDelimiter
are now used for the respective purposesigned
option in cookieOptions
config option removed (redundant), csrf tokens generated by csrf-csrf are inherently signedsize
config option now sets the size of the message used to construct the hmac, now defaults to 32
instead of 64
, this is combined with the return value of getSessionIdentifier
to construct the hmac payloadCsrfTokenCookieOverrides
renamed to CsrfTokenCookieOptions
CsrfTokenCreator
renamed to CsrfTokenGenerator
doubleCsrfProtection
renamed to DoubleCsrfProtection
RequestMethod
renamed to CsrfRequestMethod
CsrfIgnoredMethods
renamed to CsrfIgnoredRequestMethods
Changelog
3.0.7 (2024-09-21)
Added the getSessionIdentifier
parameter to the csrf-csrf
configuration. By providing the getSessionIdentifier
callback, generated tokens will only be valid for the original session identifier they were generated for.
For example: (req) => req.session.id
The token will now be signed with the session id included, this means a generated CSRF token will only be valid for the session it was generated for. This also means that if you rotate your sessions (which you should) you will also need to generate a new CSRF token for the session after rotating it.