Skip to main content

Free Content-Security-Policy Generator – Build a CSP Header

Build a Content-Security-Policy header directive by directive with plain-language explanations, unsafe-inline warnings, presets, and nonce support. Outputs header, report-only, and meta tag.

Written & reviewed by Helperzy Editorial Team · Updated July 2026

12 DirectivesPresetsNonce GeneratorReport-OnlyFree

Presets

The Google Analytics + AdSense preset lists typical domains (googletagmanager.com, google-analytics.com, googlesyndication.com, doubleclick.net) — treat these as a starting point, not a guaranteed-exhaustive list for every AdSense account or GA property.

default-src

The fallback for any fetch directive that isn't explicitly set — the safety net.

If misconfigured: Leave it too loose (e.g. '*') and every other tightened directive is undermined by anything that falls through to it.

'self'

script-src

Controls which JavaScript is allowed to execute — inline scripts, eval, and external script sources.

If misconfigured: Too strict and your analytics, ad, or third-party widget scripts silently stop running with a console error; too loose and it's the #1 line of defense against XSS you've given up.

'self'

style-src

Controls which CSS is allowed to load and apply — inline style attributes, <style> tags, and external stylesheets.

If misconfigured: Many CSS-in-JS libraries and inline style= attributes need 'unsafe-inline' here or they'll render unstyled.

'self'

img-src

Controls which sources images can load from.

If misconfigured: Block a CDN your images live on and they simply won't render — broken image icons everywhere.

'self'data:

font-src

Controls where @font-face font files can load from.

If misconfigured: Miss your web-font CDN (e.g. Google Fonts) and the page falls back to system fonts.

'self'

connect-src

Controls which endpoints fetch(), XHR, WebSocket, and EventSource can connect to.

If misconfigured: This is the one people forget — an API call to a domain not listed here fails silently in the console, not on the page.

'self'

media-src

Controls sources for <audio> and <video> elements.

If misconfigured: A video hosted on a third-party CDN not listed here refuses to play.

object-src

Controls <object>, <embed>, and <applet> — legacy plugin content.

If misconfigured: Almost nothing legitimate needs this anymore; setting it to 'none' closes off an old Flash/Java-era attack surface with essentially zero downside.

'none'

frame-src

Controls which sources can be loaded inside an <iframe> on your page.

If misconfigured: Embed a YouTube video or a payment widget's iframe without listing its domain and the embed shows a blank frame.

frame-ancestors

Controls which sites are allowed to embed YOUR page in an iframe — the modern anti-clickjacking directive.

If misconfigured: Set to 'none' and a legitimate partner trying to embed your widget gets a blank frame too; this directive doesn't work in a <meta> tag, only the HTTP header.

'self'

base-uri

Restricts what URLs can be used in a <base> tag, which changes how all relative URLs on the page resolve.

If misconfigured: Rarely breaks anything legitimate; leaving it open lets an XSS payload inject a <base> tag that hijacks every relative link and script src on the page.

'self'

form-action

Restricts which URLs a <form> can submit to.

If misconfigured: A payment form that posts to a third-party processor's domain needs that domain listed here or the submission is blocked.

'self'

Tells the browser to rewrite any http:// URL on the page to https:// before requesting it. If your site (or an embedded resource) genuinely only has an HTTP endpoint, that resource will fail to load.

When set, browsers POST a JSON report to this endpoint every time the policy blocks something — useful for finding what you'd break before switching from Report-Only to enforced.

HTTP Header (Enforced)

Content-Security-Policy: default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; font-src 'self'; connect-src 'self'; object-src 'none'; frame-ancestors 'self'; base-uri 'self'; form-action 'self'; upgrade-insecure-requests

HTTP Header (Report-Only)

Content-Security-Policy-Report-Only: default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; font-src 'self'; connect-src 'self'; object-src 'none'; frame-ancestors 'self'; base-uri 'self'; form-action 'self'; upgrade-insecure-requests

<meta> Tag Equivalent

<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; font-src 'self'; connect-src 'self'; object-src 'none'; frame-ancestors 'self'; base-uri 'self'; form-action 'self'; upgrade-insecure-requests">

frame-ancestors, report-uri/report-to, and sandbox have no effect inside a <meta> tag — browsers ignore them there. Use the real HTTP header if your policy needs any of those three.

Scope: This Tool Is CSP Only

This generator builds Content-Security-Policy values only. For HSTS, X-Frame-Options, Referrer-Policy, Permissions-Policy, and the other non-CSP security headers, use the companion Security Headers Generator.

100% Private

The policy is built entirely in your browser. Any nonce shown is generated locally via WebCrypto for preview — nothing you configure here is sent anywhere.

How to Use Content-Security-Policy Generator

1

Start From a Preset

