1.0.7 • Published 8 months ago

express-csrf-protect v1.0.7

Weekly downloads
1
License
ISC
Repository
github
Last release
8 months ago

Express JS - Cross Site Request Forgery (CSRF)

Easily add CSRF protection to your express js application

Overview

This package is a simple yet effective middleware layer of CSRF protection to your express app. It creates a CSRF cookie for requests with methods GET, HEAD, TRACE and checks the CSRF cookie against a request header for POST, PUT, PATCH, DELETE. See these links for more details on this security implementation:

Installation

This is a Node.js module available through the npm registry. Installation is done using the npm install command:

$ npm install express-csrf-protect

Demo

const express = require('express');
const expressCsrf = require('express-csrf-protect');
 
const app = express();

app.use(expressCsrf.enable());

app.get('/', (request, response) => {
  return response.json({ message: 'admit one' });
});

app.post('/', (request, response) => {
  return response.json({ message: 'admit one' });
});

const PORT = process.env.PORT || 3000;
app.listen(PORT);
console.log(`Listening on port ${PORT}...\n\n`);

The middleware can also accept an options object, similar to the csurf package:

const express = require('express');
const expressCsrf = require('express-csrf-protect');
 
const app = express();

app.use(expressCsrf.enable({
  httpOnly: false,
  domain: 'some-domain',
  path: 'some-path'
}));

const PORT = process.env.PORT || 3000;
app.listen(PORT);
console.log(`Listening on port ${PORT}...\n\n`);
1.0.7

8 months ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago