Content Security Policy: telling the browser what may run
A Content Security Policy (CSP) is a response header that lists where a page may load scripts, styles, images, frames and other resources from. The browser refuses anything else. When an attacker manages to inject a script into the page, a strict policy stops it from running, which makes CSP the second line of defence against cross-site scripting after output encoding.
Quick facts
- Header
- Content-Security-Policy, or Content-Security-Policy-Report-Only for testing
- Specification
- Content Security Policy Level 3 (W3C)
- Main protection
- Injected scripts do not run; resources load only from listed sources
- Also covers
- Who may frame the page (frame-ancestors) and where forms may submit
- Safe way to start
- Report-only mode, then enforce once reports are clean
Directive names follow MDN; the parent guide is HTTP security headers.
The CSP directives that matter most
| Directive | Controls | Typical value |
|---|---|---|
| default-src | Fallback for every resource type not listed separately | 'self' |
| script-src | Where scripts may load from, and whether inline scripts run | 'self' plus nonces or hashes |
| style-src | Where styles may load from | 'self' |
| img-src | Where images may load from | 'self' data: |
| connect-src | Where scripts may send requests | 'self' and your API origin |
| frame-ancestors | Which sites may put this page in a frame | 'self' or 'none' |
| form-action | Where forms may submit | 'self' |
| object-src | Plugins such as embedded objects | 'none' |
Directives not listed fall back to default-src, except a few such as frame-ancestors and form-action, which have no fallback and must be set on their own.
In cyber security terms, the meaning of CSP is a hardening control, not a fix. The MDN definition: a feature that helps to prevent or minimize the risk of certain types of security threats. The vulnerable code stays vulnerable; the policy narrows what an injected script can do.
A CSP error is the browser refusing something the policy does not allow, and naming the directive. Check inline script first: with default-src or script-src in the policy, inline JavaScript does not run unless a nonce, a hash or another measure allows it. Add the missing source to the right directive or move the code to a file; 'unsafe-inline' clears the error by removing the protection.
A Content Security Policy header example
A starting policy for a site that serves its own scripts and styles and calls one API might look like the one below. Replace the API origin with your own, and add sources as reports show what the site really loads.
Content-Security-Policy: default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; connect-src 'self' https://api.example.com; frame-ancestors 'self'; form-action 'self'; object-src 'none'; base-uri 'self'
On Apache, mod_headers provides directives to control and modify HTTP request and response headers, so a Content Security Policy example for Apache is one line in the server or virtual host configuration: Header always set Content-Security-Policy "default-src 'self'". The always condition adds the header even on error responses.
In Chrome, open DevTools, select the page's request in the Network panel and read Response Headers on the Headers tab to see which Content Security Policy arrived (Chrome DevTools reference).
The Permissions Policy header is separate from CSP. Permissions-Policy allows or denies browser features in a document and its iframes, while CSP limits what a page loads; MDN shows Permissions-Policy: microphone=(), geolocation=() disabling both features.
Nonces, hashes and inline scripts
The protection against injection comes from refusing inline script, because injected script is almost always inline. A policy that allows 'unsafe-inline' for scripts gives most of that protection away.
If a page needs inline script, allow it by a nonce, a random value generated per response and repeated on the script tag, or by a hash of the script's exact content. The OWASP Content Security Policy Cheat Sheet describes a strict policy built this way.
strict-dynamic and third-party scripts
Listing every host a page loads scripts from breaks as soon as a tag manager or widget loads another script from a new host. The 'strict-dynamic' keyword in CSP Level 3 takes a different approach: scripts you trust by nonce or hash may load further scripts, and the host list is ignored.
A policy such as script-src 'nonce-r4nd0m' 'strict-dynamic' then means: run the scripts this page marked, and whatever they choose to load. That moves trust from hosts to your own markup, which is easier to keep correct, and it is why many current policies are built around nonces rather than long allowlists.
Reading CSP violation reports
A policy can name an endpoint that receives a report every time the browser blocks something. Each report states the page, the directive that was violated and the blocked source, which makes it the main tool for building and maintaining a policy.
Expect noise. Browser extensions inject scripts and styles into pages, and their violations look like attacks until you recognise the sources. Group reports by blocked source and directive, deal with the largest groups first, and treat a new source on a sensitive page as worth a look.
Checking a policy
A policy can be checked in three ways. Read it: 'unsafe-inline' or 'unsafe-eval' in script-src, a bare https: or *, and a missing object-src or base-uri are the usual weaknesses, and online CSP checkers flag exactly these. Load the site with the browser's console open, where every blocked resource is reported with the directive that blocked it.
And run it as Content-Security-Policy-Report-Only against real traffic for a while, because the console only shows the pages you visited. A checker tells you whether a policy is strict; only reports tell you whether it fits your site.
Scanners report an absent policy as a finding of its own, such as the ZAP alert Content Security Policy (CSP) Header Not Set: the response has no policy to check.
Rolling out CSP without breaking the site
- Send the policy as report-only. Content-Security-Policy-Report-Only reports violations without blocking anything.
- Collect and read the reports. Each one names the directive and the blocked source.
- Fix the site or extend the policy. Move inline script to files or add nonces; add sources the site really needs.
- Enforce on a slice first. One section or a share of traffic, the same discipline as for any new block.
- Keep reporting on after enforcing. New third-party scripts and browser extensions keep producing reports.
This is the same order as introducing request filtering: observe first, then enforce, as on monitor mode.
Questions
What is Content Security Policy?
Content Security Policy is an HTTP response header that tells the browser which sources a page may load scripts, styles, images, frames and other resources from, so injected scripts do not run.
Does CSP prevent XSS?
It limits the damage. A strict policy without unsafe-inline stops most injected scripts from running, but output encoding remains the first defence.
How do I test a Content Security Policy?
Send it as Content-Security-Policy-Report-Only first, read the violation reports, fix or extend the policy, then enforce it.
What is a CSP nonce?
A random value generated for each response and placed both in the policy and on allowed script tags, so only those scripts run.
What is CSP in cyber security?
A browser-enforced policy narrowing what injected scripts can load or do; it supplements fixing the vulnerable code.