CSP Header Generator
Build Content-Security-Policy headers with directives, hashes, and nonces. Strict-mode templates, report-only fallback, plus Express and Nginx snippets. Runs in your browser.
- Runs in your browser
- Tool input is not uploaded to ZeroTool servers
- Free · No Sign-Up
Scan with WeChat to share this tool
Examples, details and FAQ Worked examples, how it compares with other tools, and answers to common questions.
Strict CSP starter
The Strict preset follows the nonce-based strict CSP described on web.dev. The HTTP header tab shows:
Content-Security-Policy: default-src 'self'; script-src 'nonce-{RANDOM}' 'strict-dynamic'; style-src 'self'; img-src 'self' data:; font-src 'self'; connect-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'; upgrade-insecure-requests
'nonce-{RANDOM}' is a placeholder, not a value. Your server must replace it with a new random value of at least 128 bits on every response, and write the same value into nonce="…" on each <script> it renders. The tool shows a warning while the placeholder is in the policy. Do not paste one fixed nonce into a static config: anyone who sees it once can reuse it in an injected script.
The placeholder replaces the 'self' that earlier versions of this preset used. With 'strict-dynamic', CSP Level 3 blocks every script that the HTML parser inserts unless it has a matching nonce or hash, and it stops checking 'self' and host sources (CSP3 §8.2). So script-src 'self' 'strict-dynamic' on its own runs no script at all in current Chrome, Firefox and Safari, and the tool now warns about that combination. If your site is a static build that cannot set a nonce, remove the placeholder and add a hash for each inline script with the hash calculator, or start from the Moderate preset.
The Express (helmet) tab does the per-response part for you: it adds a middleware that stores crypto.randomBytes(16).toString('base64') in res.locals.cspNonce and passes script-src a function, which is the pattern in the helmet README. Your templates still have to print that value on each script tag. The Nginx tab keeps the placeholder and adds a comment, because Nginx cannot put the same nonce into the HTML your application renders.
Two policies built with the tool
A static site with one inline tag snippet. Start from Moderate, type https://www.googletagmanager.com into the script-src box, then paste the inline snippet into the hash calculator. The hash only matches if you paste exactly what sits between <script> and </script>; this example assumes the tag contains these four lines with no leading line break:
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXXXXXXXX');
Compute & add puts the SHA-256 hash into script-src, and the HTTP header tab shows:
Content-Security-Policy: default-src 'self'; script-src 'self' https://www.googletagmanager.com 'sha256-vRtfVazNcneseB+qTMBbAh7HwesbIvuZpPyFnLearUI='; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self' data:; connect-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'; upgrade-insecure-requests
Moderate keeps 'unsafe-inline' in style-src only, so the tool shows no warning for script-src. Indent the snippet differently, or change the measurement ID, and the hash no longer matches: compute it again from the final HTML.
Watching before enforcing. Keep Strict, switch Mode to Report-Only, add the report-uri directive and type /csp-reports into it:
Content-Security-Policy-Report-Only: default-src 'self'; script-src 'nonce-{RANDOM}' 'strict-dynamic'; style-src 'self'; img-src 'self' data:; font-src 'self'; connect-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'; report-uri /csp-reports; upgrade-insecure-requests
The browser now reports violations to /csp-reports and blocks nothing. The output still ends with upgrade-insecure-requests, but the Upgrade Insecure Requests specification (§3.1) says this directive is ignored in a Report-Only header, and the tool shows a note saying so below the output. It starts to upgrade requests when you switch to Enforce.
Common pitfalls
'none'only works alone. If you list another source next to it,'none'has no effect and the other sources are allowed.- Some directives are HTTP only. CSP3 §3.3 ignores
frame-ancestors,report-uriandsandboxin<meta>, and Report-Only mode does not exist there. The tool leaves these out of the<meta>tab and warns you. 'strict-dynamic'needs a nonce or hash. Without one it blocks every script on the page; the tool flags this.- Hashes and nonces void
'unsafe-inline'. Modern browsers fall back to the safer of the two when both are present. - Always set
default-src. Missing fetch directives fall back todefault-src; without it the browser applies no fallback for new directives added later. - Custom hosts cannot contain semicolons. A semicolon would terminate the policy and corrupt every directive after it.
Hashes vs. nonces
Use hashes for static inline content that ships with the page — analytics snippets, critical CSS, server-rendered helpers. Hashes work for static HTML and edge-cached responses because they do not need any per-request value.
Use nonces for dynamic content rendered per request. The server emits a Content-Security-Policy header containing 'nonce-XYZ' and stamps the same value on every <script nonce="XYZ"> it renders. Pair nonces with 'strict-dynamic' so descendant scripts inherit trust without you maintaining a host allow-list.
Deployment cheat sheet
- Nginx: copy the Nginx tab into your
serverblock and reload — thealwaysflag forces the header on error responses too. - Apache: use
Header always set Content-Security-Policy "..."in.htaccessorhttpd.conf. - Express / Node: paste the helmet snippet. It sets
useDefaults: false; keep it, or helmet adds its own default directives to your policy. - Cloudflare Pages / Vercel / Netlify: paste the HTTP header form into your platform's headers config (
_headers,vercel.json,netlify.toml).
Limits
- It builds a policy; it does not test one. The tool does not fetch your page, read an existing header or know which hosts a third-party script loads later. Use Report-Only and the reports to find what is missing.
- Source checks are small. A semicolon is rejected and spaces split a value into several sources. CSP Level 3 (§2.3.1) allows only ASCII letters, digits and hyphens in a host, so a non-ASCII host is converted to punycode and the status line shows the result. Other typos, such as
https//cdn.example.com, go into the policy as typed. Values that are not hosts are not converted: atrusted-typespolicy name must use only the characters that Trusted Types §4.2.2 allows, andsandboxtokens andreport-tonames must be ASCII; otherwise the value is refused. - Hashes are exact. The browser hashes the UTF-8 bytes of the script or style text (CSP3 §6.7.3.3), so one extra space or line break gives a different hash. The calculator hashes the UTF-8 bytes of what you paste.
- Nonces need your server. The tool writes the
'nonce-{RANDOM}'placeholder. Only the Express tab creates a new value per response; header, meta and Nginx output must be filled in by your application. - Retired directives.
prefetch-srcandnavigate-toare not defined in the CSP3 Working Draft of 16 September 2026, so the tool no longer offers them; if a policy saved earlier contains them, they are removed when the page loads and the status line names them once. Theblock-all-mixed-contentswitch is still there, but Mixed Content §6.1 calls the directive obsolete because mixed content that cannot be upgraded is now blocked anyway, and the tool shows a note when the switch is on. - The policy is kept in this browser. See the privacy question below: the policy is saved in local storage and Ctrl/⌘+L does not delete it.
FAQ
What is a Content Security Policy?
A CSP is an HTTP response header that tells browsers which sources of scripts, styles, images, fonts, frames, and other content are trusted. It does not replace output encoding; it limits the damage when an injection gets through, for example by blocking an injected script that has no matching nonce or hash, and frame-ancestors controls who may frame the page.
Should I deploy Enforce or Report-Only first?
Start with `Content-Security-Policy-Report-Only` plus a `report-uri` (or `report-to`) endpoint. Watch real traffic, fix the legitimate violations in your code, then switch to the enforced `Content-Security-Policy`. Note that `upgrade-insecure-requests` has no effect in a Report-Only header, so test it in enforce mode.
Do I need 'unsafe-inline' for inline scripts and styles?
Avoid it whenever you can. Modern CSP supports per-script hashes (`'sha256-...'`, `'sha384-...'`, `'sha512-...'`) and per-request nonces (`'nonce-...'`). Use the inline-content hash calculator on this page to add a hash for a specific snippet.
What does 'strict-dynamic' do?
'strict-dynamic' lets a trusted nonced or hashed script load further scripts at runtime without listing every CDN. It is the recommended pattern for modern apps because it overrides host-based allow-lists for descendant scripts and stops trivial bypasses.
Where do I deploy the generated header?
Send `Content-Security-Policy` from your origin server (Nginx `add_header`, Apache `Header set`, Cloudflare Transform Rules, Vercel `headers`, etc.). The HTML `<meta http-equiv>` form is a fallback for static hosts; CSP Level 3 ignores `frame-ancestors`, `report-uri` and `sandbox` in `<meta>`, so the tool leaves them out of that tab. `report-to` still works there, but its endpoint is defined in a `Reporting-Endpoints` header.
Is my policy sent anywhere or saved?
Nothing you type is sent to a server: the policy, the outputs and the hashes are built in the page, and hashes use the browser's Web Crypto API. The policy itself (preset, mode, the two transport switches and every directive and source, including hashes) is saved in this browser's local storage and restored on your next visit; Ctrl/⌘+L clears text boxes and the computed hash but keeps the saved policy. Text in the hash calculator is not saved. The page loads Google Analytics and AdSense like other tool pages; each change to the policy records one `generate` event with the tool name and action only, not the policy.