Coder Social home page Coder Social logo

r0bs / csp-gen Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 47 KB

csp-gen generates a valid Content-Security-Policy header from a JavaScript object after checking the inputs for validity

License: MIT License

TypeScript 100.00%
csp content-security-policy http-header utilities

csp-gen's Introduction

csp-gen

csp-gen generates a valid Content-Security-Policy header from a JavaScript object after checking the inputs for conformity with allowed CSP directive names, keywords and values (see below)

Installation

Using npm:

$ npm i csp-gen --save

Usage by example

For a JavaScript object like this

const input = {
  "connect-src": ["'self'", "api.example.com"],
  "default-src": ["'none'"],
  "font-src": ["'self'"],
  "form-action": ["'self'"],
  "frame-ancestors": ["'none'"],
  "frame-src": ["'none'"],
  "img-src": ["'self'", "*.example.com"],
};

In Node.js run:

const csp = require("csp-gen");

const policyString = csp.generate(input);

now policyString will look like this

connect-src 'self' api.example.com; default-src 'none'; font-src 'self'; form-action 'self'; frame-ancestors 'none'; frame-src 'none'; img-src 'self' *.example.com;

Why csp-gen?

csp-gen is a dependency-free library that checks the input data for supported CSP terminology (see below) and generates a ready-to-use policy string

  • Checks for errors and typos in input data during runtime and type checks input parameters
  • 0 dependencies (no transitional pulling of npm packages)
  • Creates ready-to-use Content-Security-Policy

Checks

csp-gen runs checks on the input data and throws an error if input does not match with what can be a valid Content-Security-Policy. Input types are checked by exposing an TypeScript interface to the editor or IDE.

Allowed directive names:

  • base-uri
  • block-all-mixed-content
  • connect-src
  • default-src
  • font-src
  • form-action
  • frame-ancestors
  • frame-src
  • img-src
  • manifest-src
  • media-src
  • navigate-to
  • object-src
  • plugin-types
  • prefetch-src
  • report-to
  • report-uri
  • require-sri-for
  • require-trusted-types-for
  • sandbox
  • script-src
  • script-src-attr
  • script-src-elem
  • style-src
  • style-src-attr
  • style-src-elem
  • trusted-types
  • upgrade-insecure-requests
  • worker-src

Allowed directive values:

  • every domain-like string (needs to be more than 3 characters long and contain at least one dot)
  • hash strings like 'sha256-f12ac5b76ff2aa2da31b2c3b5fdfbfe8', supported hashing algorithms: sha256, sha384, sha512
  • 'none'
  • 'report-sample'
  • 'self'
  • 'strict-dynamic'
  • 'unsafe-allow-redirects'
  • 'unsafe-eval'
  • 'unsafe-hashes'
  • 'unsafe-inline'
  • data:

Exceptions:

The following directives must not contain values. In the input object those can be added with an empty array as value, e.g. directive: [] or upgrade-insecure-requests: []

  • block-all-mixed-content
  • upgrade-insecure-requests

csp-gen's People

Contributors

r0bs avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.