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

csrf-guard

v0.1.1

Published

Simple Anti-CSRF Token implementation for Express applications.

Downloads

33

Readme

csrf-guard

Simple Anti-CSRF Token implementation for Express applications.

This package only uses Node.js native crypto module and no other dependency. I did my best to follow OWASP CSRF token best practices. Now it's your responsibilty to follow best practices for session management. I do recommend you read this article before anything else.

Disclaimer: This package is still under development, I do NOT recommend using it for production yet.

Installation

npm:

npm install csrf-guard

yarn:

yarn add csrf-guard

GitHub:

git clone https://github.com/venomaze/csrf-guard.git

Usage

First register the middleware:

const express = require('express');
const session = require('session');
const CSRFGuard = require('csrf-guard');

const app = express();

// DO NOT USE SESSION LIKE THIS!
app.use(
  session({
    secret: 'secret_key',
  })
);

app.use(
  new CSRFGuard({
    secret: 'secret_key', // Secret key is required
  })
);

Then you have access to two getToken and isTokenValid methods from request object.

  1. Generating a token (Remember you have to use csrf_token name for the token):
app.get('/', async (req, res) => {
  const token = await req.getToken();
  const form = `
    <form action="/test" method="POST">
      <input type="hidden" name="csrf_token" value="${token}" />
      <input type="text" name="username" />
      <input type="submit" />
    </form>
  `;

  res.send(form);
});
  1. Validating the token:
app.post('/test', (req, res) => {
  const isTokenValid = req.isTokenValid();
  const message = isTokenValid ? 'The token is valid.' : 'Token is NOT valid.';

  res.send(message);
});

Token generation methods

We have to options, the first one is Synchronizer Token Pattern and the second one is HMAC Based Token Pattern. You can read more about them here.

Synchronizer Token Pattern

To be able to use this method, you have to set synchronizer to true in options object. With this method you have access to forced mode which generates a new token even if there is one already. This is the default method.
Setting up:

app.use(
  new CSRFGuard({
    secret: 'secret_key',
    synchronizer: true,
  })
);

Generating token:

const token = await req.getToken(true); // Forced is set to true. This way you'll get a new token per request. (Default to false)

HMAC Based Token Pattern

To be able to use this method, you have to set synchronizer to false in options object. With this method you have access to expiryTime option which gives you this possibility to expire tokens even if the session id isn't changed. By default, tokens won't be expired until the session is regenerated.
Setting up:

app.use(
  new CSRFGuard({
    secret: 'secret_key',
    synchronizer: false,
    expiryTime: 5000, // Tokens will be expired after 5 seconds
  })
);

Generating token:

const token = req.getToken();