Developer and Text Tools

How to Write a Content Security Policy That Does Not Break Your Site

A practical guide to Content Security Policy: directives, fallback, nonces and hashes, strict-dynamic, report-only roll-out and testing against real HTML.

Saizul Amin
Saizul Amin2 Nov 2026 · 4 min read
How to Write a Content Security Policy That Does Not Break Your Site

How to Write a Content Security Policy That Does Not Break Your Site

A Content Security Policy is one HTTP header that can turn a cross-site scripting bug from a disaster into a non-event. It tells the browser which sources of scripts, styles, images, fonts, frames and connections a page may use, and anything else is refused. The trouble is that a policy which is too loose protects nothing, and a policy which is too strict breaks the page. This guide explains how to find the middle ground, step by step, with the free Content Security Policy builder.

What a policy is made of

A policy is a list of directives separated by semicolons. Each directive names a type of resource and the sources allowed for it. default-src is the fallback for every fetch directive you do not set, script-src controls JavaScript, style-src controls CSS, img-src images, connect-src fetch and WebSocket calls, frame-src the pages you embed, and so on. A few directives have no fallback at all: base-uri, form-action and frame-ancestors only apply when you write them.

Sources can be keywords in single quotes, such as 'self' for your own origin or 'none' for nothing, schemes such as https: or data:, hosts with an optional wildcard subdomain such as *.example.com, or full paths. A host source without a scheme follows the scheme of the page, and http: quietly also allows https.

Why 'unsafe-inline' is the real problem

Most cross-site scripting attacks inject inline script: a script tag in a comment, or an onerror attribute in an image. If script-src contains 'unsafe-inline', the browser runs that code and the policy does almost nothing against XSS. The fix is to give each legitimate inline script a proof of origin. A nonce is a random value generated for each response and written both into the header and into the script tag. A hash is the SHA-256 of the exact inline code. Once a nonce or hash is present, browsers ignore 'unsafe-inline' entirely, which keeps the policy backwards compatible with old browsers while protecting new ones.

strict-dynamic for sites with loaders

Tag managers, chat widgets and analytics often insert more scripts at run time. Listing every host they might use is fragile and often unsafe, because popular CDNs and Google domains host endpoints that attackers can abuse. The keyword 'strict-dynamic' solves this: scripts trusted by nonce or hash may load further scripts, and host allowlists are ignored. A modern policy therefore often looks like script-src 'nonce-random' 'strict-dynamic' plus object-src 'none' and base-uri 'self'.

Build a first draft

Start from default-src 'self', object-src 'none', base-uri 'self', form-action 'self' and frame-ancestors 'self'. Then add what your site really needs. The builder's service menu adds the sources that vendors document for Google Analytics and Tag Manager, Google Fonts, Stripe, PayPal, reCAPTCHA, Cloudflare Turnstile, YouTube and Vimeo embeds, common CDNs and Sentry. The findings list and strength score update with every change and point out wildcards, missing directives and risky keywords.

Test against real HTML before deploying

Open your page, view its source and paste it into the test tab with the page address. The tester lists every resource and inline block, applies the directive fallback rules and the source matching rules, and tells you for each line whether it would be allowed or blocked and why. Blocked inline code shows its hash, so you can allow exactly that snippet. If you are starting from nothing, generating a policy from the page gives a reasonable first version.

Roll out in report-only mode

Even a carefully tested policy can miss something that JavaScript loads later. Send it first as Content-Security-Policy-Report-Only. The browser reports violations to the console and, if you configure report-to and a Reporting-Endpoints header, to your endpoint, but blocks nothing. After a few days of clean reports, switch to the enforcing header. When tightening an existing policy, you can send the old one enforcing and the new one report-only side by side.

Deliver it in the right place

Headers beat meta tags: frame-ancestors, sandbox and reporting are ignored when the policy is in a meta element. The builder writes the header for nginx, Apache, Netlify and Cloudflare Pages, Vercel, Next.js, Express with helmet and IIS. Remember that nonces must change on every response, so a nonce-based policy belongs in your application or edge function, not in a static configuration file.

Limits

A Content Security Policy reduces the impact of injection bugs; it does not fix them, so output escaping and input validation remain essential. The tester reads static HTML and cannot see resources added by JavaScript, which is why report-only testing in a browser matters. Browser support for the newest directives varies, and this guide is general advice, not a security audit of your site.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages