Guides · Security headers

Content-Security-Policy header

One of the checks in every audibot report. This page says exactly what audibot reads, what passes, and how to fix it.

What audibot checks

The Content-Security-Policy and Content-Security-Policy-Report-Only response headers on the home page. A policy in a <meta http-equiv> tag is not read. A policy whose script-src allows 'unsafe-inline' counts as weak unless the policy also uses a nonce or a hash.

Why it matters

A CSP lists where the page may load scripts, styles and other resources from. If an attacker manages to inject markup into the page (cross-site scripting), the browser refuses to run a script the policy does not allow. 'unsafe-inline' allows exactly the kind of script an injection produces, so most of the protection is gone.

What is a good Content-Security-Policy, and how do I add one?

Start strict and loosen only for what the site really loads. Send the policy as Content-Security-Policy-Report-Only first, fix what the browser console reports, then send the same value as Content-Security-Policy. Move inline <script> blocks into files, or allow them with a per-response nonce or a hash.

A starting policy

Content-Security-Policy: default-src 'self'; img-src 'self' data:; object-src 'none'; base-uri 'self'; frame-ancestors 'self'

nginx

add_header Content-Security-Policy "default-src 'self'; img-src 'self' data:; object-src 'none'; base-uri 'self'; frame-ancestors 'self'" always;

Apache (mod_headers)

Header always set Content-Security-Policy "default-src 'self'; img-src 'self' data:; object-src 'none'; base-uri 'self'; frame-ancestors 'self'"

Caddy

header Content-Security-Policy "default-src 'self'; img-src 'self' data:; object-src 'none'; base-uri 'self'; frame-ancestors 'self'"

Allowing one inline script by nonce

Content-Security-Policy: script-src 'self' 'nonce-R4nd0m'

<script nonce="R4nd0m">…</script>

(The nonce must be new, random and unguessable on every response.)

Related tools

Other checks in Security headers

Security headers in the methodology ·All guides

This check is one step of the website launch checklist.

How often audited websites pass this check: see the website audit benchmarks, with sample size and denominators.

Check your site

audibot runs this check and every other one in a single audit, and publishes the report with a recommendation for each finding.