Skip to content

Lesson 10 of 14 · 12m

Third-Party Scripts Are Remote Code

A script tag hands your origin to a remote operator — polyfill.io's 2024 sale and the Magecart skimming of British Airways as the evidence.

A script tag is a privilege grant

<script src="https://cdn.example/widget.js"> does not embed a feature; it hands a remote operator your origin. The fetched code executes with everything your own code gets: the DOM, document.cookie (HttpOnly only stops JS, and plenty of tokens are not HttpOnly), fetch as the user, localStorage, form fields as users type them, and the ability to rewrite or redirect the page itself.

There is no per-script sandbox. No "read-only" script load. The security posture of every host named in script src becomes part of your attack surface, and the code they serve changes without touching your repository — so the payload never appears in your diffs, your reviews, or your tests. new Worker(url) and importScripts(url) are the same grant through a different door; CSP's worker-src exists precisely because people forget workers are remote code too.

The wrong mental model is "it is just a library, libraries are vetted." Libraries are code someone else can change tonight.

polyfill.io: the CDN that sold itself

cdn.polyfill.io served JavaScript polyfills to a large fraction of the web. In February 2024 the maintainer sold the domain and GitHub account to Funnull, a Chinese company. Security folks flagged the sale immediately; by June 2024 Sansec documented what the new owner was serving: injected code that redirected mobile visitors of embedding sites to scam and betting domains through fake googie-anaiytics.com loaders. Over 110,000 websites carried the payload, including properties belonging to Intuit and the World Economic Forum.

The payload was professionally evasive — it activated only on specific devices at specific hours and went dormant when it detected an admin user or analytics instrumentation. Namecheap suspended the domain on June 26, 2024; Cloudflare and Fastly stood up clean mirrors so sites could swap without rebuilding. The incident is the canonical proof that a script tag is a live wire: nothing about the embedding sites changed, nothing needed to be hacked — the trust they had already granted was simply exercised by its new owner.

British Airways: the first-party version

Magecart's August–September 2018 skimming of British Airways needed no CDN at all. Attackers modified modernizr-2.6.2.min.js — a first-party file served from ba.com — appending a 22-line card skimmer that watched the baggage-payment form in the site and the app. Somewhere between 380,000 and 500,000 payment transactions were skimmed. The ICO proposed a £183.4 million fine; the final penalty settled at £20 million in October 2020.

Self-hosting is necessary but not sufficient. The file was on BA's own infrastructure; what failed was integrity — nothing verified the bytes being served were the bytes anyone intended. That is precisely what Subresource Integrity pins (lesson 11) and why CSP reports belong on your own hosts too. Magecart ran the same play across thousands of stores for years — formjacking and injected skimmers on checkout flows — and the common denominator was never an exotic exploit. It was always a script the site already trusted being quietly changed underneath it.

What to actually do

  • Enumerate every <script src> and new Worker(...) on your pages. Each is a trust grant; write down why it exists.
  • Prefer self-hosting vendored copies of static libraries over third-party hot-links — your own origin under your own deployment controls.
  • Where a CDN is justified, use immutable versioned URLs plus SRI hashes (lesson 11), never mutable latest endpoints.
  • Keep script-src tight, but understand its limit from lesson 9: a host allowlist does nothing when the allowed host is the one compromised. CSP gates the address; SRI gates the bytes.
  • Revisit analytics and chat pixels periodically. They carry identical privileges to an attacker's payload; each one should still be earning its keep.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.