Use this free CSP generator to build a Content-Security-Policy for your website. Start from a preset, choose what your pages may load, list any trusted third-party hosts, and copy the result as an HTTP header, a meta tag, or a server config snippet. The tool scores the policy and flags risky settings. Everything runs in your browser and nothing is uploaded or stored.

A Content Security Policy tells the browser which sources a page may load and run. A strict policy can block injected scripts even when an attacker finds a hole in your HTML.

How to use the CSP generator

Pick a starting preset, then adjust the options. The policy updates each time you change a setting.

  • Start from: Strict is the recommended starting point. Website with CDNs allows common third-party hosts. Blank blocks everything.
  • Trusted sources: list each external host on its own line, such as https://cdn.example.com. Invalid entries are skipped and reported.
  • Options: turn on only what your site needs. Each allowance is explained in the warnings.
  • Output format: choose an HTTP header, an HTML meta tag, or an Nginx or Apache line.
  • Copy: copy the snippet, or copy a report with the score and warnings.

How the score is calculated

  • Starting score: 100 points.
  • Inline scripts and styles: minus 25 points.
  • eval(): minus 25 points.
  • Images from any HTTPS site: minus 10 points.
  • data: images: minus 5 points.
  • Framing not blocked: minus 5 points.
  • Plain http sources: minus 5 points.
  • A bare wildcard (*): minus 20 points.
  • Rating: Strong for 85 and above, Moderate for 60 to 84, and Weak below 60.

The score rates the policy text only. It does not scan your pages, so a high score does not prove your site is secure.

Directives used in the policy

DirectiveGenerated valueWhat it controls
default-src'self' plus trusted sourcesFallback for resource types without their own directive
script-srcSources, plus unsafe options if enabledJavaScript files and inline code
style-srcSources, plus unsafe-inline if enabledStylesheets and inline styles
img-srcSources, plus data: and https: if enabledImages
font-src, connect-src, form-actionSourcesWeb fonts, fetch and XHR requests, and form targets
object-src'none'Plugins such as Flash or Java applets
base-uri'self'The base tag, which can redirect relative links
frame-ancestors'none' or 'self'Which sites may embed this page in a frame
upgrade-insecure-requestsIncluded when enabledLoads http resources over https

What this tool does and does not do

This tool builds and checks the policy text you configure. It does not read your pages, find the inline scripts you already have, test whether your site still works, or know which third-party services you depend on. Test the policy on a staging copy first, and consider sending it as Content-Security-Policy-Report-Only before you enforce it.

Worked examples

A static site with no third-party code

Keep the Strict preset with no trusted sources. Every directive falls back to 'self', and framing and upgrades are blocked. The score is 100, with no warnings.

A site using a CDN and Google Fonts

Choose the Website with CDNs preset. It allows inline scripts and styles, which is why the score drops to 75 and the rating is Moderate. Move inline code into separate files to remove that deduction, then turn off the option.

Testing before enforcing

Send the policy as a report-only header first. Watch the browser console for blocked resources, add only the sources you truly need, and then switch the header name to Content-Security-Policy.

Common CSP mistakes

  • Adding 'unsafe-inline' to fix one error. Move the script into a file, or use a nonce or hash instead.
  • Adding * to make things load. A wildcard allows any origin, which defeats most of the policy.
  • Leaving out default-src. Resource types without their own directive are then unrestricted.
  • Putting frame-ancestors in a meta tag. Browsers ignore it there, so send it as an HTTP header.
  • Listing http sources on an https site. Plain http resources can be changed in transit.

Frequently asked questions

Is this CSP generator free?

Yes. There is no sign-up and no limit on how many policies you generate.

Is my policy sent or stored?

No. The policy is built in your browser. Nothing is sent to a server or saved, and it disappears when you close or refresh the page, so copy the snippet before you leave.

What is a Content Security Policy?

It is a set of rules, sent as an HTTP header or meta tag, that tells the browser which sources a page may load and run. It limits the damage that injected code can cause.

Does a strict CSP stop all attacks?

No. A policy reduces the impact of many injection attacks, but it does not replace input validation, output encoding, and secure coding.

Should I use a header or a meta tag?

An HTTP header is preferred. Browsers ignore frame-ancestors in a meta tag, so framing protection needs the header.

Why does my site break after I apply the policy?

Usually an inline script, an inline style, or a third-party host is blocked. Check the browser console for blocked resources, then allow that specific source or move the code into a file.

Share this article