Fix guides · Security
How to add a Content-Security-Policy (CSP) header without breaking your site
A Content-Security-Policy limits what a hacked or tampered page can do. Start with a safe policy that won't break anything, then tighten it. Steps for Shopify, WordPress, Webflow, Wix, Squarespace and Next.js.
- Time to fix
- 15 minutes to start, longer to tighten
- Skill needed
- Developer
- Who notices
- Every visitor, if a script on your site is ever compromised
Why it matters
Most websites load scripts from many places: analytics, chat widgets, payment forms, ad tags. If any of them is compromised, or an attacker injects their own, the browser runs it with full access to your page.
A Content-Security-Policy is a list of rules the browser enforces: which sources may run scripts, whether your page can be framed by other sites, and more. It limits the damage when something goes wrong.
“Content Security Policy (CSP) is a feature that helps to prevent or minimize the risk of certain types of security threats.”
Before you start
- Start with the starter policy below: frame-ancestors 'self'; base-uri 'self'; object-src 'none'. It stops other sites framing yours, blocks old plugin content and prevents a common injection trick, without touching your scripts, styles or images.
- Restricting scripts (script-src) is where the real protection is, but a wrong rule can break checkout, analytics or chat. Test it first with the Content-Security-Policy-Report-Only header, which reports problems without blocking anything.
- Any Content-Security-Policy header clears this issue in your StudioLNC report. Tightening it further is worth doing, but plan it as a separate task.
Fix it on your platform
Shopify Managed by the platform
- Shopify runs the servers for your store and doesn't let stores on its standard plans add or change response headers, so there's nothing to edit in your theme.
- If this check matters to you (for example, for a security questionnaire), ask Shopify support about Content-Security-Policy for your store. Stores on Shopify Plus or a headless setup (Hydrogen) can set their own headers.
WordPress
- Where you add the header depends on your hosting, not WordPress itself. First check your host's dashboard: many managed WordPress hosts have a security-headers setting.
- On Apache hosting, add the line below to the .htaccess file in your site's root folder (back the file up first). On Nginx, add it to your site's server block, or ask your host to.
- If your site runs through Cloudflare with the orange-cloud proxy on, you can add the header there instead, with a Response Header Transform Rule.
Header always set Content-Security-Policy "frame-ancestors 'self'; base-uri 'self'; object-src 'none'"add_header Content-Security-Policy "frame-ancestors 'self'; base-uri 'self'; object-src 'none'" always;Webflow Managed by the platform
- Webflow only allows custom security headers on Enterprise plans. On other plans, this header can't be added.
- On Enterprise, add it in Site settings → Publishing → Custom Headers.
Wix Managed by the platform
- Wix doesn't offer a setting for custom response headers, so this can't be added from the Wix Editor.
- If you need it for compliance, contact Wix support and ask how they handle this header on your domain.
Squarespace Managed by the platform
- Squarespace has no setting for custom response headers, and code injection adds code to your pages, not headers.
- If you need it for compliance, contact Squarespace support and ask how they handle this header on your domain.
Next.js
- Add the header for every route in next.config.ts using headers().
- If you host on Vercel or Netlify, you can set it in vercel.json or netlify.toml instead; use one place, not both.
const nextConfig = {
async headers() {
return [
{
source: "/:path*",
headers: [{ key: "Content-Security-Policy", value: "frame-ancestors 'self'; base-uri 'self'; object-src 'none'" }],
},
];
},
};
export default nextConfig;Any other site
- Add the header where your site is served from: your web server, hosting control panel or CDN. Pick the example that matches your setup.
- Cloudflare (proxied, orange cloud): add it with a Response Header Transform Rule.
/*
Content-Security-Policy: frame-ancestors 'self'; base-uri 'self'; object-src 'none'Header always set Content-Security-Policy "frame-ancestors 'self'; base-uri 'self'; object-src 'none'"add_header Content-Security-Policy "frame-ancestors 'self'; base-uri 'self'; object-src 'none'" always;“The adoption of CSP continued increasing from 18.5% last year to 21.9% this year, an increase of close to 20%.”
Check that it's fixed
- Re-run your StudioLNC audit: the "Missing Content-Security-Policy header" issue should be gone.
- Click through your key pages (checkout, forms, embedded video) with your browser's developer tools open. CSP violations appear in the Console.
Find every issue like this on your site
Free audit of performance, accessibility, SEO and security, with fixes for your platform.