CentralCSP
PoliciesContent-Security-Policy

Content Security Policy (CSP), the complete reference

How CSP works, its headers, the values you put in a directive, and every directive, with reporting throughout.

Last update:

Content Security Policy (CSP) is an HTTP response header that tells the browser which resources a page is allowed to load and execute. The browser enforces it: a script, stylesheet, image, frame or connection that no directive permits is blocked before it runs. That is what makes CSP the primary defense against cross-site scripting (XSS) and content injection, and the reason it is the only control that stops an injected script even when the injection itself succeeds.

A policy is a semicolon-separated list of directives, each naming a resource type and the sources allowed for it:

Content-Security-Policy: default-src 'self'; script-src 'self' 'nonce-r4nd0m'; object-src 'none'; base-uri 'none'

CSP can also report instead of block. Content-Security-Policy-Report-Only carries the same policy, enforces nothing, and sends a csp-violation report for everything it would have blocked, so you can measure a policy against real traffic before you turn it on.

The parts of a policy

PartWhat it isReference
HeaderContent-Security-Policy enforces, -Report-Only only reports. A <meta> element can carry a policy too, with limits.Headers
DirectiveThe resource type being governed: script-src, img-src, frame-ancestors.Directives
Source listThe values after a directive: keywords, hosts, schemes, hashes, nonces.Values
FallbackMost fetch directives fall back to default-src when absent.default-src
Reportingreport-to (and legacy report-uri) name where violations are sent.report-to

Directives by role

Every directive has its own page. Grouped by what it governs:

A policy worth copying

Content-Security-Policy:
  default-src 'self';
  script-src 'self' 'nonce-{RANDOM}';
  style-src 'self' 'unsafe-inline';
  img-src 'self' data:;
  connect-src 'self';
  object-src 'none';
  base-uri 'none';
  frame-ancestors 'none';
  report-to csp-endpoint

object-src 'none' and base-uri 'none' close the two bypasses that defeat an otherwise strict policy: plugin content, and an injected <base> element that repoints every relative script URL. frame-ancestors 'none' is the modern replacement for X-Frame-Options. A fresh nonce per response is what lets you drop 'unsafe-inline' from script-src, which is where nearly all of CSP's XSS protection comes from.

Sub-sections

Start here

Reporting

Every blocked resource produces a csp-violation report naming the directive that blocked it and the URL it blocked, delivered over the Reporting API. Those reports are how a policy is built: ship it Report-Only, read what it would have blocked, and tighten. CentralCSP collects and aggregates them per site, so a rollout is driven by what your visitors' browsers actually observed rather than by guesswork.

Sources

On this page