CSP Builder
Build Content-Security-Policy headers visually. Add directives, sources, preview policy. Free online CSP generator for web security
Content Security Policy is the single most powerful XSS mitigation in browsers — and the single most common cause of "all my third-party scripts stopped loading at 3am after the deploy". This builder generates CSP headers from your stack (analytics, fonts, payment providers, video embeds), groups directives by source, and lets you choose between report-only and enforced mode. It also flags the unsafe-inline / unsafe-eval / data: combinations that downgrade CSP to security theater.
How CSP actually blocks attacks
CSP is a server-sent header (Content-Security-Policy or as a <meta http-equiv> tag) that tells the browser which sources are allowed for scripts, styles, images, frames, fonts, connections, and several other resource types. When the browser encounters something not on the allowlist, it refuses to load it and posts a violation report if a report-uri is configured.
The XSS mitigation is specifically: even if an attacker injects <script>alert(1)</script> into your HTML, the browser will not run it unless your CSP allows inline scripts ('unsafe-inline') or the attacker can inject from one of your allowlisted hosts. CSP turns "any injection is RCE" into "injection is harmless unless you also allow inline".
Working example: a typical SaaS app
Input
Stack: own scripts on /static, Google Analytics, Stripe payments, Cloudinary images, custom Roboto font from Google Fonts
Output
Content-Security-Policy: default-src 'self'; script-src 'self' https://www.googletagmanager.com https://js.stripe.com; style-src 'self' https://fonts.googleapis.com 'unsafe-inline'; font-src 'self' https://fonts.gstatic.com; img-src 'self' data: https://res.cloudinary.com https://www.google-analytics.com; connect-src 'self' https://www.google-analytics.com https://api.stripe.com; frame-src https://js.stripe.com; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'; upgrade-insecure-requests; report-uri /csp-report
Notes: 'unsafe-inline' for styles is included because most CSS frameworks inject style attributes. Replacing with a nonce-based policy is the upgrade path. frame-ancestors 'none' is the modern replacement for X-Frame-Options: DENY. object-src 'none' kills Flash and old plugin attack surface even though the elements are gone.
Directives ranked by impact
- script-src — controls JavaScript sources. The XSS-prevention workhorse. If unsafe-inline is in this directive, you have no CSP-based XSS protection.
- default-src — fallback for any directive you do not explicitly set. Start strict (default-src 'self') and override per-type.
- frame-ancestors — prevents your site from being framed by other sites. Modern replacement for X-Frame-Options.
- object-src — controls <object>, <embed>. Should be 'none' unless you actually use them. Plugin attack surface lives here.
- base-uri — prevents attackers from changing the <base> tag and rewriting all relative URLs. base-uri 'self' or 'none'.
- form-action — controls where forms can submit. Prevents form-action injection attacks.
- connect-src — controls fetch/XHR/WebSocket targets. Important for preventing data exfiltration via injected JS that reaches a controlled endpoint.
- img-src — usually permissive. data: is common (for inline icons) but enables some attacks. https: only is safer than wildcard.
- style-src — CSS injection is less critical than script injection but can still exfiltrate data via background-image: url() + selectors.
The features that make CSP useful or useless
- Nonce-based — script-src 'nonce-RANDOM'. Each page generates a fresh nonce; only inline scripts with matching nonce="RANDOM" run. Strong protection, requires server-side rendering.
- Hash-based — script-src 'sha256-...'. Allow specific inline scripts by their hash. Works for static inline scripts; brittle if scripts change.
- strict-dynamic — script-src 'strict-dynamic' 'nonce-RANDOM'. The trusted scripts can themselves load other scripts dynamically without each new one needing nonce. Modern best practice for SPAs.
- report-only — Content-Security-Policy-Report-Only: send violation reports but do not block. Use during rollout to find what would break.
- unsafe-inline — gives up most of CSP's XSS protection. Use only as a transitional step. Pair with hash or nonce to migrate off.
- unsafe-eval — allows eval(), new Function(), setTimeout("string"). Some frameworks (older Vue, AngularJS) require it. Remove if possible.
When to reach for this tool
- You are adding CSP to an existing site for the first time. Start in report-only mode for a week, fix violations, then enforce.
- You inherited a permissive CSP (default-src *) and want to tighten it. Generate a strict baseline, identify legitimate exceptions one at a time.
- You are integrating a new third party (Intercom, Mixpanel, Stripe Elements) and need the host names to add to script-src and connect-src.
- You are preparing for an audit (PCI-DSS, ISO 27001) that requires CSP enforcement on customer-facing pages.
What this tool will not do
- It will not test your live site. Use the report-only header in production for a week to discover real violations — synthetic testing misses dynamically-injected scripts from CMS embeds and customer JavaScript.
- It will not handle the unsafe-inline-to-nonce migration. That requires server-side template changes to emit the nonce attribute on every inline script and style.
- It will not enforce CSP at the browser. The header is a hint; older browsers (and any non-browser client) ignore it. CSP is defense-in-depth, not a replacement for output encoding or input validation.
Frequently asked questions
Should I use CSP via header or meta tag?
Header. Some directives (frame-ancestors, report-uri, sandbox) only work in the header. Meta-tag CSP is best-effort: it cannot block resources requested before the parser reaches the meta tag, and several directives are silently ignored.
My CSP report shows violations from chrome-extension://...
Browser extensions inject scripts and styles into pages. They bypass CSP for their own injections but show up in your report URL. Filter these out at the report receiver; they are noise, not real violations.
How strict can I get?
For a static site with own scripts only: default-src 'none'; script-src 'self'; style-src 'self'; img-src 'self'; connect-src 'self'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'; object-src 'none'. Anything looser is justified by a specific feature.
What is the difference between CSP level 1, 2, and 3?
Level 1 (2014): basic directives. Level 2 (2016): added nonce, hash, strict-dynamic. Level 3 (current): script-src-attr, script-src-elem, trusted-types, prefetch-src. All current browsers support level 2; level 3 features have varying support.
Do I need both Content-Security-Policy and X-Content-Type-Options?
Yes ��� they protect different things. X-Content-Type-Options: nosniff prevents MIME-sniffing attacks. CSP prevents resource-loading from disallowed sources. Same with X-Frame-Options (which frame-ancestors replaces) — modern browsers respect frame-ancestors, older ones still read X-Frame-Options. Set both for transitional safety.
How do I handle inline event handlers (<button onclick="...">)?
CSP blocks them with the default script-src directive. Either move handlers to JS files (addEventListener), or allow them explicitly with script-src-attr 'unsafe-inline'. The first is the right answer; the second is an escape hatch for legacy code.
Related tools
Debug CORS issues, analyze headers, generate server configurations. Support for Express, Nginx, Apache, Django, Flask, Spring Boot. Learn CORS concepts. Free online CORS tester
Generate SEO meta tags, Open Graph, Twitter Cards with live preview. Optimize website metadata for search engines and social sharing
Complete list of HTTP status codes with descriptions and examples. Learn 200, 301, 404, 500 error codes. Free HTTP response code reference guide
Check SSL certificate for any domain. View expiration date, issuer, certificate chain. Verify HTTPS security and get SSL rating
Analyze X.509 SSL/TLS certificates. View issuer, validity, SANs, certificate chain. Check SSL certificate details. Free online certificate decoder
Encode and decode HTML entities online. Convert special characters like <, >, &, quotes to HTML entities. Free HTML escape/unescape tool
Last updated · E-Utils editorial team