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
- Cross-site scripting (XSS): someone gets a script into your page through a form, a plugin flaw or a tampered third-party file. MDN calls this CSP's main use, and is clear that it adds to, rather than replaces, keeping your software and plugins safe (MDN).
- Clickjacking: another site showing your page invisibly inside a frame. The
frame-ancestorsdirective stops it. - Mixed content:
upgrade-insecure-requestsmoves http files to https.
The directives that matter
| Directive | Controls |
|---|---|
default-src | The 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-src | Where JavaScript may load from. The most important one. |
style-src | Stylesheets and inline styles. |
img-src / font-src | Images and web fonts. |
connect-src | Where scripts may send data: analytics, chat widgets, forms that submit in the background. |
frame-src | What your pages may embed: YouTube, maps, payment and booking widgets. |
frame-ancestors | Who may embed your pages. 'self' or 'none'. |
object-src | Old plug-in content. Set it to 'none'. |
base-uri / form-action | Stop 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
- Send the policy as
Content-Security-Policy-Report-Only. Nothing is blocked. - 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' …"- Add each host you recognize to the directive named in the message, and test again.
- When the Console is clean across your pages, rename the header to
Content-Security-Policyto enforce it. - Optionally add reporting, so browsers tell you about violations later. MDN recommends the newer
report-towith aReporting-Endpointsheader, alongside the olderreport-uriuntil 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:
- A nonce: a random value your server creates for each page load, put in the header and on each of your own inline
<script>tags. The strongest option; it needs a server that can add it, and Google's tag passes the nonce on to the scripts it loads. - A hash of each inline script, listed in the policy. Good for pages that don't change.
'unsafe-inline'. On its own, it allows every inline script, including injected ones. MDN says to avoid it, and the free scan marks it down.
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
- Analytics and tag managers: add Google's hosts as above, and a nonce for the inline snippet.
- Embedded videos and maps: add their host to
frame-src. - Web fonts: add the font host to
font-srcand its stylesheet host tostyle-src. - Chat, booking, payment and review widgets: each needs its hosts in
script-src, usuallyconnect-src, and sometimesframe-src. Their help pages often list them. - Spam protection on forms (such as reCAPTCHA): its script and frame hosts.
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
- A policy is in place, tested first as Content-Security-Policy-Report-Only.
- Every service your pages use is allowed, and the browser Console is clean.
- It is enforced, as Content-Security-Policy, once nothing legitimate is reported.
- Scripts are allowed by nonce or hash, not by unsafe-inline alone.
- object-src is none; frame-ancestors, base-uri and form-action are set.
- Violations are reported to an address someone reads.
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.
- Content-Security-Policy strength
- Content-Security-Policy reports violations
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.
- MDN: Content Security Policy (CSP)
- web.dev: Mitigate cross-site scripting with a strict Content Security Policy
- OWASP Cheat Sheet: HTTP security response headers
- Google Tag Platform: Use a Content Security Policy with Google tags
More guides
- How to get your business recommended by ChatGPT and other AI assistants
- Why can't ChatGPT see my website? 5 reasons and how to fix them
- robots.txt for small businesses: what to allow, what to block
- What is IndexNow, and why does it matter for your website?
- The pages every small-business website needs
- How to turn more website visitors into enquiries
- Why your site is slow, and what to fix first
- How your business shows up in Google: titles, descriptions and headings
- One address for each page: www, https, canonical tags and redirects
- A sitemap for any site, including one built by hand
- Making your site usable by everyone
- Making your site work on a phone
- Security headers for a small business website: what to set, by platform
- Stop people sending email as your business: SPF, DKIM and DMARC
- Cookies and consent: what your site sets and when you need a banner
- How your links look when people share them
- Getting your business details right for Google Maps and AI assistants
- What is GEO (generative engine optimization)? A plain-English guide
- llms.txt: what it is, and does your business need one?
- AI crawler blocked? Fixing 403, 404 and 405 errors for GPTBot, ClaudeBot and PerplexityBot
- How to see if ChatGPT visits your website
- GEO vs SEO: what's the difference for a small business?
- AI SEO for small businesses: where to start
- Answer engine optimization (AEO), explained
- SPF record: what it is and how to set one up
- DMARC record: what it is and how to set one up
- DKIM record: what it is and how to set it up
- Why do my emails go to spam? How to check SPF, DKIM and DMARC
- Website caching and CDNs: what they are and do you need one?
- security.txt: what it is and how to add one to your site
- What is DNSSEC, and should you turn it on?
- CAA record: what it is and how to set one up
- Largest Contentful Paint (LCP): why your page feels slow and how to fix it
- Local business schema: structured data for a small business, with a template
- Accessibility statement: what to write, with a template
- Trust badges and testimonials that help a website win customers
- Why isn't my website showing up on Google? How to get it there
- Google Business Profile: a checklist to make it match your website
- Custom 404 page: what a good one does, and the soft 404 trap
- Meta description length: how long should it be?
- Image alt text: what to write, with examples
- Broken links: how to find and fix them on your website
- Website audit checklist: a free check-up for a small business site
- Why does my website say 'Not secure'? How to fix it
- Is my website ADA compliant? An accessibility checklist for US small businesses
- Has my website been hacked? How to check, and what to do next
- Core Web Vitals explained: LCP, INP and CLS in plain English
- Interaction to Next Paint (INP): why your site feels slow to tap, and how to fix it
- Cumulative Layout Shift (CLS): why your page jumps while it loads, and how to stop it
- What should a small business privacy policy include? A checklist
- Contact form emails not arriving? Find where enquiries get lost
- Website monitoring: catch downtime, broken enquiries and expired certificates
- Moving or redesigning your website: a checklist so nothing gets lost
- How to audit a WordPress website: a checklist for owners
- Why isn't my business showing up on Google Maps?
- Does ChatGPT recommend your business? How to check, and how to fix what it gets wrong
- Why isn't my website getting customers? A three-question diagnosis
- How to get more Google reviews without breaking Google's rules
- Google Search Console for a small business: set it up and read the three reports that matter
- Who owns your domain and website? How to check, and how to get them back
- How to show up in Google AI Overviews and AI Mode: what Google's own guide asks for
- What is a good PageSpeed score? What the number means for a small business site
- How much should a small business website cost, and what to check before you pay the final invoice
- Is my website down, or is it just me? What to check, in order
- Is my SEO company actually doing anything? How to check, and how to spot a scam email
- Should I redesign my website or just fix it?
- Does your website still need an FAQ page now that Google's FAQ rich results are gone?