Turing Registry

Content Security Policy Explained: Nonces and strict-dynamic

A Content Security Policy is one line in an HTTP response, sometimes a few hundred bytes long, that tells a browser which scripts it may run and where the page may send data. On 16 November 2026 that line decided whether a malicious script planted in Optimizely's experimentation snippet could swap crypto addresses and lift card numbers on checkout pages. Report URI scored the policies of 159 affected organisations: 68 had none, 50 had one that let the whole attack through, 17 stopped the data leaving but not the script running, and 24 blocked it outright. Our report on the incident has the details. The header itself has a longer history, including a 2016 study that found almost every policy on the web could be walked around, and a fix from that study which is the configuration Optimizely recommends today.

What the header tells the browser

A policy is a list of directives, each naming a kind of resource and the sources allowed to supply it. script-src governs JavaScript. connect-src governs where fetch, XHR and WebSockets may connect, which makes it the exfiltration control. img-src, style-src, frame-src and font-src do what their names say, and default-src covers anything left unnamed. frame-ancestors says who may put the page in a frame, which is the clickjacking defence. A second header, Content-Security-Policy-Report-Only, applies the same rules without enforcing them and posts violations to a reporting endpoint, so a site can learn what it would break before it breaks it.

The threat CSP was built for is cross-site scripting: an attacker who can inject markup into a page, through a comment field or a reflected URL parameter, but cannot yet run code. The policy's job is to make that injected <script> inert. Hold on to that threat model, because the Optimizely attack sat outside it.

Adoption is thin, and thinner where it matters. The HTTP Archive's 2025 Web Almanac puts CSP at 21.9% in its crawl, up from 18.5% a year earlier. Most of those policies never touch scripts: in the 2024 edition, frame-ancestors and upgrade-insecure-requests each appeared in more than half of all policies, while script-src appeared in 17% of them on desktop. The single most common full policy was upgrade-insecure-requests; and nothing else, a header that fixes mixed content and stops no script.

Why allowlists failed

The original CSP model was a list of trusted hosts: script-src https://www.google-analytics.com https://ajax.googleapis.com and so on. In 2016 four Google researchers, Lukas Weichselbaum, Michele Spagnuolo, Sebastian Lekies and Artur Janc, measured how well that worked. Their paper at ACM CCS, titled "CSP Is Dead, Long Live CSP!", analysed 26,011 unique policies from 1,680,867 hosts in Google's search index and found that 94.72% of them could be trivially bypassed.

Much of that came from the obvious mistake: 87.63% of the policies that tried to stop XSS also carried 'unsafe-inline' without a nonce, which re-enables the injected inline script the policy exists to block. The more interesting failure was in the allowlists themselves. Any allowed host that serves a JSONP endpoint (a URL that wraps its response in a function name taken from the query string) lets an attacker write <script src="https://allowed-host/endpoint?callback=attacker_code"> and run code from a trusted origin. Any allowed host that serves the AngularJS library lets the attacker load Angular and smuggle code through its template syntax. The authors found 194,908 domains with JSONP endpoints and 101,330 hosting AngularJS.

The most commonly allowed host, www.google-analytics.com, appeared in 33.93% of policies and could be abused wherever the policy also allowed 'unsafe-eval'. Second was *.googleapis.com at 27.68%, which served both JSONP and Angular and gave a full bypass. Of the 15 most commonly allowed hosts, 12 gave a full bypass, two gave one if the policy also allowed 'unsafe-eval', and for one, connect.facebook.net, the tool found nothing. The median policy listed 12 hosts, and at that length the authors bypassed 94.8% of policies. The longest list held 512. Every entry is a promise that nobody at that domain will ever ship a JSONP endpoint or an old Angular build, and nobody can keep 512 of those promises.

Nonces and 'strict-dynamic'

The paper's fix moved trust from hosts to individual tags. The server generates a random nonce for every response, sends it in the header as script-src 'nonce-R4nd0m', and stamps it on each <script> it means to run. Injected markup cannot guess the value. The W3C draft (Content Security Policy Level 3, still a Working Draft, latest dated 27 October 2026) says a server "MUST generate a unique value each time it transmits a policy," and that nonces must "remain unguessable."

Nonces alone break real sites, because loaders and tag managers create further scripts at runtime and those carry no nonce. The authors proposed a new keyword for that: 'strict-dynamic'. With it, a script that already has the browser's trust may add more scripts through document.createElement('script'), and they run wherever they come from. In browsers that support it, host allowlists, 'self' and 'unsafe-inline' in script-src are ignored, so the nonce becomes the only way in. The reasoning in the paper is that an attacker with a markup-injection bug cannot call createElement() without already running JavaScript.

The paper's case study is Google Maps Activities, an application it says had 4 million monthly users. Its allowlist had grown to 15 long paths through five major changes in 2015 and still included a JSONP endpoint. After the switch to a nonce with 'strict-dynamic', the authors report, the policy did not need another change. The same paper lists the limits. If the bug is attacker data flowing into the src of a script the page creates itself, 'strict-dynamic' turns it into code execution. If the injection lands inside a script that already carries the nonce, nothing stops it. And attacks that need no script, such as stealing data through injected forms or CSS, are untouched.

Ten years on, uptake is slow. The 2025 Almanac's charts show nonces in about 20% of script policies and 'strict-dynamic' in about 10%, while 'unsafe-inline' sits near 92% and 'unsafe-eval' near 77%. Some of that 'unsafe-inline' is a deliberate fallback for old browsers that a nonce switches off in new ones, so the figure overstates the damage. It still describes a web where the 2016 advice reached about one policy in ten.

Why it did nothing for Optimizely

Optimizely's own guide to configuring CSP for Web Experimentation recommends exactly the modern pattern: put a nonce on the snippet's <script> tag, add 'strict-dynamic', and in browsers that support CSP Level 3, the guide says, "it is only necessary to apply the nonce value to the Optimizely Experimentation snippet script tag." The same guide lists https://*.optimizely.com in script-src, which those browsers then ignore, and tells customers not to enable Trusted Types (require-trusted-types-for 'script') because it blocks the changes experiments make to the page. Report URI adds that Optimizely's documentation tells customers to allow 'unsafe-eval', and that at HSBC an inline fallback copy of the payload ran through eval even though the policy blocked the external file. The CSP guide itself, as published on 17 November, does not mention 'unsafe-eval', and Report URI's post does not link the page that does.

The recommended policy works as designed. The site vouches for the snippet and the snippet vouches for whatever it loads. When an attacker put code into a project's JavaScript field, the snippet served it and fetched the attacker's file, and 'strict-dynamic' passed the site's trust along to it. The code arrived through the one script the site had chosen to trust, a case CSP's threat model never claimed to cover.

Subresource Integrity, which pins a script to a hash of its contents, cannot help here either. An experimentation snippet changes every time someone edits an experiment, so a pinned hash would break the product on the next edit.

What did work, in part, was connect-src. Report URI's 17 partial cases were sites where the script ran but the browser refused to send card data, cookies and screenshots to the attacker's servers, because those hosts were not on the connect list. It could not stop the swap of a crypto address inside the page, since no request is needed for that. The 2024 Almanac found connect-src in about one policy in ten, and on 16 November it was the directive doing most of the work.

Two numbers would show whether the web took the lesson: the share of policies carrying a connect-src in the next Almanac crawl, and a breakdown, which Report URI has not published, of how the 24 sites that blocked the attack outright wrote their script-src.

Sources