Loading...
Loading...
CSP is the "nuclear option" against Cross-Site Scripting (XSS). It tells the browser exactly which domains are allowed to run code on your page.
Without CSP, any script injected into your page (via a compromised ad, a malicious npm package, or a user comment) can execute with full permissions. It can steal cookies, read local storage, and redirect users. CSP blocks unauthorized scripts from loading entirely.
Pitfall: Don't just set `default-src: *`. That does nothing. Start with `default-src 'none'` and unlock only what you need. Use `Content-Security-Policy-Report-Only` header first to see what *would* break without actually breaking it, logging violations to a service like Sentry or URIports.
CSP ships as the `Content-Security-Policy` response header (or `Content-Security-Policy-Report-Only` during rollout). A missing or permissive policy is trivial to spot in one curl. Google's CSP Evaluator will grade an existing policy.
curl -sI https://yourdomain.com | grep -i content-security-policycurl -sI https://yourdomain.com | grep -i "^content-security-policy" | fold -s -w 120https://csp-evaluator.withgoogle.com/?url=https://yourdomain.comAlways Report-Only first. The browser parses the policy, reports violations via `report-to`, but does not block anything. Once you have zero legitimate violations across real traffic for 1-2 weeks, switch to the enforcing `Content-Security-Policy` header.
Briefly, during migration. If you need to accept inline styles for a legacy page, keep `unsafe-inline` in `style-src` only (never `script-src`) and plan the move to external stylesheets. For scripts, prefer nonces or `strict-dynamic` with a hash-based fallback.
Nonces are easier at request time (generate a random value per response, inject it into the script tag and the header). Hashes are better for static templates but need a rebuild step when the script changes. Most teams use nonces in dynamic apps and hashes for build-time assets.
Applied the configuration change? Run a live scan to confirm the vulnerability is patched.