Visual CSP Header Builder & Auditor

Build, validate, and export Content Security Policy headers visually. Live security grading, nonce architecture, bypass simulation, and Nginx/Apache/meta export.

100% Client-Side Private Engine: All CSP generation, grading, and validation run strictly in your browser RAM. Your security configurations are never uploaded to any server.
Security Grade
C
Basic Protection
1 Warning Found
Policy Presets
Report-Only Mode
Enforce Mode
Content-Security-Policy
Directives
Directive Configuration: default-src
Serves as a fallback for the other fetch directives.
Standard Keywords
Dangerous / Legacy Sources
Custom Hosts & Hashes
Live Output
Nginx
Apache
Meta Tag
Nginx nginx.conf or site.conf
SHA-256 Hash Calculator

Paste your inline <script> or <style> content below (without the tags). Get the exact 'sha256-...' value to add to your policy — no 'unsafe-inline' required.

Attack Surface Analyzer

Based on your current policy, here are the attack vectors that remain open or are blocked:

Quick Answer

What is a Content Security Policy (CSP)?

A Content Security Policy (CSP) is an added layer of HTTP header security that helps detect and mitigate Cross-Site Scripting (XSS) and data injection attacks. It works by strictly whitelisting the approved domains and origins that a browser is permitted to load resources from.

1Understanding CSP Architecture

Beyond the basic definition, CSP provides a standard method for website owners to declare a rigorous defense-in-depth security posture. By specifying a whitelist of permitted domains, a CSP essentially neutralizes XSS payloads, even if a vulnerability exists in the underlying application code.

Key Benefit: CSP shifts the security posture from a reliance on flawless input validation (which is notoriously difficult) to a robust browser-enforced perimeter that actively blocks malicious script execution.

2How to Use the Visual CSP Builder

Writing CSP headers manually is prone to syntax errors that can completely break modern web applications. The Visual CSP Header Builder solves this by providing a deterministic, GUI-driven construction environment.

