Web Development 6 min read 1121 words

The Optimizely attack and the third-party scripts we put on our websites

ES
The Optimizely attack and the third-party scripts we put on our websites

When you leave your house keys with the neighbour so she can water your plants, you’re not giving her permission to water, you’re giving her permission to get in. Whether she only waters the plants is up to her, and up to nobody taking the keys off her. Well, that’s more or less what we do every time we paste a third-party <script> into our website: we hand over the keys to the whole page.

On Sunday, 16 November, somebody got the keys to 178 houses at once. Scott Helme, from Report URI, has told the story in great detail in a report that is a bit scary to read: for most of that day, visitors to sites such as Zoom, the BBC, CNN, Salesforce, GitLab, IKEA, HSBC, Equifax, Air France-KLM or Ledger were served malicious code from the CDN of Optimizely, the experimentation and A/B testing platform all of them used.

What happened

Optimizely Web works like so many other marketing tools: each customer pastes a snippet into their site that loads from Optimizely’s CDN, and things on the page can be changed from the dashboard without touching the code. Among those options there is a field called “Project JavaScript”, whose contents run on every page that has the snippet.

Somebody managed to write into that field in the projects of many different customers that had nothing to do with each other. The rest of the snippet was untouched, so the malicious file arrived from a trusted domain with a valid certificate. As the report puts it, Optimizely’s CDN served exactly what it was given: no cache poisoning or anything exotic here. Not even Optimizely’s own sites escaped. How the attacker got that access is still unknown (the report points to a compromised internal system or a credential with access to several customers), and at the time of publication Optimizely had not responded.

flowchart LR
    A["Attacker"] -->|writes to Project JavaScript| B["Optimizely dashboard"]
    B --> C["Optimizely CDN · trusted domain"]
    C --> D["Snippet on 178 sites"]
    D -->|loads| E["track.js · attacker domain"]
    E --> F["Visitor browser"]

What the code did

The loader came in three flavours: a normal <script> tag, the same URL hidden in base64 and, in case the domain was blocked, a full copy of the code run through eval. What it loaded, a track.js the attacker rebuilt 26 times that same day, did a bit of everything:

  • Swapped cryptocurrency addresses for the attacker’s own: in the page text, in forms, in links, in the clipboard and in outgoing requests. It even checked that the address was valid before replacing it.
  • Stole cards: number, expiry date and CVV from forms. This was added in the early afternoon (UTC). Anything typed into payment iframes hosted by the payment gateway was out of its reach.
  • Took cookies (the ones that aren’t HttpOnly) and screenshots when there were wallets or payments on the page.
  • On iPhone and iPad it loaded iOS exploits depending on the system version, and on older versions it reloaded the page every minute to try again.

According to the report, by early afternoon the attacker had already moved about 7,000 dollars in cryptocurrency to exchanges. The mass cleanup started in the evening, and by 21:52 UTC a single infected snippet remained.

CSP, the great forgotten one

What interested me most in the report is that it doesn’t stop at the scare: they checked the Content Security Policy of the affected sites to see how many would have stopped the attack. These are the results for the ones they could evaluate:

SituationOrganisations
No CSP68
The CSP let both the code and the data theft through50
The code ran, but connect-src blocked the data from leaving17
The CSP blocked the code24 (15%)

Just one in seven. And the most interesting part is why the others that did have a CSP failed: two very common options opened the door for the attacker.

  • 'strict-dynamic' means any script inserted by a trusted script is trusted too. If the Optimizely snippet is trusted and the snippet loads track.js… well, track.js is trusted as well.
  • 'unsafe-eval', which according to the report Optimizely’s own documentation recommends, allowed the eval copy to run. HSBC, for example, had a CSP that blocked track.js and the data exfiltration, but the inline copy ran anyway.

The 17 in-between cases teach something too: connect-src stopped the data from leaving, but swapping cryptocurrency addresses happens inside the page itself and doesn’t need to send anything anywhere.

A restrictive CSP isn’t comfortable to maintain, as anybody who has tried to add one to a site with half a dozen marketing tools knows. But the basic idea is simple: state explicitly where scripts may be loaded from and where data may be sent to, and don’t hand out blanket permissions. Something like this (the domains are examples):

Content-Security-Policy: script-src 'self' https://cdn.vendor.example; connect-src 'self' https://api.vendor.example; report-uri /csp-report

With something like that, a track.js served from an unknown domain doesn’t load, the eval copy doesn’t run and the data has nowhere to go. And with report-uri (or its successor, report-to) you also find out that somebody tried, which is no small thing. If the marketing tool of the day requires 'unsafe-eval', it’s worth knowing that this is exactly the price being paid.

Mind you, CSP isn’t a magic fix: the code injected into the snippet itself ran anyway, because it came from the allowed domain. And Subresource Integrity (SRI), which would be the other obvious defence, doesn’t work for these snippets, because their contents change every time somebody touches something in the dashboard. But the difference between “the attacker runs code and walks off with cards” and “the attacker runs code but can’t load anything else or send anything anywhere” is quite large.

The house keys

A few weeks ago I wrote about how the Claude Code extension on the Cursor marketplace carried a backdoor for a few hours, and the underlying story is the same: we trust a third-party piece and that trust is inherited in full, with everything the piece can do, forever. As Helme himself sums it up, trusted JavaScript doesn’t always remain trustworthy.

The report’s recommendations for Optimizely customers are very specific: review the Project JavaScript for 1688360.cc, atob( or code they don’t recognise, rotate credentials, review who has access, treat cards entered on 16 November as potentially exposed and update iOS devices. For everyone else the lesson is more general: take stock of which third-party scripts we load, ask ourselves whether we really need all of them, and give them a CSP that limits what they can do. The neighbour still waters the plants, but she no longer has the keys to the rest of the house.