What is Content Security Policy?

In short
Content Security Policy (CSP) is a set of rules that a website sends to the browser in an HTTP header. The rules tell the browser which sources of scripts, styles, images and other resources are allowed to load. It helps reduce damage from cross-site scripting and similar attacks.
Content Security Policy, usually called CSP, is a security layer that a web server adds to its responses. The server sends a header named Content-Security-Policy. The header holds a list of rules. Each rule tells the browser where a certain type of content may come from. If something tries to load from a place the rules do not allow, the browser blocks it.
How it works
A policy is made of directives. A directive is a name followed by one or more values, and directives are separated by semicolons. Each directive controls one kind of resource. For example, script-src covers JavaScript, img-src covers images, and style-src covers stylesheets. The documentation lists many more, including font-src, connect-src and frame-src.
Each fetch directive accepts either the single keyword 'none', which blocks that resource type completely, or a list of source expressions. A source expression can be a host name, a URL scheme such as https:, or a keyword such as 'self', which means the same origin as the page.
Some directives act as fallbacks. If you leave out img-src but set default-src, the browser uses the default-src rule for images. Likewise, script-src covers script-src-elem and script-src-attr when those are not set. Knowing the fallback chain helps you avoid surprises.
A simple example
Here is a basic policy that allows content only from the same origin, plus images from one other host:
Content-Security-Policy: default-src 'self'; img-src 'self' https://images.example.com
With this header, scripts and styles from other domains are blocked. Images are allowed from the site itself and from images.example.com. Any other resource type falls back to default-src and must come from the same origin.
For scripts and inline styles, the documentation describes nonces and hashes. A nonce is a random value that the server creates for each response. The same value is placed on an allowed script or style element, and the browser loads it only when the values match. A hash works in a similar way, using a fingerprint of the script or style contents. If a directive contains a nonce or hash, the browser ignores unsafe-inline in that directive.
Directives that are not about loading
Some directives control other behavior. The base-uri directive limits which URLs a document's base element can use. The form-action directive limits where forms may submit. The frame-ancestors directive decides which pages may embed your page in a frame, which is useful against clickjacking. The upgrade-insecure-requests directive tells the browser to treat HTTP URLs on your site as HTTPS, which helps sites with many old links.
Reporting violations
CSP can send reports when the browser blocks something. The report-to directive names a reporting endpoint, and the endpoint is defined in other headers such as Reporting-Endpoints. The older report-uri directive gives a URL directly. The documentation says report-to is meant to replace report-uri, and that browsers which support report-to ignore report-uri. Until report-to is widely supported, it is common to send both.
Common mistakes
A frequent mistake is writing a policy that is too loose, such as allowing unsafe-inline or wide wildcards, which removes much of the protection. Another mistake is forgetting a resource type, so a site breaks after the header is added. Start with a report-only setup if your tool supports it, watch the reports, and then enforce the policy. Also remember that host sources and scheme sources are written without quotes, while keywords like 'self' and 'none' need single quotes.
If you are new to the web's security model, read about how browsers separate content by origin before writing a policy. CSP works best alongside other protections rather than as a replacement for them.
FAQ
Is Content Security Policy the same as a firewall?
No. A firewall filters network traffic. CSP is a browser feature that limits which resources a page may load and run. It only affects the page that receives the header.
Can CSP stop all cross-site scripting?
Not by itself. CSP reduces the damage of injected scripts, especially when it blocks inline code and unknown hosts. It works best together with safe output encoding and other defenses.
What is the difference between a nonce and a hash?
A nonce is a random value the server generates for each response and places on allowed elements. A hash is a fingerprint of a script or style's contents, so it stays the same as long as the content does not change.
What happens if a directive is missing?
The browser uses a fallback directive when one exists, such as default-src for images. If no fallback applies, that resource type is not restricted by the policy.
Sources
This page is based on the official documentation below. Read it for full detail.
Content Security Policy — official documentation ↗Content Security Policy in the news
Optimizely Web Experimentation Compromise Served Malware from 178 Major Sites
Attackers modified JavaScript in Optimizely's Web Experimentation product, which then ran malicious code on websites of at least 178 organisations, including BBC and CNN. The injected scripts were served from Optimizely's legitimate CDN.
