csp-bun-cli
v2.0.2
Published
A CLI that generates and injects a Content Security Policy (CSP) for your SPA application.
Maintainers
Readme
CSP Bun CLI
CLI app for adding a Content Security Policy to your SPA application using the Bun runtime.
Features
- ✨ Automatically calculates Subresource Integrity (SRI) hashes of JavaScript and CSS assets and adds them to the CSP.
- 📚 Automatically detects and handles Google Fonts.
- 🏎 Zero dependencies. Uses Bun's built-in implementation of Cloudflare's HTMLRewriter for parsing HTML.
Installation
# Local project install
bun add -D csp-bun-cli
# Global install
bun add -g csp-bun-cli
# Use without installing
bunx csp-bun-cli --helpBasic Usage
Add the meta CSP tag to the <head> of your index.html file:
<head>
<meta http-equiv="Content-Security-Policy" content="" />
</head>Configuration (Optional)
Note: Setting a config is optional. If no config is provided, then the default values will be used.
Create a csp.config.ts file with your CSP configuration. The config must be exported as the default export.
import type { CspPluginConfiguration } from "csp-bun-cli";
export default {
algorithm: "sha256",
policy: {
"script-src": ["'self'"],
},
} as CspPluginConfiguration;You can also explicitly specify a config file to use with the --config CLI option (resolved relative to cwd):
csp -d path/to/dist/dir --config ./my-config.tsUsage
# Basic Usage
csp -d path/to/dist/dir
# Custom Config
csp -d path/to/dist/dir --config path/to/config.ts
# Base Path
csp -d path/to/dist/dir --base myapp