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

csp-parse

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

csp-parse

Content-Security-Policy policy parser

  • 0.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5.8K
decreased by-22.94%
Maintainers
1
Weekly downloads
 
Created
Source

csp-parse

NodeJS module for parsing Content-Security-policy policies.

The module takes raw CSP policy strings, and parses them into Policy objects. From there the objects can be manipulated (adding/removing different values from the directives).

Afterwards policy.toString() can be called to get the updated policy.

Installation

npm install csp-parse

Or to use as plain javascript, remove the last line.

Usage

var Policy = require('csp-parse');

var example = "script-src 'self' www.google-analytics.com ajax.googleapis.com; style-src 'self';"
var policy = new Policy(example)

policy.get('script-src') // "'self' www.google-analytics.com ajax.googleapis.com"
policy.add('script-src', 'code.jquery.com');
policy.get('script-src') // "'self' www.google-analytics.com ajax.googleapis.com code.jquery.com"

policy.toString() // "script-src 'self' www.google-analytics.com ajax.googleapis.com code.jquery.com; style-src 'self';"

policy.set('connect-src', 'socket.webserver.com socket2.webserver.com');

policy.toPrettyString()
//script-src
//  'self' www.google-analytics.com ajax.googleapis.com code.jquery.com;
//style-src
//  'self';
//connect-src
//  socket.webserver.com socket2.webserver.com

For more examples see the test cases.

API

Policy(string)

Takes a CSP policy and returns a Policy object.

policy.get(string directive)

Returns a list of allowed values for 'directive'

policy.add(string directive, string value)

Adds the value to the directive string

policy.set(string diretcive, string line)

Sets the 'directive' value to 'line'

Policy.toString()

Returns string representation of policy

Policy.toPrettyString()

Returns a pretty string representation of the policy

Test

mocha

/brain explodes

Contact

c0nrad@c0nrad.io

Keywords

FAQs

Package last updated on 06 Jan 2015

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