Skip to main content
AllDevToolsHub
๐Ÿ›ก๏ธ

CSP Builder

100% Local

Generate secure Content Security Policy (CSP) headers.

CSP Builder

default-src

Fallback for other fetch directives.

'self'
Quick Add:

script-src

Valid sources for JavaScript.

'self'

style-src

Valid sources for stylesheets.

'self''unsafe-inline'

img-src

Valid sources of images and favicons.

'self'data:

connect-src

Valid sources for fetch, XMLHttpRequest, WebSocket, and EventSource.

font-src

Valid sources for fonts loaded using @font-face.

object-src

Valid sources for the <object>, <embed>, and <applet> elements.

media-src

Valid sources for audio and video.

frame-src

Valid sources for nested browsing contexts (e.g. <iframe>).

worker-src

Valid sources for Worker, SharedWorker, or ServiceWorker scripts.

Deployment Examples

Try:
This tool runs entirely in your browser. Your input is never uploaded, logged, or sent to AllDevToolsHub or anyone else, and it keeps working offline once the page has loaded.

Toggle directives for script-src, style-src, img-src, and more. Copy the assembled header when done.

Overview

What is CSP Builder?

Visually build Content Security Policy headers to block XSS and injection attacks. Configure default-src, script-src, style-src directives and copy output.
FAQ

Frequently Asked Questions

Reference

Technical Deep Dive

CSP Builder

Visually build Content Security Policy headers to protect against Cross-Site Scripting (XSS) and data injection attacks. Configure directives like default-src, script-src, and style-src, and easily copy the generated policy for Nginx, Apache, or HTML Meta tags.

A CSP you copy from a gist usually breaks your own scripts. This builder emits a header you can paste into Nginx or next.config.ts.

Start with default-src 'self'; script-src 'self' and add one origin at a time. Google Tag Manager needs extra script-src hosts; do not jump to 'unsafe-inline'.

Report-Only mode is how you discover breaks without locking users out. Ship enforcing CSP only after the report endpoint is quiet.

01 CSP Directive Threat Matrix

Directive Protects Against Recommended Value Impact
default-srcFallback Logic'self'High
script-srcInline XSS / Injection'nonce-...' 'strict-dynamic'Critical
connect-srcData ExfiltrationSpecific API OriginsHigh
frame-ancestorsClickjacking'none' or 'self'Medium

02 Hardening Workflow Pipeline

1
Baseline Definition Start with a restrictive default-src 'none' and incrementally allowlist trusted assets (fonts, images, scripts).
2
Report-Only Validation Deploy using the Report-Only header to audit policy violations without breaking existing production functionality.
3
Enforcement Promotion Transition to the enforcing Content-Security-Policy header once violation reports are silenced.

03 The nonce lifecycle

A nonce-based CSP only works if the nonce is genuinely fresh per response. The server generates a random value (16+ bytes, base64), puts it in the header as script-src 'nonce-r4nd0m', and stamps the same value onto every first-party <script nonce="r4nd0m"> tag it renders. The browser runs a script only if its nonce attribute matches the header, then strips the attribute from the DOM so injected markup cannot read it back.

Two things break this. Caching a full HTML response re-serves a stale nonce, so either mark nonce'd pages Cache-Control: no-store or use per-request edge rendering. And static-site generators cannot emit a runtime nonce at all โ€” for those, use hashes ('sha256-โ€ฆ' of each inline block) instead, which the browser recomputes and matches on every load with no server involvement.

04 Directives people forget

base-uri 'self' โ€” without it, an injected <base href> tag can silently repoint every relative script URL on the page at an attacker's host, defeating a script-src allowlist entirely. It costs nothing to set.

object-src 'none' โ€” blocks legacy <object>, <embed>, and Flash-era plugin vectors that default-src does not always cover cleanly.

form-action โ€” restricts where forms can POST, which stops an injected form from exfiltrating tokens to a third-party endpoint even if the markup renders.

frame-ancestors โ€” the modern replacement for X-Frame-Options; it supports multiple origins and is the one browsers actually consult. Set it even if you already send the old header.

upgrade-insecure-requests โ€” rewrites http:// subresource URLs to https:// at request time, useful while migrating a site with mixed-content debt.

05 Reading a violation report

In Report-Only mode the browser POSTs a JSON body to your report-to endpoint for every block. The fields that matter are effective-directive (which rule fired), blocked-uri (what it tried to load), and source-file plus line-number (where the offending code lives). Group incoming reports by blocked-uri: a handful of legitimate third-party hosts you forgot to allowlist will dominate, and a long tail of eval, inline, and browser-extension noise sits underneath.

Allowlist the real hosts, convert your own inline scripts to nonce'd or external files, and ignore the extension noise (you cannot control what a user's ad-blocker injects). When the report volume from real users drops to that irreducible noise floor, promote the header from Content-Security-Policy-Report-Only to Content-Security-Policy.

You Might Also Need