Content Security Policy (CSP) for a small business website

Last updated

The short answer

A Content Security Policy (CSP) is a header your server sends with each page that lists where scripts, styles, images and other files may load from. The browser refuses anything else, so a script someone manages to inject into your page can't run. Start with the report-only version, which blocks nothing and only reports, fix what it flags, then switch it to enforce. Don't rely on 'unsafe-inline' for scripts: on its own, it lets injected scripts through.

What it protects against

The directives that matter

DirectiveControls
default-srcThe fallback for the loading rules, such as script-src and img-src, when they aren't set. It doesn't cover frame-ancestors, base-uri or form-action: set those separately. 'self' means your own site.
script-srcWhere JavaScript may load from. The most important one.
style-srcStylesheets and inline styles.
img-src / font-srcImages and web fonts.
connect-srcWhere scripts may send data: analytics, chat widgets, forms that submit in the background.
frame-srcWhat your pages may embed: YouTube, maps, payment and booking widgets.
frame-ancestorsWho may embed your pages. 'self' or 'none'.
object-srcOld plug-in content. Set it to 'none'.
base-uri / form-actionStop injected code changing your page's base address or where its forms submit.

A starter policy

For a typical small-business site using Google Analytics through Google's tag, the hosts below are the ones Google lists for Analytics without its advertising features. Start in report-only mode:

Content-Security-Policy-Report-Only:
  default-src 'self';
  script-src 'self' https://www.googletagmanager.com;
  connect-src 'self' https://www.googletagmanager.com https://*.google-analytics.com https://*.google.com;
  img-src 'self' data: https:;
  style-src 'self' 'unsafe-inline';
  font-src 'self' https://fonts.gstatic.com;
  frame-ancestors 'self';
  object-src 'none';
  base-uri 'self';
  form-action 'self'

Your site will use other services: add each one's host to the directive the reports point to. The header is sent as one line; it is split here to read.

Test it without breaking your site

  1. Send the policy as Content-Security-Policy-Report-Only. Nothing is blocked.
  2. Open each kind of page (home, a service page, contact form, checkout or booking) with the browser's developer tools open, and read the Console. Each problem looks like this:
Refused to load the script 'https://widget.chat-tool.example/loader.js'
because it violates the following Content Security Policy directive: "script-src 'self' …"
  1. Add each host you recognize to the directive named in the message, and test again.
  2. When the Console is clean across your pages, rename the header to Content-Security-Policy to enforce it.
  3. Optionally add reporting, so browsers tell you about violations later. MDN recommends the newer report-to with a Reporting-Endpoints header, alongside the older report-uri until every browser supports it.

Inline scripts: nonces, hashes and unsafe-inline

Many sites have small scripts written straight into the page, such as the Google tag snippet. A policy blocks those unless you allow them, and there are three ways:

One exception: if the same directive also has a nonce, a hash or 'strict-dynamic', browsers that understand those ignore 'unsafe-inline'. Sites sometimes keep it there for very old browsers, and the free scan doesn't mark it down in that case. Judge the policy as a whole, not one keyword.

web.dev's strict CSP guide describes the nonce-based approach in full; it is the target if whoever runs your site can implement it.

What usually breaks, and the fix

Where to set it

A CSP is a response header, set in the same place as your other security headers: .htaccess on Apache and WordPress, web.config on Windows hosting, the server block on nginx, a transform rule on Cloudflare, or the headers file on Netlify and Vercel. The security headers guide shows each one. Hosted builders such as Shopify, Wix and Squarespace generally don't let you set it.

Checklist

Questions people ask

Will a Content Security Policy break my website?
An enforced policy that misses something your pages use will stop it loading. That's why you start with the report-only header, which blocks nothing and only reports, and switch to enforcing once nothing legitimate is reported.
What does 'refused to load the script because it violates the Content Security Policy' mean?
Your policy doesn't allow that script's address. If the script is yours or a service you use, add its host to script-src (or give it a nonce). If you don't recognize it, the policy is doing its job.
Is 'unsafe-inline' safe?
On its own, no: it undoes most of what a policy protects against for scripts, because an injected inline script is allowed too. MDN says to avoid it. Use nonces or hashes for your own inline scripts instead. If the same directive already has a nonce or hash, browsers that support them ignore 'unsafe-inline', so it does no harm there.
Can I add a CSP on Shopify, Wix or Squarespace?
Usually not: the builder controls the server and its headers. On WordPress, traditional hosting, Cloudflare, Netlify and Vercel you can.
Does a CSP help my Google ranking?
Not directly. It protects your visitors and your reputation if a script is tampered with, which is why security scanners and our free scan check for it.

What the free scan checks for this

These are the FocalScore checks that cover this guide's subject. Each one in your report says what it found, shows the evidence and gives the fix.

Sources

Last checked . Documentation, settings and rules change; if one of these pages now says something different, it is right and this guide is out of date. Tell us at info@focalscore.com.

  1. MDN: Content Security Policy (CSP)
  2. web.dev: Mitigate cross-site scripting with a strict Content Security Policy
  3. OWASP Cheat Sheet: HTTP security response headers
  4. Google Tag Platform: Use a Content Security Policy with Google tags