CSP header generator — build a Content-Security-Policy

Fill in the sources each kind of content may load from, and the tool writes a Content-Security-Policy as a raw header, an nginx or Apache line, or a meta tag, quoting keywords such as 'self' for you.

Live demo · real result from this tool
Output as

Policy

Content-Security-Policy: default-src 'self'; script-src 'self' https://www.googletagmanager.com; style-src 'self'; img-src 'self' data: https:; font-src 'self'; connect-src 'self' https://www.google-analytics.com; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'; upgrade-insecure-requests 

Start in report-only mode, watch the browser console for violations, then enforce. Keywords such as self and none are quoted for you.

Processed on your device — nothing is uploaded.

How to use the CSP header generator

  1. Go through the directive fields, from default-src to frame-ancestors. Separate sources with spaces, for example 'self' https://cdn.example.com. Leave a field blank to omit that directive.
  2. Keep upgrade-insecure-requests ticked to load http:// resources over https.
  3. Optionally enter a report-uri that collects violation reports.
  4. Tick Report-only (test without blocking) for a first rollout.
  5. Pick Output as: HTTP header, nginx, Apache or <meta> tag, then copy the policy and read any warnings below it.

What it does and when to use it

A Content-Security-Policy tells the browser which origins may supply scripts, styles, images, fonts, frames and network requests for your page. If an attacker manages to inject a script tag, a strict policy stops it from running. It also controls who may embed your site in a frame.

The defaults suit a typical site with Google Tag Manager and Google Analytics. Replace those hosts with your own. Put the nginx line inside a server block from the nginx config generator, or the Apache line into a file from the .htaccess generator. The HTTP headers reference explains the other security headers that usually travel with CSP.

How it works

Each field becomes one directive; blank fields are dropped, and duplicate sources are removed. The tool then checks for risky combinations:

  • 'none' mixed with other sources (browsers ignore the others);
  • 'unsafe-inline' in script-src without a nonce or hash;
  • 'unsafe-eval' in script-src;
  • * in script-src or default-src.

The directives are joined with ; and wrapped for the chosen server. The directive names follow MDN’s Content-Security-Policy reference.

Worked examples

Default policy as a header (shortened):

Content-Security-Policy: default-src 'self'; script-src 'self' https://www.googletagmanager.com; … frame-ancestors 'none'; upgrade-insecure-requests

Typing keywords without quotes, for nginx. script-src self unsafe-inline becomes script-src 'self' 'unsafe-inline' inside add_header Content-Security-Policy "…" always;. A warning says unsafe-inline “removes most of the XSS protection CSP provides”.

Meta tag with report-only ticked. No tag is written. The tool explains that a meta tag cannot be report-only, because browsers would enforce it, and asks you to choose a header format or untick Report-only.

Apache, report-only. The line starts Header always set Content-Security-Policy-Report-Only "default-src 'self'; …".

Limits and tips

  • The tool does not scan your pages. You must list every third-party host yourself.
  • Nonces must be new on every response, so they belong in server code, not in a fixed header.
  • report-uri is deprecated in favour of report-to, which needs a separate Reporting-Endpoints header the tool does not write.
  • Test in report-only mode for a few days, then enforce.

Frequently asked questions

Do I have to type the single quotes around self and none?
No. Keywords such as self, none, unsafe-inline, strict-dynamic and wasm-unsafe-eval, plus nonce- and sha256- values, are quoted automatically. Host names and schemes such as https and data (written with their colon) are left as they are.
What does Report-only do?
It names the header Content-Security-Policy-Report-Only. Browsers then log violations without blocking anything, so you can see what a policy would break before you enforce it.
Why is frame-ancestors missing from my meta tag?
Browsers ignore frame-ancestors, report-uri, report-to and sandbox in a meta tag, so the tool leaves them out and warns you. Send the policy as an HTTP header if you need them.
My site broke after adding the header. What now?
Open the browser console; every blocked request is listed with the directive that blocked it. Add the missing source to that directive, or switch to report-only mode while you collect the full list.
Why does the tool warn about unsafe-inline?
In script-src it allows any inline script, including one an attacker injects, which removes most of what CSP protects against. Use nonces or hashes for the inline scripts you really need.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close