Content-Security-Policy (CSP) Generator — Build a CSP Header Online
Build a Content-Security-Policy header by picking directives and sources. Generate a ready-to-paste CSP for script-src, style-src, img-src, connect-src and more — with sensible strict defaults. 100% local.
Last updated:
CommentsPick directives (script-src, style-src, img-src…) and sources to build a Content-Security-Policy header you can paste straight into your server config. Starts from strict defaults.
Content-Security-Policy: default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; frame-ancestors 'none'; object-src 'none'; base-uri 'self'Tip: deploy as Content-Security-Policy-Report-Only first, review the violation reports, then switch to the enforcing header above.
What is CSP Generator?
A Content-Security-Policy (CSP) is an HTTP response header that whitelists where a page may load scripts, styles, images, fonts, frames, and connections from. It's one of the most effective defenses against cross-site scripting (XSS): even if an attacker injects a <script>, the browser refuses to run it unless its origin is on the allow-list. This generator lets you assemble a policy directive by directive — set a restrictive default-src 'self', then selectively allow the CDNs and APIs you actually use — and copy the finished header. The recommended rollout is to ship it first as Content-Security-Policy-Report-Only, review the violation reports, then switch to the enforcing header. Avoid 'unsafe-inline' and 'unsafe-eval', which re-open the exact hole CSP is meant to close; prefer nonces or hashes for the rare inline script you can't remove.
How to generate a CSP header
- 1Start from the strict default-src 'self' base.
- 2Add the directives you need (script-src, style-src, img-src, connect-src, font-src, frame-src) and list each allowed source.
- 3Copy the generated Content-Security-Policy value.
- 4Deploy it as Content-Security-Policy-Report-Only first, review violations, then enforce.
Key Concepts
- default-src
- The fallback source list applied to any resource type you don't set explicitly. Start with 'self' and open up from there.
- nonce / hash
- A per-response random token (nonce) or content hash that lets a specific inline script run without resorting to 'unsafe-inline'.
- report-only mode
- The Content-Security-Policy-Report-Only header logs violations without blocking, so you can tune a policy safely before enforcing it.
Tips & Best Practices
- ▸Set the real header on the server — a <meta> CSP tag can't use several directives (like frame-ancestors or report-uri).
- ▸frame-ancestors replaces the old X-Frame-Options for clickjacking protection.
- ▸Keep the policy as tight as it can be while the site still works — every extra source weakens XSS protection.
Frequently Asked Questions
What is a Content-Security-Policy?
CSP is a response header that tells the browser which sources of scripts, styles, images, fonts, and other resources are allowed to load. A good CSP is one of the strongest defenses against cross-site scripting (XSS), because an injected <script> from an untrusted origin simply won't execute.
Should I start with report-only mode?
Yes. Deploy the policy as Content-Security-Policy-Report-Only first so violations are logged but nothing is blocked. Watch the reports for a few days, fix legitimate sources, then switch to the enforcing Content-Security-Policy header.
Why avoid 'unsafe-inline' and 'unsafe-eval'?
Both re-open the XSS hole CSP is meant to close: 'unsafe-inline' allows inline <script> and event handlers, and 'unsafe-eval' allows eval()/new Function(). Prefer nonces or hashes for the few inline scripts you truly need.
Try Next
Meta Tag Preview
Preview how your page will appear in Google search results and social share cards on Facebook, Twitter/X, and LinkedIn. Parses <title>, meta description, canonical, and full Open Graph / Twitter Card tags with diagnostics. 100% local.
Related Tools
HTTP Status Codes
Complete reference of HTTP status codes with meanings, use cases, and RFC citations. Search 60+ codes from 100 Continue to 511 Network Authentication Required.
cURL Converter
Convert cURL commands to Python (requests), JavaScript (fetch), Node.js, Go (net/http), PHP, or Java (HttpClient). Parses -H, -d, --data-urlencode, -F, -u, --json and more. 100% local.
URL Parser
Parse any URL into scheme, host, port, path, and query parameters — or build a URL from scratch with a form. Great for debugging redirect chains, tracking pixels, and OAuth flows. 100% local.
CIDR Calculator
Calculate IPv4 subnets from CIDR notation online. Get network address, broadcast, netmask, wildcard, usable host range, IP class, and binary representation. 100% local — nothing leaves your browser.
Color Converter
Convert colors between HEX, RGB, HSL, and HSV instantly with a live color preview and copy-ready CSS strings. Free, no signup, runs entirely in your browser.
User Agent Parser
Parse User-Agent strings to identify browser, engine, operating system, and device. Instantly see what your own browser sends.