In short
Content Security Policy is an HTTP header that tells browsers which scripts and other resources a page may load. Its main purpose is limiting the damage of cross-site scripting. Most sites that handle logins, payments or personal data should have one. Start with Content-Security-Policy-Report-Only, fix what it flags, then enforce it. Prefer nonces or hashes over long domain allow-lists, which are often bypassable.
What it does
By default, a browser will run any script that appears in your page. So if an attacker finds a way to inject one, for example through a comment field, it runs with full access to the page. That's cross-site scripting, or XSS.
Content Security Policy adds a second line of defence. You send a header listing what's allowed, and the browser blocks anything else. MDN describes its main purpose as defending against XSS, by controlling which resources a page may load. It can also help prevent clickjacking, and help enforce HTTPS.
CSP doesn't replace proper output escaping. It limits the damage if something gets through.
Do you need one?
| Your site | Recommendation |
|---|---|
| App with logins, payments or personal data | Yes |
| E-commerce | Yes; payment page scripts are a target |
| Site with user-generated content | Yes |
| Admin panels | Yes |
| Simple brochure site with a form | Useful; a basic policy is fairly easy |
| Static site with no scripts | Easy to add, so why not |
A simple example
Content-Security-Policy:
default-src 'self';
script-src 'self';
style-src 'self';
img-src 'self' data:;
object-src 'none';
base-uri 'none';
frame-ancestors 'none';
In plain English, this says that:
default-src 'self': by default, load things only from my own domain.script-src 'self': only run scripts from my domain, with no inline scripts.img-src 'self' data:: images can come from my domain or be embedded.object-src 'none': no plugins.base-uri 'none': stops attackers from changing how relative URLs resolve.frame-ancestors 'none': nobody can put my site in a frame, which helps prevent clickjacking.
The header must be sent as one line in practice. See how to secure a static site with HTTP headers for the other headers worth adding.
Why long allow-lists are weak
A common first attempt looks like this:
script-src 'self' https://www.googletagmanager.com https://cdn.example.com https://*.google.com;
It looks strict, but it can be bypassable. MDN notes that domain allow-lists are often bypassable, for example when an allowed domain hosts other content that an attacker can use. They're also hard to maintain.
Strict CSP: nonces and hashes
The recommended approach is a strict CSP, using nonces or hashes.
With a nonce, your server generates a new random value for each response:
Content-Security-Policy: script-src 'nonce-R4nd0mV4lue' 'strict-dynamic'; object-src 'none'; base-uri 'none';
Then you add it to each legitimate script:
<script nonce="R4nd0mV4lue" src="/app.js"></script>
An injected script doesn't know the nonce, so it won't run. 'strict-dynamic' lets a trusted script load other scripts, which makes it practical for real apps. The nonce must be unpredictable and different for every response, so never hard-code it.
src="/app.js">
// runs
// no nonce, so it's blocked
Hashes work for static pages, in which you can't generate a value for each response. You list a hash of each inline script's contents.
Rolling out without breaking your site
Don't turn on enforcement immediately. Browsers offer a test mode:
- Start in report-only mode. Send
Content-Security-Policy-Report-Only, which logs violations without blocking anything. - Collect reports. Check the browser's console, and consider a reporting endpoint.
- Remove inline handlers. Things like
onclick="…"andstyle="…"will need to move, or need other arrangements. Move these into files. - List your third-party scripts. Analytics, chat, payments, tag managers. Remove what you don't need, as every script is a risk.
- Fix each violation, or make a conscious decision to allow it.
- Switch to enforcing, and keep monitoring.
- Review after each new integration.
Test on staging first, using all your key journeys, such as login and checkout. Many frameworks, including Next.js, have documentation for setting up nonces. If you're using a builder platform, check what it allows.
What about third parties?
Tag managers are a common difficulty, because they exist to load arbitrary scripts, which is the opposite of what CSP is trying to do. You may need to accept weaker rules on some pages, or use the tag manager's recommended setup. A sensible compromise is a strict policy for your checkout, login and admin pages, where risk is highest. Reducing third-party scripts improves your speed as well. See how to choose a third-party dependency.
Payment providers usually publish the CSP entries they need. Follow their documentation.
Common mistakes
'unsafe-inline'in script-src. This removes most of the protection, since injected inline scripts can run.'unsafe-eval', unless you have no alternative.- Wildcards such as
https:or*. - Reusing a nonce across responses, or caching a page that contains one.
- Testing only the homepage.
- Setting the policy only on some routes, when others need it too.
- Assuming a meta tag is equal to a header. Some directives, such as
frame-ancestors, don't work through a meta tag. - Forgetting report-only isn't protection. Remember to switch it on.
Checking your policy
Open developer tools, and look at the console for CSP errors. Google's CSP Evaluator can review your policy, too. Then test whether it works: in a staging environment, inject a test script, and confirm that it's blocked.
Where it fits
CSP is one layer among several. You still need to escape output, validate input, keep dependencies updated and handle secrets properly. See how to prevent XSS and the vibe-coded app security checklist. AI-generated apps very often ship with no CSP at all, so it's worth checking yours.
If you'd like help setting up security headers without breaking your site, book a call.
Common questions
What does a Content Security Policy do?
It tells the browser which scripts, styles, images and other resources a page may load. If an attacker injects a script that isn't allowed, the browser blocks it. Its main purpose is to reduce the impact of cross-site scripting.
Do I need a CSP on a simple website?
It's helpful, and usually fairly easy on a simple site. It's most important for anything with logins, payments, personal data or user-generated content. A basic policy costs little, and it adds protection in case something else goes wrong.
What is CSP report-only mode?
It's a way of testing a policy without enforcing it. Sending the Content-Security-Policy-Report-Only header makes browsers report violations, while still loading everything. Use it to find problems before you switch to the enforcing header.
Is unsafe-inline bad in a CSP?
In script-src, yes. It allows inline scripts, which is how many XSS attacks run, so it removes most of the benefit. Use nonces or hashes instead. It's less risky for styles, although strict setups try to avoid it there too.