CSP Header Builder
Build a Content-Security-Policy header that matches how your site actually works. Pick a strictness preset, tick the third-party services you really use - Google Fonts, analytics, YouTube embeds, CDNs - and the builder assembles a complete policy, scores how restrictive it is, explains every directive in plain English, and hands you ready-to-paste nginx, Apache, Cloudflare, Django or Express configuration.
1. Start from a preset
The preset sets your baseline strictness. Everything after this is additive - each requirement only opens the specific doors it needs.
2. Add your site's requirements
Tick only what you actually use. Every tick adds the minimum sources needed for that service - nothing more.
3. Frame & network options
Who may embed your page, and whether to force upgrade insecure requests.
4. Custom directives (optional)
One directive per line: directive value1 value2. Custom lines override the preset and requirement values for that directive.
Your policy
Rebuilds live as you change anything above.
Content-Security-Policy: | Directive | Sources | What it controls | Assessment |
|---|
How to use the CSP Header Builder
The builder works in four steps, top to bottom. First, choose a preset: Strict is default-deny and suitable for sites that have already removed inline scripts and styles; Balanced is the right starting point for most modern sites; Permissive exists so legacy sites can adopt CSP at all in the first place. Second, tick the services your site genuinely uses - fonts, analytics, video embeds, CDNs - and enter a domain where a service runs on infrastructure you control. Third, decide who may embed your page with frame-ancestors and whether to force-upgrade insecure requests. Finally, add any custom directives the presets do not cover, such as a checkout domain in form-action.
The output updates live. Copy the header straight into your web server, or use one of the export tabs for the exact syntax of nginx, Apache, Cloudflare Transform Rules, Django middleware or Express middleware. If you are not confident the policy is complete, enable Report-Only mode: the browser will log violations to your report endpoint without blocking anything, so you can see exactly what a strict policy would break before you turn it on for real.
Worked example
Say you run a Django blog with Google Fonts, Matomo analytics self-hosted at stats.example.com, and the occasional YouTube embed. You would pick the Balanced preset, tick Google Fonts, Self-hosted analytics (entering stats.example.com) and YouTube embeds, leave frame-ancestors at the preset value of 'self', and enable upgrade-insecure-requests. The builder produces:
Content-Security-Policy: default-src 'self'; script-src 'self' www.youtube.com www.youtube-nocookie.com; style-src 'self' fonts.googleapis.com; img-src 'self' data: *.ytimg.com i.ytimg.com; font-src 'self' fonts.gstatic.com; connect-src 'self' stats.example.com; object-src 'none'; base-uri 'self'; frame-ancestors 'self'; frame-src www.youtube.com www.youtube-nocookie.com; upgrade-insecure-requestsEvery source in that header exists because you asked for it. The score comes back high, the per-directive table shows each line is assessed strict, and the nginx tab gives you the add_header ... always; line to drop into your server block. Nothing about your site - not the domain name, not the analytics host - left your browser while you built it.
How it works
A Content-Security-Policy header is a list of directives. Each directive names a kind of resource - scripts, styles, images, fonts, connections, frames - and the list of sources that are allowed to supply it. When the browser needs to fetch something, it checks the matching directive; if no directive covers that fetch type, it falls back to default-src; if there is no default-src either, the request is unrestricted.
The builder composes your policy in that same order: the preset provides the baseline directives, each ticked requirement appends sources to the specific directives it needs (YouTube touches frame-src, script-src and img-src; Google Fonts touches style-src and font-src), your frame and network options override the embedding and upgrade behaviour, and any custom lines you write take precedence over everything else. The strictness score then walks the finished policy and deducts points for each known weakness - missing default-src, 'unsafe-inline' in scripts, an unrestricted object-src, http sources or scheme-wide wildcards - so you can see at a glance how much protection the header actually buys.
Restrictive versus permissive rules
The single most important choice in any CSP is the baseline: what happens to a resource type nobody thought to list? A restrictive policy answers "block it" by setting default-src 'self' and then explicitly opening only the doors that are needed. Under that model, a new third-party script you add next month will simply fail to load until you approve it - which is exactly the behaviour you want, because it forces every dependency through a conscious decision. A permissive policy answers "allow it" with sources like https: or *, or by omitting directives entirely. Such policies still block plugins and stop some attack vectors, but an attacker who has already injected a script can load that script from any https origin on the internet, which removes most of the protection CSP exists to provide.
The same gradient applies inside individual directives. For scripts, the order from strongest to weakest is: named origins only, then 'self' plus named origins, then 'unsafe-inline' (which re-enables inline scripts and event handlers and largely defeats XSS protection), then 'unsafe-eval' (dynamic code execution). For styles, 'unsafe-inline' is far less dangerous because style injection cannot run code on its own - it is a cosmetic and layout risk rather than an execution risk. The builder flags every one of these weakenings in the score notes and in the per-directive table, so a policy can never quietly drift permissive without you noticing.
Common CSP mistakes
- Forgetting that
default-srcis the fallback. A policy with nodefault-srcleaves every unlisted fetch type completely open. The builder deducts 30 points for this - it is the most common real-world gap. - Adding
'unsafe-inline'toscript-src"to make it work". That silently disables CSP's core XSS protection. The correct fix is nonce-based or hash-based allowances, or moving inline code into external files - the builder marks this toggle as a weakening so you know what you are trading away. - Confusing
frame-srcwithframe-ancestors.frame-srccontrols what you may embed in iframes (YouTube);frame-ancestorscontrols who may embed you. They are opposite directions, and onlyframe-ancestorsbelongs in a Content-Security-Policy header for clickjacking protection. - Testing on localhost and forgetting the production origins. A policy tuned against
http://localhost:8000will block your CDN, your analytics and your checkout in production. Build the policy from the real list of services first, then verify with Report-Only mode against live traffic. - Sending the header only on success responses. In nginx, an
add_headerwithoutalwaysis dropped on 4xx/5xx responses - exactly the pages an attacker probes. All export tabs in this builder include the correct syntax for your server. - Turning a strict policy on directly. The safe rollout is: deploy in Report-Only mode, watch the violation reports for a week, fix the legitimate misses, then flip to enforcing. Report-Only mode here is one checkbox, not a second configuration.
Privacy
This page is a static document with client-side JavaScript. The preset logic, the requirement tables, the scoring and the config generation all run in your browser's JavaScript engine; there are no form submissions, no analytics calls from this tool and no server round-trips. Your domain names and policy never leave your machine, which matters because a CSP under construction is a useful map of your infrastructure for anyone eavesdropping.
Suggested
Luke Keam | techgeek.biz