How to Spot Third-Party Script Risks on Your Own Website
Analytics, chat and testing scripts run with your page’s full power. Learn to inventory them, limit damage with CSP and SRI, and spot changes fast.
Published
Updated

Open the network tab on almost any commercial website and you will find scripts loaded from a dozen or more outside companies. Each one runs in your visitors’ browsers with the same permissions as your own code. It can read what is on the page, change it, and send data elsewhere. When a vendor is compromised, as happened with Optimizely this month, your site can start serving malware without a single change to your own repository.
This guide covers how to find the risk, reduce it, and notice quickly when something goes wrong.
Step 1: Build an inventory
You cannot manage what you cannot see. Start with a list of every third-party script on your most sensitive pages: login, account, checkout and any page that handles personal data.
Practical ways to find them:
- Browser developer tools. Open the Network tab, filter by JS, and sort by domain. Anything not on your own domain is third party.
- Tag manager audit. If you use a tag manager, list every tag, who added it, and when. Tag managers make it easy to add code without a code review, which is exactly why they are risky.
- Scanners and monitors. Several commercial and open-source tools crawl your site and report loaded resources and changes over time.
- CSP reports. A Content Security Policy in report-only mode (below) collects a free inventory of every source your pages load from.
For each script, record the vendor, purpose, owner on your team, which pages it loads on, and whether it truly needs to be there. You will often find old tools that nobody uses.
Step 2: Remove what you do not need
Every script is attack surface. Remove abandoned trials, duplicated analytics tools and widgets that no longer earn their keep. Do this before you invest in fancier defences.
Step 3: Limit what scripts can do
Use a Content Security Policy (CSP). A CSP tells the browser which origins may load scripts, connect to servers and so on. Start in report-only mode so nothing breaks while you learn:
Content-Security-Policy-Report-Only:
default-src 'self';
script-src 'self' https://cdn.example.com;
connect-src 'self' https://api.example.com;
report-uri /csp-reports
Once the reports look clean, enforce it. A strict connect-src is especially valuable because it can stop injected code from sending stolen data to an attacker’s server.
Be honest about its limits. A CSP cannot stop a vendor you have allowed from serving bad code. In the Optimizely incident, the malicious code arrived through a trusted vendor, so allow-lists alone would not have prevented it. CSP reduces the damage; it does not remove the trust problem.
Use Subresource Integrity (SRI) for static files. SRI pins a script to a cryptographic hash so the browser refuses it if the file changes:
<script src="https://cdn.example.com/lib.min.js"
integrity="sha384-BASE64HASHHERE"
crossorigin="anonymous"></script>
SRI works well for versioned libraries. It does not work for vendor scripts that legitimately change often or are generated per customer, which describes most testing, personalisation and tag-manager scripts.
Isolate high-risk widgets. Where possible, load chat, ads and similar widgets inside a sandboxed iframe so they cannot touch your main page. On payment pages, use your payment provider’s hosted fields or frames. Researchers noted the Optimizely malware could not reach card details entered into hosted frames from providers such as Stripe and Adyen, but could target details typed into a site’s own fields.
Keep third-party code off your most sensitive pages. Your login and checkout pages should load as few external scripts as possible. If a marketing tool is not needed there, do not load it there.
Step 4: Control who can change what
Many incidents start with access, not code. Make sure that:
- Only a small, named group can edit scripts that run on production pages.
- Accounts for tag managers and testing tools use multi-factor authentication.
- API tokens are scoped narrowly and rotated regularly.
- Changes to third-party tags go through review, even if the tool does not require it.
Optimizely’s attackers abused a field that lets customers run custom JavaScript across pages. Features like that deserve the same scrutiny as your deployment pipeline.
Step 5: Monitor for change
Assume that something will eventually go wrong and make it detectable:
- Watch for script changes. Tools can hash third-party scripts regularly and alert you when they change unexpectedly.
- Collect CSP and browser error reports and review spikes.
- Run synthetic checks that load key pages from a clean browser and flag new domains or unusual network calls.
- Monitor for fake or lookalike pages that copy your brand.
If you handle card payments, note that PCI DSS 4.0 requires organisations to manage and monitor the scripts on payment pages (requirements 6.4.3 and 11.6.1), so this is also a compliance topic.
Step 6: Ask vendors the right questions
Before adding a script, ask:
- How do you secure the systems that publish code to my pages?
- Do you support MFA, audit logs and granular permissions?
- How quickly do you notify customers of a security incident?
- Can you serve scripts from a pinned version, or are they always updated?
- Have you had incidents before, and what changed afterwards?
The answers will not guarantee safety, but they reveal how seriously a vendor treats the risk.
Step 7: Prepare an incident plan
Write down, before you need it, how you would respond if a vendor script turned malicious. The plan should cover how to disable the script quickly (a kill switch in your tag manager is ideal), who decides, how you notify users and regulators, and how you rotate credentials. In the Optimizely case, security firms advised customers to review injected code, rotate tokens and check their logs, which is a sound template.
A quick checklist
- Inventory of third-party scripts on sensitive pages
- Unused scripts removed
- CSP in report-only, then enforced
- SRI on static libraries
- Hosted payment fields on checkout
- MFA and tight permissions on tag and testing tools
- Alerts on script changes
- Vendor security questions answered and recorded
- Incident plan with a tested kill switch
The goal is not zero third-party code, which is unrealistic. It is knowing what you run, limiting what it can do, and finding out fast when something changes.
Was this article helpful?
Comments
Comments are reviewed before they appear. Links are displayed as plain text and are not clickable.




