CSP Generator

Build a Content Security Policy without locking yourself out.

CSP Generator tool

Directives

Deploy in report-only mode first. It reports what would have been blocked without blocking anything, which is how you find out what your site actually loads before you start enforcing.

About the CSP Generator

A Content Security Policy tells browsers exactly which sources a page may load scripts, styles, images, fonts and frames from, making cross-site scripting attacks far harder to exploit. This CSP generator builds a policy one directive at a time, warns when you add risky values such as unsafe-inline or a wildcard source, and can output a report-only version so you can watch for problems before enforcing it. Starting strict and loosening only what real pages need is the safest approach. Remember to include any analytics, advertising or font services your site actually uses. For the other protective headers, use the Security Headers Generator, and to check URLs of allowed sources, the URL Parser.

Get a policy wrong and parts of your own site stop working, which is why this generator assembles it one directive at a time and explains every source value as you add it. You can see exactly what self, none, a domain name, a nonce and the unsafe keywords each allow, instead of copying a policy from a forum and hoping it fits.

Two features make a policy workable on a real website. Report-only mode sends the policy in a header that records violations without blocking anything, so you can discover everything your pages load before enforcement begins. The tool also highlights unsafe-inline and unsafe-eval, because a script policy that contains unsafe-inline gives away nearly all of the protection the header was meant to add.

How to use the CSP Generator

  1. Pick a starting pointBegin from one of three ready-made policies, from very locked down to a gradual trial, or from an empty form.
  2. Fill in the directivesEach source you add comes with a short explanation of what it permits.
  3. Roll out in report-only modeGather violation reports first, before anything is blocked.
  4. Copy the resultTake it as a header line or as ready-made server configuration.

CSP Generator features

  • The common directives, each with an explanation
  • Sources such as self, none, nonces, hashes and domains
  • Report-only mode for a gradual rollout
  • Prominent warnings for unsafe-inline and unsafe-eval
  • Strict, typical and report-only presets
  • Output as a header line or as Apache and nginx configuration
  • A note on why frame-ancestors replaces X-Frame-Options

Common uses

  • Adding a security policy to a live site without breaking pages
  • Removing unsafe-inline from a policy that relies on it
  • Controlling which websites may show your pages in a frame
  • Making sense of a policy someone else configured
  • Collecting violation reports before switching enforcement on

Content Security Policy in practice: a policy for a static site, hashes versus nonces, inline handlers and a safe rollout

By the FreeToolsWorld editorial team · Updated November 2026 · How we write and check our guides

A content security policy limits where a page may load scripts, styles, fonts and other resources from, so injected code cannot run. Writing one is straightforward for a simple site and fiddly for a site with many third-party services, and the hardest part is rolling it out without breaking anything. This guide builds a policy for a static site using Google Fonts, explains why cached static pages should use hashes rather than nonces, shows what happens to inline event handlers and sets out a safe rollout plan.

A worked example: a static site with web fonts

A static site serves its own scripts, styles and images, uses Google Fonts and shows a few small images embedded as data URIs. Google Fonts loads its stylesheet from fonts.googleapis.com and the font files from fonts.gstatic.com, so both must be allowed in different directives:

  • default-src 'self';
  • style-src 'self' https://fonts.googleapis.com;
  • font-src https://fonts.gstatic.com;
  • img-src 'self' data:;
  • object-src 'none'; base-uri 'self'; frame-ancestors 'self'

Each line answers one question about what the page loads. Tracing every resource in the browser's Network panel before writing the policy ensures nothing is missed.

Directives people forget

object-src 'none' blocks old plug-in content that attackers can still abuse, and base-uri 'self' stops an injected base tag from redirecting every relative script address to an attacker's server. frame-ancestors controls which sites may embed your pages in frames and is the modern replacement for the older X-Frame-Options header. These three add strong protection at almost no risk of breaking anything.

Hashes for static pages

A nonce must be a fresh random value for every response. On a static site served from a cache such as Varnish or a CDN, every visitor receives the same cached page and therefore the same nonce, which defeats its purpose. Static sites should allow their few inline scripts by hash instead: the policy lists 'sha256-' followed by the Base64 form of the script's digest. Note that this is the Base64 encoding of the hash, not the hexadecimal form most hash tools display, and the hash changes whenever the script changes by even one character.

