Skip to content

Lesson 7 of 14 · 14m

A CSP That Actually Blocks Things

The deny-by-default policy with nonces, strict-dynamic, base-uri, and form-action — and why script-src 'self' 'unsafe-inline' protects nothing.

What the header actually buys you

A Content Security Policy is a per-response allowlist the browser enforces on your page: which origins may supply scripts, styles, images, connections — and, critically for XSS, whether inline script may run at all. That second property is the whole reason CSP exists as an XSS defense. Injected markup becomes inert when the injected <script> or onerror= handler has no permission to execute.

The wrong model is "the site sends a CSP header, so it has CSP." Roughly half the policies in the wild contain script-src 'self' 'unsafe-inline', and 'unsafe-inline' deletes the XSS protection entirely: every injected <img onerror> and inline <script> is permitted. A policy with unsafe-inline for scripts is a resource-loading rule, not an XSS defense.

A policy that blocks

Content-Security-Policy:
  default-src 'none';
  script-src 'nonce-r4nd0mP3rR3spons3' 'strict-dynamic';
  style-src 'self' 'unsafe-inline';
  img-src 'self' data:;
  font-src 'self';
  connect-src 'self';
  object-src 'none';
  base-uri 'none';
  form-action 'self';
  frame-ancestors 'none';
  require-trusted-types-for 'script'
  • default-src 'none' makes the policy deny-by-default; anything you did not name is refused.
  • script-src 'nonce-...' runs only script elements carrying that response's nonce. 'strict-dynamic' lets nonced scripts load their own dependencies — covered in lesson 8.
  • style-src 'self' 'unsafe-inline' is conventional, not ideal: CSS has no script, but attribute-selector exfiltration is real (lesson 9). Tighten it when you can afford the refactor.
  • object-src 'none' kills plugin content. Flash is gone; the directive costs nothing and should stay forever.
  • base-uri 'none' blocks injected <base href> from redirecting every relative URL on the page to an attacker host.
  • form-action 'self' constrains where any <form> may submit — without it, injected forms exfiltrate tokens under a CSP that otherwise holds.
  • frame-ancestors 'none' is the clickjacking defense — lesson 14.
  • require-trusted-types-for 'script' is the Trusted Types layer from lesson 6.

What the migration costs

Inline event handlers — onclick=, onload= — can never carry a nonce. They must become addEventListener in a nonced file. Inline <script> blocks must either move to external files or be authorized by hash: script-src 'sha256-…' covers a script's exact bytes, and a trailing newline changes the hash. Hashes are for the small static bootstrap snippet you cannot move, not for application code. CSP3 also splits script-src into script-src-elem (script elements) and script-src-attr (inline handlers), so a transitional policy can tighten one while you migrate the other. 'unsafe-eval' is required by webpack's eval-based dev sourcemaps — dev only; it reopens string-to-code sinks in production.

Delivery mechanics that bite people

CSP is an HTTP header, enforced per response. A <meta> tag can carry a policy, but three directives are header-only: frame-ancestors, report-uri, and sandbox — a meta-tag policy cannot defend against framing or report violations. Send the header.

If two policies apply to one response (two headers, or a header plus a meta), a resource must pass both. Policies combine by intersection, never union — a sloppy second header can only tighten, and a broken first one is not rescued by the second.

Rollout is Content-Security-Policy-Report-Only first: violations post JSON reports to your report-uri/Reporting-Endpoints target without blocking anything. Weeks of reports tell you which inline handlers and stray hosts exist; fix them, then move the same policy to the enforcing header. One nuance worth keeping: browser extension content scripts run in isolated worlds and do not trip your CSP — when a report names a script you don't recognize, check the extension column before blaming your own code.

What to actually do

Write the policy above, adapt img-src/connect-src to reality, deploy report-only, fix what reports surface, enforce. The policy that blocks is nonce-based, deny-by-default, and has no unsafe-inline for scripts. Everything else is a header-shaped decoration.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.