Перейти к содержимому
Skip to content
← All articles

Content Security Policy (CSP) — A Complete Configuration Guide

Content Security Policy (CSP) is a security header that tells the browser which sources are allowed to load resources: scripts, styles, images, fonts, frames, and more. CSP is one of the most powerful tools for defending against XSS attacks (Cross-Site Scripting), code injection, and clickjacking.

Why You Need CSP

XSS remains one of the most common web vulnerabilities according to the OWASP Top 10. Even with careful input filtering, there is a risk of missing an attack vector. CSP acts as the last line of defense: even if an attacker injects malicious code, the browser won't execute it if the source isn't allowed by the policy.

What CSP protects against:

CSP Syntax

CSP is delivered via an HTTP header:

Content-Security-Policy: directive-1 value1 value2; directive-2 value3;

Each directive controls a specific type of resource:

DirectiveControlsExample
default-srcFallback for all resource typesdefault-src 'self'
script-srcJavaScriptscript-src 'self' cdn.example.com
style-srcCSSstyle-src 'self' 'unsafe-inline'
img-srcImagesimg-src 'self' data: https:
font-srcFontsfont-src 'self' fonts.gstatic.com
connect-srcAJAX, WebSocket, Fetchconnect-src 'self' api.example.com
frame-srcIframe sourcesframe-src youtube.com
frame-ancestorsWho can embed you in an iframeframe-ancestors 'none'
form-actionWhere forms can submitform-action 'self'
base-uriRestriction for <base> tagbase-uri 'self'
object-srcFlash, Java appletsobject-src 'none'

Source Values

ValueDescription
'self'Same origin only
'none'Block everything
'unsafe-inline'Allow inline code (reduces protection)
'unsafe-eval'Allow eval() (reduces protection)
'nonce-abc123'Allow elements with the specified nonce
'strict-dynamic'Trust scripts loaded by trusted scripts
https:Any HTTPS source
data:Data URIs (e.g., data:image/png)
*.example.comAny subdomain of example.com

CSP Configuration Examples

Basic Policy

Content-Security-Policy: default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self'; frame-ancestors 'none'; form-action 'self'; base-uri 'self'; object-src 'none'

Site with Google Analytics and Fonts

Content-Security-Policy: default-src 'self'; script-src 'self' https://www.googletagmanager.com https://www.google-analytics.com; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; img-src 'self' data: https://www.google-analytics.com; font-src 'self' https://fonts.gstatic.com; connect-src 'self' https://www.google-analytics.com; frame-ancestors 'none'

Strict Policy with Nonce

Content-Security-Policy: default-src 'self'; script-src 'nonce-rAnd0mVaLu3' 'strict-dynamic'; style-src 'self' 'nonce-rAnd0mVaLu3'; object-src 'none'; base-uri 'self'

When using nonce, each <script> and <style> tag must include the attribute:

<script nonce="rAnd0mVaLu3">
  // This script will execute
</script>

Report-Only Mode

Before enabling CSP in production, use report-only mode:

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

In this mode, the browser doesn't block resources but only sends JSON reports about violations. This lets you identify all third-party resources before enforcing restrictions.

Step-by-Step CSP Deployment

  1. Check your current security headers using the enterno.io HTTP header checker
  2. Compile a list of all third-party resources on your site (analytics, fonts, CDN, widgets)
  3. Create a policy in Report-Only mode
  4. Monitor violation reports for 1–2 weeks
  5. Add missing sources to the policy
  6. Enable CSP in enforcement mode
  7. Continue monitoring reports

Common Mistakes

CSP and Performance

CSP not only improves security but can also positively impact performance:

Checking CSP

Use the enterno.io HTTP header checker to analyze your site's current CSP policy. The tool will show the Content-Security-Policy header and help identify its absence.

Browser developer tools are also useful: the Console tab will show all CSP violations with detailed information about blocked resources.

Check your website right now

Check now →
More articles: Security
Security
Security Headers: The Complete Guide
14.03.2026 · 17 views
Security
CORS Explained: Cross-Origin Resource Sharing Guide
16.03.2026 · 9 views
Security
WAF Rules: Writing Effective Web Application Firewall Policies
16.03.2026 · 15 views
Security
HSTS — What It Is and Why Your Website Needs It
12.03.2026 · 15 views