Most websites load JavaScript from other companies: analytics, chat widgets, tag managers, fonts and libraries from public CDNs. Each of those scripts runs with the same access as your own code. It can read what visitors type, change links and send data anywhere. If the provider is compromised, or the domain changes hands, every site that loads the script is affected at once.
A real example: polyfill.io
In June 2024, after the polyfill.io domain changed ownership, the service began serving code that redirected some visitors to unwanted sites. Security researchers estimated that more than 100,000 websites were loading it. Sites that pinned the file locally or restricted where scripts could load from were not affected.
Controls that limit the damage
- Keep an inventory of every third-party script, who owns it and why you need it. Remove the ones you no longer use.
- Host stable libraries yourself instead of loading them from a public CDN.
- Add Subresource Integrity hashes to scripts that do not change, so the browser refuses a modified file.
- Send a Content-Security-Policy that lists the script sources you trust, and avoid broad sources such as https: or *.
- Use a report-only policy first to find what breaks, then enforce it.
<script src="/vendor/library-3.2.1.min.js"
integrity="sha384-REPLACE_WITH_THE_FILE_HASH"
crossorigin="anonymous"></script>Content-Security-Policy: script-src 'self' https://analytics.example.com; object-src 'none'; base-uri 'none'Glossary
- Supply-chain attack
- An attack that compromises a trusted supplier, such as a script provider, to reach that supplier's customers.
- Subresource Integrity
- A browser feature that checks a downloaded file against a cryptographic hash before running it.