Vulnerability Analysis

Content Security Policy: A Complete Deep Dive

June 28, 202615 min read·Sentinel Security Team
CSPXSScontent security policyheadersbrowser security

What Is CSP?

Content Security Policy (CSP) is an HTTP header that tells the browser what sources of content are allowed to load on your page. It's the single most effective defense against Cross-Site Scripting (XSS) attacks when properly configured.

How CSP Works

When a browser receives a CSP header, it creates an allowlist of approved content sources:

Content-Security-Policy: default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'

Any resource that doesn't match the policy is blocked by the browser before it ever executes.

Common CSP Directives

DirectiveControlsExample
default-srcFallback for all resource typesdefault-src 'self'
script-srcJavaScript sourcesscript-src 'self' 'strict-dynamic'
style-srcCSS sourcesstyle-src 'self' 'unsafe-inline'
img-srcImage sourcesimg-src 'self' data:
connect-srcXHR/Fetch/WebSocketconnect-src 'self' https://api.example.com
frame-ancestorsWho can embed the pageframe-ancestors 'none'

Building Your CSP

Step 1: Start in Report-Only Mode

Never deploy CSP enforcement without testing first:

Content-Security-Policy-Report-Only: default-src 'self'; report-uri /csp-report

This sends violation reports without blocking anything.

Step 2: Use 'strict-dynamic' for Modern CSP

For single-page applications, 'strict-dynamic' allows scripts loaded by other trusted scripts to execute:

script-src 'strict-dynamic' 'nonce-{random}' https:

This eliminates the need to maintain a growing allowlist of CDN hosts.

Step 3: Monitor and Iterate

Set up a report-uri or report-to endpoint to collect violations. Over time, you'll discover legitimate resources you missed.

Common CSP Mistakes

  1. Using 'unsafe-inline' with scripts — This bypasses CSP for inline scripts and makes the policy nearly useless against XSS
  2. Too permissivedefault-src '*' allows everything from everywhere
  3. Forgotten third-party integrations — Analytics, chat widgets, and CDN scripts often break CSP on first deployment

Testing CSP with Sentinel

Use Sentinel's free header scan to check your CSP configuration:

  • Is CSP present?
  • Is it missing 'strict-dynamic'?
  • Does it use 'unsafe-inline' (a red flag)?
  • Is a report-uri configured?

Summary

CSP is your strongest defense against XSS. Start in report-only mode, use 'strict-dynamic', monitor violations, and iterate. Sentinel can help you audit your CSP in seconds.

Scan Your Domain

Get a free security scan of your website. No signup required.

Start Free Scan
Sentinel Scanner © 2026Home