How to add a Content-Security-Policy
Published 21 October 2026 · 6 min read
A Content-Security-Policy (CSP) is a single line you add to your app's responses that tells the browser exactly which scripts, styles, and other resources it is allowed to load. Add it, and the browser will refuse to run anything that isn't on your approved list — including malicious code an attacker manages to sneak onto your page.
The quickest safe starting point is a policy that only allows resources from your own site. You set it as an HTTP response header, usually in your hosting platform's settings or a proxy in front of your app. Below is what it does, a starter policy you can copy, and the one gotcha (inline scripts) that trips most people up.
What a CSP actually protects against
The main threat it blocks is cross-site scripting, or XSS. That's when an attacker gets their own JavaScript to run inside your app — often through a form field, a URL, or user-generated content that wasn't properly cleaned. Once their script runs, it can read your users' sessions, steal data they typed, or impersonate them.
A CSP is what security people call “defense in depth.” It doesn't fix the underlying bug that let the script in, but it gives the browser a second chance to stop it: if an injected script tries to run from a source you didn't approve, the browser simply blocks it. Even a well-built app benefits, because it catches mistakes before they become incidents.
A safe starter policy
Here is a sensible, locked-down policy that only trusts your own domain:
default-src 'self'— the fallback rule: only load things from your own site unless a more specific rule says otherwise.script-src 'self'— only run JavaScript served from your own domain.style-src 'self'— only load stylesheets from your own domain.img-src 'self' data:— allow your own images, plus small inlinedata:images that many frameworks use.object-src 'none'— block legacy plugins like Flash entirely.frame-ancestors 'none'— stop other sites from embedding your app in an iframe (clickjacking protection).base-uri 'self'— prevent attackers from rewriting where relative links point.
Written as one header, that's:
Content-Security-Policy: default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; object-src 'none'; frame-ancestors 'none'; base-uri 'self'
If you load fonts, analytics, or a payment widget from another domain, you'll need to add that domain to the matching rule (for example script-src 'self' https://js.stripe.com). Add only what you actually use.
How to add it
A CSP is delivered as a response header, so you set it wherever your app's responses are configured — not inside your page's HTML. Depending on your setup:
- Hosting platform settings: many hosts (Netlify, Vercel, Cloudflare Pages and others) let you add custom headers in a config file or dashboard.
- A proxy or CDN in front of your app: services like Cloudflare can inject the header for every request without touching your code.
- Your server or framework: if you control the backend, set the header on outgoing responses.
Test before you enforce. You can ship Content-Security-Policy-Report-Only first — it logs what would be blocked without actually blocking anything. Watch your browser's console and the reports, loosen the policy until nothing legitimate breaks, then switch to the real enforcing header.
The one big gotcha: inline scripts
The strict policy above blocks inline JavaScript — code written directly inside the page, like onclick="..." attributes or <script>...</script> blocks. That's intentional, because inline scripts are exactly how most XSS attacks run. But it also means a policy that's too strict can break your own app.
You have two options:
- Avoid it (best): move your code into separate
.jsfiles served from your own domain. Thenscript-src 'self'just works. - Allow it carefully: if you can't move everything, use a nonce — a one-time random token your server adds to both the header and each trusted script tag. Only scripts carrying the matching token run.
Avoid 'unsafe-inline' if you possibly can. It switches off the single most valuable protection a CSP gives you, and once it's there it tends to stay forever.
Why this matters for no-code and AI-built apps
Many no-code and AI-builder platforms don't set a CSP for you by default. Your app can look finished and work perfectly while shipping no CSP at all — which means the browser has no instructions to fall back on if something goes wrong. It's one of the most common gaps we see, and it's usually a quick fix once you know where to set headers on your platform.
Check your app in 30 seconds
Faultline scans no-code & AI-built apps for exactly these issues — free.
Run a free scanFaultline's free surface scan works on any website — it checks your TLS setup, security headers (including whether a CSP is present), and your DNS and email records, then tells you in plain English what's missing. If you'd rather check by hand, open your browser's developer tools, reload your page, and look at the response headers for your main document; no Content-Security-Policy line means you don't have one yet. Our deeper data-exposure engine currently specializes in Base44, with more platforms rolling out.
Related: The no-code security checklist · The #1 flaw in AI-built apps