×
Preventing Cross-Site Scripting (XSS) with Strict Content Security Policy (CSP)

1. The Persistent Danger of XSS

Cross-Site Scripting (XSS) remains one of the most pervasive web application vulnerabilities. If an attacker injects malicious JavaScript into a web page viewed by a victim, the attacker script can steal session cookies, capture form keystrokes, and forge authenticated actions on behalf of the user. While input sanitization is essential, defense-in-depth requires browser-enforced execution boundaries.

2. The Limitations of Legacy Whitelist CSPs

Early Content Security Policy (CSP) implementations relied on domain whitelists (e.g. script-src https://trusted.com). Security research proved that static whitelists are almost universally bypassable via open redirects, JSONP endpoints, and CDN-hosted vulnerable libraries. Modern AppSec mandates strict, nonce-based policies.

3. Strict Nonce-Based CSP Architecture

A strict CSP generates a cryptographically random, unguessable Base64 token (a "nonce") on the web server for every incoming HTTP request. The server includes this token in the response header: Content-Security-Policy: script-src 'nonce-r@nd0m123' 'strict-dynamic'; object-src 'none'; base-uri 'none';. In the HTML markup, only scripts tagged with the matching attribute