Securing applications against CSRF is a developer responsibility and it should not be fully trusted to any third party modules.
We do not claim that this module is able to protect an application without a clear study of CSRF, its impact and the needed mitigations.
fastify-csrf provides a series of utilities that developers can use to secure their application.
We recommend using fastify-helmet to implement some of those mitigations.
Security is always a tradeoff between risk mitigation, functionality, and developer experience.
As a result we will not consider a report of a plugin default configuration option as security
vulnerability that might be unsafe in certain scenarios as long as this module provides a
way to provide full mitigation through configuration.
If you use fastify-csrf with fastify-cookie, the CSRF secret will be added to the response cookies.
By default, the cookie used will be named _csrf, but you can rename it via the cookieKey option.
When cookieOpts are provided, they override the default options. Make sure you restore any of the default options which provide sensible and secure defaults.
fastify.register(require('fastify-cookie'))fastify.register(require('fastify-csrf'))// if you want to sign cookies:fastify.register(require('fastify-cookie'), { secret }) // See following section to ensure securityfastify.register(require('fastify-csrf'), { cookieOpts: { signed: true } })// generate a tokenfastify.route({ method: 'GET', path: '/', handler: async (req, reply) => { const token = await reply.generateCsrf()
If you use fastify-csrf with fastify-session, the CSRF secret will be added to the session.
By default, the key used will be named _csrf, but you can rename it via the sessionKey option.
If you use fastify-csrf with fastify-secure-session, the CSRF secret will be added to the session.
By default, the key used will be named _csrf, but you can rename it via the sessionKey option.
Apart from these safeguards, it is extremely important to use HTTPS for your website/app to avoid a bunch of other potential security issues like MITM etc.
API
Module Options
Options
Description
cookieKey
The name of the cookie where the CSRF secret will be stored, default _csrf.
The option userInfo is required if getUserInfo has been specified in the module option.
The provided userInfo is hashed inside the csrf token and it is not directly exposed.
This option is needed to protect against cookie tossing.
fastify.csrfProtection(request, reply, next)
A hook that you can use for protecting routes or entire plugins from CSRF attacks.
Generally, we recommend to use the onRequest hook, but if you are sending the token
via the body, then you should use preValidation or preHandler.
// protect the entire pluginfastify.addHook('onRequest', fastify.csrfProtection)// protect a single routefastify.route({ method: 'POST', path: '/', onRequest: fastify.csrfProtection, handler: async (req, reply) => { return req.body }})
You can configure the function to read the CSRF token via the getToken option, by default the following is used: