Security headers
How to Configure a Strict Content Security Policy (CSP) Without Breaking Your Site
A practical guide to deploying a strict Content Security Policy using nonces or hashes to eliminate Cross-Site Scripting (XSS).
Content Security Policy (CSP) is one of the most powerful browser security mechanisms ever standardized, yet many development teams avoid it due to fears of breaking user interfaces. A poorly configured policy can disable essential third-party analytics, fonts, and payment gateways.
Deploying a modern “strict” CSP using cryptographic nonces provides robust Cross-Site Scripting (XSS) immunity without requiring endless domain allowlists.
The Flaw in Traditional Domain Allowlisting
In earlier CSP specifications, developers maintained extensive lists of approved domains:
Content-Security-Policy: script-src 'self' https://cdnjs.cloudflare.com https://apis.google.com;
Security researchers proved that domain allowlists are inherently fragile:
- If an allowed CDN hosts an outdated JSONP endpoint or vulnerable Angular library, an attacker can bypass the CSP completely.
- Every time marketing adopts a new software vendor, the engineering team must update server header policies.
The Modern Nonce-Based Approach
Google and OWASP recommend a Strict CSP architecture:
Content-Security-Policy:
object-src 'none';
script-src 'nonce-{RANDOM_BASE64}' 'strict-dynamic' https: 'unsafe-inline';
base-uri 'none';
How it works:
- On every HTTP response, your web server generates a cryptographically random 128-bit string (e.g.,
nonce-4B9Xk2Lp0zQ...). - That token is placed in the HTTP header and attached to valid inline script tags:
<script nonce="4B9Xk2Lp0zQ...">. - The browser only executes scripts matching that specific one-time token.
- Any malicious script injected via stored or reflected XSS lacks the nonce and is blocked immediately.
Sources
Frequently asked questions
- What is a CSP nonce?
- A nonce (number used once) is a unique, cryptographically random token generated by the server for each HTTP request that allows specific trusted inline scripts to run.
- How does CSP prevent data exfiltration?
- By specifying connect-src directives, CSP restricts which API endpoints and domains browser scripts can send data to.
About the author
Written by the LockdownGuide Editorial Team, which researches and maintains every guide on this site. Read more about our editorial standards or contact us with corrections.
Keep reading
Zero Trust Architecture for Small Business: How to Move Beyond Legacy VPNs
How SMBs can implement modern Zero Trust Network Access (ZTNA) to grant least-privilege access without cumbersome legacy VPNs.
Read the guide →SIM Swapping Attacks: How Attackers Steal Numbers & How to Lock Your Carrier
How SIM swapping works, how attackers bypass SMS two-factor authentication, and step-by-step instructions to set carrier account lock PINs.
Read the guide →Securing Home Routers and IoT Devices: The Remote Worker Hardening Guide
How to segment smart home IoT gadgets from corporate work computers using VLANs and guest Wi-Fi networks.
Read the guide →