Toolsky

Free Online Tools & Utilities for Every Task.

CSP Header Generator

Create secure Content Security Policy (CSP) headers for your website.


Instructs browsers to treat all of a site's insecure URLs (those over HTTP) as though they have been replaced with secure URLs (those over HTTPS).

Prevents loading any assets using HTTP when the page is loaded over HTTPS.

Generated CSP Header

Add this to your Content-Security-Policy HTTP response header or a <meta> tag.

Most web attacks succeed by getting a browser to run something it should not — a script from a stranger's server, an injected form, a framed copy of your page. A Content Security Policy is your site answering back: an HTTP header that tells browsers exactly which sources are allowed to supply scripts, styles, images and more. A CSP header generator helps you compose that policy correctly, directive by directive, instead of handwriting a long, error-prone string.

What a Content Security Policy Is

The Content-Security-Policy header is a list of directives, each controlling one kind of resource. default-src sets the fallback for anything not specified. script-src governs where JavaScript may load from, style-src does the same for CSS, img-src for images, font-src for fonts, connect-src for the APIs and endpoints your page may talk to, and frame-ancestors decides which sites, if any, may embed your page in a frame. Each directive takes sources: 'self' for your own origin, specific domains you trust, data: for inline images, and keywords such as 'unsafe-inline' that loosen the rules for inline code.

Deployed well, CSP is the safety net beneath everything else: even if an attacker slips malicious markup into a page, a strict policy stops the browser from executing a script hosted on the attacker's server. The authoritative reference for the syntax and semantics is MDN's Content Security Policy guide.

What This Generator Produces

  • A ready-to-use header value — the complete policy string, assembled from your choices, ready to paste into a server configuration, framework middleware or hosting panel.
  • Directive-by-directive control — set sources for scripts, styles, images, fonts, connections and framing independently, rather than writing one blanket rule.
  • Sensible starting points — a restrictive baseline such as default-src 'self' that you relax only where your site genuinely needs an outside source.
  • Fewer syntax slips — semicolons, quoting of keywords and directive names are handled by the builder, removing the typos that silently disable hand-written policies.

Once your policy exists, the security headers auditor can check what your live site actually sends, and the HTTP headers parser helps you inspect headers from any response while you debug.

How to Build Your CSP Header

  1. List the external origins your site truly uses — analytics, fonts, CDNs, payment widgets, embedded media.
  2. Start from a restrictive default, typically default-src 'self'.
  3. Set each directive in the generator, adding only the sources your inventory justifies.
  4. Review the assembled policy string for sources you do not recognise or no longer use.
  5. Copy the header into your server or application configuration.
  6. Test the site thoroughly — every page type, form and checkout — watching the browser console for CSP violation reports.
  7. Tighten further over time, removing 'unsafe-inline' and broad sources as you refactor.

A Small Policy, Read Line by Line

Consider this modest policy for a typical brochure site: default-src 'self'; img-src 'self' data:; style-src 'self' fonts.googleapis.com; font-src fonts.gstatic.com; frame-ancestors 'none'. Read in English, it says: by default load only from our own origin; images may also come from inline data URIs; stylesheets may come from Google Fonts' CSS host and ours; font files from Google's font host; and no site at all may frame our pages. Every allowance exists because the site demonstrably needs it, and nothing else is permitted.

Teams use the generator at several moments in a site's life. During a security review, it turns an auditor's instruction — "add a CSP" — into a concrete header the same afternoon. When a developer adds a third-party widget, the policy must grow by exactly one source, and generating the new string beats editing the old one by hand in a wall of semicolons. Site owners responding to a scanner's warning use it to move from no policy to a sensible one without breaking their contact forms, then iterate. The pattern is always inventory, restrict, test.

Rolling Out a Policy Without Breaking Your Site

The biggest CSP risk is self-inflicted: a policy stricter than your site's reality will block legitimate scripts and styles, and the breakage can be subtle — a checkout button here, a map there. The standard defence is Content-Security-Policy-Report-Only, a twin header that reports violations without enforcing them. Run in report-only mode first, watch what your real pages request, adjust the policy until the reports go quiet, and only then switch to the enforcing header. Your own site's inventory will surprise you; third-party tags have a habit of loading fourth-party resources nobody wrote down.

Two more honest limitations. CSP does not fix the vulnerabilities it contains — it limits the damage of cross-site scripting rather than removing the bugs, so output encoding and patching still matter. And the 'unsafe-inline' keyword, often added in a hurry to stop breakage, quietly surrenders much of the protection for inline scripts; treat it as a temporary compromise with a plan to remove it via nonces or hashes, not as a destination.

Frequently Asked Questions About CSP Headers

Where do I put the Content-Security-Policy header?

In your server's response headers: web-server configuration (Apache, Nginx), application middleware in frameworks, or a hosting control panel's header settings. It must arrive as an HTTP header, not page content, for full effect.

What does default-src 'self' mean?

It sets the fallback rule: resources may load only from the same origin as the page itself, unless a more specific directive (such as img-src) overrides it for that resource type.

Will a CSP break my website?

It can, if the policy forbids something your pages actually use. Test with the Report-Only header first, review the violation reports, and adjust before enforcing the policy.

Does a CSP stop all cross-site scripting?

No. It is a mitigation layer that makes many XSS attacks fail in the browser, but it does not repair the underlying bugs. Secure coding and patching remain essential.

What is the difference between CSP and the Report-Only version?

The enforcing header blocks disallowed resources. Content-Security-Policy-Report-Only only logs and reports them, which makes it ideal for safe testing before you enforce.

Why does my policy need fonts.gstatic.com as well as googleapis.com?

Google Fonts splits its service: the stylesheet comes from fonts.googleapis.com (style-src), while the font files themselves are served from fonts.gstatic.com (font-src). Both directives need their matching host.

Can I allow a CDN for scripts only?

Yes. Add the CDN's origin to script-src while leaving other directives restrictive. CSP is deliberately granular so each resource type can have its own allowlist.

What does frame-ancestors 'none' protect against?

It prevents any site from embedding your pages in a frame or iframe, which blocks clickjacking — an attack that overlays your page to trick users into clicking disguised controls.

A Header Worth an Afternoon

Of all the hardening a website can get, a Content Security Policy offers an unusual bargain: one header, no code changes to your pages, and an entire class of attacks blunted. Generate a restrictive policy, test it honestly in report-only mode, and enforce it when the reports go quiet. Your future self, reading a clean security scan, will be glad you did.