Skip to content

Kompile Learn

Content Security Policy (CSP)

Limit which scripts can run on your pages, so an injected script is blocked even if an attacker gets one in.

What it is

The Content-Security-Policy header lists where scripts, styles, images and other content may come from. The browser refuses everything else.

Why it matters

Cross-site scripting is one of the most common web attacks. A strict CSP is the safety net that stops an injected script from running or sending data away.

How to fix it

Start in report-only mode (any server)

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

nginx (enforce)

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

Apache (enforce)

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

Watch the browser console in report-only mode, allow only what your site really needs, then switch to the enforcing header. Avoid 'unsafe-inline' and 'unsafe-eval' in script-src; use nonces or hashes for inline scripts instead.

Does your site have this problem?

Kompile Scan checks for this and the other problems in these guides in under a minute. Free for one site.

Check my site