Short answer
A Content Security Policy (CSP) is a response header that tells the browser which sources of scripts, styles, images and other resources a page may use, and blocks everything else. Its main job is to limit the damage of cross-site scripting: injected inline scripts and unknown script hosts are refused. It is an extra layer, not a replacement for escaping and sanitizing. Start in report-only mode.
Ask an AI about this page:ChatGPTClaude
On this page
A Content Security Policy (CSP) is a set of rules, sent by your site in the Content-Security-Policy response header, that tells the browser which sources of scripts, styles, images, frames and connections a page may use. Anything the policy doesn’t allow, the browser refuses to load or run. The current version is specified by the W3C as Content Security Policy Level 3, which is still a Working Draft.
A small policy looks like this:
Content-Security-Policy: default-src 'self'; img-src 'self' https:; frame-ancestors 'none'default-src 'self' allows resources only from your own origin, img-src adds images from any HTTPS host, and frame-ancestors 'none' stops other sites from embedding your app in a frame, which protects against clickjacking.
CSP in an AI-built app
A Vite or Lovable app is mostly JavaScript served from your own domain, which makes a tight policy realistic. The main benefit is against cross-site scripting: MDN explains that a CSP can block inline scripts, inline event handlers, javascript: URLs and eval() unless you explicitly allow them. If some user content ever slips into the page as HTML, the injected script still doesn’t run.
What an AI-built app usually needs to allow:
connect-srcfor your Supabase project URL (and its WebSocket address if you use Realtime), plus any API the browser calls directly,script-srcandframe-srcfor third-party widgets such as a payment form or analytics,img-srcfor avatars or images from storage.
A common mistake is “fixing” a blocked resource by adding 'unsafe-inline' or 'unsafe-eval' to script-src, which removes most of the XSS protection.
How to check your app
Open your live site, open the browser’s developer tools, go to the Network tab and click the first document request. Look for Content-Security-Policy in the response headers. If it’s missing, you have no policy. If it’s there, read the script-src (or default-src) part: 'unsafe-inline', 'unsafe-eval' or a bare https: there means the policy does little against XSS.
How to fix or prevent it
Roll it out in two steps, as MDN describes. First send Content-Security-Policy-Report-Only with your planned policy and a reporting endpoint; the browser reports violations without blocking anything. Once only expected sources remain, switch to the enforcing header. For script control, MDN recommends a strict policy with nonces or hashes over long host lists. Where and how to set headers for your hosting is covered in Security headers for Vite and Lovable apps. Pair it with HSTS, which keeps the connection itself on HTTPS.
Frequently asked questions
Does a CSP replace input sanitizing?
No. MDN says sites should sanitize input and set a CSP, as defense in depth. OWASP calls CSP an additional layer of defense. If your code renders untrusted HTML, fix that first.
Can I set a CSP with a meta tag in my Vite app?
Yes, a meta http-equiv tag works for single-page apps with static resources, but it doesn’t support every feature: report-only mode, for example, needs the HTTP header. Set the header at your host where you can.
How do I add a CSP without breaking my app?
Send it first as Content-Security-Policy-Report-Only. The browser then reports what would be blocked without blocking it. Fix or allow each legitimate source, then switch to the enforcing header.
Sources
- 1.Content Security Policy (CSP) — MDN Web Docs
- 2.Content Security Policy Level 3 — W3C
- 3.Cross Site Scripting Prevention Cheat Sheet — OWASP Cheat Sheet Series