Socket
Socket
Sign inDemoInstall

content-security-policy-builder

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

content-security-policy-builder

Build Content Security Policy directives.


Version published
Weekly downloads
485K
increased by2.01%
Maintainers
1
Weekly downloads
 
Created

What is content-security-policy-builder?

The content-security-policy-builder npm package is a utility for building Content Security Policy (CSP) headers in a structured and programmatic way. It helps developers create CSP headers by providing a simple API to define various directives and their values.

What are content-security-policy-builder's main functionalities?

Basic CSP Header Construction

This feature allows you to construct a basic CSP header by specifying directives and their sources. The example demonstrates how to create a policy that allows scripts and styles from 'self' and 'example.com'.

const cspBuilder = require('content-security-policy-builder');
const policy = cspBuilder({
  directives: {
    defaultSrc: ['self'],
    scriptSrc: ['self', 'example.com'],
    styleSrc: ['self', 'example.com']
  }
});
console.log(policy); // Outputs: "default-src 'self'; script-src 'self' example.com; style-src 'self' example.com"

Adding Nonce Values

This feature allows you to add nonce values to your CSP directives, which can be used to allow specific inline scripts or styles. The example shows how to add a nonce value to the scriptSrc and styleSrc directives.

const cspBuilder = require('content-security-policy-builder');
const policy = cspBuilder({
  directives: {
    scriptSrc: ['self', 'nonce-2726c7f26c'],
    styleSrc: ['self', 'nonce-2726c7f26c']
  }
});
console.log(policy); // Outputs: "script-src 'self' 'nonce-2726c7f26c'; style-src 'self' 'nonce-2726c7f26c'"

Report URI

This feature allows you to specify a URI where CSP violation reports should be sent. The example demonstrates how to set up a report URI directive.

const cspBuilder = require('content-security-policy-builder');
const policy = cspBuilder({
  directives: {
    defaultSrc: ['self'],
    reportUri: ['/csp-report-endpoint']
  }
});
console.log(policy); // Outputs: "default-src 'self'; report-uri /csp-report-endpoint"

Other packages similar to content-security-policy-builder

Keywords

FAQs

Package last updated on 13 Jun 2019

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