CSP Header Generator
Web ToolsGenerate Content Security Policy headers with a structured form for all CSP directives, trusted sources, and advanced features.. Free, private — all processing in your browser.
This tool is coming soon. Check back later!
The CSP Header Generator builds a Content Security Policy header for your site. CSP is a powerful defense against cross-site scripting (XSS) and data injection attacks — it tells browsers which sources of JavaScript, CSS, images, and other resources are allowed. A well-configured CSP can prevent most XSS attacks even if your code has a vulnerability. A poorly configured or missing CSP leaves your site exposed.
Configure CSP via a structured form for each directive (script-src, style-src, img-src, connect-src, frame-ancestors, etc.) and add trusted sources, nonces, hashes, or keywords. The tool produces a complete CSP header ready to add to your web server configuration or set via HTML meta tag. Common presets: strict CSP (maximum security), moderate CSP (allows common external services), report-only mode (monitor violations without blocking), inline-script-compatible (for legacy sites). Advanced features include strict-dynamic for modern CSP, nonce-based inline scripts, SRI hashes, and violation reporting. All generation runs in your browser.
CSP Header Generator — key features
All CSP directives
Form covers script-src, style-src, img-src, connect-src, frame-ancestors, and all other Level 3 directives.
Source type builder
Add 'self', URLs, schemes, nonces, hashes, or keywords via structured interface.
Presets
Strict, moderate, and permissive templates as starting points.
Report-only mode
Generate report-only header for monitoring violations without blocking.
Validation
Warns about weak configurations (unsafe-inline without nonce, too permissive defaults).
Nonce generation
Generate random nonces with instructions for correct server-side integration.
Hash calculator
Compute SHA-256 hashes for inline scripts and include in CSP.
Client-side only
Configuration stays in your browser; no server round trip.
How to use the CSP Header Generator
- 1
Choose baseline
Start from a preset (strict, moderate) or from scratch.
- 2
Configure directives
Add allowed sources per directive (script-src, style-src, etc.).
- 3
Enable nonces or hashes
For inline scripts you need, generate nonces or calculate hashes.
- 4
Add reporting
Set up report-uri or report-to endpoint to receive violation reports.
- 5
Deploy in report-only first
Use the report-only version to catch violations before enforcing. Switch to enforcing after fixing issues.
Common use cases for the CSP Header Generator
Security hardening
- →XSS prevention: Block injected scripts from executing even if attacker injects HTML into your page.
- →Clickjacking prevention: Use frame-ancestors to prevent other sites from embedding yours in iframes.
- →Data exfiltration prevention: Restrict connect-src to limit where injected scripts could send stolen data.
Compliance
- →PCI DSS compliance: CSP is a recommended control for e-commerce under PCI DSS, especially v4.0.
- →Audit requirements: Security audits frequently flag sites lacking CSP; deploying satisfies common findings.
- →Enterprise policy: Many enterprise security policies now require CSP for customer-facing web properties.
Modern web development
- →Third-party script control: Limit which third-party scripts (analytics, ads, widgets) can run on your pages.
- →Subresource Integrity enforcement: Combine with SRI hashes for defense in depth against CDN compromise.
- →Incident response: In security incidents, CSP can be quickly updated to block malicious domains.
CSP Header Generator — examples
Strict same-origin
Most restrictive policy.
strict preset
default-src 'self'; object-src 'none'; frame-ancestors 'none'; base-uri 'self';
With Google Analytics
Allow GA scripts.
moderate preset + google-analytics.com
default-src 'self'; script-src 'self' https://www.google-analytics.com; connect-src 'self' https://www.google-analytics.com;
With nonce
Secure inline scripts.
nonce-based
script-src 'self' 'nonce-abc123'; style-src 'self' 'nonce-abc123'; HTML: <script nonce=\"abc123\">...
Report-only
Monitor without blocking.
strict policy in report-only
Content-Security-Policy-Report-Only: default-src 'self'; report-uri /csp-report;
Strict-dynamic
Modern pattern for trusted script loading.
strict-dynamic with nonce
script-src 'nonce-abc123' 'strict-dynamic' 'unsafe-inline' https:; (unsafe-inline and https ignored when strict-dynamic is set and nonce is present)
Technical details
Content Security Policy (CSP) is a browser security feature defined by W3C. Deliver via HTTP header (Content-Security-Policy) or HTML meta tag (more limited).
Core directives:
- default-src: fallback for all fetch directives
- script-src: JavaScript sources (critical for XSS prevention)
- style-src: CSS sources
- img-src: image sources
- connect-src: XHR, fetch, WebSocket, EventSource destinations
- font-src: @font-face sources
- media-src: audio and video sources
- object-src: Flash and other plugins (set to none; plugins are deprecated)
- frame-src: iframe sources
- frame-ancestors: who can embed this site in iframes (clickjacking prevention)
- base-uri: allowed values for <base> element
- form-action: allowed form submission destinations
Source values:
- 'self': same origin
- 'unsafe-inline': inline scripts and styles (avoid if possible)
- 'unsafe-eval': eval() and similar (avoid)
- 'unsafe-hashes': specific inline event handlers
- URL: https://example.com
- Scheme: https:, data:, blob:
- Nonce: 'nonce-abc123' (random value in both header and inline <script nonce=\"abc123\">)
- Hash: 'sha256-hash' (SHA-256 of inline script content)
- 'none': disallow everything
- 'strict-dynamic': allow scripts loaded by trusted scripts (modern CSP pattern)
- *: wildcard (very permissive)
Typical strict CSP:
Content-Security-Policy: default-src 'self'; script-src 'self' 'nonce-abc123' 'strict-dynamic'; style-src 'self' 'nonce-abc123'; img-src 'self' data: https:; connect-src 'self'; frame-ancestors 'none'; base-uri 'self'; form-action 'self';
Nonces: generate a cryptographically random nonce per page load, include in CSP header AND in each inline <script nonce=\"...\">. Browser allows only scripts with matching nonce. Prevents injected scripts even if attacker can inject HTML.
Hashes: calculate SHA-256 of inline script content, include hash in CSP. Browser only allows scripts with matching hash. Harder to maintain than nonces but doesn\u2019t require per-request generation.
strict-dynamic: advanced pattern where the top-level script (via nonce or hash) can load other scripts, and those scripts can load more. Effectively trusts the initial script\u2019s transitive dependencies. Modern recommendation.
Report-only mode: Content-Security-Policy-Report-Only header — browser reports violations without blocking. Useful for rollout — deploy in report-only first, fix violations, then switch to enforcing mode.
Violation reporting:
- report-uri: legacy, POSTs to the URL (deprecated but still widely supported)
- report-to: modern, uses Reporting API
Frame-ancestors vs X-Frame-Options: frame-ancestors is the CSP way to prevent clickjacking. X-Frame-Options is older and less flexible. Modern sites should use frame-ancestors.
Common issues:
- unsafe-inline in script-src negates most XSS protection
- Too many trusted hosts reduce security
- Missing directive inherits from default-src
- CSP in meta tag doesn\u2019t support frame-ancestors or report-uri
Common problems and solutions
⚠unsafe-inline negates protection
script-src 'self' 'unsafe-inline' allows any inline script to run, including injected ones. Almost defeats CSP. Use nonces or hashes instead.
⚠Too permissive
script-src *; img-src *; is effectively no CSP. Lock down each directive to specific trusted sources.
⚠Breaking site functionality
Overly strict CSP can block your own scripts, styles, or images. Deploy in report-only first to find issues before enforcing.
⚠Missing directive inherits from default
If you set default-src 'self' but don’t specify script-src, scripts are limited to 'self'. Sometimes intentional; sometimes unexpected.
⚠Meta tag limitations
CSP in HTML <meta> can’t use frame-ancestors, report-uri, or other directives. Use HTTP header for full CSP.
⚠Browser compatibility
Older browsers may not support CSP Level 3 features (strict-dynamic, Trusted Types). Target CSP level that matches your audience.
⚠Nonce implementation
Nonces must be cryptographically random and regenerated per request. Reusing the same nonce across requests defeats the purpose.
CSP Header Generator — comparisons and alternatives
Compared to writing CSP by hand, this tool provides structured input and warns about weak configurations. Hand-writing CSP is error-prone due to complex syntax.
Compared to CSP Evaluator (csp-evaluator.withgoogle.com), this tool is for generation; the evaluator is for validation. Use this tool to build, then paste into the evaluator to verify.
Compared to security plugins (helmet for Node.js, Talisman for Flask), this tool is language-agnostic. Plugins integrate with specific frameworks; this tool produces the header for any server.
Frequently asked questions about the CSP Header Generator
▶What is Content Security Policy?
A browser security feature that prevents XSS and data injection attacks by telling browsers which sources of JavaScript, CSS, images, and other resources are trusted. Attackers can inject content; CSP prevents injected scripts from running if they don’t come from trusted sources.
▶Why should I use CSP?
To prevent cross-site scripting (XSS) attacks. Even if your application has a vulnerability that lets an attacker inject HTML, CSP prevents the injected scripts from executing. Without CSP, any XSS is a complete takeover; with strict CSP, XSS is significantly neutered.
▶What is a CSP nonce?
A random, cryptographically-generated value included in both the CSP header and inline <script nonce=\"...\"> tags. The browser allows only scripts with matching nonce. Effectively allows your inline scripts while blocking attacker-injected ones. Must be unique per request.
▶What is strict-dynamic?
Advanced CSP feature where the initial script (validated via nonce or hash) can load other scripts, which can load more, transitively. Simplifies modern JS apps while maintaining security. Recommended approach for new CSP deployments.
▶Should I use report-only first?
Yes, highly recommended. Content-Security-Policy-Report-Only header lets browsers report violations without blocking resources. Fix violations visible in reports, then switch to enforcing mode. Prevents accidentally breaking your site.
▶How does frame-ancestors compare to X-Frame-Options?
frame-ancestors is the modern CSP way to prevent clickjacking. More flexible (can specify multiple allowed origins) and browser-preferred. X-Frame-Options is older but still widely supported. Use both for maximum compatibility, or frame-ancestors alone if you target modern browsers.
▶Does CSP work in meta tags?
Partially. <meta http-equiv=\"Content-Security-Policy\"> works but has limitations: no frame-ancestors, no report-uri. Use HTTP header when possible for full CSP capability.
▶Is CSP configuration logged?
No. Generation runs in your browser. Your trusted hosts, nonces, and report endpoints stay local.
Additional resources
- MDN Content Security Policy — Comprehensive MDN reference on CSP directives and implementation.
- W3C CSP specification — Authoritative specification for CSP Level 3.
- Google CSP Evaluator — Google’s tool for evaluating CSP strength and identifying weaknesses.
- OWASP CSP cheat sheet — Security-focused guidance on CSP implementation.
- CSP strict-dynamic — Google’s guide to strict-dynamic and modern CSP patterns.
Related tools
All Web ToolsBase64 Encoder/Decoder
Encode and decode Base64 strings, files, and images instantly
Hash Generator
Generate MD5, SHA-1, SHA-256, SHA-512 hashes for text and files
HMAC Generator
Generate HMAC signatures (SHA-256, SHA-512) for API auth and webhook verification
Htaccess Generator
Generate Apache .htaccess files with redirects, URL rewrites, password protection, compression, caching, and security headers.
HTTP Headers Lookup
Complete reference for HTTP request and response headers with usage examples, RFC references, and common values.
HTTP Status Code Reference
Complete reference for every HTTP status code with meaning, usage guidelines, and examples for REST API design.
Explore more tools
200+ free tools that run in your browser.
Browse all tools →