Xerobit

Content Security Policy Generator

Use this content security policy generator to build a CSP header or meta tag by filling in the directives you need. It flags risky values like unsafe-inline, unsafe-eval and wildcard origins so you can tighten your policy before deploying. Everything runs locally in your browser.

Ask browsers to load http:// subresources over https://.

Result

Content-Security-Policy: default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'
  • Policy
    default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'
  • HTTP header
    Content-Security-Policy: default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'
  • Report-only header
    Content-Security-Policy-Report-Only: default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'
  • Meta tag
    <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; object-src 'none'; base-uri 'self'; form-action 'self'">
  • Check
    No known weaknesses found
  • Directives
    8
  • Note 1
    frame-ancestors is ignored by browsers in a meta tag, so it is left out of the meta tag below. Send it in the HTTP header instead.

How to use the csp generator

  1. Fill in the directives you need. Start with default-src and add more specific ones such as script-src and img-src.
  2. Separate sources with spaces: keywords such as 'self', schemes such as data: and hosts such as cdn.example.com. Leave a box empty to leave that directive out.
  3. Choose how to show the result: an HTTP header, a report-only header or a meta tag.
  4. Read the check row and the warnings, then copy the result into your server config or HTML head.

How a Content Security Policy works

A Content Security Policy is an HTTP header or HTML meta tag that tells the browser which origins are permitted to load resources. The syntax is a semicolon-separated list of directives, where each directive has a name followed by one or more source expressions. For example, default-src 'self'; script-src 'self' https://cdn.example.com; restricts all fetches to the same origin by default but allows scripts from a specific CDN.

The default-src directive acts as a fallback. If you do not specify img-src, the browser applies the default-src value to images. Directives like frame-ancestors have no fallback and must be declared explicitly if you want to control who can embed your page.

Common CSP directives reference

This table summarizes the most frequently used directives you can configure with this content security policy generator.

DirectiveControlsFallback
default-srcAll fetch directives not explicitly setNone (is the fallback)
script-srcJavaScript files and inline scriptsdefault-src
style-srcCSS files and inline stylesdefault-src
img-srcImages and faviconsdefault-src
font-srcWeb fontsdefault-src
connect-srcFetch, XHR, WebSocket, EventSourcedefault-src
frame-srcIframesdefault-src
object-srcPlugins (Flash, Java applets)default-src
base-uriAllowed URLs in <base> tagsNone
form-actionForm submission targetsNone
frame-ancestorsOrigins allowed to embed the pageNone

Header versus meta tag delivery

You can deliver a CSP via the Content-Security-Policy HTTP response header or through a <meta http-equiv="Content-Security-Policy"> tag in the HTML head. The HTTP header is preferred because it supports every directive, including frame-ancestors and reporting endpoints.

The meta tag approach is useful when you cannot modify server headers, such as on static hosting platforms. However, browsers ignore frame-ancestors, report-uri, report-to and sandbox when they appear in a meta tag. This content security policy generator outputs both formats so you can choose the appropriate delivery method.

Why unsafe-inline and unsafe-eval are dangerous

Adding 'unsafe-inline' to script-src or style-src permits inline event handlers, javascript: URIs and embedded script blocks. This defeats the primary purpose of CSP, which is to prevent injected code from executing. Instead of 'unsafe-inline', move scripts to external files or use nonces and hashes.

The 'unsafe-eval' keyword allows functions like eval(), setTimeout() with string arguments and new Function(). These patterns let attackers execute arbitrary strings as code. A strict policy omits both keywords entirely. When you add them using this tool, the warnings section highlights the risk so you can make an informed decision.

Deploying and testing your policy

Start with a Content-Security-Policy-Report-Only header to collect violation reports without blocking legitimate resources. Once you confirm the policy does not break your site, switch to the enforcing Content-Security-Policy header. You can verify your deployed headers using a security headers checker.

Keep your policy as tight as possible. Avoid the wildcard * in default-src because it allows resources from any origin. Specify exact domains rather than broad scheme sources like https:. Review your CSP periodically as third-party dependencies change, and consider pairing it with other protections like HSTS and X-Frame-Options.

Worked examples

