A Content Security Policy (CSP) is an HTTP response header that tells the browser which sources of scripts, styles, images, and other resources are allowed to load — the single most effective defence against cross-site scripting and content injection. This builder lets you fill in each directive, explains what it controls, warns about weak values, and assembles a correctly formatted header you can paste straight into your server config. Nothing is transmitted.
How it works
CSP is a single header whose value is a semicolon-separated list of directives, each followed by a space-separated source list:
Content-Security-Policy: default-src 'self'; script-src 'self' https://cdn.example.com; frame-ancestors 'none'
The builder follows the spec’s rules:
- Keyword sources (
'self','none','unsafe-inline','unsafe-eval', nonces, hashes) are single-quoted. - Host and scheme sources (
https://cdn.example.com,data:) are written bare. - Directives you leave empty are omitted, so the header stays minimal.
- Each non-empty directive is joined with
;to form the final header string.
It also flags risk: adding 'unsafe-inline' to script-src triggers a warning because it undermines the policy.
Tips and notes
- Start strict with
default-src 'self', then open up only the specific directives and hosts your app actually needs. - Prefer nonces or hashes over
'unsafe-inline'for any inline script or style you cannot remove. - Always set
frame-ancestorsto stop clickjacking;'none'blocks all framing, or list trusted origins. - Test with the
Content-Security-Policy-Report-Onlyheader first so violations are reported without breaking the page, then switch to the enforcing header.