CSP Generator
Build a Content-Security-Policy directive by directive, with a live preview and a safe Report-Only rollout. The strongest defence against cross-site scripting.
Build your policy
Optional. Where browsers POST a JSON report whenever the policy is violated.
Resulting policy
default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; font-src 'self' data:; connect-src 'self'; frame-ancestors 'self'; base-uri 'self'; form-action 'self'; object-src 'none'; upgrade-insecure-requests
Your Content-Security-Policy
Header
Content-Security-Policy: default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; font-src 'self' data:; connect-src 'self'; frame-ancestors 'self'; base-uri 'self'; form-action 'self'; object-src 'none'; upgrade-insecure-requests
Server snippet
add_header Content-Security-Policy "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; font-src 'self' data:; connect-src 'self'; frame-ancestors 'self'; base-uri 'self'; form-action 'self'; object-src 'none'; upgrade-insecure-requests" always;
Understanding CSP
What it is. A Content-Security-Policy is an allowlist you send with every page. It tells the browser exactly which origins may supply scripts, styles, images, fonts and frames. Anything not allowlisted is blocked — so even if an attacker manages to inject a malicious <script>, the browser simply refuses to run it.
Directives. A policy is built from directives. default-src is the catch-all fallback; more specific ones like script-src and img-src override it for that content type. Each lists sources: the keyword 'self' for your own origin, explicit hosts like cdn.example.com, or schemes like https:.
Roll out safely. A strict CSP can block resources you actually need. Deploy it in Report-Only mode first: the browser reports violations to your report-uri but blocks nothing. Watch real traffic, allowlist the legitimate sources, then switch to enforcing.
Nonces over unsafe-inline. Inline scripts are convenient but dangerous under CSP. Rather than open the door with 'unsafe-inline', generate a fresh random nonce on the server for each request, put it in the policy as 'nonce-…', and add the same value to each trusted inline tag. Only matching scripts run.