Inline event handlers

Attributes such as onclick written directly in HTML are inline scripts, and a policy without unsafe-inline blocks them. Older pages full of these handlers stop responding to clicks the moment a strict policy is enforced. Moving the behaviour into script files using addEventListener is the clean fix and makes the code easier to maintain as well.

Third-party scripts that load others

Analytics, advertising and tag management scripts often load further scripts from changing addresses. Listing every domain becomes impossible to maintain. The 'strict-dynamic' source lets scripts you have already trusted through a nonce or hash load further scripts, so the policy stays short while still blocking injected code. Support is good in modern browsers, but test carefully with each third-party service.

Policy in a meta tag

A policy can be placed in a meta tag in the page head instead of a header, which helps on hosting where headers cannot be changed. Some directives do not work this way, including frame-ancestors and violation reporting, and the policy only applies to content after the tag. A header remains the better option wherever you control the server.

A safe rollout

Start with the report-only header, which reports violations without blocking anything. Collect reports for one or two weeks, covering every page type and visitor journey, then adjust the policy and switch to enforcement. Expect noise from browser extensions, which inject their own scripts and appear in reports from addresses such as chrome-extension; these can be filtered out. When tightening later, run the stricter version in report-only mode alongside the enforced policy first.

Common mistakes

  • Allowing fonts.googleapis.com but not fonts.gstatic.com, so fonts never load.
  • Using fixed nonces on cached static pages.
  • Pasting a hexadecimal hash instead of the Base64 form.
  • Enforcing a policy before collecting reports.

Related tools

To add the other protective headers, use Security Headers Generator. To compute digests, try SHA-256 Generator, remembering to convert to Base64, and to check content types, MIME Type Lookup.

Images and media from other sites

The img-src directive must list every place images come from: your own domain, a CDN, analytics pixels, avatar services and map tiles. Video and audio need media-src. Some sites take the pragmatic route of allowing any HTTPS image source with img-src https: data:, since images carry far less risk than scripts, while keeping script sources tightly restricted.

Forms and connections

Two directives do not fall back to default-src in the way people expect. form-action, which limits where forms may submit, has no fallback at all, so it must be set explicitly to stop injected forms sending data elsewhere. connect-src controls the addresses scripts may contact using fetch and similar features, so APIs and analytics collection endpoints must be listed there or their requests fail.

Keeping the policy maintained

A policy needs updating whenever the site adds a service such as a chat widget, payment form or new analytics tool. Keeping the policy in version control with a comment explaining why each source is allowed makes it easy to remove sources when services are dropped, so the policy does not slowly grow into an allow-everything list.

Privacy

Policies are built in your browser. The domains and sources you enter are not sent anywhere or stored.

Frequently asked questions

Where should I start with a new policy?

Begin with report-only mode. The header is sent as Content-Security-Policy-Report-Only, which logs what would have been blocked without blocking it. Leave it for about a week, review the reports, adjust the policy and only then enforce it.

What is wrong with unsafe-inline?

It allows every inline script on the page, and an injected attack script is exactly that. With unsafe-inline in script-src the policy offers little real protection, so mark the inline scripts you genuinely need with a per-response token or a content hash instead.

What exactly is a nonce?

A random token created fresh for every page response and placed both in the policy and on each permitted script tag. Only scripts carrying the matching token run, so it needs server-side code, and reusing the same value defeats it.

Does default-src apply to every directive?

It acts as the fallback for most of the resource-loading directives, but not for all of them. frame-ancestors, base-uri and form-action ignore it entirely, so each of those must be written out on its own.

Could a policy break my analytics or advertising?

Yes, if those services' domains are missing from the policy. That is the main reason for running report-only mode first: third-party scripts tend to load from more hosts than their documentation lists, and the reports reveal them.

Is a Content Security Policy enough protection by itself?

No. It is a strong second layer of defence. Escaping and validating everything your pages output is still the first line, and the policy limits the harm when something gets past it.