Web security

Content Security Policy (CSP)

Definition

Content Security Policy (CSP) is an HTTP header through which a site tells the browser which sources of scripts, styles, images or frames it is allowed to load. Anything outside that list is blocked, which greatly limits the impact of a cross-site scripting (XSS) vulnerability.

Content Security Policy (CSP), Web security

In practice, a CSP is an allowlist sent by the server with every page. The browser reads it before running anything, then refuses whatever isn't on it: a script from an unknown domain, an injected style, a frame or a form pointing elsewhere. Its main benefit is to limit the damage of a cross-site scripting (XSS) vulnerability: even if an attacker manages to slip code into a page, the browser can refuse to run it.

How a Content Security Policy works

A policy is made of directives separated by semicolons. Each directive targets a type of resource and lists the allowed sources:

  • default-src serves as the fallback for every resource type that doesn't have its own directive;
  • script-src, style-src, img-src, font-src cover scripts, styles, images and fonts;
  • frame-ancestors says which sites can display the page in a frame, which protects against clickjacking;
  • object-src 'none', base-uri and form-action close lesser-known injection paths.

As soon as a script-src or default-src directive exists, scripts written directly in the HTML and onclick attributes are blocked. The 'unsafe-inline' keyword lifts that ban, but MDN points out that it cancels much of the benefit of CSP. The recommended method, called strict CSP, allows scripts through a nonce (a random value that changes with every response, copied into the <script> tag) or through the script's SHA-256 hash.

Five-step diagram: the server sends the page with a Content-Security-Policy header, a script injected from an attacker's domain sits in the page, the browser compares its origin and nonce with the policy, refuses to run it and can send a violation report to the site

Header, meta tag and report-only mode

The policy is preferably sent in the Content-Security-Policy HTTP header. A <meta http-equiv> tag also works, but the W3C specification states that it ignores frame-ancestors, sandbox and report-uri, and that it can't carry a report-only policy.

Report-only mode is the key to a rollout that doesn't break anything. With Content-Security-Policy-Report-Only, the browser blocks nothing: it only reports what it would have blocked, in the console or to an address declared with report-to and the Reporting-Endpoints header. You adjust the list, then switch to enforcing mode.

Content-Security-Policy-Report-Only: default-src 'self'; script-src 'self' 'nonce-r4nd0m'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'

For WordPress

WordPress is tough terrain for a strict CSP: core, themes and many plugins print inline scripts. Since WordPress 5.7, the wp_get_inline_script_tag() and wp_print_inline_script_tag() functions and the wp_script_attributes filter let you add a nonce attribute to scripts that are loaded properly, but not to those a theme hard-codes. That's why many sites start with 'unsafe-inline' and then tighten things up page by page.

  • Test in report-only mode the home page, a post, the block editor, forms and checkout.
  • Set the policy at the server level (.htaccess, Nginx) if a cache serves pages without going through PHP.
  • Inventory third-party domains: fonts, analytics, videos, payment gateway.

Our guide to security headers for WordPress details a starter policy and the testing method, and the article on XSS vulnerabilities in WordPress shows what a CSP would have limited. CSP belongs to a broader family of HTTP security headers.

Common mistakes

  • Copying a strict policy from a tutorial and applying it as is: the editor or the cart stops working.
  • Reusing the same nonce from page to page: it must be unpredictable and change with every response, which doesn't play well with page caching.
  • Believing CSP replaces data escaping: OWASP presents it as defense in depth, not as the only protection against XSS.

WP Feint Pro offers a basic CSP that blocks browser plugins, protects the base URL and upgrades resources to HTTPS. It deliberately doesn't restrict scripts: a policy against XSS still has to be tailored, in the server or CDN configuration.

Further reading

  • Article How to set up HTTP security headers on WordPress: CSP, HSTS and the rest
  • Page WordPress security headers

Sources

  1. MDN: Content Security Policy (CSP) (external link, opens in a new tab)
  2. W3C: Content Security Policy Level 3 (external link, opens in a new tab)
  3. OWASP: Content Security Policy Cheat Sheet (external link, opens in a new tab)
  4. Make WordPress Core: fonctions d’attributs de scripts dans WordPress 5.7 (external link, opens in a new tab)

All definitions

Start free, go Pro when you’re ready

The free version already hides WordPress and moves the login page. Pro adds aliases, two-factor authentication and monitoring.