How to add a Content-Security-Policy header
A Content-Security-Policy tells the browser which places scripts, styles, images and frames may load from. If an attacker manages to inject a script into one of your pages, a good policy stops the browser running it. It is the strongest single header you can add, and the one most often got wrong.
Updated 2026-10-23
Why it matters
- It limits the damage of cross-site scripting bugs, which are among the most common web vulnerabilities.
- It also stops your pages loading content from places you didn't intend, such as an unknown third-party script.
- Report-only mode lets you learn what your site loads before anything is blocked.
How to fix it
- Add this header to every response from example.com.
- This starts a Content-Security-Policy in report-only mode, which never blocks anything. Watch the browser console for violations, adjust the policy, then rename the header to Content-Security-Policy to enforce it.
Pick what your site runs on. Replace example.com with your own domain where it appears.
After you deploy, re-check the site. A CDN or the browser cache can keep serving the old headers for a few minutes.
nginx
This starts a Content-Security-Policy in report-only mode, which never blocks anything. Watch the browser console for violations, adjust the policy, then rename the header to Content-Security-Policy to enforce it.
add_header Content-Security-Policy-Report-Only "default-src 'self'" always;
Apache
This starts a Content-Security-Policy in report-only mode, which never blocks anything. Watch the browser console for violations, adjust the policy, then rename the header to Content-Security-Policy to enforce it.
Header always set Content-Security-Policy-Report-Only "default-src 'self'"
Caddy
This starts a Content-Security-Policy in report-only mode, which never blocks anything. Watch the browser console for violations, adjust the policy, then rename the header to Content-Security-Policy to enforce it.
header {
Content-Security-Policy-Report-Only "default-src 'self'"
}Cloudflare
This starts a Content-Security-Policy in report-only mode, which never blocks anything. Watch the browser console for violations, adjust the policy, then rename the header to Content-Security-Policy to enforce it.
Content-Security-Policy-Report-Only: default-src 'self'
Vercel
This starts a Content-Security-Policy in report-only mode, which never blocks anything. Watch the browser console for violations, adjust the policy, then rename the header to Content-Security-Policy to enforce it.
{
"headers": [
{
"source": "/(.*)",
"headers": [
{
"key": "Content-Security-Policy-Report-Only",
"value": "default-src 'self'"
}
]
}
]
}Netlify
This starts a Content-Security-Policy in report-only mode, which never blocks anything. Watch the browser console for violations, adjust the policy, then rename the header to Content-Security-Policy to enforce it.
/* Content-Security-Policy-Report-Only: default-src 'self'
Next.js
This starts a Content-Security-Policy in report-only mode, which never blocks anything. Watch the browser console for violations, adjust the policy, then rename the header to Content-Security-Policy to enforce it.
const nextConfig = {
async headers() {
return [
{
source: "/(.*)",
headers: [
{ key: "Content-Security-Policy-Report-Only", value: "default-src 'self'" },
],
},
];
},
};
export default nextConfig;Express
This starts a Content-Security-Policy in report-only mode, which never blocks anything. Watch the browser console for violations, adjust the policy, then rename the header to Content-Security-Policy to enforce it.
app.use((req, res, next) => {
res.setHeader("Content-Security-Policy-Report-Only", "default-src 'self'");
next();
});Common mistakes
- Turning on an enforced policy written from memory. It will block your own analytics, fonts or payment widget. Start with Content-Security-Policy-Report-Only and read the console.
- Leaving 'unsafe-inline' and 'unsafe-eval' in forever. They are fine as a first step, but they remove most of the protection. Move to nonces or hashes when you can.
- Forgetting that every third-party script needs to be listed: analytics, chat widgets, tag managers, embedded video.
- Setting it only on some pages. Set it on every response.
Check that it worked
Run your domain through the free security headers checker. It takes a few seconds, needs no account, and shows anything still missing.
Open the security headers checkerQuestions
- What is report-only mode?
- The browser checks every load against your policy and reports violations in the developer console, but blocks nothing. Once the console is quiet while you use the site, rename the header to Content-Security-Policy to enforce it.
- Can I get reports sent to me?
- Yes. Add a report-to or report-uri directive pointing at an endpoint that collects them. Several hosted services exist for this. It's optional: the browser console is enough for a small site.
- Is X-Frame-Options still needed?
- The frame-ancestors directive in a Content-Security-Policy does the same job, but sending X-Frame-Options as well covers older browsers.
Related guides
Don't want to check by hand? Upzy watches your site around the clock and tells you when a header, certificate or email record slips. Start free.