CSP Generator
Build a Content-Security-Policy header for your site's resources.
Content-Security-Policy (CSP) is a security header that tells the browser which origins may serve scripts, styles, images, fonts and network connections. A well-tuned CSP dramatically reduces the impact of XSS (cross-site scripting), data exfiltration and clickjacking. This CSP generator lets you toggle allowed sources per directive such as default-src and script-src, add custom domains, and assembles the matching header string in real time.
You apply the result via the Content-Security-Policy response header or a <meta>tag. A good rollout is to ship it first in report-only mode (Content-Security-Policy-Report-Only), collect violation reports, refine the policy, and only then enforce it. Everything runs entirely in your browser and nothing you enter is ever sent anywhere.
Enable the directives you want, pick source keywords, or enter custom domains.
default-src 'self'
<meta http-equiv="Content-Security-Policy" content="default-src 'self'">
Understanding the key directives
CSP grants allowed origins per resource type. The most important starting point is default-src, which acts as the fallback policy for any resource type without its own directive.
- default-src: fallback applied when a more specific
*-srcis absent. - script-src: where JavaScript may be loaded and executed. The most security-sensitive one.
- style-src: origins for stylesheets and inline styles.
- img-src: origins images may be loaded from.
- font-src: origins for web fonts.
- connect-src: origins for fetch, XHR, WebSocket and EventSource connections.
- frame-src: origins allowed to be embedded as iframes.
Source keywords and the danger of unsafe-inline
Each directive accepts a mix of keywords and domains. 'self' means the same origin (scheme + host + port); 'none' blocks every origin. data: allows inline data URIs and https: allows any HTTPS origin.
- 'unsafe-inline' permits inline
<script>code, inline handlers likeonclick, and inline<style>. Enabling it onscript-srceffectively cancels CSP's XSS protection, so avoid it and use nonces or hashes instead. - Overly broad sources like https: or a wildcard (
*) can allow untrusted third-party resources. Narrow them to the specific CDN and API domains you actually use. - 'none' is only meaningful on its own. Combined with other sources it has no effect.
Applying it and rolling out gradually
- Deploy first with the
Content-Security-Policy-Report-Onlyheader to collect violations. - Review the reports and add the origins your site legitimately needs to each directive.
- Once violations stop, switch to the enforcing
Content-Security-Policyheader.
For the strongest XSS protection, drop 'unsafe-inline' from script-src and rely on a nonce or hash based approach. Once deployed, use the security headers checker to confirm the CSP is actually being sent in your responses.