Step Action Description
1. Select Preset Choose a Baseline Start with a Strict, Balanced, or Dev preset to establish the foundational policy footprint.
2. Configure Directives Toggle Core Sources Navigate through default-src, script-src, and style-src in the left sidebar to explicitly whitelist your CDNs and APIs.
3. Inject Custom Hosts Add Third-Parties Use the Custom Hosts input to authorize specific domains (e.g., https://api.stripe.com) or cryptographic hashes (e.g., 'sha256-...').
4. Live Validation Check the Grade Monitor the real-time security grading engine. If you accidentally enable 'unsafe-inline', the engine will immediately flag the critical vulnerability.

3Anatomy of CSP Directives

A CSP is composed of one or more directives, separated by semicolons. Each directive governs a specific type of resource fetching or execution context within the browser document.

fetch
Resource loading controls
document
DOM property controls
navigation
Form & Frame controls

The most critical fetch directive is script-src, which restricts where JavaScript can be loaded from and how it can be executed. Other vital directives include style-src (CSS), img-src (images), and connect-src (XHR/Fetch/WebSockets).

4Default-Src vs. Specific Directives

The default-src directive serves as a fallback for the majority of fetch directives. If a specific directive (like font-src) is completely omitted from the policy, the browser will enforce the rules defined in default-src for that resource type.

Crucial exception: default-src does not act as a fallback for all directives. Directives like frame-ancestors, report-uri, and sandbox are entirely unaffected by default-src and will default to allowing everything if omitted.

Best Practice: Always set default-src 'none'; as the foundation of your policy, and then explicitly open up specific directives as needed. This "default deny" approach is the cornerstone of secure CSP design.

5The Dangers of 'unsafe-inline' & 'unsafe-eval'

Many developers, frustrated by CSP breaking their legacy applications, resort to adding 'unsafe-inline' to their script-src. This is a critical security failure.

'unsafe-inline' allows the execution of inline <script> blocks and inline event handlers (like onclick). Since almost all XSS vulnerabilities rely on injecting inline scripts, authorizing 'unsafe-inline' effectively neutralizes the primary protection CSP offers.

Similarly, 'unsafe-eval' permits the use of eval() and related string-to-code APIs. While less dangerous than inline scripts, it still provides an execution vector for attackers who can manipulate string inputs.

6Implementing Nonce-Based Strict CSP

To secure inline scripts without using 'unsafe-inline', the modern web relies on Nonces (Number Used Once). A nonce is a cryptographically strong, base64-encoded random string generated dynamically on the server for every single page load.

The server injects this nonce into the CSP header:
Content-Security-Policy: script-src 'nonce-rAnd0m123' 'strict-dynamic';

And matches it on the authorized inline scripts:
<script nonce="rAnd0m123">...</script>

Because an attacker injecting a malicious script cannot predict the randomly generated nonce for that specific HTTP response, the browser will refuse to execute the injected payload.

7Hash-Based CSP for Static SPAs

Single Page Applications (SPAs) deployed via static CDNs (like Vercel or Netlify) often cannot generate dynamic nonces on a per-request basis. In these scenarios, Hashes provide the alternative.

By computing the SHA-256 (or SHA-384/512) hash of the exact inline script content, developers can authorize that specific code block within the header:

Content-Security-Policy: script-src 'sha256-B2yPHKaXnvFWtRChIbabYmUBFZdVfKKXHbWtWidDVF8=';

If an attacker injects a script, its hash will inevitably differ, and the browser will block it. This ensures pristine integrity for statically compiled frontend assets.

8CSP Reporting: report-uri vs report-to

Deploying a strict CSP on a legacy application can cause massive breakage. To mitigate this, CSP offers a Report-Only mode via the Content-Security-Policy-Report-Only header. In this mode, the browser will not block resources; it will only simulate the blocks and send JSON violation reports to a specified endpoint.

Directive Status Description
report-uri Deprecated The legacy method of specifying an endpoint URL. Still widely supported by older browsers.
report-to Modern The modern Reporting API. Requires a separate Report-To HTTP header to define the endpoint group. Provides out-of-band, batched telemetry.

9Defeating Clickjacking with frame-ancestors

Clickjacking occurs when an attacker embeds your site within a transparent <iframe> on a malicious domain, tricking users into clicking buttons they didn't intend to. The legacy defense was the X-Frame-Options header.

The CSP frame-ancestors directive supersedes X-Frame-Options. It allows you to explicitly declare exactly which parent origins are allowed to embed your application.

Content-Security-Policy: frame-ancestors 'self' https://partner.com;

If you do not want your site embedded anywhere, ever, use frame-ancestors 'none';.

10HTTP Header vs. HTML Meta Tag CSP

While CSP is primarily delivered via HTTP response headers, it can also be delivered directly within the HTML document using a `` tag. This is useful for static sites where developers lack access to the web server configuration.

<meta http-equiv="Content-Security-Policy" content="default-src 'self';">

Limitations: Meta tag CSPs cannot use reporting directives (report-uri, report-to), framing directives (frame-ancestors), or the sandbox directive. For complete security, HTTP headers are strictly required.

11Technical Glossary

Source List

The space-separated string of authorized domains, keywords (like 'self'), or hashes that follow a directive.

'strict-dynamic'

A keyword that trusts scripts loaded by an already-trusted (nonce-authorized) script, simplifying dependency management.

XSS (Cross-Site Scripting)

A vulnerability where an attacker injects executable code into a web page viewed by other users.

Mixed Content

Occurs when an initial HTML document is loaded over a secure HTTPS connection, but other resources are loaded over insecure HTTP.

Data URI (data:)

A scheme that allows creators to embed small files inline in documents, commonly used for base64 encoded images.

CSP Evaluator

Tools (like this one) that parse and syntactically validate the security posture of a generated policy string.

12CSP for WordPress: A Complete Guide

WordPress is the most widely targeted CMS for XSS attacks due to its plugin ecosystem. A correctly configured CSP is the single most impactful server-hardening step you can take for any WordPress site.

⚠️ The Core WordPress Challenge: Most WordPress themes and plugins inject inline scripts, load from CDNs, and use eval() via jQuery — making a strict CSP extremely difficult. The correct approach is a nonce-based CSP injected via functions.php.

The best practice is to generate a per-request nonce in functions.php and use the wp_script_attributes filter to inject it into all enqueued scripts automatically:

add_filter('wp_script_attributes', function($attr) {
  if (isset($attr['id'])) $attr['nonce'] = get_nonce_for_request();
  return $attr;
});

Then inject the CSP header via the send_headers action hook, including your generated nonce:

add_action('send_headers', function() {
  $nonce = get_nonce_for_request();
  header("Content-Security-Policy: default-src 'self'; script-src 'self' 'nonce-{$nonce}' 'strict-dynamic'; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com; img-src 'self' data: https:; object-src 'none'; frame-ancestors 'self';");
});
Common WordPress PluginRequired CSP ExceptionRisk Level
Google Analytics (GA4)script-src https://www.googletagmanager.com; connect-src https://www.google-analytics.comLow
Google reCAPTCHAscript-src https://www.google.com https://www.gstatic.com; frame-src https://www.google.comLow
Stripe Paymentsscript-src https://js.stripe.com; frame-src https://js.stripe.comLow
Elementor Page Builderstyle-src 'unsafe-inline' (unavoidable without patching)Medium
WooCommerce + Inline JSNonce-based approach required; can't avoid inline scriptsMedium

13CSP for Next.js, React & SPAs

Single Page Applications built with Next.js or Create React App inject significant amounts of inline script, including runtime chunk bootstrapping. The recommended approach differs significantly depending on whether you use the Next.js App Router or the older Pages Router.

ApproachMechanismBest For
App Router (Next.js 13+)Use next.config.js headers() with a dynamic nonce via middleware.tsSSR / SSG hybrid apps
Pages RouterOverride _document.tsx to inject nonce into <Head> and pass via getInitialPropsLegacy Next.js apps
Static Export (next export)Use SHA-256 hashes for all inline scripts. Generate hashes at build time.Static CDN deployments
Vite / CRAUse the vite-plugin-csp or manual hash generation as a post-build stepSPA without SSR
Next.js App Router examplemiddleware.ts:

import { NextResponse } from 'next/server';
export function middleware(request) {
  const nonce = Buffer.from(crypto.randomUUID()).toString('base64');
  const csp = `default-src 'self'; script-src 'self' 'nonce-${nonce}' 'strict-dynamic'; style-src 'self' 'nonce-${nonce}'; object-src 'none';`;
  const response = NextResponse.next();
  response.headers.set('Content-Security-Policy', csp);
  return response;
}

14Common CSP Bypass Techniques & Mistakes

Even a well-intentioned CSP can be completely bypassed if common mistakes are made. The following techniques are used by attackers to circumvent poorly configured policies — and are exactly what the Attack Surface Analyzer tool above detects.

Bypass TechniqueTrigger ConditionRemediation
JSONP Endpoint AbuseAny whitelisted domain that hosts a JSONP endpoint (e.g., googleapis.com) can be used to inject arbitrary JSPrefer nonce/hash over domain whitelisting; use 'strict-dynamic'
Angular Template InjectionWhitelisting https://ajax.googleapis.com to load AngularJS 1.x enables CSP bypass via template injectionNever whitelist known JSONP/Angular CDN hosts; load from self
Open Redirect AbuseA whitelisted domain that has an open redirect can be used to pivot to attacker domainsAudit all whitelisted domains for open redirect vulnerabilities
Missing base-uriWithout base-uri 'self', an attacker can inject <base href="..."> to hijack relative URL loadingAlways add base-uri 'self'; or base-uri 'none';
Missing object-srcBrowsers will allow Flash and plugin content, which can execute arbitrary codeAlways include object-src 'none';
data: in script-srcAllows execution of <script src="data:text/javascript,alert(1)">Never add data: to script-src

15Complete CSP Directive Reference (All 25)

The full CSP Level 3 specification defines 25 distinct directives across four categories. This is the most comprehensive reference for developers needing to understand every possible configuration option.

DirectiveCategoryPurposeFallback To
default-srcFetchFallback for all fetch directives not explicitly specified
script-srcFetchControls JavaScript sources and executiondefault-src
script-src-elemFetchControls <script> elements specificallyscript-src
script-src-attrFetchControls inline event handlers like onclickscript-src
style-srcFetchControls CSS sourcesdefault-src
style-src-elemFetchControls <style> and <link rel=stylesheet>style-src
style-src-attrFetchControls inline style attributesstyle-src
img-srcFetchControls image and favicon sourcesdefault-src
font-srcFetchControls font sources via @font-facedefault-src
connect-srcFetchRestricts fetch, XHR, WebSocket, and EventSource URLsdefault-src
media-srcFetchControls audio and video sourcesdefault-src
object-srcFetchControls Flash and plugin contentdefault-src
frame-srcFetchControls valid sources for <iframe>child-src
child-srcFetchControls workers and embedded framesdefault-src
worker-srcFetchControls Worker/SharedWorker/ServiceWorker scriptschild-src
manifest-srcFetchControls Web App Manifest sourcesdefault-src
prefetch-srcFetchControls prefetch and prerender targetsdefault-src
frame-ancestorsNavigationRestricts which parents can embed this page (replaces X-Frame-Options)
form-actionNavigationRestricts form submission target URLs
navigate-toNavigationRestricts URLs that the document may navigate to
base-uriDocumentRestricts <base> element URLs
sandboxDocumentApplies restrictions similar to iframe sandbox attribute
upgrade-insecure-requestsOtherRewrites HTTP URLs to HTTPS before fetching
block-all-mixed-contentOtherBlocks all HTTP resources on HTTPS pages (deprecated)
report-to / report-uriReportingSpecifies endpoint for violation reports

16Nginx & Apache: Production Configuration Patterns

The correct syntax for deploying a CSP header differs between Nginx and Apache. Both must be configured at the server or virtual host level for the header to apply to all responses, including static assets.

ServerConfig FileSyntax
Nginxnginx.conf or /etc/nginx/sites-available/*.confadd_header Content-Security-Policy "default-src 'self';" always;
Apache 2.4+.htaccess or httpd.confHeader always set Content-Security-Policy "default-src 'self';"
Apache (mod_header)Requires mod_headers enableda2enmod headers; systemctl restart apache2
Cloudflare WorkersWorker scriptresponse.headers.set('Content-Security-Policy', '...')
Vercelvercel.json{ "headers": [{ "key": "Content-Security-Policy", "value": "..." }] }
Critical Nginx Tip: Always use the always parameter in add_header directives. Without it, the CSP header will not be sent on error responses (4xx, 5xx), leaving those pages unprotected.
Report-Only mode for production rollout:
add_header Content-Security-Policy-Report-Only "default-src 'self'; report-uri /csp-violations" always;

Deploy this first for 1–2 weeks, monitor the violation reports, then switch to enforcing mode once all issues are resolved.

17CSP Browser Support Matrix (2026)

CSP Level 3 is supported by all modern browsers as of 2026. However, some newer directives and keywords have varying support levels that developers must account for, especially when supporting older enterprise environments running Internet Explorer 11.

FeatureChromeFirefoxSafariEdgeIE 11
CSP Level 1 (basic)✓ Full✓ Full✓ Full✓ Full✓ Partial
CSP Level 2 (nonce, hash)✓ v40+✓ v35+✓ v10+✓ v15+✗ None
'strict-dynamic'✓ v52+✓ v52+✓ v15.4+✓ v79+✗ None
frame-ancestors✓ v40+✓ v33+✓ v10+✓ v15+✗ None
script-src-elem✓ v75+✓ v105+✓ v16+✓ v79+✗ None
report-to (new API)✓ v70+⚠ Partial⚠ Partial✓ v79+✗ None
'wasm-unsafe-eval'✓ v97+✓ v102+✓ v16+✓ v97+✗ None

IE 11 Compatibility: If you must support IE 11, include both X-Content-Security-Policy (Firefox 3.x/IE10 prefixed header) alongside the standard CSP header. However, IE 11 will only enforce a very limited subset.

18CSP vs. Other HTTP Security Headers

CSP is a powerful tool but works best as part of a complete HTTP security header stack. Here is how CSP relates to and complements (or supersedes) other security headers:

HeaderProtects AgainstRelationship to CSPStill Needed?
X-Frame-OptionsClickjackingSuperseded by CSP frame-ancestors; CSP is more flexibleLegacy fallback only
X-Content-Type-Options: nosniffMIME-sniffing attacksComplementary — prevents browser from misinterpreting file typesYes, always add
Strict-Transport-Security (HSTS)SSL stripping, man-in-the-middleComplementary — CSP's upgrade-insecure-requests is related but not a substituteYes, always add
Permissions-PolicyFeature abuse (camera, mic, geolocation)Complementary — controls browser API access, not resource loadingYes, recommended
Referrer-PolicyInformation leakage via Referer headerComplementary — controls what URL info is shared with third partiesYes, recommended
X-XSS-ProtectionReflected XSS (IE/Chrome legacy)Deprecated — CSP is the modern replacement and is far more comprehensiveNo — disable it
Gold Standard Security Header Stack: Deploy all of the following on every response: Content-Security-Policy, Strict-Transport-Security, X-Content-Type-Options, Referrer-Policy, and Permissions-Policy. This combination achieves an A+ grade on security header scanners like securityheaders.com.

FAQFrequently Asked Questions

What is a Content Security Policy (CSP)?

A Content Security Policy (CSP) is an added layer of HTTP security that helps detect and mitigate certain types of attacks, primarily Cross-Site Scripting (XSS) and data injection attacks.

It works by allowing server administrators to explicitly declare approved sources of content (scripts, images, stylesheets) that the browser is permitted to load. If an attacker injects a malicious script into your page, the browser will refuse to execute it because it did not originate from a whitelisted source in your CSP header.

What is the difference between default-src and specific directives like script-src?

default-src serves as a fallback for the majority of fetch directives. If a specific directive like script-src or img-src is omitted, the browser applies the rules defined in default-src.

Best practice dictates setting a highly restrictive default-src 'none' or default-src 'self', and then explicitly opening up permissions using specific directives (like script-src for JS, style-src for CSS, and connect-src for APIs). This ensures a "deny-by-default" security posture.

What are CSP Nonces and how do they work?

A nonce (Number Used Once) is a randomly generated, unguessable base64 string created by your server for every single page request. You include it in your CSP header: script-src 'nonce-xyz123', and attach it to your inline scripts: <script nonce="xyz123">.

This is the most robust defense against XSS. Even if an attacker injects a script tag into your HTML, they cannot guess the unique nonce for that page load, and the browser will block their malicious script from executing.

What is strict-dynamic and why is it recommended by Google?

The 'strict-dynamic' keyword (introduced in CSP Level 3) revolutionizes script management. It dictates that if a script is trusted (via a nonce or a hash), any subsequent scripts dynamically generated and appended to the DOM by that trusted script are also automatically trusted.

This solves the nightmare of managing CSP whitelists for massive widgets like Google Analytics or YouTube embeds, which dynamically load dozens of secondary scripts from various domains. It makes maintaining a secure CSP infinitely easier.

Why did my CSP break my website's inline styles and scripts?

By design, a strict CSP disables the execution of inline JavaScript (e.g., <script>console.log()</script> or onclick="...") and inline CSS (e.g., style="color:red;") to prevent injection attacks.

To fix this, you have three options:

  1. Refactor your code to move all inline logic into external files (Best practice).
  2. Use cryptographic nonces or SHA hashes for specific inline blocks (Secure).
  3. Add 'unsafe-inline' to your policy (Insecure, defeats the purpose of CSP against XSS).
Why is my eval() code throwing a CSP violation?

The eval() function, along with related mechanisms like setTimeout(String), allows dynamic string-to-code execution, which is a massive security risk. CSP blocks this behavior by default.

If a legacy library requires it, you can enable it by adding 'unsafe-eval' to your script-src. However, modern JavaScript frameworks (React, Vue, Angular) do not require eval() in production builds, so you should strive to remove this exception.

How can I test a CSP without breaking my live website?

Use the Content-Security-Policy-Report-Only HTTP header instead of the enforcing header. In Report-Only mode, the browser evaluates the policy against the page and generates violation reports in the console (and sends them to your reporting endpoint), but it does not actually block any content.

This allows you to deploy a policy, monitor the reports for a week to catch false positives (like forgotten third-party tracking pixels), fix your policy, and then switch to enforcing mode.

How does the report-uri / report-to directive work?

When a CSP violation occurs (e.g., a blocked malicious script), the browser can automatically send a JSON payload detailing the violation to a specified server endpoint.

report-uri is the legacy directive for this, while report-to is the modern implementation utilizing the Reporting API. Setting up a reporting endpoint (or using a service like Report URI or Sentry) is critical for monitoring active attacks and uncovering broken assets on your live site.

Can I use meta tags instead of HTTP headers for CSP?

Yes, you can deploy a policy using <meta http-equiv="Content-Security-Policy" content="...">. However, there are significant limitations:

  • Meta tag CSPs cannot use the report-uri, report-to, or frame-ancestors directives.
  • You cannot use Report-Only mode via a meta tag.
  • It only protects the HTML document, not secondary resources.

HTTP response headers are always the strongly preferred method for delivering a CSP.

How do I prevent my site from being framed (Clickjacking protection)?

Use the frame-ancestors directive. This dictates which domains are allowed to embed your site via <iframe>, <object>, or <embed>.

  • frame-ancestors 'none' prevents any domain from framing your site.
  • frame-ancestors 'self' allows only your own domain.
  • frame-ancestors https://trusted-partner.com whitelists a specific domain.

This modern CSP directive replaces the older X-Frame-Options header.

Rate Visual CSP Header Builder & Auditor

Help us improve by rating this tool.

4.9/5
1,123 reviews