Home / Guides / How to Write a Strict Content Security Policy (CSP) Without unsafe-inline
How to Write a Strict Content Security Policy (CSP) Without unsafe-inline
A Content-Security-Policy (CSP) header tells the browser which scripts, stylesheets, images, fonts and network endpoints a page is allowed to load or execute. When configured strictly, CSP stops most cross-site scripting (XSS) and clickjacking attacks even if an injection bug exists in your application.
Unfortunately, many production sites ship policies that look protective on the surface but offer almost no real XSS defence because they leave 'unsafe-inline', 'unsafe-eval' or broad wildcards in script-src.
Why unsafe-inline, unsafe-eval and wildcards break CSP
If your policy contains script-src 'self' 'unsafe-inline' without a cryptographic nonce or hash, any attacker who manages to inject <script>...</script> or an inline event handler like <img src=x onerror=...> can execute arbitrary JavaScript. From the browserโs perspective, injected inline code is indistinguishable from your own inline code.
Watch out for these common pitfalls:
'unsafe-inline'inscript-srcordefault-src: Allows inline scripts andjavascript:URLs unless neutralized by a'nonce-...'or'sha256-...'hash.'unsafe-eval': Allowseval(),new Function()and string arguments tosetTimeout(), giving attackers easy execution gadgets.- Wildcard
*or whole schemes (https:,http:,data:): Allowingscript-src https:lets an attacker load malicious scripts from any HTTPS domain on the internet, including attacker-controlled CDNs or open-redirect hosts. - Missing
object-src 'none'andbase-uri: Withoutobject-src 'none', legacy plugin tags can execute payloads; withoutbase-uri 'self'(or'none'), injected<base href="https://evil.example/">tags hijack relative<script src="app.js">URLs.
Baseline directives for a strict modern CSP
Start from a deny-by-default baseline (default-src 'none') and grant only what each resource type genuinely needs:
default-src 'none'; script-src 'self'; style-src 'self'; img-src 'self' data:; font-src 'self'; connect-src 'self'; object-src 'none'; frame-ancestors 'none'; base-uri 'self'; form-action 'self'; upgrade-insecure-requests
What each directive locks down:
default-src 'none': Blocks every fetch directive not explicitly listed below.script-src 'self': Allows JavaScript only from your own origin. If you load third-party scripts from a CDN, pin exact hostnames and pair them with Subresource Integrity hashes generated with our SRI Hash Generator & Verifier.object-src 'none': Disables<object>,<embed>and<applet>.frame-ancestors 'none': Prevents other sites from embedding your pages in an<iframe>, replacing legacyX-Frame-Optionsfor clickjacking protection.base-uri 'self'&form-action 'self': Stops<base>hijacking and restricts where forms can submit data.upgrade-insecure-requests: Upgrades legacyhttp://subresource links tohttps://.
How to handle inline scripts without unsafe-inline
If your HTML templates rely on small inline <script> blocks (for example, applying a saved light/dark theme before first paint), you have three clean alternatives to 'unsafe-inline':
- Move scripts to static external
.jsfiles served from'self'. This works well for static sites and caches cleanly. - Use SHA-256 hashes: Compute the Base64 SHA-256 digest of the exact inline script body (excluding the
<script>tags) and list'sha256-...'inscript-src. - Use per-request nonces: On server-rendered pages, generate a fresh cryptographically random Base64 nonce on every HTTP response, add
'nonce-<value>'toscript-srcand<script nonce="<value>">. Never reuse or cache a nonce across responses.
Ready-to-copy Cloudflare Pages and Nginx examples
For Cloudflare Pages, place the header in public/_headers:
/*
Content-Security-Policy: default-src 'none'; script-src 'self'; style-src 'self'; img-src 'self' data:; font-src 'self'; connect-src 'self'; object-src 'none'; frame-ancestors 'none'; base-uri 'self'; form-action 'self'; upgrade-insecure-requests
For Nginx, use add_header with always so error responses carry the policy too:
add_header Content-Security-Policy "default-src 'none'; script-src 'self'; style-src 'self'; img-src 'self' data:; font-src 'self'; connect-src 'self'; object-src 'none'; frame-ancestors 'none'; base-uri 'self'; form-action 'self'; upgrade-insecure-requests" always;
Roll out safely with Report-Only mode
Deploying a strict CSP directly in enforce mode on an existing app can break third-party analytics, payment widgets or inline styles. Roll it out in two phases:
- Publish the policy using
Content-Security-Policy-Report-Onlyfirst. Browsers log every violation to DevTools (and optionally areport-toendpoint) without blocking anything. - Fix or pin each legitimate origin in your policy, then switch the header name to
Content-Security-Policy.
Audit and build your policy offline
Before deploying, paste your existing header into our CSP Analyzer & Builder to spot 'unsafe-inline', wildcards, duplicate directives and missing clickjacking rules, or use its visual Builder to export ready-to-copy Cloudflare and Nginx snippets. Once live, verify all of your siteโs response headers together with the HTTP Security Headers Checker.