Content-Security-Policy Reference

Every CSP directive with source-list syntax, fetch vs document vs navigation types.

Interactive Content-Security-Policy reference covering fetch, document, navigation and reporting directives plus source-list keywords like nonce, self and strict-dynamic, with examples for hardening against XSS and clickjacking. Bundled offline.

What is the most important CSP directive for stopping XSS?

script-src is the key directive. Restricting which scripts can run — ideally with a per-response nonce or hash plus 'strict-dynamic' and no 'unsafe-inline' — is what actually neutralises injected JavaScript. default-src provides a fallback but should be tightened per type.

A Content-Security-Policy is an allowlist the browser enforces over what a page may load and run, and it is the single strongest browser-side defence against cross-site scripting and clickjacking. The policy is a list of directives, each naming a resource type and the sources permitted for it. This reference catalogues every directive by category along with the source-list keywords you combine to build a policy.

How it works

A CSP is delivered in the Content-Security-Policy response header (or a meta tag) as semicolon-separated directives. Fetch directives (script-src, style-src, img-src, connect-src, …) govern where each resource type may load from and fall back to default-src when unset. Document directives (base-uri, sandbox) and navigation directives (form-action, frame-ancestors) constrain the document itself and where it may go — these do not fall back. Reporting directives (report-to, report-uri) send violation reports.

Each directive takes a source list of keywords and origins. The keyword 'self' means the page’s own origin, 'none' blocks everything, and a 'nonce-<value>' or 'sha256-<hash>' allows a specific inline block. 'strict-dynamic' propagates trust from a nonced script to scripts it loads, letting you drop fragile host allowlists.

Tips and examples

A strong baseline policy:

default-src 'self';
script-src 'self' 'nonce-RANDOM' 'strict-dynamic';
object-src 'none';
base-uri 'self';
frame-ancestors 'none';
form-action 'self';
upgrade-insecure-requests;
report-to csp-endpoint

Avoid 'unsafe-inline' and 'unsafe-eval' in script-src — they re-open the XSS hole CSP is meant to close. Roll out new policies with the Content-Security-Policy-Report-Only header first so you can collect violations via report-to and fix legitimate breakage before switching to enforcement.