npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fastify-xsurf

v1.0.10

Published

A Fastify plugin that adds CSRF protection to HTTP routes

Downloads

22

Readme

fastify-xsurf

NPM Version Node.js Version NPM Downloads Node.js CI

A plugin for Fastify that adds CSRF protection to HTTP routes.

This plugin uses xsurf for generating CSRF tokens and their respective checksums. The anti-CSRF protection scheme follows this specification.

Installation

Via npm:

npm i fastify-xsurf

Via yarn:

yarn add fastify-xsurf

Usage API

This plugin does not decorate fastify's request or reply with new methods.

To validate CSRF tokens for all routes, simply register the plugin once after fastify-cookie. Only the routes using the HTTP methods specified in options.ignoredMethods will be excluded from validation.

With CommonJS require syntax:

fastify.register(require('fastify-cookie'));
fastify.register(require('fastify-xsurf'), {
  /*...options */
});

With ES module import syntax:

import cookiePlugin from 'fastify-cookie';
import csrfPlugin from 'fastify-xsurf';

fastify.register(cookiePlugin);
fastify.register(csrfPlugin, {
  /* ...options */
});

To validate CSRF tokens on only specific routes, set validateOnRequest: false on the options object:

fastify.register(csrfPlugin, {
  validateOnRequest: false,
});

A validate function will then be exposed at the property fastify.validateCsrf, which can be called on individual onRequest hooks:

fastify.route({
  method: 'POST',
  path: '/',
  onRequest: async (req, reply) => {
    fastify.validateCsrf(req, reply);
    /* ...other onRequest hook handlers */
  }
  handler: async (req, reply) => {
    // ...route handler
  }
})

On the browser client, ensure that the most recent value of the csrfToken cookie is used and copied to of the following headers (case sensitive) for each protected request: x-csrf-token, csrf-token, xsrf-token, or x-xsrf-token.

// Get token from cookies
const token = /(?:^|;\s*)csrfToken=([^;]+)/.exec(document.cookie)?.[1];
fetch(url, {
  method: 'POST',
  headers: {
    'x-csrf-token': token, // copy token to header
  },
});

Options (object)

Typescript users: options type is exported as CsrfPluginOptions.

options.secret (string)

Shared secret used to create the token checksum.

options.validateOnRequest (boolean)

Validate CSRF tokens on all requests to the server. If set to false, a validate function is exposed at the property fastify.validateCsrf, which can be called on an individual route's onRequest hook.

  • Optional
  • Default: true

options.ignoreMethods (string[])

Methods to ignore checking for a CSRF token.

  • Optional
  • Default: ['GET', 'HEAD', 'OPTIONS']

options.tokenKey (string)

Cookie key to store the CSRF token.

  • Optional
  • Default: 'csrfToken'

options.checksumKey (string)

Cookie key to store the token checksum. Defaults to 'csrfChecksum'

  • Optional
  • Default: 'csrfChecksum'

options.errorMessage (string)

Error message that will be sent in response to the client with invalid tokens.

  • Optional
  • Default: 'Invalid CSRF token provided.'

options.cookie (object)

Cookie options from fastify-cookie. Will override defaults on both the token and checksum cookies.

// csrfToken cookie
{
  path: '/',
  sameSite: 'strict',
}

// csrfChecksum cookie
{
  path: '/',
  sameSize: 'strict',
  httpOnly: true,
}

License

MIT License