CSP describes where a page may load scripts, styles, images and other resources from, and which code it may run. The browser blocks anything the policy does not allow, so a script injected by an attacker usually does not run: the main job of CSP is to limit the damage from XSS.
How it works
The server sends the policy in the Content-Security-Policy response header. A policy is a series of directives separated by semicolons, each with its own value:
Content-Security-Policy: default-src 'self'; script-src 'self' https://cdn.example.com; img-src 'self' data:; frame-ancestors 'none'Directives such as script-src and img-src restrict the sources for one type of resource, while default-src sets the default for those that the policy does not list. Each directive has a list of sources: 'self' (the same origin), domains, schemes such as data:, a nonce or a hash. All policies apply at once: the browser loads a resource only if every one of them allows it, so additional policies can only narrow what a page can do. CSP checks are built into the Fetch loading algorithms: both requests and responses are checked.
A policy can also be set with a <meta http-equiv="Content-Security-Policy"> tag, but then frame-ancestors, sandbox and report-only mode do not work. That is why the policy is usually sent by the server or a CDN.
Key directives
| Directive | What it restricts |
|---|---|
default-src | default sources for the other fetch directives |
script-src | where scripts can be loaded from; by default it blocks inline scripts and eval() |
style-src | stylesheets and inline styles |
img-src | images |
connect-src | destinations for fetch, XMLHttpRequest and WebSocket |
frame-ancestors | who can embed the page in an iframe; replaces the X-Frame-Options header and protects against clickjacking |
report-to | where to send violation reports |
Inline scripts: nonces and hashes
By default, CSP blocks inline scripts, event handler attributes and eval(). You can allow specific ones with a nonce, a one-time value that the server puts in the header and in the nonce attribute of each of its own <script> elements:
Content-Security-Policy: script-src 'nonce-416d1177-4d12-4e3b-b7c9-f6c409789fb8'; object-src 'none'; base-uri 'none'If a script changes rarely, use a hash instead of a nonce: the browser compares the element’s content with 'sha256-…'. The 'strict-dynamic' keyword passes trust from an allowed script to the scripts it loads; this approach, with no domain lists, is called a strict CSP. By contrast, 'unsafe-inline' and 'unsafe-eval' allow inline code and eval() wholesale and reduce the protection to a domain list.
Rolling out a policy
Introduce a policy in stages. Report-only mode (the Content-Security-Policy-Report-Only header) blocks nothing but reports violations: together with the report-to directive and the Reporting-Endpoints header, the browser sends a report in JSON format. Violations also show up in the browser console, and in page code through ReportingObserver. Once there are no violations, switch to enforcement. A report includes, among other things:
age, the age of the report;url, the address of the page whose policy was violated;body.documentURL, the document in which the violation was found;body.blockedURL, the blocked resource (or its type);body.effectiveDirective, the directive that triggered;body.disposition, eitherenforceorreport: blocked, or only reported.
Sources
the term is written from these pagesVerified 9 Nov