consolelog.tools

CSP Generator

Build a hardened Content-Security-Policy header directive by directive. Pick a preset, add per-directive sources (keywords, hosts, nonces), see a live security audit, and export copy-paste config for HTTP headers, HTML meta, nginx, Apache, Vercel, Netlify, Next.js, or Express.

About this ToolHow it works, benefits & use cases

Content-Security-Policy is one of the strongest defenses against cross-site scripting, but its directive syntax is unforgiving and easy to get subtly wrong. This generator builds a complete policy visually: start from one of six presets — Strict (nonce + strict-dynamic), Basic self-only, Google Analytics / Tag Manager, Common CDNs, Locked-down API, or an Allow-all report-only starting point — then fine-tune every directive. You get the full standard directive set: default-src, the script-src family (script-src, script-src-elem, script-src-attr), style-src and style-src-elem, img-src, font-src, connect-src, media-src, object-src, frame-src, worker-src, manifest-src, child-src, prefetch-src, frame-ancestors, base-uri, and form-action, plus the boolean directives upgrade-insecure-requests and block-all-mixed-content and the report-to / report-uri reporting endpoints. For each directive you add keyword sources ('self', 'none', 'unsafe-inline', 'unsafe-eval', 'strict-dynamic', 'wasm-unsafe-eval', https:, http:, data:, blob:, mediastream:, *), custom hosts, or a freshly generated nonce. A live audit panel grades the policy against real-world weaknesses — 'unsafe-inline' or data: in script-src, 'unsafe-eval', wildcards, and missing object-src, base-uri, or frame-ancestors — while a plain-English summary explains what the policy allows and blocks. A Report-Only toggle emits the Content-Security-Policy-Report-Only header so you can deploy safely. Finally, export copy-paste config for eight targets: a raw HTTP header, an HTML meta tag, nginx, Apache, Vercel, Netlify, Next.js, or Express. Everything runs in your browser and the full policy is captured in the URL so you can share it.

How to Use

  1. 1Pick a preset (Strict, Basic self-only, Google Analytics, Common CDNs, Locked-down API, or Report-only start) as your starting point.
  2. 2For each directive, add keyword sources from the picker, type a custom host, or click Nonce to insert a fresh nonce into a script directive.
  3. 3Toggle the special directives (upgrade-insecure-requests, block-all-mixed-content) and add a report-to or report-uri endpoint.
  4. 4Read the live policy audit and the plain-English summary, and remove any flagged weaknesses.
  5. 5Flip Report-Only on to test the policy without blocking, then choose your export target (HTTP, meta, nginx, Apache, Vercel, Netlify, Next.js, or Express).
  6. 6Copy or download the generated config and deploy it.

Key Benefits

  • Six presets including a modern nonce + strict-dynamic policy and a locked-down API policy
  • Full directive coverage: every standard fetch directive plus document, boolean, and reporting directives
  • Per-directive source picker with keywords, custom hosts, and one-click nonce generation
  • Live weakness audit that flags unsafe-inline, unsafe-eval, wildcards, and missing object-src / base-uri / frame-ancestors
  • Report-Only toggle that emits Content-Security-Policy-Report-Only for safe rollout
  • Copy-paste export for eight targets: HTTP, HTML meta, nginx, Apache, Vercel, Netlify, Next.js, Express
  • Runs entirely in the browser — no policy is sent anywhere, and the full config is shareable via URL

Common Use Cases

  • Authoring a first CSP for an app and validating it against common pitfalls before launch
  • Migrating from 'unsafe-inline' scripts to a nonce-based, strict-dynamic policy
  • Generating a default-src none policy for a JSON API that renders no HTML
  • Whitelisting Google Analytics, Tag Manager, or popular CDNs without memorizing their endpoints
  • Deploying a policy in Report-Only mode to collect violations before enforcing it
  • Producing ready-to-paste header config for nginx, Apache, Vercel, Netlify, Next.js, or Express
Content-Security-Policy
default-src 'self'; script-src 'nonce-{RANDOM}' 'strict-dynamic' https:; style-src 'self'; img-src 'self' data:; font-src 'self'; connect-src 'self'; object-src 'none'; frame-ancestors 'none'; base-uri 'none'; form-action 'self'; upgrade-insecure-requests

Allows resources from your own origin by default. Cannot be embedded in any iframe (clickjacking blocked). Upgrades insecure http:// subresource requests to https://. Inline scripts must carry the matching nonce.

Policy audit
  • infoNo reporting endpoint — add report-to (or report-uri) to monitor violations.

Drop into csp-header.txt

Content-Security-Policy: default-src 'self'; script-src 'nonce-{RANDOM}' 'strict-dynamic' https:; style-src 'self'; img-src 'self' data:; font-src 'self'; connect-src 'self'; object-src 'none'; frame-ancestors 'none'; base-uri 'none'; form-action 'self'; upgrade-insecure-requests

Preset

Reporting

Special directives

Rewrite http:// subresource requests to https://.

Block any mixed (http) content (deprecated).

Directives

default-src

Fallback for every other fetch directive.

'self'
script-src

Valid sources for JavaScript.

'nonce-{RANDOM}''strict-dynamic'https:
script-src-elem

Sources for <script> elements.

script-src-attr

Sources for inline event handlers.

style-src

Valid sources for stylesheets.

'self'
style-src-elem

Sources for <style>/<link> styles.

img-src

Valid sources for images and favicons.

'self'data:
font-src

Valid sources for @font-face fonts.

'self'
connect-src

Sources for fetch, XHR, WebSocket, EventSource.

'self'
media-src

Sources for <audio> and <video>.

object-src

Sources for <object>/<embed> plugins.

'none'
frame-src

Sources for nested browsing contexts (iframes).

worker-src

Sources for Worker, SharedWorker, ServiceWorker.

manifest-src

Sources for the web app manifest.

child-src

Legacy fallback for frame-src/worker-src.

prefetch-src

Sources for prefetch/prerender requests.

frame-ancestors

Who may embed this page (anti-clickjacking).

'none'
base-uri

Restricts <base> URLs.

'none'
form-action

Valid endpoints for <form> submissions.

'self'

Was this tool helpful?

Share Your Experience

Help others discover this tool!

Related tools