Developer tool
CSP Header Generator
Free, no sign-up. Runs in your browser; nothing is uploaded.
Start from a preset
Directives
Options
Your policy
Content-Security-Policy: default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; img-src 'self' data: https:; font-src 'self' data: https://fonts.gstatic.com; connect-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'self'; upgrade-insecure-requests
How to use the csp header generator
Build a policy
- Start from a preset: strict (same origin only), a typical website, or a site with Google Analytics and YouTube.
- For each directive, tick the keywords you need and type any extra hosts, such as
https://cdn.example.com. - Read the warnings. They flag risky choices such as
'unsafe-inline', wildcards and missing directives. - Pick the output you need: an HTTP header, a meta tag, an nginx or Apache line, or a Next.js
headers()block.
What the main directives control
default-srcis the fallback for any directive you do not set.script-srcandstyle-srccontrol JavaScript and CSS. Scripts matter most.img-src,font-src,media-srcandconnect-srccontrol images, fonts, audio or video, and fetch, XHR and WebSocket requests.frame-srccontrols what you embed, andframe-ancestorscontrols who may embed you.object-src 'none'blocks old plugins, andbase-uri 'self'stops injected base tags.form-actionrestricts where forms can submit.
Rolling it out safely
- Deploy as report-only and browse every page type, including checkout and the admin area.
- Fix or allow the violations you find. Third-party widgets, fonts and analytics are the usual surprises.
- Switch to enforcing, keeping a short rollback plan.
- Re-check after adding any new third-party script.
Remember
CSP is a second line of defence. It does not replace escaping output and validating input. Test the final header with your browser's developer tools, which list each blocked request and the rule that blocked it.
Frequently asked questions
What is a Content-Security-Policy?
It is a response header that tells the browser which sources of scripts, styles, images and frames the page may use. If an attacker injects a script from an unlisted source, the browser refuses to run it.
Should I start in report-only mode?
Yes. Content-Security-Policy-Report-Only logs violations without blocking anything, so you can find what your site loads before you enforce the policy. Add a report URI to collect them.
Why avoid unsafe-inline?
It allows any inline script, including injected ones, which removes most of the protection. Use nonces or hashes for the inline scripts you need, and move styles to files where possible.
Why is frame-ancestors missing from the meta tag?
Browsers ignore frame-ancestors, report-uri and the sandbox directive when a policy is delivered through a meta tag. Use the HTTP header to get them.
Is my policy uploaded?
No. It is built in your browser.