DevKits

Content-Security-Policy (CSP) Generator — Build a CSP Header Online

Build a Content-Security-Policy header by picking directives and sources. Generate a ready-to-paste CSP for script-src, style-src, img-src, connect-src and more — with sensible strict defaults. 100% local.

Last updated:

Comments

Pick directives (script-src, style-src, img-src…) and sources to build a Content-Security-Policy header you can paste straight into your server config. Starts from strict defaults.

Fallback for any directive you don't set
Where scripts may load from
Where stylesheets may load from
Where images may load from
Where fonts may load from
fetch/XHR/WebSocket endpoints
What may be framed by this page
Who may frame this page (anti-clickjacking)
<object>/<embed> sources
Allowed <base> URLs
Content-Security-Policy: default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; frame-ancestors 'none'; object-src 'none'; base-uri 'self'

Tip: deploy as Content-Security-Policy-Report-Only first, review the violation reports, then switch to the enforcing header above.

What is CSP Generator?

A Content-Security-Policy (CSP) is an HTTP response header that whitelists where a page may load scripts, styles, images, fonts, frames, and connections from. It's one of the most effective defenses against cross-site scripting (XSS): even if an attacker injects a <script>, the browser refuses to run it unless its origin is on the allow-list. This generator lets you assemble a policy directive by directive — set a restrictive default-src 'self', then selectively allow the CDNs and APIs you actually use — and copy the finished header. The recommended rollout is to ship it first as Content-Security-Policy-Report-Only, review the violation reports, then switch to the enforcing header. Avoid 'unsafe-inline' and 'unsafe-eval', which re-open the exact hole CSP is meant to close; prefer nonces or hashes for the rare inline script you can't remove.

How to generate a CSP header

  1. 1Start from the strict default-src 'self' base.
  2. 2Add the directives you need (script-src, style-src, img-src, connect-src, font-src, frame-src) and list each allowed source.
  3. 3Copy the generated Content-Security-Policy value.
  4. 4Deploy it as Content-Security-Policy-Report-Only first, review violations, then enforce.

Key Concepts

default-src
The fallback source list applied to any resource type you don't set explicitly. Start with 'self' and open up from there.
nonce / hash
A per-response random token (nonce) or content hash that lets a specific inline script run without resorting to 'unsafe-inline'.
report-only mode
The Content-Security-Policy-Report-Only header logs violations without blocking, so you can tune a policy safely before enforcing it.

Tips & Best Practices

  • ▸Set the real header on the server — a <meta> CSP tag can't use several directives (like frame-ancestors or report-uri).
  • ▸frame-ancestors replaces the old X-Frame-Options for clickjacking protection.
  • ▸Keep the policy as tight as it can be while the site still works — every extra source weakens XSS protection.

Frequently Asked Questions

What is a Content-Security-Policy?

CSP is a response header that tells the browser which sources of scripts, styles, images, fonts, and other resources are allowed to load. A good CSP is one of the strongest defenses against cross-site scripting (XSS), because an injected <script> from an untrusted origin simply won't execute.

Should I start with report-only mode?

Yes. Deploy the policy as Content-Security-Policy-Report-Only first so violations are logged but nothing is blocked. Watch the reports for a few days, fix legitimate sources, then switch to the enforcing Content-Security-Policy header.

Why avoid 'unsafe-inline' and 'unsafe-eval'?

Both re-open the XSS hole CSP is meant to close: 'unsafe-inline' allows inline <script> and event handlers, and 'unsafe-eval' allows eval()/new Function(). Prefer nonces or hashes for the few inline scripts you truly need.

Try Next

Meta Tag Preview

Preview how your page will appear in Google search results and social share cards on Facebook, Twitter/X, and LinkedIn. Parses <title>, meta description, canonical, and full Open Graph / Twitter Card tags with diagnostics. 100% local.

Related Tools

Reference & Guides