Visual CSP Builder
Tick the sources you want for default-src, script-src, style-src, img-src and more. Add your own hosts, and the tool fixes missing quotes and rejects invalid sources for you.
Build a Content Security Policy visually, review it for common mistakes and copy ready-to-use code. Fast, free and no signup.
Paste a policy or a full header line to load it into the builder and review it.
| Directive | Allowed sources |
|---|
Start from a preset, import your current policy, or build from scratch.
Tick the sources each directive should allow and add your own hosts.
Fix the warnings, then copy the code for your server or framework.
Looking for a free CSP header generator? Build your Content Security Policy with a simple visual editor, see a plain-English security review, and copy ready-to-use code for an HTTP header, a meta tag, Apache, Nginx, PHP, Node.js or Laravel. A Content Security Policy (CSP) tells the browser which scripts, styles, images, fonts, frames and connections your page may load, which is one of the best defenses against cross-site scripting (XSS) and data injection. Our CSP generator covers every common directive, includes presets for typical websites, can import and analyze an existing policy, and creates script hashes and test nonces for inline code. There is nothing to install and no account to create. Everything runs in your browser with no third-party service.
Tick the sources you want for default-src, script-src, style-src, img-src and more. Add your own hosts, and the tool fixes missing quotes and rejects invalid sources for you.
Get warnings about unsafe-inline, unsafe-eval, wildcard sources, a missing object-src or base-uri, and other common CSP mistakes, with clear advice on how to fix them.
Copy the finished policy as an HTTP header, a meta tag, or ready code for Apache, Nginx, PHP, Node.js and Laravel. Switch between Enforce and Report-Only with one click.
You can build a working CSP in a few minutes. Follow these simple steps.
'self' for your own domain, and add other hosts such as https://cdn.example.com in the extra sources box.'unsafe-inline'.Always test a new policy on a staging site first. A policy that is too strict can block scripts, styles or images that your pages need.
A Content Security Policy is a security standard that lets a website tell the browser which sources of content are trusted. The policy is delivered in the Content-Security-Policy HTTP response header, or in a meta tag. When the browser finds a script, image, stylesheet or frame that breaks the policy, it blocks it. This limits the damage if an attacker manages to inject malicious code into your pages, because the injected script is not allowed to run or to send data to an unknown server.
A CSP does not replace secure coding, but it adds a strong second layer of defense. It is recommended for any site that handles logins, payments or personal data.
A policy is a list of directives separated by semicolons. Each directive has a name followed by a list of allowed sources.
Content-Security-Policy: default-src 'self'; img-src 'self' data: https://images.example.com; script-src 'self' https://cdn.example.com; object-src 'none'
In this example, everything may load from your own origin by default, images may also come from data URIs and one image host, scripts may also come from one CDN, and plugins are blocked completely. Directives that you do not list fall back to default-src, where that fallback applies.
| Directive | What it controls | Example |
|---|---|---|
default-src | Fallback for most fetch directives that you do not set | default-src 'self' |
script-src | JavaScript files and inline scripts | script-src 'self' https://cdn.example.com |
style-src | Stylesheets and inline styles | style-src 'self' https://fonts.googleapis.com |
img-src | Images and favicons | img-src 'self' data: https: |
font-src | Web fonts | font-src 'self' https://fonts.gstatic.com |
connect-src | Fetch, XHR, WebSocket and other connections | connect-src 'self' https://api.example.com |
media-src | Audio and video files | media-src 'self' https://media.example.com |
object-src | Plugins such as object and embed | object-src 'none' |
frame-src | Pages you embed in iframes | frame-src https://www.youtube.com |
worker-src | Web workers and service workers | worker-src 'self' blob: |
manifest-src | Web app manifest files | manifest-src 'self' |
base-uri | Allowed values for the base tag | base-uri 'self' |
form-action | Where forms can be submitted | form-action 'self' |
frame-ancestors | Who may embed your page in a frame, which helps stop clickjacking | frame-ancestors 'none' |
upgrade-insecure-requests | Asks the browser to load http:// resources over https:// | upgrade-insecure-requests |
report-uri and report-to | Where violation reports are sent | report-uri /csp-report |
Note that base-uri, form-action and frame-ancestors do not fall back to default-src, so you must set them yourself.
| Source | Meaning |
|---|---|
'self' | Your own origin: same scheme, host and port |
'none' | Block everything for this directive |
'unsafe-inline' | Allow inline scripts or styles. This weakens XSS protection for scripts. |
'unsafe-eval' | Allow eval and similar functions that run text as code |
'strict-dynamic' | Trust scripts loaded by a script that already has a nonce or hash |
'nonce-abc123' | Allow only inline code that has a matching nonce attribute |
'sha256-...' | Allow only inline code with this exact hash |
https: | Any host over HTTPS. This is broad, so use it with care. |
data: and blob: | Data URIs and blob URLs, often needed for images and workers |
https://cdn.example.com | One specific host |
*.example.com | Any subdomain of example.com |
Keywords such as self and none must be written with single quotes. Without the quotes, the browser treats them as host names, which is one of the most common CSP mistakes. This generator adds the quotes for you.
These examples match the presets in the tool. Adjust them to the hosts your own site uses.
# Strict: only your own origin
default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; font-src 'self'; connect-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'; upgrade-insecure-requests
# Basic website
default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self' data:; connect-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'self'
# Embedding YouTube videos
default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https://i.ytimg.com; frame-src https://www.youtube.com https://www.youtube-nocookie.com; object-src 'none'; base-uri 'self'; frame-ancestors 'self'
The Google Analytics and Fonts preset adds the Google Tag Manager, Analytics and Fonts hosts. The standard Google tag snippet is an inline script, so you will also need a nonce or a hash for it. Check Google's current documentation, because the required hosts can change.
Inline scripts are the main reason a strict CSP breaks pages. You have three options.
'nonce-VALUE' and puts the same value in the script tag. Only scripts with that nonce can run. A nonce must be unpredictable and different on every page load.The nonce button in this tool creates a test value so you can try the policy. In production, generate a fresh nonce for each response on your server.
You can set a policy with a meta tag in the head of your page, which is handy when you cannot change server headers. However, the meta tag has limits. It does not support frame-ancestors, report-uri, report-to or sandbox, it cannot be used in Report-Only mode, and it only affects content that comes after the tag. Whenever possible, send the policy as an HTTP header.
The tool creates the code for each platform. Here is what the common ones look like, using a short sample policy.
# Apache (.htaccess or virtual host, needs mod_headers)
Header always set Content-Security-Policy "default-src 'self'; object-src 'none'"
# Nginx (server or location block)
add_header Content-Security-Policy "default-src 'self'; object-src 'none'" always;
// PHP
<?php
header("Content-Security-Policy: default-src 'self'; object-src 'none'");
// Node.js (Express)
res.setHeader("Content-Security-Policy", "default-src 'self'; object-src 'none'");
// Laravel (in a middleware's handle method)
$response->headers->set('Content-Security-Policy', "default-src 'self'; object-src 'none'");
In Nginx, add_header lines are inherited only if the current block has no add_header of its own, so repeat the header where needed, and use the always flag so it is also sent on error pages. After you change server configuration, reload the service and check the response headers in your browser's developer tools.
'self' and 'none' with quotes.object-src 'none' unless you really need plugins.No single header can secure a website. A CSP reduces the impact of XSS, but you still need to escape output, validate input, keep software updated, use HTTPS and set other security headers such as Strict-Transport-Security and X-Content-Type-Options. Treat CSP as one important layer in a defense-in-depth approach.
Web developers use it to add a policy to new projects. Security engineers use it to review and tighten existing policies. WordPress and e-commerce site owners use it to protect their stores, DevOps engineers use it to prepare server configuration, and students use it to learn how browsers enforce security rules.
Securing your site? Try these related tools: the HMAC Generator, the Hash Generator, the Base64 Encode / Decode tool and the JWT Decoder. You can also browse all of our free online tools.
Our free CSP header generator helps you build, review and export a Content Security Policy without signup or a watermark. Everything runs in your browser, so you can create a policy, test it in Report-Only mode and copy the code whenever you need it.
Find answers to common questions about creating a Content Security Policy, report-only mode and CSP headers.
A CSP header generator is a tool that helps you build a Content Security Policy without writing it by hand. You choose which sources are allowed for scripts, styles, images and other content, and the tool creates the header value and ready-to-use code for your server.
Pick a preset or start empty, tick the allowed sources for each directive, add any extra hosts, and review the warnings. Then choose your platform, such as Apache, Nginx, PHP, Node.js or Laravel, and copy the code. Start in Report-Only mode and test before you enforce it.
In enforce mode the browser blocks anything that breaks the policy. In Report-Only mode the browser only reports violations and blocks nothing, which lets you test a new policy safely. Switch to enforce mode when the reports show no important problems.
Allowing unsafe-inline in script-src lets every inline script run, including scripts an attacker injects, which removes much of the protection a CSP offers. Use a nonce, which the server generates for each response, or a hash of the exact inline script instead.
Yes, but with limits. A meta tag does not support frame-ancestors, report-uri, report-to or sandbox, it cannot be used for Report-Only mode, and it only applies to content after the tag. An HTTP header is the better choice whenever you can set one.
It can if the policy blocks resources your pages need, such as inline scripts, analytics, fonts or embedded videos. Avoid surprises by testing in Report-Only mode, checking the browser console for violations, and allowing only the sources you really use before you enforce the policy.
No. The policy is built and reviewed inside your browser, so it stays on your device and is not uploaded or stored by this tool.
Merge, split, compress and convert files in seconds. Explore powerful free tools for all your everyday needs.
Explore All Tools →