ExampleInputResult
Strict starter policydefault-src: 'self'; script-src: 'self'; style-src: 'self'; img-src: 'self' data:; object-src: 'none'; base-uri: 'self'; form-action: 'self'; frame-ancestors: 'none'; upgrade-insecure-requests: no; Show as: headerPolicy: default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'; HTTP header: Content-Security-Policy: default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'; Report-only header: Content-Security-Policy-Report-Only: default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'; Meta tag: <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; object-src 'none'; base-uri 'self'; form-action 'self'">; Check: No known weaknesses found; Directives: 8; Note 1: frame-ancestors is ignored by browsers in a meta tag, so it is left out of the meta tag below. Send it in the HTTP header instead.
Allow a CDN and Google Fontsdefault-src: 'self'; script-src: 'self' https://cdn.jsdelivr.net; style-src: 'self' https://fonts.googleapis.com; img-src: 'self' data:; font-src: 'self' https://fonts.gstatic.com; object-src: 'none'; base-uri: 'self'; form-action: 'self'; frame-ancestors: 'none'; upgrade-insecure-requests: no; Show as: headerPolicy: default-src 'self'; script-src 'self' https://cdn.jsdelivr.net; style-src 'self' https://fonts.googleapis.com; img-src 'self' data:; font-src 'self' https://fonts.gstatic.com; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'; HTTP header: Content-Security-Policy: default-src 'self'; script-src 'self' https://cdn.jsdelivr.net; style-src 'self' https://fonts.googleapis.com; img-src 'self' data:; font-src 'self' https://fonts.gstatic.com; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'; Report-only header: Content-Security-Policy-Report-Only: default-src 'self'; script-src 'self' https://cdn.jsdelivr.net; style-src 'self' https://fonts.googleapis.com; img-src 'self' data:; font-src 'self' https://fonts.gstatic.com; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'; Meta tag: <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self' https://cdn.jsdelivr.net; style-src 'self' https://fonts.googleapis.com; img-src 'self' data:; font-src 'self' https://fonts.gstatic.com; object-src 'none'; base-uri 'self'; form-action 'self'">; Check: No known weaknesses found; Directives: 9; Note 1: frame-ancestors is ignored by browsers in a meta tag, so it is left out of the meta tag below. Send it in the HTTP header instead.
Meta tag for static hostingdefault-src: 'self'; script-src: 'self'; style-src: 'self'; img-src: 'self' data:; object-src: 'none'; base-uri: 'self'; form-action: 'self'; frame-ancestors: 'none'; upgrade-insecure-requests: no; Show as: metaPolicy: default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'; HTTP header: Content-Security-Policy: default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'; Report-only header: Content-Security-Policy-Report-Only: default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'; Meta tag: <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; object-src 'none'; base-uri 'self'; form-action 'self'">; Check: Gaps: see the warnings; Directives: 8; Warning 1: frame-ancestors is ignored by browsers in a meta tag, so it is left out of the meta tag below. Send it in the HTTP header instead.
Risky policy with warningsdefault-src: 'self'; script-src: 'self' 'unsafe-inline' 'unsafe-eval'; style-src: 'self'; img-src: *; object-src: 'none'; base-uri: 'self'; form-action: 'self'; frame-ancestors: 'none'; upgrade-insecure-requests: no; Show as: headerPolicy: default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self'; img-src *; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'; HTTP header: Content-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self'; img-src *; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'; Report-only header: Content-Security-Policy-Report-Only: default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self'; img-src *; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'; Meta tag: <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self'; img-src *; object-src 'none'; base-uri 'self'; form-action 'self'">; Check: Weak: see the warnings; Directives: 8; Warning 1: 'unsafe-inline' in the script policy allows inline scripts and event handlers, which removes most of the protection against cross-site scripting. Use nonces or hashes instead.; Warning 2: 'unsafe-eval' lets eval(), new Function() and string timers run, which gives injected text a way to become code.; Warning 3: img-src contains the wildcard *, which allows any http, https and (for some directives) other-scheme origin. List the exact hosts you use.; Note 1: frame-ancestors is ignored by browsers in a meta tag, so it is left out of the meta tag below. Send it in the HTTP header instead.

FAQ

What is a content security policy generator?

It is a tool that helps you construct a valid Content-Security-Policy string by selecting directives and entering allowed sources, then formatting the result as an HTTP header or HTML meta tag.

Should I use the HTTP header or the meta tag?

The HTTP header is preferred because it supports all directives, including frame-ancestors and reporting. Use the meta tag only when you cannot configure server headers.

What happens if I omit default-src?

Without default-src, any fetch directive you do not explicitly declare is unrestricted. Always include default-src as a secure baseline, typically set to 'self' or 'none'.

Can I use nonces or hashes with this tool?

Yes. Enter values like 'nonce-abc123' or 'sha256-...' directly into the source fields. Nonces and hashes let you allow specific inline scripts without using 'unsafe-inline'.

Does this tool upload my policy anywhere?

No. The content security policy generator runs entirely in your browser. Nothing you type is sent to a server.

Why is frame-ancestors missing from the meta tag output?

Browsers ignore frame-ancestors when delivered via a meta tag. It only works through the HTTP header, so the tool omits it from the meta tag version.