Build a correct Content-Security-Policy header in minutes: a visual directive builder, a real nonce & SHA-256/384/512 hash generator, a validator that catches 20+ misconfigurations, and copy-paste deployment snippets for WordPress, nginx, Apache, IIS, Express, and Laravel. Free forever, no signup — everything runs 100% in your browser, nothing is uploaded anywhere.
frame-ancestors, report-uri, report-to and sandbox are ignored in meta tags — use the HTTP header for those. The validator flags them automatically.Two ways to allow a specific inline script or style without 'unsafe-inline':
'nonce-…')<script nonce="abc…">. One token covers all inline scripts. Requires server-side rendering.'sha256-…')openssl dgst uses. Nonces shown here are random demo values; on a real site your server must generate a new nonce on every request and inject it into both the header and the HTML.Hashes will appear here…
Nonce will appear here…
Paste an existing policy. The validator checks 20+ misconfiguration patterns: typos, contradictory 'none', bare 'unsafe-inline', 'strict-dynamic' without nonce/hash, wildcard/data:/http: sources, missing object-src/base-uri/frame-ancestors, deprecated directives, sandbox footguns, and more.
One click loads a preset into the Builder. Every preset serializes through the same spec-correct engine and is re-validated before display.
'nonce-{NONCE}' as a placeholder — a static tool cannot rotate nonces for you. Your server must generate a fresh 128-bit nonce per request and substitute it in both the header and each <script nonce="…"> tag. The validator understands this placeholder.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. It is the single most effective XSS (cross-site scripting) mitigation available: even if an attacker manages to inject markup into your page, the browser refuses to run scripts from sources you never allowed.
A minimal policy looks like this:
Content-Security-Policy: default-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'
Each directive (default-src, script-src, …) carries a list of allowed sources: 'self' (your own origin), 'none' (nothing), host names, https:, data:, nonces, and hashes. Anything not listed is blocked and reported.
| Directive | Controls | Attack it prevents |
|---|---|---|
default-src | Fallback for all unset fetch directives | Baseline for everything — without it there is no default deny |
script-src | Which scripts may execute | XSS — the main event. No attacker scripts, no payload |
style-src | Stylesheets & inline styles | CSS-based data exfiltration, UI redressing helpers |
img-src | Images | Tracking pixels from attacker domains |
connect-src | fetch / XHR / WebSocket / EventSource | Data exfiltration to attacker servers |
font-src | Web fonts | Malicious font parsing exploits |
frame-src | Embedded frames | Malicious iframes, drive-by content |
frame-ancestors | Who may embed your page | Clickjacking — replaces X-Frame-Options |
object-src | <object> / <embed> | Legacy plugin XSS — set to 'none' |
base-uri | <base> tag URL | Base-tag injection hijacking relative URLs |
form-action | Where forms may submit | Credential harvesting via injected forms |
worker-src | Workers / ServiceWorkers | Persistent attacker code via service worker |
upgrade-insecure-requests | Auto-upgrades http: → https: | Mixed-content tampering |
report-uri / report-to | Violation report endpoint | Visibility — you can't fix what you can't see |
WordPress core, themes, and plugins rely heavily on inline scripts, so a strict policy will break the admin area. Workflow: (1) generate the WordPress preset above, (2) deploy it as Content-Security-Policy-Report-Only first, (3) watch violation reports for 1–2 weeks and allowlist what your plugins legitimately need, (4) switch to enforcing. Send the header from your server config or with header('Content-Security-Policy: …') in PHP (or a security plugin).
Add inside server or location (on Linux, typically under /etc/nginx/) and reload: add_header Content-Security-Policy "…" always; — the always parameter ensures the header is sent on error responses too. Use the nginx output format above for the exact snippet.
Header always set Content-Security-Policy "…" in your virtual host or .htaccess (requires mod_headers). The Apache output format generates it verbatim.
Add a <customHeaders> entry under system.webServer/httpProtocol, or set it in IIS Manager → HTTP Response Headers. Use the IIS output format for the XML snippet.
Set the header in middleware with res.setHeader('Content-Security-Policy', '…') — or use the helmet npm package, which manages CSP (including per-request nonces) for you. The Express output format gives you the middleware snippet.
Attach the header in middleware: return $next($request)->header('Content-Security-Policy', '…'); — or use spatie/laravel-csp for nonce support. The Laravel output format generates the snippet.
Set headers in next.config.js under async headers() for the /(.*) source. For nonces, use Next.js middleware to generate a per-request value and pass it via request.headers.
<meta http-equiv="Content-Security-Policy" content="…"> works when you can't touch server config (static hosting, some CMS themes) — but frame-ancestors, report-uri, report-to, and sandbox are silently ignored in meta tags. Prefer the header.
Same header, different plumbing: Spring Security's headers().contentSecurityPolicy("…"), Django via django-csp middleware, Flask by setting the header in after_request. The policy syntax this tool generates is identical everywhere — only the delivery mechanism changes.
Chrome extensions (Manifest V3): extensions declare their own policy via the content_security_policy key in manifest.json (extension_pages and sandbox contexts) — remote scripts are banned entirely there, so generate hashes for any bundled inline code.
Mobile apps & WebViews (Android/iOS): a CSP delivered by your web server also applies inside Android WebView and iOS WKWebView. Keep connect-src tight — it governs what API endpoints your hybrid app can reach.
Blogs: the Balanced preset fits most blogs — allow your theme's hosts plus https: for embeds, then tighten with report-only data.
Google Analytics 4: GA4 needs script-src access to www.googletagmanager.com and connect-src to www.google-analytics.com — add both as custom hosts in the Builder rather than opening https: wholesale.
Local development: allow localhost:* / 127.0.0.1:* in connect-src and script-src while developing, and strip them before deploying to production.
Microsoft Edge: Edge enforces the same CSP core as Chrome (both Chromium) — test your policy in Edge, Firefox, and Safari, since Safari lags on Level 3 features.
Libraries: third-party JavaScript libraries — any library like jQuery, an analytics snippet, or a chat widget — are the usual reason policies need https: or extra hosts. See the directive reference table above for concrete examples of what each directive allows.
This page doubles as a step-by-step tutorial: the rollout playbook above summarizes CSP best practices — the core principle is default-deny with explicit allowlists. Review your policy in the Validator tab after every deploy that adds scripts (Next.js — often searched as "nextjs" — nodejs/Express, TypeScript builds included — CSP applies to the served JavaScript, whatever compiled it).
Content-Security-Policy-Report-Only) with a report-uri collector for 1–2 weeks.Troubleshooting "not working": open Chrome or Firefox DevTools → Console. Every blocked resource logs a precise violation naming the directive and the blocked URL — add the missing source and redeploy. Edge and Firefox enforce the same core; Safari lags on some Level 3 features (navigate-to, parts of Trusted Types), so test there too.
| Tool | Type | What it does well | Where this generator wins |
|---|---|---|---|
| This generator (nrtechstudio.com) | Web app | Visual builder, real nonce/hash crypto, validator, framework presets, SEO guides | — |
| Google CSP Evaluator | Web app | Excellent at evaluating an existing policy | It's an evaluator, not a generator — it won't build you a policy or emit server snippets |
| csp-builder (GitHub Pages single HTML) | Web app | Decent checklist UI | No framework presets, no nonce/hash generation, no validator, no guides or SEO content |
| CLI/crawler generators (HashCSP, csp-gen, …) | CLI / server-side | Auto-discover sources by crawling your site | Require installing/running code and server access; different intent (crawl vs. guided builder) |
| MDN CSP docs | Documentation | The authoritative reference | Not a tool — no builder, no validation, no snippets |
Comparison is factual to the best of our knowledge as of October 2026; features change — verify before choosing.
This generator is 100% client-side: your policy drafts, inline scripts, and hashes never leave your browser — there is no server to send them to. Hashes use the Web Crypto API (the same SHA-256/384/512 your OS ships). The tool is defensive by design: it helps you harden websites against XSS and clickjacking, and the validator actively warns against weak configurations.
Free tool by NRTechStudio
Explore similar security tools and calculators. 100% free, accurate, and runs completely in your browser without sign-up.
2 taps, no typing needed — diagnostics are sent automatically.