Content Security Policy (CSP) Generator
Understand every CSP directive, build secure policies, and deploy Content Security Policy headers that prevent XSS and data injection attacks.
What Is Content Security Policy?
Content Security Policy is an HTTP response header that provides a declarative allowlist of content sources for your web page. When a browser loads a page with a CSP header, it enforces the policy by blocking any resource that does not match the declared sources. CSP is the strongest browser-side defense against Cross-Site Scripting because it prevents injected scripts from executing even if an attacker successfully injects HTML into the page.
CSP was designed as a defense-in-depth layer. It does not replace input validation or output encoding — it catches the attacks that slip through those defenses. A well-configured CSP can neutralize stored XSS, reflected XSS, clickjacking, form hijacking, and malicious resource injection. Google's deployment of strict CSP across their properties eliminated entire classes of XSS bugs that had persisted for years.
CSP Directive Reference
default-src: The fallback directive for all resource types not explicitly configured. Setting default-src 'self' restricts all resources to the same origin by default. Every other directive overrides default-src for its specific resource type.
script-src: Controls which scripts can execute. This is the most critical directive for XSS prevention. Recommended: script-src 'nonce-{random}' 'strict-dynamic'. Avoid 'unsafe-inline' and 'unsafe-eval'.
style-src: Controls which stylesheets can be applied. style-src 'self' 'unsafe-inline' is common because inline styles are lower risk, but nonce-based style policies are more secure.
img-src: Controls which image sources are allowed. img-src 'self' data: https: allows same-origin images, data URIs, and any HTTPS image source.
font-src: Controls web font sources. font-src 'self' https://fonts.gstatic.com is typical for sites using Google Fonts.
connect-src: Controls which URLs the page can connect to via fetch, XMLHttpRequest, WebSocket, and EventSource. Critical for APIs: connect-src 'self' https://api.example.com.
frame-src: Controls which URLs can be loaded in iframes. frame-src 'none' prevents all framing. Use this to block clickjacking and rogue iframe injection.
object-src: Controls plugin content (Flash, Java applets). Always set object-src 'none' — plugins are a legacy attack vector.
base-uri: Restricts URLs for the <base> element. Set base-uri 'self' or base-uri 'none' to prevent base tag injection, which can redirect all relative URLs.
form-action: Controls where forms can submit. form-action 'self' prevents form hijacking where an attacker changes the form's action URL.
frame-ancestors: Controls which pages can embed this page in an iframe. Replaces the X-Frame-Options header. frame-ancestors 'none' prevents all framing (equivalent to X-Frame-Options: DENY).
worker-src: Controls sources for Worker, SharedWorker, and ServiceWorker scripts. Falls back to script-src if not set.
manifest-src: Controls which manifest files can be loaded. manifest-src 'self' restricts to same-origin manifests.
report-uri / report-to: Specifies where CSP violation reports are sent. Use report-uri /csp-report or the newer report-to directive with a Reporting API endpoint. Violation reports tell you when legitimate resources are blocked or when actual attacks are mitigated.
Common CSP Configurations
Strict policy (recommended):
Content-Security-Policy: default-src 'none'; script-src 'nonce-{random}' 'strict-dynamic'; style-src 'self' 'nonce-{random}'; img-src 'self' https:; font-src 'self'; connect-src 'self'; frame-ancestors 'none'; base-uri 'self'; form-action 'self'; object-src 'none';
Starter policy for existing sites:
Content-Security-Policy: default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self' https://fonts.gstatic.com; object-src 'none'; frame-ancestors 'self';
Report-only for testing:
Content-Security-Policy-Report-Only: default-src 'self'; script-src 'self'; report-uri /csp-report;
Nonce-Based CSP
Nonce-based CSP is the recommended approach for modern applications. Generate a cryptographically random nonce (at least 128 bits of entropy) for each HTTP response. Add this nonce to the CSP header and to every legitimate script tag. The browser will only execute scripts that carry the matching nonce, blocking all injected scripts.
<!-- Server generates: nonce="a1b2c3d4e5f6" -->
<script nonce="a1b2c3d4e5f6" src="/app.js"></script>
<!-- CSP header includes the same nonce -->
Content-Security-Policy: script-src 'nonce-a1b2c3d4e5f6' 'strict-dynamic';
With 'strict-dynamic', scripts loaded dynamically by nonced scripts are also trusted. This means you only need to add the nonce to your initial script tags — everything they import or create inherits trust.
CSP Violation Reporting
Set up a reporting endpoint to monitor CSP violations in production. Each violation report includes the blocked URI, the violated directive, the document URI, and a sample of the blocked content. Services like report-uri.com, Sentry, and custom logging endpoints can aggregate these reports. Monitor for patterns: a spike in violations from a specific directive may indicate an active XSS attack being mitigated by your policy.