What is CSP (Content Security Policy) — and why it blocks your own script first

You added one header — and the page went white. The console is red: Refused to execute inline script. Your script, your file, your hosting. Blocked by the browser.
Here's the thing about CSP: it can't tell your code from someone else's. It can only tell where the code came from. So the first thing it kills is whatever is written straight into the HTML — which is, most likely, yours.
What CSP is, in plain words
Content Security Policy (CSP) is a response header where your server tells the browser: "on this page, run scripts only from here, load images only from there, and don't talk to anyone else."
The most modest policy looks like this:
Content-Security-Policy: default-src 'self'
It reads: everything — scripts, styles, images, requests — from your own domain only. default-src acts as the fallback for the other directives: didn't set img-src? Images fall back to it.
Why bother. If a foreign script ever lands on your page — through a comment, a leaky widget, a swapped file on a CDN — the browser will run it without asking. It's in your HTML, after all. CSP takes that right away: not on the list, not executed.
The browser doesn't know which script is yours
This is the part to accept. To the browser, a page is just text. The script you typed by hand and the script that slipped in through a form look identical: both are a <script> tag inside HTML.
So CSP can't judge by content. It judges by source:
- a file from your domain is yours, if
'self'is on the list; - a file from another domain is yours only if you named that domain explicitly;
- code written directly in the HTML belongs to nobody — it has no address.
Hence the rule that saves hours of debugging: CSP doesn't break "bad" code, it breaks address-less code. An onclick="..." handler in a tag, styles inside the page, eval() in an old library — all of it is code with no source.
By the way, eval() and friends get disabled automatically the moment your policy has a default-src or script-src. That's not a separate switch, it's part of the deal.
Three ways to allow your own scripts
By source. script-src 'self' https://cdn.example.com — run files from my domain and from that address. The cheapest path: move all inline code into separate files and forget about it.
By one-time number (nonce). For every response the server generates a random string and puts it both in the header and in the nonce attribute of each of its scripts. The browser compares the two. Foreign code can't guess the number. Important detail: the string must be new on every response — so plain static HTML can't do this, you need a server that injects it on the fly.
By hash. You compute SHA-256 of a script's content and put the hash in the policy. Fine for one or two frozen snippets, painful for a living project: change a space, update the hash.
And the trap everyone hits. If 'unsafe-inline' sits next to 'nonce-...' in the same directive, the browser ignores 'unsafe-inline'. That's by design: it's a fallback for old browsers that don't know nonces. A modern browser sees the nonce — and will demand one from every inline script, no matter how many permissions you add.
Report-Only: a policy that breaks nothing
The most useful button in the whole topic. The Content-Security-Policy-Report-Only header works the same way but doesn't block: the browser counts violations and sends reports to the address you gave, while the page keeps working.
Content-Security-Policy-Report-Only: default-src 'self'; report-to csp-endpoint
The order that doesn't take your site down: turn on Report-Only → watch for a week what would break → add the missing sources → only then switch to the enforcing header. You can keep both at once: one enforces, the other quietly tests the next version of your policy.
Two things that save an evening. Report-Only cannot be delivered via a meta tag — only as a real response header. And if you're vibe-coding on static hosting, the policy goes into the host's config rather than your markup — how that's done on Vercel, Netlify and nginx is in the security headers guide.
What to do with all this
Take one mental image away: CSP is a guest list on the door, not a metal detector. It checks where you came from, not what you're carrying. So:
- start with
default-src 'self'in Report-Only; - move inline scripts into files — that fixes 80% of violations;
- add third-party domains one at a time, guided by reports, not "just in case."
If the policy is already on and something is broken, the symptoms and three common causes are covered separately: why CSP blocks my own scripts.
Does a small site need CSP?
If the site is static with no third-party widgets, the gain is small. The moment you add a form, comments, a chat or external analytics, CSP becomes a second layer in case foreign code slips through them.
Does CSP protect against everything?
No. It limits where code on the page may come from, and doesn't replace validating data on the server. A CORS error, for example, has nothing to do with CSP: that's a different browser rule, about reading other sites' responses.
Where exactly do I write this header?
Wherever the response is built: the hosting config, nginx, your framework's middleware. A meta tag can only carry the enforcing policy, and even then with caveats — the header is more reliable.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





