Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

micro-csrf

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

micro-csrf

Anti-CSRF middleware for micro

  • 1.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

micro-csrf

Build Status

micro-csrf is a csrf middleware for Zeit.co's micro framework. This module is heavily inspired by express-csurf.

Installation

$ npm install micro-csrf
# or
$ yarn add micro-csrf

Example Usage


// Use the micro-session middleware for storing the token secret
const SessionManager, { MemoryStore } = require('micro-session');
const { csrfMiddleware } = require('micro-csrf');

const sessionManager = SessionManager({
  store: new MemoryStore(),
  secret: 'my session secret'
})
const csrf = csrfMiddleware();

module.exports = async (req, res) => {
  let session = await getSession(req, res);

  // This will automatically end the request with a 403 error
  // if this is a POST, PUT, PATCH, DELETE request without a valid
  // CSRF Token.
  const csrfToken = await csrf(session, req, res);

  // ...

  return {
    csrfToken
  };
};

Token Validation

The token is automatically read from the following locations:

    req.body._csrf - requires a parsed request body
    req.query._csrf - requires a query parser
    req.headers['csrf-token'] - the CSRF-Token HTTP request header.
    req.headers['xsrf-token'] - the XSRF-Token HTTP request header.
    req.headers['x-csrf-token'] - the X-CSRF-Token HTTP request header.
    req.headers['x-xsrf-token'] - the X-XSRF-Token HTTP request header.

License

MIT

Keywords

FAQs

Package last updated on 16 May 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc