Content Security Policy Generator

Build a Content-Security-Policy header (or meta tag) from common directive choices, without hand-writing the syntax from a spec page.

Directives

Generated policy

How to use this tool

  1. Set default-src first: it’s the fallback for any directive you don’t explicitly override below.
  2. Set script-src, style-src, img-src, and connect-src based on what your page actually loads from and where.
  3. Enable frame-ancestors ‘none’ if you don’t need your page embedded in an iframe elsewhere: this is a common clickjacking defense.
  4. Copy either the HTTP header form (set via your server config or app) or the equivalent meta tag if you can’t set custom headers.
  5. Test in report-only mode first if your server supports it: a misconfigured CSP silently breaks scripts and styles rather than erroring loudly.

A Content Security Policy is a browser-enforced allowlist: it tells the browser exactly which sources are permitted to load scripts, styles, images, and other resources on your page, and the browser blocks anything outside that list, including injected scripts from an XSS vulnerability, which is CSP’s main purpose. The tradeoff is that a policy which is too strict breaks legitimate functionality, and one that’s too loose (particularly ‘unsafe-inline’ on script-src) defeats much of the protection.

‘self’ restricts a directive to your own origin: same scheme, host, and port. It’s the right default for most directives on most sites. ‘unsafe-inline’ allows inline <script> and <style> tags and inline event handlers, which is convenient but reopens a large part of the XSS surface CSP exists to close; a nonce- or hash-based approach is the more secure alternative for sites that need inline scripts, though it requires generating a fresh nonce per request.

frame-ancestors controls who can embed your page in an iframe: setting it to ‘none’ is a clean way to prevent clickjacking without the older, less flexible X-Frame-Options header. upgrade-insecure-requests tells the browser to automatically rewrite any http: resource requests on the page to https:, useful during a migration where some links haven’t been updated yet.

Frequently asked questions

Should I set this via an HTTP header or a meta tag?

The HTTP header is preferred when you can set it: it applies before the page starts parsing and supports a few directives (like frame-ancestors and report-uri) that the meta tag version doesn’t fully support. Use the meta tag only when you can’t control server headers, such as on some static hosts.

Why is ‘unsafe-inline’ discouraged on script-src?

Because it allows any inline script to execute, including one injected by an XSS vulnerability: which defeats a large part of what CSP is meant to prevent. If you need inline scripts, a nonce (a random value regenerated per request and matched in both the policy and the script tag) or a hash of the exact script content is the safer alternative.

What happens if a resource is blocked by my policy?

The browser silently refuses to load it and logs a CSP violation to the console: it doesn’t throw a JavaScript error your code can catch. This is why testing matters: a misconfigured policy can break a page in ways that are easy to miss without checking the browser console.

Can I test a policy without it breaking anything?

Yes, if your server supports the Content-Security-Policy-Report-Only header: it reports violations without actually blocking anything, which is the standard way to validate a policy before enforcing it in production.

Related tools