Skip to content
Tracking and technical

What is a Content Security Policy (CSP)?

3 min read · Updated · By the Reveata team

Short answerA Content Security Policy (CSP) is a set of rules a website sends to the browser, usually in a Content-Security-Policy header, listing which sources of scripts, styles, images and network connections the page is allowed to use, so injected or unexpected code is blocked.

A close up of a padlock on a door
Photo by Kaffeebart on Unsplash

CSP is a defence against cross-site scripting. If an attacker manages to inject a <script> tag into your page, a good policy stops the browser from running it, because it does not come from a source you allowed. MDN's CSP guide (opens in a new tab) describes this as its main purpose, with clickjacking and mixed-content protection as further uses.

How a policy looks

A policy is a list of directives separated by semicolons:

text
Content-Security-Policy: default-src 'self'; script-src 'self' https://cdn.example.com; connect-src 'self'
Directive Controls
default-src The fallback for any type not listed separately
script-src Where scripts may load from, and whether inline scripts run
connect-src Where the page may send requests with fetch, beacons, XHR and WebSockets
img-src, style-src, font-src Images, styles and fonts
frame-ancestors Which sites may embed your page in a frame

The header is the recommended way to send a policy. A <meta http-equiv="Content-Security-Policy"> tag also works on static hosts, but it does not support every feature.

Nonces, hashes and strict policies

Allowlists of domains are hard to keep safe, so MDN recommends a strict policy based on nonces or hashes. A nonce is a random value the server generates for every response and puts on each script it trusts. A hash is a fingerprint of a script's exact contents. 'strict-dynamic' lets a trusted script load further scripts. Avoid 'unsafe-inline' and 'unsafe-eval', which remove most of the protection.

What an analytics script needs

When a policy forgets the analytics provider, the browser blocks it and the console shows Refused to load the script or Refused to connect. No visits arrive.

Reveata's script needs exactly two things (Content Security Policy):

  • script-src: the address the script loads from.
  • connect-src: the address events are sent to, because they go with navigator.sendBeacon or fetch.

It loads no images, frames, fonts or styles, and uses no eval.

How you load Reveata script-src connect-src
Standard snippet the Reveata address the Reveata address
Managed proxy on stats.example.com https://stats.example.com https://stats.example.com
Your own proxy on your domain 'self' 'self'

The optional queue snippet, which lets you call reveata() before the script loads, is inline. Allow it by moving it to a file you serve, by giving it your nonce, or with the exact sha256 hash listed in the docs.

Rolling out a policy safely

  1. Send it as Content-Security-Policy-Report-Only first. The browser reports what it would block without blocking anything.
  2. Fix every violation that belongs to a tool you use.
  3. Switch to the enforcing header.
  4. Recheck after adding any new tool or tag manager tag.

Frequently asked questions

Why did my analytics stop after we added a CSP?

The policy probably lists your own scripts but not the analytics address in script-src, connect-src or both. The browser console names the blocked address. Add it to the existing directives rather than replacing them.

Does a first-party proxy simplify CSP?

Yes. When the script and events go through your own domain, 'self' covers both, and there is no third-party address to maintain. See first-party proxy.

Is CSP a replacement for sanitising input?

No. MDN says CSP is not a substitute for sanitising input; treat it as a second line of defence. You still need to escape and validate anything users can put on a page.

See it with your own numbersReveata ties every visit and sale to the source that brought it. Add your website in a minute.
Get started
Was this page useful?
GlossaryWhat is a tracking script?A tracking script is a small JavaScript file you add to every page of a website, which runs in each visitor's browser and sends page views, clicks and other actions to an analytics service.GlossaryWhat is a first-party analytics proxy?A first-party analytics proxy routes the analytics script and the events it sends through an address on your own domain, such as stats.example.com or /dt/event, so the browser sees them as part of your site instead of a request to another company.GlossaryWhat is a tag manager?A tag manager is a tool, such as Google Tag Manager, that you install once on your website and then use to add, change and fire analytics and marketing scripts (tags) from a web dashboard instead of editing your site's code each time.GlossaryWhat are Core Web Vitals?Core Web Vitals are three measurements Google uses to describe a page's real-world user experience, namely Largest Contentful Paint (loading), Interaction to Next Paint (responsiveness) and Cumulative Layout Shift (visual stability).GlossaryWhat are event properties?Event properties are key–value details attached to an analytics event, such as the plan chosen or the button clicked on a signup event, so you can break one event down by those details instead of creating a separate event for every variation.GlossaryWhat is a canonical URL?A canonical URL is the one preferred address you want search engines to index and show for a page whose content is reachable at several URLs, usually declared with a rel="canonical" link in the page's head.