# Content Security Policy: how do you limit what your website is allowed to load?

> A Content Security Policy tells the browser which scripts, images and frames your site may load. Key directives, report-only mode, an example and common errors.

Updated on October 15, 2026 · https://app.pilotiko.com/en/glossary/content-security-policy

A Content Security Policy (CSP) is a set of instructions a website sends to the browser, usually in the Content-Security-Policy HTTP header, restricting which resources a page may load and what it may do. Its main purpose is to limit the damage from cross-site scripting (XSS) injection and from clickjacking attempts.

## In short
- A policy is a series of directives separated by semicolons, such as default-src 'self'.
- It should accompany every response, not just the main page.
- frame-ancestors stops other sites from displaying your pages in a frame.
- The Content-Security-Policy-Report-Only header lets you test without blocking anything.
- Google Analytics requires specific domains in script-src-elem, img-src and connect-src.

## What does a CSP protect against?

MDN says the main use of a CSP is to control which resources, JavaScript in particular, a document may load, as a defence against cross-site scripting (XSS) [1]. In an XSS attack, someone gets their own code to run inside your page, for example through a poorly filtered form field or URL parameter. That code can then read or change page content, or send requests on the visitor's behalf [1].

MDN is clear that sanitizing input remains the first line of defence; a CSP is defence in depth that limits the harm when sanitizing fails [1]. It also guards against clickjacking, where a malicious site shows yours inside an invisible frame, and can help ensure resources load over HTTPS [1].

The reference specification is published by the W3C; Content Security Policy Level 3 currently has Working Draft status [2].

## Which directives make up a CSP?

The policy travels in the Content-Security-Policy response header and should be set on every response [1]. It can also sit in a <meta http-equiv> tag, though that route doesn't support every feature [1]. MDN describes frame-ancestors as a more flexible replacement for the older X-Frame-Options header [1].

| Directive | What it controls | Cautious value for a business website |
|---|---|---|
| default-src | Fallback rule for every resource type | 'self' (same origin only) |
| script-src | Allowed JavaScript files | 'self' plus the domains of tools you actually use |
| img-src | Image sources | 'self' and any image domains you need |
| connect-src | Requests sent by scripts | 'self' and measurement services, if any |
| frame-ancestors | Who may show the page inside a frame | 'none' or 'self' |
| upgrade-insecure-requests | Upgrades http:// resources to HTTPS | On if http:// links linger |

Source: MDN [1]. The cautious values are suggestions to adapt to each site.

## How do you roll out a CSP without breaking your site?

MDN describes a careful approach: publish the policy first in the Content-Security-Policy-Report-Only header [1]. The browser then reports what it would have blocked without blocking anything. You adjust the list, then switch to the enforcing header.

For scripts written directly into the HTML, Google's recommended method for Tag Manager is a nonce: a random, unguessable value the server generates for each response and adds to both the directive and the <script> tag [3]. The 'unsafe-inline' option exists, but Google presents it as a fallback only when a nonce or hash isn't workable [3].

## What must you allow to add a measurement tool?

**Example: a fictional dental clinic in Saint-Hyacinthe wants to add Google Analytics**

The “Yamaska Dental Sample” website runs a strict CSP: default-src 'self'. To add Google Analytics without advertising features, Google's documentation lists the domains to allow in three directives [3].

The count below comes from that documentation; the clinic is fictional.

- script-src-elem: domains to add : 1 (www.googletagmanager.com)
- img-src: domains to add : 2 (googletagmanager.com, *.google-analytics.com)
- connect-src: domains to add : 3 (googletagmanager.com, *.google-analytics.com, *.google.com)
- Total entries added to the policy : 6
- With Google Ads features turned on : more domains, including *.g.doubleclick.net
- **= Six more entries in the policy, each worth documenting: every allowed domain is a third party that can run code or receive data from your visitors.**

## Which CSP mistakes should you avoid?

- Shipping a strict policy with no report-only trial, so the quote form or the embedded map stops working [1].
- Adding 'unsafe-inline' to script-src for convenience, which badly weakens protection against injected scripts.
- Allowing an entire domain with a wildcard (*) without knowing what it hosts.
- Setting the CSP on the home page only, when it should accompany every response [1].
- Forgetting to update the policy when you add a tool, such as a booking widget or [Google Analytics 4](https://app.pilotiko.com/en/glossary/google-analytics-4).
- Relying on the CSP instead of validating and sanitizing form input [1].

## Is a CSP connected to Quebec's Law 25?

Indirectly. Every domain allowed in connect-src or script-src is an outside service that may receive data from your visitors. Keeping that list current tells you which tools collect information, which helps you write an accurate privacy policy in the spirit of [Law 25](https://app.pilotiko.com/en/glossary/quebec-law-25). Our [Law 25 guide](https://app.pilotiko.com/en/guides/quebec-law-25-for-service-businesses) covers the actual obligations; a CSP isn't one of them in itself.

## What does Pilotiko do with Content Security Policy?

The [site Pilotiko hosts](https://app.pilotiko.com/en/product/website) is served with a restrictive Content Security Policy: default-src 'self', scripts and stylesheets limited to the site itself, images from the site or over HTTPS, forms posting only to the site, frame-ancestors 'none' and base-uri 'none'. It also sends the X-Frame-Options DENY header. No third-party script is loaded, and adding Google Analytics to the site isn't offered yet: that feature is listed as coming soon.

## Frequently asked questions

### Is a CSP worth it for a small business website?

Yes, especially if it has a form. Even a simple site can load third-party scripts or be framed by another site; a CSP limits those risks at little cost.

### Can a CSP break my website?

Yes, if it blocks a resource you need, such as a map or booking script. That is why you test it first with the Content-Security-Policy-Report-Only header.

### CSP in a meta tag or in a header?

The HTTP header is the recommended route and supports every feature. The meta tag helps when you have no server access, but some directives don't work that way.

### Do I still need X-Frame-Options alongside frame-ancestors?

MDN describes frame-ancestors as a more flexible replacement for X-Frame-Options. Nothing stops you from sending both, as long as they say the same thing.

### What does 'self' mean in a CSP?

It is the page's own origin, in other words your own site. default-src 'self' means that by default the page only loads resources from your website.

## Sources

1. MDN Web Docs, « Content Security Policy (CSP) », https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CSP (accessed on 2026-10-15)
2. W3C, « Content Security Policy Level 3 (Working Draft) », https://www.w3.org/TR/CSP3/ (accessed on 2026-10-15)
3. Google Tag Platform, « Use Tag Manager with a Content Security Policy », https://developers.google.com/tag-platform/security/guides/csp (accessed on 2026-10-15)
