Home › Developer Tools › Content Security Policy Generator

Content Security Policy Generator

Pick the sources your site really needs and get a ready-to-paste Content-Security-Policy header, meta tag and server config. Generate nonces and script hashes, switch to report-only for safe testing, and see warnings for weak directives as you build.

Last updated:

Start from:
Allow services:
Import an existing policy

Options

Nonce & hash helpers

Nonce
—

Your server must create a new random nonce for every response. Use this one for templates and testing.

Hash of an inline script or style
More directives (child-src, manifest-src, sandbox, Trusted Types, raw directives)
Raw directives to append, separated by ; (e.g. script-src-attr 'none')

    
    Advertisement
    Advertisement

    How to use the Content Security Policy Generator

    1. Start from a preset (Strict, Basic, or add Google Fonts / Analytics) or import a policy you already use.
    2. Edit each directive: click the chips to add keywords or type hosts such as https://cdn.example.com.
    3. Add nonces or hashes for inline scripts instead of allowing 'unsafe-inline'.
    4. Turn on report-only while testing so violations are reported but nothing is blocked.
    5. Copy the output for your server (Nginx, Apache, _headers, Express) or as a meta tag, deploy it and check the browser console for violations.

    CSP directives at a glance

    DirectiveControlsFalls back to default-src?
    default-srcFallback for the fetch directives below-
    script-srcJavaScript files, inline scripts, event handlers, eval()Yes
    style-srcStylesheets, <style> blocks, style attributesYes
    img-src, font-src, media-srcImages, web fonts, audio and videoYes
    connect-srcfetch, XMLHttpRequest, WebSocket, EventSource, sendBeaconYes
    frame-src, worker-srcEmbedded iframes; Web and Service WorkersYes (via child-src)
    object-src<object> and <embed>Yes
    base-uriURLs allowed in <base href>No
    form-actionForm submission targetsNo
    frame-ancestorsWhich sites may embed this pageNo (header only)
    upgrade-insecure-requestsRewrites http:// subresource URLs to https://-

    Source expressions

    • 'self': the page's own origin (same scheme, host and port). 'none': nothing at all; it must be the only value.
    • Hosts: https://cdn.example.com, *.example.com, https://api.example.com:8443. Prefer full https URLs.
    • Schemes: https:, data:, blob:, wss:. data: is fine for images but dangerous in script-src.
    • 'nonce-<base64>': allows elements with a matching nonce attribute. 'sha256-<base64>': allows inline code whose hash matches exactly.
    • 'strict-dynamic': trust propagates from a nonced or hashed script to the scripts it loads; host lists are then ignored.
    • 'unsafe-inline', 'unsafe-eval', 'unsafe-hashes': weaken protection; avoid them in script-src when you can.

    A safe way to roll out a CSP

    1. Start with the Basic or Strict preset and add the third-party services you actually use.
    2. Deploy it as Content-Security-Policy-Report-Only with a report endpoint and browse every type of page on your site.
    3. Fix or allow each legitimate violation (move inline scripts to files, add nonces, add hosts).
    4. Switch to the enforcing Content-Security-Policy header and keep reporting enabled to catch regressions.

    Frequently asked questions

    What is a Content Security Policy?

    A Content Security Policy (CSP) is an HTTP response header that tells the browser which sources of scripts, styles, images, frames and other content a page may load. Anything not on the list is blocked, which makes cross-site scripting (XSS) and data injection attacks much harder.

    Should I use the header or the meta tag?

    Use the HTTP header whenever you can. The meta tag works for most directives, but browsers ignore frame-ancestors, report-uri, report-to and sandbox in a meta tag, and report-only mode is not available at all. The meta tag also only protects content that comes after it, so it must be at the top of the head.

    What is a CSP nonce?

    A nonce is a random, single-use value that you add to the policy as 'nonce-abc123' and to each allowed inline script as nonce="abc123". The server must generate a new unpredictable nonce for every response; a fixed nonce gives no protection. Nonces generated here are 128-bit random values for testing or templates.

    What does 'strict-dynamic' do?

    With 'strict-dynamic', a script that was allowed through a nonce or hash may load further scripts, and host allowlists and 'self' in script-src are ignored by browsers that support CSP Level 3. It is the basis of Google's recommended strict CSP: script-src 'nonce-...' 'strict-dynamic', with https: and 'unsafe-inline' only as fallbacks for older browsers.

    How do I test a CSP without breaking my site?

    Send it as Content-Security-Policy-Report-Only first. Browsers then report violations in the developer console (and to your report-uri or report-to endpoint) but load everything as usual. When the reports are clean, switch to the enforcing Content-Security-Policy header.

    Why is 'unsafe-inline' a problem?

    'unsafe-inline' allows any inline script and event handler on the page, which is exactly what most XSS attacks inject, so it removes most of the protection of CSP. Prefer nonces or hashes. Note that when a nonce or hash is present in script-src, CSP Level 2+ browsers ignore 'unsafe-inline', so it can stay as a fallback for very old browsers.

    Does CSP replace X-Frame-Options?

    The frame-ancestors directive is the modern replacement for X-Frame-Options and is more flexible (it accepts a list of allowed origins). frame-ancestors 'none' equals DENY and 'self' equals SAMEORIGIN. Many sites send both headers for older browsers.

    Are all the tools on FreeToolCabin tested for quality and accuracy?

    Yes! Every tool on our site undergoes automated Q/A testing in real browser environments to ensure high precision, correct output, and zero errors. If you happen to spot a bug or have a suggestion, please contact us at [email protected] and our team will review it right away.

    Related tools

    SRI Hash GeneratorCreate Subresource Integrity hashes (sha256/384/512) and ready <script> or <link> tags.Htaccess & Nginx Redirect GeneratorBuild 301 redirects for .htaccess, Nginx and Netlify or Cloudflare _redirects.Meta Tag AnalyzerPaste page HTML to audit title, description, canonical, robots, Open Graph and hreflang tags.SHA-256 Hash GeneratorSHA-256 and HMAC-SHA256 hashes of text, lines or files, in hex or Base64.Privacy Policy GeneratorCreate a privacy policy for your website or app with CCPA, CalOPPA, COPPA and AdSense sections.UTM Campaign URL BuilderAdd UTM tracking parameters to links and make a QR code.Meta Tag GeneratorCreate SEO, Open Graph and X meta tags with live previews.Robots.txt GeneratorBuild a robots.txt file with rules, sitemap lines and AI-bot blocking presets.Sitemap GeneratorTurn a list of URLs into a valid XML sitemap file.SERP Snippet PreviewPreview your title tag and meta description with pixel-accurate truncation on desktop and mobile.Hreflang Tag GeneratorCreate hreflang tags, sitemap entries and Link headers for every language and region, validated.Canonical Tag GeneratorNormalize URLs and create rel=canonical tags and HTTP headers, for one page or a whole list.Robots.txt TesterTest whether a URL is allowed or blocked by robots.txt for Googlebot and other crawlers.Sitemap URL ExtractorExtract every URL from a sitemap.xml or sitemap index, validate it and export to CSV or TXT.Cookie Consent Banner GeneratorDesign a cookie banner with accept, reject and settings buttons and copy the HTML/CSS/JS.FAQ Schema GeneratorTurn questions and answers into FAQPage JSON-LD markup with validation and a preview.Google Review Link GeneratorTurn a Google Place ID into a direct review link, QR code and printable review card.Keyword Suggestion ToolTurn one seed keyword into hundreds of Google and YouTube autocomplete keyword ideas.Local Business Schema GeneratorCreate LocalBusiness JSON-LD with address, geo, opening hours, holiday hours and sameAs links.Structured Data ValidatorCheck JSON-LD, microdata and RDFa against schema.org and Google rich result requirements.Terms and Conditions GeneratorCreate terms of service for a website, store, SaaS or app, with payments and governing law.Breadcrumb Schema GeneratorBuild BreadcrumbList JSON-LD plus the matching HTML breadcrumb from names and URLs.Cookie Policy GeneratorCreate a cookie policy with a ready cookie table for Google Analytics, AdSense, Meta Pixel and more.Disclaimer GeneratorWrite a website disclaimer for affiliate, health, finance or blog sites, with short post notices.Keyword CombinerCombine up to 4 keyword lists into every combination in broad, phrase and exact match.Long Tail Keyword GeneratorTurn a seed keyword into long-tail keywords with best, how to, near me, year and for X modifiers.On-Page SEO CheckerPaste page HTML and a keyword to audit title, meta, headings, alt text and keyword use.App Privacy Policy GeneratorCreate a privacy policy for your iOS or Android app from its SDKs, permissions and data.URL Tracking RemoverStrip utm, fbclid, gclid and other tracking parameters from one link or hundreds at once.YouTube Keyword ToolFind YouTube search suggestions for a video topic with A-Z expansion and copy them as tags.
    See all 28 SEO & Marketing Tools →