November 23, 2026
The Optimizely Malware Had a Plan for Your CSP. It Was Called eval.
Third-party JavaScript security after the Optimizely attack: the CSP and SRI setup for Rails

By Raza Hussain
8 min read
What does third-party JavaScript security need after the Optimizely supply chain attack? A host allowlist is not enough.
At 04:21 UTC on November 16, a few lines of JavaScript showed up in the BBC's Optimizely snippet. By the evening, Report URI had counted 188 infected snippets belonging to 178 organisations, and some sites were skimming card numbers from their own checkout forms.
The attacker had written the payload a fallback. If a Content Security Policy blocked the script URL, an inline copy ran through eval, and Optimizely's own docs tell customers to allow eval.
Here's the short answer. Third-party JavaScript security in a Rails app starts with a CSP that names every script host, a tight connect-src that blocks the beacon this payload used, and a reports endpoint that tells you when a new host appears.
Report-only mode shows you the problem. Only an enforced policy blocks it.
I set this up in a fresh Rails 8.1.4.1 app and posted simulated violation reports at it. Here's what came out.
- A Rails reports endpoint built on
ActionController::Baserejected my simulated reports with a 422 until I skipped CSRF protection for it. - Report URI's data shows a host allowlist that includes the vendor stops only the second stage when the vendor's own file is the one injected, and the payload carried a fallback for that.
connect-srcblocked the data theft on 17 affected sites whosescript-srclet the payload run.- An
evalviolation shows up as a report from a host calledeval, if your policy forbids it. Under Optimizely's recommended policy, it never reports at all.
Two sources carry this, and I read both. Scott Helme's post on Report URI is the primary research.
BleepingComputer ran its own urlscan.io checks and got Optimizely's statement. Report URI sells CSP reporting, so read its framing with that in mind. The data is detailed and checkable.
Optimizely told BleepingComputer an unauthorised party exploited a vulnerability in the Experimentation account signup flow and used it to add JavaScript to customer projects. The code went into Project JavaScript, the field where customers put custom code that runs on every page. The rest of each snippet was untouched.
The injected loader pulled a second script from https://1688360.cc/track.js. The payload swapped crypto wallet addresses, took card numbers, expiry dates and CVVs from form submits, and sent cookies, page URLs and screenshots home. Cards typed into hosted payment iframes were out of its reach.
The card skimmer was added at 13:43 UTC. Most snippets were cleaned between about 20:40 and 21:10, and Report URI says every infected file was clean by 23:11.
That's the JavaScript supply chain attack in one line. A script you trust ran code you didn't write, with the same access as your own.
A script tag doesn't get a sandbox. Whatever it loads can read every form field on the page, any token you left in localStorage, and any cookie not marked HttpOnly. This payload used three of those powers, and it didn't need anything else.
And one vendor compromise reaches every customer at once. The attacker didn't break into 178 companies. They broke into one signup flow.
Why allowlisting cdn.optimizely.com didn't help
This is the part I'd have gotten wrong. My first instinct for Content Security Policy in Rails is a script-src that lists my host and my named vendors. Report URI's verdict on that is blunt. Allowing cdn.optimizely.com was never going to help.
The loader lived inside the allowed file. An enforced script-src without 1688360.cc blocks the second stage, but the inline fallback runs through eval, and a policy that follows Optimizely's docs allows 'unsafe-eval'. HSBC's CSP blocked track.js, and the inline copy ran anyway.
Report URI checked 256 affected hosts and broke out four groups. 68 had no CSP. 50 allowed both the payload and the data theft.
24 blocked the payload with a restrictive script-src. And 17 let the payload run but blocked the theft, because their connect-src didn't allow 1688360.cc.
That last group is the one I'd copy. Card data left through navigator.sendBeacon, and CSP's connect-src covers beacons as well as fetch and XHR.
It doesn't close every exit. Code running with first-party power can still leak data through an image request to a host img-src allows, through navigation or window.open, or through a vendor endpoint you allowed on purpose. connect-src stopped this payload's beacon, and a different payload would pick a different door.
Content Security Policy Rails setup in report-only mode
Rails ships the DSL in config/initializers/content_security_policy.rb. Know what report-only means before you copy this.
The browser sends the header Content-Security-Policy-Report-Only, logs violations and reports them, and blocks nothing. It's how you find every host you depend on before enforcement breaks a page.
# config/initializers/content_security_policy.rb
Rails.application.configure do
config.content_security_policy do |policy|
policy.default_src :self
policy.script_src :self, "https://cdn.optimizely.com" # list each vendor by host
policy.connect_src :self, "https://logx.optimizely.com" # where scripts may send data
policy.img_src :self, :data
policy.object_src :none
policy.base_uri :self
policy.form_action :self
policy.report_uri "/csp_reports"
end
โ
โ
config.content_security_policy_report_only = true
end# config/initializers/content_security_policy.rb
Rails.application.configure do
config.content_security_policy do |policy|
policy.default_src :self
policy.script_src :self, "https://cdn.optimizely.com" # list each vendor by host
policy.connect_src :self, "https://logx.optimizely.com" # where scripts may send data
policy.img_src :self, :data
policy.object_src :none
policy.base_uri :self
policy.form_action :self
policy.report_uri "/csp_reports"
end
โ
โ
config.content_security_policy_report_only = true
endThe Optimizely hosts are examples. Put in the ones your pages actually load.
Notice what's missing, though. There's no :unsafe_eval. If a vendor needs it, that's a decision to write down, because it's the exact gap this attack used.
My lab's response header came back like this.
content-security-policy-report-only: default-src 'self'; script-src 'self' https://cdn.optimizely.com; connect-src 'self' https://logx.optimizely.com; img-src 'self' data:; object-src 'none'; base-uri 'self'; form-action 'self'; report-uri /csp_reportscontent-security-policy-report-only: default-src 'self'; script-src 'self' https://cdn.optimizely.com; connect-src 'self' https://logx.optimizely.com; img-src 'self' data:; object-src 'none'; base-uri 'self'; form-action 'self'; report-uri /csp_reportsRails security headers like this one are easy to set and easy to forget. Run report-only for a couple of weeks, read what it reports, then flip content_security_policy_report_only off.
Where Subresource Integrity helps, and where it breaks
Subresource Integrity is a hash on the script tag. The browser downloads the file, hashes it, and refuses to run it if the hash doesn't match. In this attack, the injected snippet would have failed that check.
So why didn't everyone have it? Because the Optimizely snippet changes on purpose.
It's rebuilt from your project settings, which is how the attacker's Project JavaScript reached it, and that also means a pinned hash breaks the next time your own team publishes an experiment. That's my inference from how the snippet is built, not something either source tested.
SRI fits scripts with a version in the URL. For those, compute the hash once and pin it, and add the host to script_src too, or the enforced policy blocks the pinned file.
curl -s https://cdn.example.com/widget-1.4.2.js \
| openssl dgst -sha384 -binary | openssl base64 -Acurl -s https://cdn.example.com/widget-1.4.2.js \
| openssl dgst -sha384 -binary | openssl base64 -AThat prints the base64 digest. Put it after sha384- in the tag, and add crossorigin so the browser can hash a file from another origin.
<%= javascript_include_tag "https://cdn.example.com/widget-1.4.2.js",
integrity: "sha384-<hash from the command above>",
crossorigin: "anonymous" %><%= javascript_include_tag "https://cdn.example.com/widget-1.4.2.js",
integrity: "sha384-<hash from the command above>",
crossorigin: "anonymous" %>Rails 8.1.4.1 rendered both integrity and crossorigin on the tag in my lab. I didn't test the browser's rejection here. That part comes from the SRI spec.
Cross-origin SRI also needs the vendor's CDN to send CORS headers, so check the network tab after you add it.
For a script the vendor edits in place, the choice is harder. Self-hosting a reviewed copy gives you a stable hash, and it stops the tool updating itself.
For an experimentation tool, that usually means it stops working the way your marketing team expects. For a checkout page, I'd make that trade. For a blog, probably not.
A violation reports endpoint that alerts on new hosts
Report-only mode is only useful if someone reads the reports. So the reports go to a Rails controller, each one gets stored, and any host you haven't approved raises an alert. A new script host on a checkout page is an incident until someone proves it isn't.
# db/migrate/20261117000001_create_csp_reports.rb
class CreateCspReports < ActiveRecord::Migration[8.1]
def change
create_table :csp_reports do |t|
t.string :document_uri, null: false
t.string :blocked_host, null: false
t.string :directive, null: false
t.jsonb :raw, null: false
t.timestamps
end
add_index :csp_reports, [:blocked_host, :directive]
end
end# db/migrate/20261117000001_create_csp_reports.rb
class CreateCspReports < ActiveRecord::Migration[8.1]
def change
create_table :csp_reports do |t|
t.string :document_uri, null: false
t.string :blocked_host, null: false
t.string :directive, null: false
t.jsonb :raw, null: false
t.timestamps
end
add_index :csp_reports, [:blocked_host, :directive]
end
endThat table is PostgreSQL-flavoured because of jsonb. Use json on MySQL or SQLite. It grows with every report, so give it a retention job, and I'd keep 30 days.
The approved hosts live in the repo, not in the database. My first version learned known hosts from earlier reports, and that was a hole. Anyone can post to this endpoint, so one fake report for 1688360.cc would have silenced the real alert forever.
# config/initializers/csp_approved_hosts.rb
Rails.application.config.x.csp_approved_hosts = %w[cdn.optimizely.com logx.optimizely.com]# config/initializers/csp_approved_hosts.rb
Rails.application.config.x.csp_approved_hosts = %w[cdn.optimizely.com logx.optimizely.com]Know the controller's limits before you copy it.
- Anyone can post to it, so a report is a lead, not proof. A fake report can still mute one host's alert for the 10-minute window, but not forever.
rate_limitkeeps its counts in your Rails cache store and keys them onrequest.remote_ip. It needs a shared store, like Solid Cache or Redis, and correct trusted proxies. On:memory_storeeach process counts separately. Behind a proxy whose IPs you don't trust, every visitor shares one bucket, and the reports you most need get dropped first.- A report rejected by the limit, or lost when the database write fails, is gone. I wouldn't count on the browser sending it again.
- It reads the classic
report-uriformat. The newer Reporting API sends a JSON array with a different shape, which this controller rejects as a bad request. Rails 8.1.4.1's CSP DSL hasreport_uriand noreport_todirective. - It strips query strings and drops
script-samplebefore storing, because both can carry tokens or personal data.
# app/controllers/csp_reports_controller.rb
class CspReportsController < ActionController::Base
skip_forgery_protection # browsers post reports with no CSRF token
rate_limit to: 100, within: 1.minute # per client IP, needs a shared cache store
โ
โ
MAX_BYTES = 10_000
โ
โ
def create
body = request.raw_post
return head(:payload_too_large) if body.bytesize > MAX_BYTES
โ
โ
data = JSON.parse(body)
report = data["csp-report"] if data.is_a?(Hash)
return head(:bad_request) unless report.is_a?(Hash)
โ
โ
blocked = report["blocked-uri"].to_s
host = (URI.parse(blocked).host || blocked).first(255) # "inline", "eval" have no host
directive = (report["effective-directive"] || report["violated-directive"]).to_s.first(100)
page = report["document-uri"].to_s.split("?").first.to_s.first(500) # drop query strings
โ
โ
CspReport.create!(document_uri: page, blocked_host: host, directive: directive,
raw: report.except("document-uri", "script-sample"))
alert(host, directive, page) unless Rails.configuration.x.csp_approved_hosts.include?(host)
โ
โ
head :no_content
rescue JSON::ParserError, URI::InvalidURIError
head :bad_request
end
โ
โ
private
โ
โ
# At most one alert per host and directive every 10 minutes.
def alert(host, directive, page)
key = "csp-alert/#{directive}/#{host}"
return unless Rails.cache.write(key, true, unless_exist: true, expires_in: 10.minutes)
โ
โ
Rails.logger.warn("[csp] UNAPPROVED #{directive} host #{host} on #{page}")
end
end# app/controllers/csp_reports_controller.rb
class CspReportsController < ActionController::Base
skip_forgery_protection # browsers post reports with no CSRF token
rate_limit to: 100, within: 1.minute # per client IP, needs a shared cache store
โ
โ
MAX_BYTES = 10_000
โ
โ
def create
body = request.raw_post
return head(:payload_too_large) if body.bytesize > MAX_BYTES
โ
โ
data = JSON.parse(body)
report = data["csp-report"] if data.is_a?(Hash)
return head(:bad_request) unless report.is_a?(Hash)
โ
โ
blocked = report["blocked-uri"].to_s
host = (URI.parse(blocked).host || blocked).first(255) # "inline", "eval" have no host
directive = (report["effective-directive"] || report["violated-directive"]).to_s.first(100)
page = report["document-uri"].to_s.split("?").first.to_s.first(500) # drop query strings
โ
โ
CspReport.create!(document_uri: page, blocked_host: host, directive: directive,
raw: report.except("document-uri", "script-sample"))
alert(host, directive, page) unless Rails.configuration.x.csp_approved_hosts.include?(host)
โ
โ
head :no_content
rescue JSON::ParserError, URI::InvalidURIError
head :bad_request
end
โ
โ
private
โ
โ
# At most one alert per host and directive every 10 minutes.
def alert(host, directive, page)
key = "csp-alert/#{directive}/#{host}"
return unless Rails.cache.write(key, true, unless_exist: true, expires_in: 10.minutes)
โ
โ
Rails.logger.warn("[csp] UNAPPROVED #{directive} host #{host} on #{page}")
end
endRoute it with post "/csp_reports", to: "csp_reports#create", and swap the log line for whatever pages your on-call engineer.
The first thing that broke was CSRF. A controller inheriting ActionController::Base checks an authenticity token on every POST, and violation reports don't carry one.
Without skip_forgery_protection, my endpoint answered my curl-posted reports with 422 and InvalidAuthenticityToken, so nothing would ever have been stored. ActionController::API controllers skip that check already.
With it in place, I posted reports shaped the way a browser sends them for this attack, on a single-process dev server.
POST track.js report (with ?token= in URL) 204 [csp] UNAPPROVED script-src-elem host 1688360.cc on https://shop.example/checkout
POST same report again 204 (stored, no second alert)
POST logx.optimizely.com connect-src 204 (approved host, no alert)
POST eval report 204 [csp] UNAPPROVED script-src host eval on ...
POST numbers where strings belong 204 (stored as text, no 500)
POST invalid JSON 400
over the limit 429 (lab limit of 5 per minute)POST track.js report (with ?token= in URL) 204 [csp] UNAPPROVED script-src-elem host 1688360.cc on https://shop.example/checkout
POST same report again 204 (stored, no second alert)
POST logx.optimizely.com connect-src 204 (approved host, no alert)
POST eval report 204 [csp] UNAPPROVED script-src host eval on ...
POST numbers where strings belong 204 (stored as text, no 500)
POST invalid JSON 400
over the limit 429 (lab limit of 5 per minute)Look at the eval line. That report only exists if your policy forbids eval.
Under a policy with 'unsafe-eval', the fallback runs silently and the only reports you'd get are the blocked track.js load and the beacon to 1688360.cc. Keep connect-src tight and you get both, even when the payload itself gets through.
What I tested and what I didn't
ClaimStatusWhereReport-only header and directives from the Rails DSLTestedRails 8.1.4.1 dev serverReports endpoint returns 422 without skip_forgery_protectionTestedSame app, ActionController::Base, curl not a browserStore, strip query strings, alert on unapproved hosts once per windowTestedSame app, PostgreSQL 16.10, curl-posted reportsrate_limit returns 429TestedSingle-process dev server, :memory_storeBad JSON and non-object bodies return 400TestedSame app and a parse scriptintegrity and crossorigin render on the tagTestedSame appBrowser rejects a script whose hash doesn't matchFrom the SRI specNo browser test hereWhich CSPs blocked the real payloadReportedReport URI, 256 affected hostsHow the signup flow was exploitedUnknownOptimizely has not published detailsTable โ screenshot this for Medium (tables are not supported on paste)
Do this before Friday
- Turn on CSP report-only mode with
report_uripointed at a reports endpoint, and leave out'unsafe-eval'unless you've written down which vendor needs it. - List every external script host your pages load, starting with checkout and login, and give each one an owner.
- Write
connect-srcas tightly asscript-src, because that's the line that stopped this payload's beacon on 17 sites that let the payload run.
Over the next year I'd expect more of these attacks to aim at vendor dashboards and signup flows rather than CDNs, because one write path into a vendor reaches every customer at once. Your CSP can't stop the vendor being breached. It can decide what the vendor's code is allowed to reach once it is.
Next, I'll take this policy from report-only to enforced on a real checkout page and show what broke on the way.
Related reads