node-csp

1.0.1 • Public • Published

Node Csp

Coverage Status License

Node Csp is a general purpose module to add Content Security Policy header to your http response. It can be used with any node http server.

Features

  1. Cross Browser support as per http://caniuse.com/#feat=contentsecuritypolicy
  2. Supports all directives using http://content-security-policy.com/
  3. Has support for nonce
  4. Fully tested.

Vanilla Http Server

const http = require('http')
const csp = require('node-csp')
const fs = require('fs')
 
const options = {
  directives: {
    defaultSrc: ['self'],
    scriptSrc: ['self', 'cdnjs.cloudflare.com', '@nonce']
  },
  nonce: '614d9122-d5b0-4760-aecf-3a5d17cf0ac9' // make sure to have unique nonce for each request
}
 
http.createServer(function (req, res) {
  const html = fs.readFile(__dirname + '/index.html', function (err, contents) {
    csp.add(req, res, options)
    res.writeHead(200)
    res.write(contents)
    res.end()
  })
}).listen(4000)
 

Options

{
   directives: {
    defaultSrc: ['self'],
    scriptSrc: ['@nonce'],
    styleSrc: ['self', 'cdnjs.cloudflare.com'],
    ...
   },
   nonce: '614d9122-d5b0-4760-aecf-3a5d17cf0ac9',
   reportOnly: false, // only set reporty only headers
   setAllHeaders: false, // sets all csp headers as per http://content-security-policy.com/
   disableAndroid: false // csp is buggy on android
}

The MIT License

Copyright (c) 2016 Harminder Virk

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -->

Readme

Keywords

Package Sidebar

Install

npm i node-csp

Weekly Downloads

2,516

Version

1.0.1

License

MIT

Last publish

Collaborators

  • amanvirk