Guides · Security
Content Security Policy (CSP): a practical guide
Published 18 November 2026 · Updated 18 November 2026
In short: a Content-Security-Policy header tells the browser which sources a page may load scripts, styles, images, frames and connections from, so injected code from anywhere else is blocked. Start from default-src 'self', add only the hosts you really use, test in Report-Only mode, then enforce.
What is a Content Security Policy?
A Content Security Policy is an HTTP response header, Content-Security-Policy, that lists the sources a browser may load content from for a page. Anything not on the list is blocked and reported in the console. Its main job is to limit cross-site scripting: even if an attacker injects a script tag, the browser refuses to run it.
CSP also covers clickjacking through frame-ancestors, where forms may submit with form-action, and automatic upgrading of http links with upgrade-insecure-requests. It is defined in the W3C Content Security Policy specification and supported by every current browser.
What does a CSP header look like?
A CSP is one header made of directives separated by semicolons. Each directive names a content type and the sources allowed for it. The example below suits a small site that loads Google Analytics: everything else may come only from the site itself, plugins are off and other sites cannot frame it.
Content-Security-Policy: default-src 'self'; script-src 'self' https://www.googletagmanager.com; style-src 'self'; img-src 'self' data: https:; connect-src 'self' https://www.google-analytics.com; font-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'; upgrade-insecure-requests'self' means your own origin (same scheme, host and port). Quotes matter: 'self', 'none' and nonces are written in single quotes, host names are not.
Which CSP directives matter most?
Start with default-src, which is the fallback for most content types, then script-src, because scripts are what attackers inject. Add object-src 'none', base-uri and frame-ancestors, which close common bypasses and clickjacking. Set style-src, img-src, connect-src and font-src only where your pages need more than the default.
default-src: the fallback for fetch directives that are not set.script-src: where scripts may load from. The most important directive for stopping XSS.style-src,img-src,font-src: stylesheets, images and fonts.connect-src: where scripts may send requests with fetch, XHR or WebSocket. Analytics and APIs go here.frame-src: which sites you may embed, such as a video or payment frame.frame-ancestors: which sites may embed you.'none'replaces X-Frame-Options: DENY.object-src 'none'andbase-uri: close two well-known ways around a policy.form-action: where forms may submit.
Should I use nonces, hashes or 'unsafe-inline'?
Avoid 'unsafe-inline' for scripts, because it lets any injected inline script run and removes most of the protection. Use a nonce, a fresh random value sent in the header and on each script tag, or a hash of each inline script. Adding 'strict-dynamic' lets scripts loaded by a trusted script run too.
Content-Security-Policy: script-src 'nonce-R4nd0m' 'strict-dynamic'; object-src 'none'; base-uri 'none'
<script nonce="R4nd0m" src="https://example.com/app.js"></script>A nonce must be new on every response, so it needs a server or edge function. Static sites with a few fixed inline scripts can use 'sha256-…' hashes instead. Styles are lower risk; many sites keep 'unsafe-inline' in style-src at first and tighten it later.
How do I roll out a CSP without breaking my site?
Send the policy as Content-Security-Policy-Report-Only first. It blocks nothing and only reports what would have been blocked. Browse every important page, including checkout, forms, logged-in areas and embeds, add the sources you really need, and switch to the enforcing header once the reports stay clean for a week or two.
Content-Security-Policy-Report-Only: default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'; report-uri https://example.com/csp-reports- List the outside services your pages load: analytics, fonts, chat widgets, payment and video embeds.
- Build a first policy with the CSP generator and deploy it in Report-Only mode.
- Open the browser console on each page type and read the violations. Each one names the blocked URL and directive.
- Add the missing hosts, or move inline scripts to files or nonces.
- Switch the header name to
Content-Security-Policyand keep watching reports after each release.
Which CSP mistakes are most common?
The most common mistakes are adding 'unsafe-inline' and 'unsafe-eval' to script-src so nothing is really restricted, allowing whole schemes such as https: or wide wildcards, forgetting connect-src so analytics silently stops, putting frame-ancestors in a meta tag where it is ignored, and sending two different policies from the CDN and the app.
'unsafe-inline'inscript-srcwithout a nonce or hash. Injected inline scripts still run.https:or*as a script source. Any HTTPS site can then serve scripts to your page.- Allowing a large shared host for scripts, such as a public CDN that serves files from anyone. An attacker can host a script there too.
- No
connect-srcentry for analytics or your API, so requests fail quietly. frame-ancestorsin a<meta>tag. Browsers ignore it there.- A policy set both at the CDN and in the application. Both are enforced together.
How do I check my CSP?
Run the free security headers check on your page. It reads the Content-Security-Policy header and flags a missing policy, 'unsafe-inline' or 'unsafe-eval' in script-src, and wildcard sources, together with the other security headers, and gives an A to F grade. Use the CSP generator to build or rebuild a policy.
Read Security headers explained for how CSP fits with HSTS, X-Frame-Options and the other headers, and the X-Frame-Options guide for clickjacking.
Free tool: CSP generator
Tick what your site loads and copy a ready Content-Security-Policy for Nginx, Apache, Cloudflare or a meta tag. Runs in your browser.
Frequently asked questions
Does a CSP replace input validation and output escaping?
No. A Content-Security-Policy is a second line of defence that limits the damage when an injection bug slips through. You still need to validate input and escape output; CSP makes a missed case much harder to exploit.
Can I set CSP in a meta tag?
Yes, for most directives, with <meta http-equiv="Content-Security-Policy">. Browsers ignore frame-ancestors, report-uri and sandbox in a meta tag, so clickjacking protection and reporting need the HTTP header.
Does CSP affect SEO or Google rankings?
Google has not said that CSP is a ranking factor. It can hurt indirectly if a policy blocks your own scripts, styles or analytics, which is why you test in Report-Only mode first.
What happens if a page sends two CSP headers?
The browser enforces both, so content must pass every policy. That usually means the stricter one wins and something breaks. Set the policy in one place, either the CDN or the application, not both.