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

@runtimerevolution/csrf_token_listener

v1.1.3

Published

Attaches a listener which retrieves a CSRF Token in every request made to a backend server

Downloads

2

Readme

CSRF Token Listener

This allows to certain exchanges with a backend server to exchange CSRF Tokens, allowing more security between the server using Fetch API or Axios Client

To use axios make sure to install it in your app as a dependency, flagged as a peerDepency only.

Fetch API Documentation Axios Documentation

Browser Support

Chrome | Firefox | Safari | Opera | Edge | IE | --- | --- | --- | --- | --- | --- | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | 11 Fetch(✗) Axios (✔) |

Installing

Package manager

Using npm:

$ npm install csrf_token_listener

Using yarn:

$ yarn add csrf_token_listener

Feature

FetchCsrfTokenListener/AxiosCsrfTokenListener

FetchCsrfTokenListener/AxiosCsrfTokenListener requires

  • backendEndpoint(string), the Base Endpoint that listen for CSRF and send on every request
  • sameOrigin (Boolean), specifies if both App & Backend are working in the same Domain. If not credentials will be applied to persist session. Defaulted as true

CsrfTokenListener

Same as FetchCsrfTokenListener/AxiosCsrfTokenListener with the addition of mode

  • mode(string), accepts only axios or fetch. Defaulted to fetch

Example

Add it into the App.js inside a new React app, this way each request made in all sub-components and routes are catched via CsrfTokenListener

Fetch API Example

import FetchCsrfTokenListener from 'csrf_token_listener/listeners/FetchCsrfTokenListener'
...
function App() {
  FetchCsrfTokenListener('http://localhost:3000', false);

  return (
    <div>
      // Write your App here
    </div>
  )
import { CsrfTokenListener } from 'csrf_token_listener'
...
function App() {
  CsrfTokenListener('http://localhost:3000', false, 'fetch');

  return (
    <div>
      // Write your App here
    </div>
  )

Axios API Example

import AxiosCsrfTokenListener from 'csrf_token_listener/listeners/AxiosCsrfTokenListener'
...
function App() {
  AxiosCsrfTokenListener('http://localhost:3000', false);

  return (
    <div>
      // Write your App here
    </div>
  )

Make sure to do this changes to your Backend Server

Once you have the package installed, make sure that your Backend server returns a CSRF Server and validates CSRF in every API request

Rails

Add inside the top-level ApplicationController/ApiController:

skip_before_action :verify_authenticity_token
before_action :verify_request
after_action :set_csrf_cookie
...
def verify_request
  valid_request = !protect_against_forgery? || request.get? || request.head? ||
                  valid_authenticity_token?(session, form_authenticity_param) ||
                  valid_authenticity_token?(session, request.headers['X-CSRF-Token'])

  head :unprocessable_entity if valid_request == false
end

def set_csrf_cookie
  response.set_header('x-csrf-token', form_authenticity_token)
end

This enables all requests & responses on the API, to retrieve a csrf token and requiring a csrf verification for posts/patch/delete actions

We still disable the verify_authenticity due to it's check on origin (needs to be same origin). Since an API can accept multiple different Origins, a rearranged method is application verify_request