How to fix missing content security policy on Headless / Custom (I build in code)

Add a Content-Security-Policy (CSP) HTTP response header to every page to block unauthorized scripts, styles, and resources from loading in your visitors' browsers.

Steps for Headless / Custom (I build in code)

  1. Set the Content-Security-Policy header in your server's HTTP response for every route.
  2. Express.js: use the 'helmet' npm package — app.use(helmet.contentSecurityPolicy({ directives: { defaultSrc: ["'self'"], scriptSrc: ["'self'"], objectSrc: ["'none'"], baseUri: ["'self'"] } }));
  3. Next.js: add the header in next.config.js inside the headers() async function, or set it in middleware.ts using the NextResponse API.
  4. Nginx: add_header Content-Security-Policy "default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self';" always; in your server {} block.
  5. Apache: Header always set Content-Security-Policy "default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self';" in .htaccess or VirtualHost config.
  6. Cloudflare Workers / Pages: set the header inside the fetch handler Response constructor or via _headers file.
Official Headless / Custom (I build in code) documentation ↗

What is missing content security policy?

A Content Security Policy (CSP) is an instruction you send to your visitors' web browsers, embedded in a special HTTP header, that tells the browser exactly which sources of scripts, images, styles, fonts, and other content it is allowed to load on your site. Think of it as a strict guest list at a nightclub door: anything not on the list gets turned away. Without a CSP header, the browser will happily load resources from anywhere — including malicious scripts injected by attackers. Your site currently sends no CSP header at all, which means that protection layer is completely absent.

Without a CSP, your store is significantly more vulnerable to Cross-Site Scripting (XSS) attacks — one of the most common ways attackers steal customer data, hijack sessions, or install payment-skimming code (like Magecart) directly in your shoppers' browsers. A successful XSS attack can expose customers' credit card numbers, passwords, and personal information, leading to chargebacks, fines under GDPR/CCPA, PCI-DSS violations, and severe reputational damage. Google also considers site security as a ranking signal, so a visibly compromised site can lose organic search rankings fast. Adding a CSP is recognized by OWASP as a critical defense-in-depth control against injection attacks (A03:2021).

See the complete Missing content security policy guide for every platform and the full background.

Not sure if your Headless / Custom (I build in code) store has this?

Run a free SEOLZ scan — we’ll find missing content security policy and every other issue across your whole site.

Scan my site free

Fix missing content security policy on another platform