Content-Security-Policy Header Not Set: How to Fix It
TL;DR: Add this header, in report-only mode first:
Content-Security-Policy-Report-Only: default-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'; form-action 'self'
Watch the browser console for a few days, allow what your site really needs, then rename the header to Content-Security-Policy. You can test any policy with our CSP evaluator.
What the warning means
Scanners like ZAP, Nessus and Qualys print "Content Security Policy (CSP) header not set" when your page response has no Content-Security-Policy header. That's all it checks. It doesn't mean someone hacked you.
CSP is a list of rules you send to the browser. It says where scripts, styles, images and frames may load from. If an attacker finds a way to inject a <script> into your page (a cross-site scripting bug, XSS), the browser checks the script against your rules and blocks it if it doesn't match.
So the real risk is this: without CSP, any XSS bug you have works at full power. With a good CSP, many of them stop working. MDN calls CSP defense in depth and says plainly it's "not an alternative to sanitizing input". You still need to fix the bugs. CSP is the second line.
It also covers clickjacking through the frame-ancestors directive. More on that in our X-Frame-Options guide.
Why start with report-only
A CSP that is too strict breaks your site. Inline scripts stop, analytics stop, fonts from a CDN stop. The Content-Security-Policy-Report-Only header uses the same rules but only reports what it would block. Nothing breaks.
Open your site with DevTools open and look at the Console. Each blocked item shows up with directive that blocked it. Add the sources you trust, reload, repeat. When the console is clean on your main pages, switch the header name.
If you want reports sent to a server, add report-to and a Reporting-Endpoints header. MDN suggests sending the older report-uri too, because report-to doesn't have full browser support yet.
Real example: when we ran curl -sI https://www.google.com on December 5, 2026, it returned a content-security-policy-report-only header with a nonce and 'strict-dynamic'. Big sites test this way too.
The policy, explained
default-src 'self': by default, load things only from your own origin.object-src 'none': no Flash-style plugins.base-uri 'self': stops an injected<base>tag from changing where relative URLs point.frame-ancestors 'self': only your own site may put your pages in a frame.form-action 'self': forms can only post to your own site.
default-src 'self' is strict. Most real sites need extra sources, for example script-src 'self' https://www.googletagmanager.com. For the strongest setup, MDN recommends a nonce-based policy (script-src 'nonce-RANDOM') where the server makes a new random value per response. That needs server-side code, so start with the simple one.
How to add it on your server
Every example below uses the report-only header. When you are ready, change only the header name.
nginx
server {
listen 443 ssl;
server_name example.com;
add_header Content-Security-Policy-Report-Only "default-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'; form-action 'self'" always;
}
Two things trip people up. Without always, nginx skips the header on many status codes, including 404 and 500. And add_header is inherited from the parent block only if the current block has no add_header of its own. One add_header inside a location drops all the server-level ones for that location. Repeat them there, put them in a snippet you include, or on nginx 1.29.3+ use add_header_inherit merge; (nginx docs).
Apache (.htaccess)
Header always set Content-Security-Policy-Report-Only "default-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'; form-action 'self'"
Needs mod_headers (a2enmod headers on Debian and Ubuntu) and AllowOverride FileInfo for the folder. always also covers error pages and redirects.
Caddy
example.com {
header Content-Security-Policy-Report-Only "default-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'; form-action 'self'"
root * /var/www/html
file_server
}
Keep the quotes, the value has spaces.
IIS (web.config)
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Content-Security-Policy-Report-Only" value="default-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'; form-action 'self'" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
This is the customHeaders element from Microsoft's docs. It works for classic ASP.NET and for ASP.NET Core apps hosted in IIS.
Cloudflare (Transform Rules / _headers for Pages)
Proxied site: go to Rules > Overview > Create rule > Response Header Transform Rule. Pick all incoming requests, choose Set static, header name Content-Security-Policy-Report-Only, paste the value, then Deploy.
Cloudflare Pages: put a file named _headers in your build output folder:
/*
Content-Security-Policy-Report-Only: default-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'; form-action 'self'
Cloudflare's docs say _headers is not applied to responses from Pages Functions. Set the header in the function code for those.
Next.js (next.config.js headers())
// next.config.js
const csp = "default-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'; form-action 'self'";
module.exports = {
async headers() {
return [
{
source: '/(.*)',
headers: [{ key: 'Content-Security-Policy-Report-Only', value: csp }],
},
];
},
};
Next.js injects some inline scripts, so a strict script-src will report them. The Next.js CSP guide shows a nonce setup in proxy.js. Note that nonces force dynamic rendering for every page.
Express (helmet)
import express from 'express';
import helmet from 'helmet';
const app = express();
app.use(
helmet({
contentSecurityPolicy: {
useDefaults: false,
directives: {
defaultSrc: ["'self'"],
objectSrc: ["'none'"],
baseUri: ["'self'"],
frameAncestors: ["'self'"],
formAction: ["'self'"],
},
reportOnly: true,
},
}),
);
Plain app.use(helmet()) already sends an enforced CSP with sensible defaults. If a scanner says CSP is missing on a helmet app, search your code for contentSecurityPolicy: false.
Spring Boot
Spring Security doesn't add CSP by default. Add it to your existing SecurityFilterChain:
@Bean
SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.headers(headers -> headers
.contentSecurityPolicy(csp -> csp
.policyDirectives("default-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'; form-action 'self'")
.reportOnly()));
// keep your authorizeHttpRequests(...) and other config here
return http.build();
}
Remove .reportOnly() to enforce.
WordPress (plugin-free option)
Add this to your child theme's functions.php or to a small file in wp-content/mu-plugins/:
add_action( 'send_headers', function () {
header( "Content-Security-Policy-Report-Only: default-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'; form-action 'self'" );
} );
Expect a lot of reports. Most themes and plugins use inline scripts and styles. That's normal, its exactly why you start in report-only mode. This hook only runs for pages PHP renders, not for images or CSS your web server sends directly.
How to check it worked
curl -sI https://example.com | grep -i content-security-policy
You should see your header line. Then see the full picture with our live check, for example github.com's headers, or run your own domain in the security headers tool. Paste the policy into the CSP evaluator to spot weak spots.
Common mistakes
- Missing quotes.
'self','none'and'unsafe-inline'need the single quotes. Without them,selfmeans a host called "self". - Adding
'unsafe-inline'to script-src to silence reports. It allows any inline script, which is what XSS uses. Move scripts to files or use nonces. - Two CSP headers. If your app and your proxy both send one, the browser enforces both. Content has to pass every policy.
- Using a meta tag for everything.
<meta http-equiv="Content-Security-Policy">ignoresframe-ancestors, and report-only can't be sent that way at all. - Staying in report-only forever. Report-only blocks nothing. Set a date to switch.
- Testing only the home page. Check login, checkout and admin pages too.
FAQ
Is "CSP header not set" a real vulnerability?
Not on its own. It's a missing layer of defense. Nobody can attack you just because the header is absent. But if you have an XSS bug, CSP is what limits the damage.
What is a good content security policy header example?
default-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'; form-action 'self' is a safe start. Add the third-party sources your site needs, and move to nonces when you can.
How do I set the CSP header in web.config?
Add an <add name="Content-Security-Policy" value="..." /> line under system.webServer/httpProtocol/customHeaders, like the IIS example above.
How do I add a CSP header in a React app?
A React app built with Vite or Create React App is static files, so set the header where those files are served: nginx, Cloudflare Pages _headers, or your host's config. If you use Next.js, use headers() in next.config.js.
Do I need X-Frame-Options if I have CSP?
frame-ancestors replaces it in modern browsers, but sending both is cheap. See X-Frame-Options vs frame-ancestors. While you're here, check HSTS too, scanners usually flag them together.
Is your site affected? Run the free P-256 check.