Build a Content-Security-Policy header that actually stops XSS.

Pick a preset, allow the hosts your site really uses, and get a copy-ready header — with nonce and hash helpers, a validator that scores any existing policy, and Nginx, Apache and Cloudflare output. Built entirely on your device.

  • Presets + validator
  • Nonce & hash helpers
  • Nginx, Apache, Cloudflare
  • Nothing leaves your device

Build your Content-Security-Policy

Balanced: Works for most sites, including ones that still use inline styles.

Full https:// URL that receives violation reports. Superseded by report-to, but still used by older browsers.

Name of a Reporting-Endpoints group (set up the endpoint header separately on your server).

Your header
Pick a preset to build your header.

    No header built yet. Everything stays on this device. The header updates as you toggle.

    Nonce generator

    A nonce lets one inline script run without 'unsafe-inline'. Your server must generate a fresh 128-bit nonce on every response — this button makes a random demo value for testing.

    Hash generator

    Hash an inline script or style to allow exactly that code — it works on fully static pages, where a nonce cannot rotate. Paste the exact code, byte for byte.

    Test your header — deployment checklist

    • Deploy Report-Only first. Start with the Report-Only header and a report-uri endpoint, so violations are logged instead of breaking the page.
    • Watch the reports. Leave Report-Only running for a few days and read the violation reports. Every blocked URL your site genuinely needs goes into the matching directive.
    • Remove 'unsafe-inline' and 'unsafe-eval'. Replace inline scripts with nonces or hashes, then drop the unsafe keywords. A policy with 'unsafe-inline' in script-src stops very little.
    • Check the browser console. Load every page type (home, checkout, admin) with the console open (F12). Fix every blocked resource before you enforce.
    • Switch to the enforcing header. Change the header name from Content-Security-Policy-Report-Only to Content-Security-Policy and deploy.
    • Re-check after every deploy. New scripts, fonts or third-party embeds need new sources. Re-run this builder after any front-end change.

    Privacy you can verify, not just trust

    A Content Security Policy is a map of your infrastructure — it names your CDN, your API hosts, your payment provider and every analytics vendor you have ever bolted on. That is exactly the kind of detail you should not hand to a random website. This builder can't log anything: the header is assembled by plain JavaScript running in your browser. Your hostnames, nonces and hashes never leave the device — the only third-party requests are the site's own analytics, which count page views — and it uploads nothing, ever. There is no file fetched after load for the building itself. Don't take our word for it:

    New cross-origin requests during use: 0
    1. Press F12 to open developer tools, then the Network tab.
    2. Build any policy above. Watch: apart from the site's analytics, nothing new is requested — no uploads, and no hostname or policy ever leaves your device.
    3. Extra credit: disconnect your internet after this page loads — building still works.

    How to build a Content-Security-Policy that actually protects you

    Every free CSP generator on the web does the same basic thing: you pick directives and it formats a header string. The differences that matter show up after deployment — a policy that quietly allows 'unsafe-inline' and a wildcard together, a nonce you cannot rotate, a header that breaks the checkout page because it was never run in Report-Only first, or a meta tag that silently drops frame-ancestors. The pages that top the results for "content security policy generator" — StrateCode, BlockSurvey, ToolGrid, nrtechstudio, ismycodesafe, Xerobit, ByteTools and Toolypet — are solid builders, and a few do one advanced thing well: BlockSurvey pairs a builder with a checker, nrtechstudio computes real hashes, ismycodesafe ships framework presets. None of them combines the builder and the checker, a real Web Crypto hash generator, a nonce generator, six deployment formats including a Cloudflare Transform Rule, and a step-by-step rollout checklist in one page.

    This tool does exactly that. When you build a policy here, your browser:

    1. Starts from a preset — Strict (default-deny with a nonce), Balanced (works for most sites), or Permissive (a legacy-compatible starting point you tighten afterwards).
    2. Lets you tune every directive — toggle the common sources per directive, add your own hosts, and set report-uri and report-to for violation reports.
    3. Warns about dangerous combinations — 'unsafe-inline' with a wildcard, 'strict-dynamic' without a nonce, a missing object-src, base-uri or frame-ancestors — before you deploy.
    4. Formats it for your stack — the plain header, the Report-Only header, a meta tag (with the directives a meta tag cannot carry left out), Nginx, Apache, or a Cloudflare Transform Rule.
    5. Checks what you already have — paste any existing policy into the Check tab and get a score out of 100, every finding ranked, and a plain-English breakdown of each directive.

    What makes this builder different

    The ranking pages for "csp generator" and "csp header generator" are mostly single-purpose builders from small tool sites, and the strongest ones each cover one advanced feature — a checker, a hash calculator, or framework presets — but not the others. This page is the only one we found that combines the builder and the validator, real SHA-256/384/512 hashing in the browser, nonce generation with an honest note that your server must rotate nonces per response, six output formats, and a deployment checklist, with no upload, no account and no limits, plus a live network-request counter so you can verify the privacy claim yourself. It runs entirely in your browser — there is no server to upload to. The directive names and source keywords follow the MDN Content-Security-Policy reference.

    When is this the right tool?

    Whenever you ship a website and want to stop cross-site scripting at the browser level: a new launch, a security audit, a securityheaders.com scan that came back red, or a penetration test that found an XSS hole. Start with the Balanced preset, add the hosts your site really loads, run it in Report-Only for a few days, then enforce it. For a WordPress or legacy site, start with Permissive and tighten one directive at a time.

    Honest limitations

    The nonce is a demo value

    A nonce only works if your server generates a fresh one on every response and injects it into both the header and each script tag. This builder makes a random 128-bit value for testing — it cannot rotate nonces for a live site. If your pages are fully static, use hashes instead.

    It cannot see your site

    The builder knows the policy you type, not the resources your site actually loads. Only your server logs and violation reports can tell you which hosts are missing — which is why the checklist starts with Report-Only.

    Meta tags cannot do everything

    Browsers ignore frame-ancestors, report-uri and report-to in a meta tag, and a meta tag cannot protect the page before it is parsed. The HTTP header is always preferred; the Meta tag format here leaves the unsupported directives out and says so.

    The score is a guide, not a verdict

    The 0–100 score weights the classic misconfigurations (wildcards, unsafe keywords, missing hardening directives). A policy can score 100 and still break a site that loads an unlisted host, or score lower and be the right trade-off for a legacy app. Read the findings, don't just chase the number.

    report-to needs a separate header

    The report-to directive only names an endpoint group — the group itself is defined by a Reporting-Endpoints header on your server. This builder writes the directive; your server still has to define the endpoint.

    Old browsers ignore new directives

    Very old browsers ignore directives they do not recognise, and some ignore CSP entirely. A CSP is one layer of defence, not a replacement for escaping output and keeping dependencies patched.

    Frequently asked questions

    What is a Content Security Policy?

    A Content Security Policy (CSP) is an HTTP response header — Content-Security-Policy — that tells the browser exactly which sources a page may load scripts, styles, images, fonts, frames and connections from. Anything not on the list is blocked, which stops most cross-site scripting (XSS) attacks even if an attacker finds a way to inject code into your page.

    What does 'self' mean in a CSP?

    'self' means the same origin as the page itself — the same scheme, host and port. A policy of default-src 'self' allows your own scripts, styles and images and blocks everything else. It is the safest starting point for most sites, and you add named hosts (like cdn.example.com) only for the third-party services you actually use.

    Why is 'unsafe-inline' dangerous?

    'unsafe-inline' lets any inline script block, inline event handler or javascript: link run on your page. That is exactly what a cross-site scripting attack injects, so a policy with 'unsafe-inline' in script-src stops very little. Use a nonce or a hash for the inline scripts you genuinely need, then remove 'unsafe-inline' — this builder's nonce and hash helpers do both in your browser.

    What is a nonce and how do I use one?

    A nonce is a random value your server generates fresh for every response, for example 'nonce-r4nd0mV4lue'. You put it in the header (script-src 'self' 'nonce-r4nd0mV4lue') and on each inline script tag as its nonce attribute. The browser only runs inline scripts whose nonce matches. The nonce this builder generates is a random demo value — a static tool cannot rotate nonces for you, so your server must generate a new one per response.

    What is the difference between Report-Only and enforce mode?

    The enforcing header (Content-Security-Policy) blocks anything the policy does not allow. The Report-Only header (Content-Security-Policy-Report-Only) logs violations to your report-uri or report-to endpoint without blocking anything. Always deploy Report-Only first, watch the reports for a few days, fix the policy, and only then switch to enforcing — otherwise you can break your own site.

    Can I put a CSP in a meta tag instead of a header?

    You can, with limits. A meta tag with http-equiv set to Content-Security-Policy works for most fetch and document directives, but browsers ignore frame-ancestors, report-uri and report-to inside a meta tag, and a meta tag cannot protect the page before it is parsed. The HTTP header is always preferred — use the meta tag only when you cannot configure server headers. This builder's Meta tag format leaves the unsupported directives out and says so.

    What should I do if my site breaks after adding a CSP?

    Open the browser console (F12) and look for blocked-resource messages — they name the directive and the URL that was blocked. Add that host to the matching directive, or switch back to Report-Only while you collect the full list of hosts your site actually loads. The deployment checklist on this page walks through the rollout step by step.

    Do my policy drafts or hostnames upload anywhere?

    No. The policy is built entirely in your browser with plain JavaScript — your hostnames, nonces and hashes never leave the device, and the only third-party requests are the site's own analytics, which count page views. You can verify it yourself with the network-request counter in the privacy section below the builder.