3.4.7 • Published 1 year ago

fusion-plugin-csrf-protection v3.4.7

Weekly downloads
1,074
License
-
Repository
github
Last release
1 year ago

fusion-plugin-csrf-protection

Build status

Provides a modified fetch that is automatically secure against CSRF attacks for non-idempotent HTTP methods.

This plugin handles csrf protection by adding a server side middleware that checks for a valid csrf token on requests for non-idempotent HTTP methods (e.g. POST). It generates a csrf secret once per session based on a combination of a timestamp and a server side stored secret and stores this using the provided session plugin (usually via an encrypted cookie). It uses this csrf secret to generate and validate csrf tokens per request.

NOTE: If you're making requests to CSRF protected endpoints from React, you should use fusion-plugin-csrf-protection-react instead of this package.


Table of contents


Installation

yarn add fusion-plugin-csrf-protection

Usage

import {createPlugin} from 'fusion-core';
import {FetchToken} from 'fusion-tokens';

const pluginUsingFetch = createPlugin({
  deps: {
    fetch: FetchToken,
  },
  provides: ({fetch}) => {
    return {
      getUser: () => {
        return fetch('/get-user');
      }
    }
  },
});

Setup

// src/main.js
import React from 'react';
import {FetchToken, SessionToken} from 'fusion-tokens';
import App from 'fusion-react';
import Session from 'fusion-plugin-jwt';
import CsrfProtection, {
  FetchForCsrfToken,
  CsrfExpireToken,
  CsrfIgnoreRoutesToken,
} from 'fusion-plugin-csrf-protection';
import fetch from unfetch;

export default () => {
  const app = new App(<div></div>);
  app.register(SessionToken, Session);
  app.register(FetchForCsrfToken, fetch);
  app.register(FetchToken, CsrfProtection);
  if (__BROWSER__) {
    app.register(FetchForCsrfToken, fetch);
    // see usage example above
    app.register(someToken, pluginUsingFetch);
  } 
  // optional
  app.register(CsrfExpireToken, 60 * 60 * 24); 
  // optional
  __NODE__ && app.register(CsrfIgnoreRoutesToken, []);
}

API

Registration API

CsrfProtection
import CsrfProtection from 'fusion-plugin-csrf-protection';

The csrf protection plugin. Typically, it should be registered to the FetchToken. Provides the fetch api and a server side middleware for validating csrf requests.

FetchToken
import {FetchToken} from 'fusion-tokens';

The canonical token for an implementation of fetch. This plugin is generally registered on that token. For more, see the fusion-tokens repo.

Dependencies

CsrfExpireToken
import {CsrfExpireToken} from 'fusion-plugin-csrf-protection';

The number of seconds for csrf tokens to remain valid. Optional.

Types

type CsrfExpire = number;

Default value

The default expire is 86400 seconds, or 24 hours.

CsrfIgnoreRoutesToken
import {CsrfIgnoreRoutesToken} from 'fusion-plugin-csrf-protection';

A list of routes to ignore csrf protection on. This is rarely needed and should be used with caution.

Types

type CsrfIgnoreRoutes = Array<string>;

Default value

Empty array []

FetchForCsrfToken
import {FetchForCsrfToken} from 'fusion-plugin-csrf-protection';

An implementation of fetch to be used by the fusion-plugin-csrf-protection. Usually this is simply a polyfill of fetch, or can even be a reference to window.fetch. It is useful to exist in the DI system however for testing.

For type information, see the FetchToken docs. Required.

SessionToken
import {SessionToken} from 'fusion-tokens';

The canonical token for an implementation of a session. For type information, see the SessionToken docs. Required.

Service API

const response: Response = fetch('/test', {
  method: 'POST',  
})

fetch: (url: string, options: Object) => Promise - Client-only. A decorated fetch function that automatically does pre-flight requests for CSRF tokens if required.

See https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API for more on the fetch api.

3.4.7

1 year ago

3.4.4

1 year ago

3.4.6

1 year ago

3.4.5

1 year ago

3.3.6

2 years ago

3.4.0

1 year ago

3.4.3

1 year ago

3.4.2

1 year ago

3.4.1

1 year ago

3.3.5

2 years ago

3.3.4

2 years ago

3.3.3

2 years ago

3.3.1

2 years ago

3.3.0

2 years ago

3.3.2

2 years ago

3.2.5

2 years ago

3.2.4

2 years ago

3.2.2

2 years ago

3.2.3

2 years ago

3.2.1

2 years ago

3.2.0

3 years ago

3.1.12

3 years ago

3.1.11

3 years ago

3.1.10

3 years ago

3.1.9

3 years ago

3.1.8

3 years ago

3.1.7

3 years ago

3.1.6

3 years ago

3.1.5

4 years ago

3.1.4

4 years ago

3.1.2

4 years ago

3.1.1

4 years ago

3.1.0

4 years ago

3.0.9

4 years ago

3.0.8

4 years ago

3.0.7

4 years ago

3.0.6

5 years ago

3.0.5

5 years ago

3.0.4

5 years ago

3.0.3

5 years ago

3.0.2

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.0.1

5 years ago

2.0.1-0

5 years ago

2.0.0

5 years ago

2.0.0-0

6 years ago

1.0.7

6 years ago

1.0.7-0

6 years ago

1.0.6

6 years ago

1.0.6-1

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.3.4

6 years ago

0.3.3

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

7 years ago

0.1.8

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago