Content Security Policy

Technical reference of content security policy

Content Security Policy (CSP) allows website owners to restrict the type of resources that execute on a web page. The idea is that we should try to restrict the web page so that it only executes good things and none of the bad things.

CSP's main goal is to prevent a web vulnerability called Cross-Site Scripting (XSS). An XSS occurs when an attacker is able to inject and execute javascript on a user's web page. The XSS can then be used for a number of attacks such as account compromises, data exfiltration, credit card skimming and more.

So even if an attacker finds a way to exploit an XSS on a web page, the CSP should block the attack (and notify the website owner).

What does Content Security Policy (CSP) look like?

The recommended way to deploy Content-Security-Policy requires both an HTTP header, and some changes to <script> tags and inline handlers. This is an example policy the browser receives:

The policy specifies that only HTML script tags with nonce="random123" are allowed to execute. The nonce should be unique per page request (e.g. nonce-d3da0a4550fa2d00fc945074fccf8b99), and completely random. This means the policy will change slightly on every web request.

Since we require all HTML script tags to have that nonce, we need to update our script tags to look like:

<script nonce="random123" src="/main.js"></script>

If it's not possible to use nonces (static content / CDN), it's also possible to hash the allowed content and use 'sha256-abc123' in the policy. But where possible, it's recommended to use nonces.

The 'strict-dynamic' keyword allows previously allowed scripts to include futher scripts. Both report-uri and report-to provide a feedback mechanism that gives you insight into what your policy is actively blocking on web pages. The object-src and base-uri directives further restrict against other attacks (embeds, base hijacking).

What makes deploying CSP difficult?

When we enable CSP on a web page, we're telling the browser to block all JavaScript that is not marked with a nonce or a matching hash. This also includes inline event handlers (onclick=) and javascript: URIs, which cannot use nonces and must be refactored into JavaScript event listeners.

So as part of deploying, we'll need to find and categorize all the scripts and either tag them with a nonce, move inline event handlers to JavaScript event listeners, or hash the content.

A couple of things make this easier in some cases:

  • The report-to / report-uri feature of CSP will explicitly tell us how much work we have and where we need to update the web page
  • We can use Content-Security-Policy-Report-Only to only report violations instead of blocking until we're comfortable with the policy
  • Modern frontend frameworks (React, Angular, etc.) work well with CSP and usually don't require too much migration work
  • LLMs and coding agents are pretty good at doing the heavy lifting in migrating apps to CSP

How do I get started?

Here's a step-by-step guide on creating a Strict Content Security Policy.

An Introduction To Content Security Policy

An introduction to content security policy and cross site scripting.

Getting Started with Content Security Policy using NodeJS/Express and Csper

This guide covers building and installing content security policy (CSP) on a nodejs express server. This guide covers content security policy, generating a content security policy, report-uri, inline scripts, and deployment.

Other Security Features of Content Security Policy

Other security features of content security policy including upgrade-insecure-requests, block-all-mixed-content, frame-ancestors, sandbox, form-actions, and more