Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
content-security-policy-builder
Advanced tools
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.
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"
helmet-csp is a middleware for Express.js that helps set Content Security Policy headers. It provides a more integrated approach for Express applications, allowing for easy configuration and dynamic policy generation. Compared to content-security-policy-builder, helmet-csp is more focused on being a middleware solution for Express.
csp-header is a package that helps generate CSP headers in a flexible and customizable way. It offers a similar API to content-security-policy-builder but includes additional features like preset policies and easier integration with various frameworks. It is more feature-rich and provides more out-of-the-box configurations compared to content-security-policy-builder.
csp-headers is another package for generating CSP headers. It focuses on simplicity and ease of use, providing a straightforward API for defining CSP directives. While it offers similar functionality to content-security-policy-builder, it is designed to be more lightweight and easier to integrate into small projects.
Take an object and turn it into a Content Security Policy string.
It can handle a lot of things you can you throw at it; camelCased
or dash-separated
directives, arrays or strings, et cetera.
Usage:
const builder = require("content-security-policy-builder");
// default-src 'self' default.com; script-src scripts.com; whatever-src something; object-src
builder({
directives: {
defaultSrc: ["'self'", "default.com"],
scriptSrc: "scripts.com",
"whatever-src": "something",
objectSrc: true,
},
});
This module is considered "complete". I expect to continue maintenance if needed, but I don't plan to add features or make breaking changes.
2.2.0 - 2024-04-25
FAQs
Build Content Security Policy directives.
The npm package content-security-policy-builder receives a total of 214,055 weekly downloads. As such, content-security-policy-builder popularity was classified as popular.
We found that content-security-policy-builder demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.