Content Security Policy (CSP Level 3) Policy Builder
Visually build, audit, and validate strict Content-Security-Policy (CSP) headers with nonce/hash support, script-src, frame-ancestors, and server export.
Compiled CSP Header
Switch to the Security Evaluator tab to inspect full XSS injection vectors, base-tag hijacking checks, and remediation guidance.
Google Recommended Strict CSP
- Always set object-src 'none' to prevent plugin-based script execution.
- Always set base-uri 'none' or 'self' to block relative script hijacking.
- Avoid domain whitelists for scripts; prefer 'nonce-...' 'strict-dynamic'.
- Never add 'unsafe-inline' without an accompanying nonce or hash.
Audit Policy & XSS Vector Evaluation
Paste any existing Content-Security-Policy header to audit against XSS bypass vectors, JSONP CDN escapes, and misconfigurations.
Audit Diagnostics & Remediation(7 items)
default-src 'none' restricts plugin object loading.
base-uri is missing. Note: base-uri does NOT fall back to default-src! An attacker who can inject HTML can inject a <base href="https://evil.com/"> tag.
Neither script-src nor default-src is configured. Browsers will execute any JavaScript without restrictions.
data: in script-src permits executing scripts embedded directly in data URLs (<script src="data:text/javascript,alert(1)">).
frame-ancestors is missing. Without this directive (or an X-Frame-Options header), arbitrary third-party websites can render your page in an invisible iframe.
Clickjacking is mitigated by controlling valid framing parents.
form-action is missing. Note: form-action does NOT fall back to default-src! Injected forms or modified form actions can submit credentials or CSRF tokens to an attacker-controlled endpoint.
Parsed Directive Breakdown (11 statements)
| Directive | Values & Sources | Status |
|---|---|---|
| default-src | 'none' | Enforced |
| script-src | 'nonce-r4nd0mStr1ng' 'strict-dynamic' https: 'unsafe-inline' | Enforced |
| style-src | 'self' 'unsafe-inline' | Enforced |
| img-src | 'self' https: data: | Enforced |
| font-src | 'self' data: | Enforced |
| connect-src | 'self' | Enforced |
| object-src | 'none' | Enforced |
| base-uri | 'none' | Enforced |
| form-action | 'self' | Enforced |
| frame-ancestors | 'none' | Enforced |
| upgrade-insecure-requests | (boolean flag) | Enforced |
Cryptographic Nonce Generator
Generates cryptographically random 128-bit (16-byte) base64 nonces using window.crypto.getRandomValues.
// Generate per-request nonce in middleware.ts
const nonce = Buffer.from(crypto.randomUUID()).toString('base64');
const csp = `script-src 'nonce-${nonce}' 'strict-dynamic';`;
response.headers.set('Content-Security-Policy', csp);Inline Script / Style Hash Calculator
Calculate SHA-256, SHA-384, or SHA-512 hashes of inline JavaScript or CSS code via SubtleCrypto.
Note: The browser calculates the hash over the exact inner text content (including whitespace, spaces, and linebreaks) between the <script> tags.
Server & Framework Configuration Generator
Ready-to-deploy configuration snippets for popular web servers, CDNs, and application frameworks.
# Add to your server {} or location {} block
add_header Content-Security-Policy "default-src 'none'; base-uri 'none'; form-action 'self'; object-src 'none'; script-src 'nonce-r4nd0mStr1ng' 'strict-dynamic' https: 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' https: data:; font-src 'self' data:; connect-src 'self'; frame-ancestors 'none'; upgrade-insecure-requests;" always;CSP Level 3 Specification Compliance
Supports modern W3C directives including script-src-elem, strict-dynamic, trusted-types, and frame-ancestors.
XSS & Data Injection Mitigation
Restricts executable script sources to cryptographic nonces or SHA hashes, eliminating unsafe-inline script vulnerabilities.
Zero-Downtime Server Export Snippets
Generates tested header injection snippets for Nginx, Apache httpd, Caddy, Cloudflare Workers, and Next.js headers config.
Csp Policy Builder Developer Reference
Developer utility for csp-policy-builder.
Key Parameters & Controls
| Parameter | Type / Unit | Description | Default |
|---|---|---|---|
| directive | string | The CSP directive token defining the policy boundary for a specific resource type, such as default-src, script-src, or frame-ancestors. | "default-src" |
| sourceList | string[] | Array of authorized source expressions, including host origins, protocol schemes, cryptographic nonces, or reserved keywords. | ["'self'"] |
| isFlag | boolean | Indicates standalone boolean directive flags that take no source arguments, such as upgrade-insecure-requests. | false |
| initialMode | "builder" | "evaluator" | Workspace operational mode toggling between interactive visual directive assembly and raw policy AST parsing with audit scoring. | "builder" |
Pro Tips & Best Practices
Pair 'strict-dynamic' with random cryptographic nonces on script-src to allow dynamically loaded trusted scripts while deprecating brittle domain allowlists.
Deploy updates first with Content-Security-Policy-Report-Only and a valid report-to endpoint to monitor policy violations without breaking live production assets.
Replace obsolete X-Frame-Options headers with frame-ancestors 'none' or explicit origins, as default-src does not govern framing permissions.
Common Gotchas & Troubleshooting
⚠️Missing single quotes around CSP keywords
Wrap reserved tokens like 'self', 'none', 'unsafe-inline', and 'unsafe-eval' in single quotes. Without quotes, browsers interpret them as hostnames and attempt DNS lookups.
⚠️Directives not falling back to default-src
Explicitly declare form-action, frame-ancestors, and base-uri. The W3C CSP Level 3 specification excludes navigation and framing directives from default-src fallback.
⚠️Meta tag ignoring frame-ancestors directive
Serve framing, sandbox, and reporting directives exclusively via HTTP response headers. Browsers discard frame-ancestors and report-uri when declared in HTML meta tags.
Frequently Asked Questions
Why are frame-ancestors and report-uri ignored in HTML meta tags?▼
W3C CSP Level 3 specifications explicitly disallow framing and reporting directives inside HTML meta elements because framing boundaries and violation endpoints must be registered prior to document parsing.
Does Content Security Policy degrade DOM parsing or network latency?▼
Parsing an HTTP CSP header adds negligible sub-millisecond CPU overhead during document initialization. However, excessively long header strings exceeding MTU sizes can trigger extra TCP packets, so avoid bloated domain lists.
How does 'strict-dynamic' interact with existing domain allowlists?▼
In CSP Level 3 compliant browsers, specifying 'strict-dynamic' alongside a valid nonce or hash causes the engine to ignore domain allowlists and 'unsafe-inline', maintaining backward compatibility for older CSP Level 2 engines.
Are generated CSP policies evaluated locally or sent to external servers?▼
All AST parsing, syntax validation, and security auditing occur entirely client-side in the browser using Web APIs. No policy fragments, hostnames, or internal architecture details are transmitted over the network.
Related Tools & Utilities
All Tools →Create fine-grained Kubernetes NetworkPolicy YAML manifests with Ingress/Egress CIDR blocks, podSelector labels, namespaceSelector, and port restrictions.
Build autoscaling HPA v2 YAML manifests with CPU and Memory utilization targets, custom metrics, and scale-up/scale-down stabilization policies.
Generate Cloudflare Pages _headers and _redirects configuration files with HSTS, CSP, X-Frame-Options, caching rules, and 301/302 URL redirects.
Generate tuned redis.conf configuration files with maxmemory policies, RDB/AOF persistence, network binding, and security authentication.
Validate Kubernetes Deployment, Pod, Service, Ingress, and ConfigMap manifests against official Kubernetes OpenAPI v1.28+ schemas. Catch missing apiVersion fields, container resource limits, and privilege escalation risks.
Analyze Dockerfiles against Hadolint and Docker CIS security benchmarks. Detect root user vulnerabilities, unpinned base image digests, missing package manager cache cleanups, and multi-stage build optimization opportunities.