🔐 Security & Privacy

Content Security Policy Generator

Build a CSP header that blocks XSS — with ready-to-paste server config.

  • Presetscommon setups
  • 6 formatsheader to Express
  • Report-onlysafe testing
  • Warningsunsafe settings

How to use the CSP Generator

  1. 1

    Start from a preset

    Or set each directive yourself.

  2. 2

    Allow your sources

    Add the domains your site loads scripts, styles, fonts and images from.

  3. 3

    Choose report-only first

    Test without blocking anything.

  4. 4

    Copy the config

    As an HTTP header, meta tag, Apache, Nginx, _headers or Express code.

Features

XSS protection

Block scripts that did not come from you.

All main directives

default, script, style, img, font, connect, frame and more.

Unsafe warnings

Flags unsafe-inline and wildcards.

Report-only mode

See violations before enforcing.

Import

Paste an existing policy to edit it.

Server snippets

Ready for Apache, Nginx, Netlify, Cloudflare, Express.

Who uses it — and for what

Website owners

Add a security header many audits flag as missing.

Developers

Build a policy without memorising syntax.

Security audits

Fix “CSP not set” findings.

Shops

Reduce the risk of card-skimming scripts.

What CSP does

A Content Security Policy tells the browser which sources of scripts, styles, images, fonts, frames and connections your page may use. If an attacker manages to inject a <script> (cross-site scripting), the browser refuses to run it because it isn’t from an allowed source. It also stops clickjacking (frame-ancestors), plugin abuse (object-src 'none') and base-tag hijacking (base-uri).

Click a directive’s box, then click tokens such as 'self' or https: to add them, or type hosts like https://cdn.example.com. Empty directives are left out and fall back to default-src.

Rolling it out safely

  1. Start in report-only mode — nothing is blocked, but violations appear in the browser console (and are sent to your report URL).
  2. Open every page of your site, fix or allow what legitimately breaks, then switch to enforcing.
  3. Prefer nonces or hashes with 'strict-dynamic' over long host allow-lists — Google’s research found most allow-list policies can be bypassed.

Note: frame-ancestors, report-uri and sandbox are ignored in a meta tag; send the policy as an HTTP header when you can. Sites running ads (AdSense and other networks) load scripts from many changing domains — use the nonce-based strict preset for those.

The directives you will use most

DirectiveControls
default-srcFallback for every type not listed
script-srcJavaScript files and inline scripts
style-srcStylesheets and inline styles
img-srcImages, including data: URIs
font-srcWeb fonts
connect-srcfetch, XHR and WebSocket calls (APIs, analytics)
frame-srcEmbedded iframes such as YouTube or maps
frame-ancestorsWho may embed your page — stops clickjacking

Avoiding unsafe-inline

'unsafe-inline' in script-src lets any inline script run, which defeats most of CSP’s protection against cross-site scripting. Move inline code into .js files, replace onclick="" attributes with event listeners, and for scripts that must stay inline use a nonce (a random value per page load) or a hash of the script. Inline styles are a lower risk; many sites keep 'unsafe-inline' in style-src while tightening scripts first.

Worked examples

A simple static site with Google Analytics

default-src 'self'; script-src 'self' https://www.googletagmanager.com; img-src 'self' data: https://www.google-analytics.com; connect-src 'self' https://*.google-analytics.com; upgrade-insecure-requests

Pro tips

  • Check the browser console — CSP violations are reported there with the exact source.
  • Add frame-ancestors 'self' to stop other sites framing your pages.

Glossary

XSS
Cross-site scripting — injecting malicious scripts into a page.
Directive
One rule in a CSP, such as script-src.
Nonce
A one-time random token that marks trusted inline scripts.
Clickjacking
Tricking users into clicking a page hidden inside a frame.

Frequently asked questions

Header or meta tag?

The HTTP header is preferred and supports every directive; the meta tag is a fallback for static hosts where you can’t set headers.

Why does my site break after adding CSP?

Inline scripts, inline event handlers (onclick=…) and third-party resources must be allowed or refactored. Test in report-only mode first.

What is a nonce?

A random value generated on every page load, added to the policy and to each trusted <script nonce="…"> tag.

Will CSP slow my site down?

No — it is a header the browser checks, with no noticeable cost.

Does CSP replace input validation?

No — it is an extra layer; always escape user content too.

Found it useful? Share the CSP Generator:

Last updated 2026-11-28 · Built by Vinayak Agarwal · Part of Security & Privacy (20 tools)