Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
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. Useful for building Content Security Policy libraries.
It can handle a lot of things you can you throw at it; camelCased
or dash-separated
directives, arrays or strings, et cetera.
Usage:
var 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
}
})
FAQs
Build Content Security Policy directives.
The npm package content-security-policy-builder receives a total of 173,330 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
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.