On 16 November, an attacker used a flaw in Optimizely's Web Experimentation sign-up flow to add malicious JavaScript to customer projects. Researchers at Report URI found 188 infected snippets belonging to 178 organisations, among them Zoom, GitLab, Salesforce and the BBC. The first injection was seen at 04:21 UTC, and by 21:52 UTC all but one of the infected snippets had been cleaned. The affected organisations did not need to change their own code or servers for the malicious script to reach their visitors.
Content Security Policy (CSP) could have blocked the attack, but few sites had one that did. Report URI's crawl found that only 24 of the affected organisations, about 15%, had a policy strict enough to stop it, and 68 had no CSP at all.
No single header solves this problem, but a well-built set of controls can limit what a compromised vendor script can reach and make its behaviour visible. This guide walks through the steps in order.
Step 1: Inventory your third-party scripts
You cannot restrict what you have not listed. Open your site in a browser, open the Network panel, filter by JS, and note every domain that is not your own. Start with your highest-risk pages: login, account, checkout and payment.
For each script, record:
- The domain and what it does (analytics, A/B testing, chat, tag manager)
- Which pages load it
- Whether it needs to be on sensitive pages at all
Tag managers deserve extra attention because they load further scripts you may not have reviewed. The simplest win at this stage is removal. A testing or analytics snippet that does not need to run on your payment page should not run there.
Step 2: Start CSP in report-only mode
Enforcing a policy blind is a good way to break your site. Report-only mode lets the browser tell you what would have been blocked, without blocking anything.
Reporting-Endpoints: csp-endpoint="https://example.com/csp-reports"
Content-Security-Policy-Report-Only:
default-src 'self';
script-src 'self' https://cdn.vendor.example;
connect-src 'self' https://api.vendor.example;
form-action 'self';
base-uri 'self';
object-src 'none';
report-to csp-endpointThe policy is split across lines here for readability. In a real response, each header must sit on a single line.
Browser support for report-to has been uneven, so many teams include the older report-uri directive alongside it during rollout. Check current support for your audience before relying on either. Leave the policy in report-only mode for at least a week, long enough to catch pages that load scripts only occasionally.
Step 3: Tighten script-src, and watch two risky settings
Domain allowlists are often dismissed as weak, but they are not useless. In the Optimizely incident, the vendor's snippet carried a small loader, and the main payload was fetched from an attacker-controlled domain. A tight script-src that did not list that domain blocked it. Allowing the vendor's own domain made no difference, because the loader arrived through that domain. What mattered was that the attacker's domain was not allowed.
Two settings weakened otherwise reasonable policies:
'unsafe-eval'let an inline fallback copy of the payload run.'strict-dynamic'extends trust to any script that an already-trusted script inserts. If a nonced vendor snippet is compromised, everything it loads inherits that trust, and browsers that support the keyword ignore your host allowlist.
A safer starting point for pages that must load a third-party snippet looks like this:
Content-Security-Policy: script-src 'nonce-r4nd0mV4lue' https://cdn.vendor.example; object-src 'none'; base-uri 'none'
html
<script nonce="r4nd0mV4lue" src="https://cdn.vendor.example/snippet.js"></script>Nonces remain useful because they block injected scripts that lack the nonce, which makes them strong against classic XSS. They do not protect you from a vendor file you have deliberately allowed. If that file is tampered with, it will run, and no policy can stop code that sits inside a script you trust. That is why the remaining steps focus on limiting damage and spotting changes.
Step 4: Use Subresource Integrity where the file is static
SRI pins a script to a cryptographic hash. If the file changes by even one byte, the browser refuses to run it.
bash
openssl dgst -sha384 -binary lib-1.2.3.min.js | openssl base64 -A
html
<script src="https://cdn.example.com/lib-1.2.3.min.js"
integrity="sha384-HASH_FROM_ABOVE"
crossorigin="anonymous"></script>SRI works well for versioned library files that never change. It does not suit snippets that a vendor updates or customises per customer, because every legitimate change would break the hash. That is the situation with a service like Optimizely, where each customer's snippet contains their own experiments and custom code. For those scripts, rely on the controls in the next steps.
Step 5: Limit where a compromised script can send data and what it can read
Even if you cannot stop a trusted script being tampered with, you can limit what it does next:
connect-srcrestricts the destinations forfetch, XHR and WebSocket requests. List only the endpoints you actually use.form-action 'self'stops forms from being submitted to other origins.base-uriprevents attackers from changing how relative URLs resolve.- Hosted payment fields. Card fields served in an iframe from your payment provider's domain cannot be read by scripts on your own page.
HttpOnlycookies. Mark session and authentication cookiesHttpOnly, along withSecureandSameSite, so injected scripts cannot read them.
Know the limits. A tight connect-src limits exfiltration but cannot stop a script from changing what is on the page, such as swapping cryptocurrency wallet addresses, which was one of the Optimizely payloads. Navigation is also outside its scope. Security researcher Scott Helme described a separate Magecart skimmer earlier this year that, when direct exfiltration looked risky, sent the victim to attacker infrastructure with the stolen card data in the URL and then bounced them back to the real site. These directives raise the cost for an attacker without removing the need for monitoring.
Step 6: Watch the reports
A policy you never read does little. Send reports to an endpoint that aggregates them, and look for:
- New domains appearing in
script-srcorconnect-srcviolations that were not in your inventory - Sudden volume spikes on a single page, especially checkout
- Violations tied to one script, which often points to a compromised or misbehaving vendor tag
Set an alert for any previously unseen blocked or reported domain on sensitive pages. In the Optimizely case, an unfamiliar attacker domain showing up in reports on the day of the attack would have been an early signal, and Optimizely's own guidance to customers started with reviewing CSP violation reports. Sites with no reporting had nothing to review.
Also monitor the scripts themselves. A trusted script can change without any deployment on your side, so track new code and new network destinations in the files you load, and know how to disable a third-party script quickly.
Step 7: Enforce, then re-scan after every change
Once report-only mode is quiet, switch the header to Content-Security-Policy and keep the reporting endpoint in place. Then confirm the policy is actually live. Headers are often set at different layers (application, CDN, load balancer) and can be dropped, duplicated or overridden on some routes. Run a header scan after each deployment, and again whenever you add a new vendor.
Finally, ask your vendors how they secure customer accounts. For your own accounts with script vendors, enforce MFA or SSO and limit who can publish code, since in this incident the attacker's access came through the vendor's account flow rather than the customers' sites.
A short checklist
- Every third-party script is listed, with an owner
- Sensitive pages load only the scripts they need
- CSP ran in report-only mode for a week or more
script-srcavoids'unsafe-eval'and'strict-dynamic'unless you have tested the effect- Static library files have SRI hashes
connect-src,form-actionandbase-uriare set- Card entry uses hosted payment fields and session cookies are
HttpOnly - Reports are collected and someone reads them
- A scan confirms the headers on every key page