Choose Strict, Moderate, the Google Analytics plus AdSense preset, or the nonce-based strict starter. Each fills in a sensible baseline you can then adjust, so you are not building a policy from a blank slate and risking a broken or overly permissive result.

2

Adjust Directives and Watch the Warnings

Toggle source tokens like 'self', 'none', or specific https hosts per directive, or add custom CDN hosts. If you enable 'unsafe-inline' or 'unsafe-eval', a warning explains the risk so you can choose a nonce or hash instead where it matters.

3

Copy the Header, Report-Only, or Meta Output

Copy the enforced Content-Security-Policy header, the Report-Only variant for safe rollout, or the meta-tag equivalent. Note the caveat that frame-ancestors and report-uri only work in the real HTTP header, not in a meta tag.

What a Content-Security-Policy Does and How to Build One Safely

A Content-Security-Policy, or CSP, is an HTTP response header that tells the browser which sources of scripts, styles, images, fonts, and other resources your page is allowed to load and execute. It is one of the strongest defenses against cross-site scripting (XSS), because even if an attacker manages to inject a malicious script tag, a well-configured CSP stops the browser from running it. This generator builds that policy directive by directive, explaining what each one controls and what breaks if you get it wrong, so front-end developers, security engineers, and site owners can produce a working policy without memorizing the entire specification. A CSP is a semicolon-separated list of directives, each naming a resource type and the sources allowed for it. default-src is the fallback for anything not explicitly set; script-src controls JavaScript; style-src controls CSS; img-src, font-src, connect-src, media-src, frame-src, and others cover their respective resource types; and frame-ancestors controls who may embed your page in an iframe, the modern anti-clickjacking control. Sources are expressed as tokens like 'self' (same origin), 'none' (block everything), specific https hosts, or the risky 'unsafe-inline' and 'unsafe-eval'. The tool assembles your selections into three outputs: the enforced Content-Security-Policy header, a Content-Security-Policy-Report-Only variant that logs violations without blocking, and an equivalent meta tag, noting that frame-ancestors and report-uri are ignored inside a meta tag. Start from the strict preset and you get default-src 'self' with object-src 'none', base-uri 'self', form-action 'self', and frame-ancestors 'self' — a policy that only allows resources from your own origin. If you then add 'unsafe-inline' to script-src to make a legacy inline script work, the tool immediately shows a warning banner explaining that 'unsafe-inline' lets any injected inline script run, which largely defeats the XSS protection CSP is meant to provide. The recommended alternative is the nonce starter preset, which produces script-src with a generated 'nonce-<value>' plus 'strict-dynamic', so only scripts carrying that exact per-request nonce execute. The tool generates a sample nonce locally for preview, while making clear that a real nonce must be freshly minted on the server for every single request. A developer hardening a new site pastes the strict preset, then adds only the specific CDN and analytics hosts their app genuinely needs, watching the warning stay quiet as long as no unsafe token is enabled. A team running Google Analytics and AdSense uses the dedicated preset that pre-fills the typical Google domains, treating it as a starting point they verify against their own account rather than a guaranteed-complete list. A security engineer rolling out CSP to a large existing site first deploys the report-only variant with a report-uri endpoint, collects violation reports for a week to see exactly what would break, and only then switches to the enforced header — the standard, low-risk way to adopt CSP without taking a live site down. The pitfall that trips people up most is reaching for 'unsafe-inline' the moment something stops working, which quietly removes most of the protection; the correct fix is almost always a nonce or a hash for the specific inline script, and the tool nudges you toward that. A second gotcha is expecting a meta-tag CSP to do everything a header does — frame-ancestors, report-uri, and sandbox simply have no effect in a meta tag, so if your policy needs any of those you must use the real HTTP header. This tool builds the policy text only; a production nonce has to be generated server-side per request and injected into both the header and the matching script tag, since a reused or client-generated nonce provides no real protection. Every policy is assembled locally in your browser, and nothing you configure is sent anywhere.

Examples: Content-Security-Policy Generator

Input

Strict preset

Result

default-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'self'; upgrade-insecure-requests

Allows resources only from your own origin and blocks legacy plugin content — a safe baseline that resists most XSS injection.

Input

script-src with 'unsafe-inline' added

Result

Warning banner: 'unsafe-inline' largely defeats CSP's XSS protection

The tool flags the token because it lets any injected inline script run; the recommended fix is a nonce or hash instead.

Input

Nonce starter preset

Result

script-src 'nonce-<generated>' 'strict-dynamic' https:

Only scripts carrying the exact per-request nonce execute, and strict-dynamic extends trust to scripts they load — a strict yet practical policy.

Frequently Asked Questions – Content-Security-Policy Generator

Primarily cross-site scripting. Even if an attacker injects a script into your page, a strict CSP prevents the browser from executing it because the script's source is not in your allowed list. CSP also mitigates clickjacking via frame-ancestors and can block mixed content and data exfiltration.