Bookmarks

CSP — Content Security Policy

A web page protection mechanism: an HTTP header with a list of directives that the browser uses to decide which scripts, styles and other resources the page may load and run.

Also: CSP, content security policy header

checked 9 Nov

All terms

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:

httpScripts only from the site itself and the CDN, images from the site and data:, and the page cannot be embedded in another site’s iframe
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

DirectiveWhat it restricts
default-srcdefault sources for the other fetch directives
script-srcwhere scripts can be loaded from; by default it blocks inline scripts and eval()
style-srcstylesheets and inline styles
img-srcimages
connect-srcdestinations for fetch, XMLHttpRequest and WebSocket
frame-ancestorswho can embed the page in an iframe; replaces the X-Frame-Options header and protects against clickjacking
report-towhere 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:

http
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, either enforce or report: blocked, or only reported.

Sources

the term is written from these pages

Verified 9 Nov

Not to be confused with

what it is mixed up with and how it differs

  • CORScross-origin resource sharing

    CORS decides who from another origin can read the server’s responses; CSP decides which resources a page may load and run.

  • CSRFcross-site request forgery

    CSRF makes the browser send a forged request with your cookies; CSP does not protect against it, you need a CSRF token.

  • XSScross-site scripting

    XSS is a vulnerability that lets someone else’s script into a page; CSP limits its consequences and complements escaping.

In depth in the references

where the topic is covered in detail