CSP generator
Build a Content Security Policy directive by directive, from a strict nonce-based or a self-only preset. Get the header, nginx, Apache and meta tag output.
Directives
The fallback for every fetch directive that is not set.
Hosts and schemes, separated by spaces.
Who may show this page in a frame. Does not fall back to default-src.
Hosts and schemes, separated by spaces.
What a <base> tag may point to. Does not fall back to default-src.
Hosts and schemes, separated by spaces.
Where forms may be sent. Does not fall back to default-src.
Hosts and schemes, separated by spaces.
Your policy
Content-Security-Policy: default-src 'self'; frame-ancestors 'self'; object-src 'none'; base-uri 'self'; form-action 'self'
add_header Content-Security-Policy "default-src 'self'; frame-ancestors 'self'; object-src 'none'; base-uri 'self'; form-action 'self'" always;
Header always set Content-Security-Policy "default-src 'self'; frame-ancestors 'self'; object-src 'none'; base-uri 'self'; form-action 'self'"
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'">
A meta tag cannot carry frame-ancestors, report-uri or sandbox: browsers ignore them there, so frame-ancestors is left out of the tag. Send the policy as a header where you can.
Everything you type here stays in your browser. Nothing is sent to HardenKit or to anyone else.
How to use the CSP generator
- Start from a preset: strict and nonce-based for an application, or self only for a simple site.
- For each directive, choose what it allows and add the hosts your site really loads from.
- Read the warnings: they point at sources that undo the policy.
- Copy the header, the nginx or Apache line, or the meta tag.
- Paste the result into the CSP evaluator to check it.
About this tool
A Content Security Policy (CSP) is a response header that tells the browser where scripts, styles, images, frames and connections may come from. Its main job is to contain cross-site scripting: when an attacker manages to inject markup into a page, a good policy stops the injected script from running. It is a second line of defence, not a replacement for escaping output.
There are two ways to write one. An allowlist names the hosts that may serve scripts, which is simple but easy to bypass when a listed host also serves other people’s files. A strict policy, the approach CSP Level 3 recommends, trusts scripts by a random nonce that the server puts in the header and on each script tag, new for every response. This generator offers both as presets.
The common mistake is adding 'unsafe-inline' to script-src to make the errors go away, which switches the protection off. Roll a policy out with the Content-Security-Policy-Report-Only header first, fix what it reports, then enforce it. A meta tag works for static hosting, but it cannot carry frame-ancestors, report-uri or sandbox.
Sources
Questions
For an application that renders its pages on a server, use a nonce-based policy: script-src with a nonce and 'strict-dynamic', object-src 'none' and base-uri 'none'. For a static site with no inline script, default-src 'self' with object-src 'none' is a sound start. Test either with the Report-Only header before enforcing it.
A nonce is a random value the server generates for each response. It goes in the policy as 'nonce-VALUE' and in the nonce attribute of every script tag the server writes. An injected script does not know the value, so the browser refuses to run it. The nonce must be unpredictable and never reused.
Yes, with a meta http-equiv tag in the head of the page, which helps on hosts where you cannot set headers. Browsers ignore frame-ancestors, report-uri and sandbox in a meta tag, and the policy only applies to what comes after the tag. A header is the better choice when you have one.
It lets a script that was trusted through a nonce or a hash load further scripts, which is what most loaders and tag managers need. In return the browser ignores host and scheme sources in that directive. That is why a strict policy does not need a list of script hosts.