Skip to content
FunnelGuard

Fix guide by FunnelsGuard

Compromised npm or CDN library serving malicious code: fix it

OPS-197High impactOther toolBy FunnelGuardPublished Updated
On this page

What this issue is

Most websites load code they did not write. It comes from npm packages bundled into your build, or from a CDN (content delivery network, a server that hosts shared files) through a <script src> tag. This is called your supply chain.

A compromise happens when an attacker takes over a package maintainer account, publishes a poisoned version, or takes control of a CDN domain or file. Your site then serves the attacker's code to every visitor, with the same access as your own scripts. That code can read form fields, redirect visitors, inject fake fields or send data elsewhere.

How a poisoned library reaches your visitors
  1. Library publishedPackage or CDN file is trusted
  2. Attacker alters itStolen account or hijacked domain
  3. Your page loads itNew build or live CDN request
  4. Visitor affectedRedirects, skimming, fake forms

The attacker never touches your server. They change code you already trust.

Why it costs you leads or sales

Malicious code can redirect visitors to other sites, swap your form for a copy, or siphon off personal and payment details. You lose conversions while it runs, and you may also face browser or search engine warnings, ad account suspensions, data breach duties and lost customer trust.

It is often silent. Pages look normal to you, because the code may target only some countries, devices or visitors who came from ads.

Example: how a compromise can unfold
  1. Day 0

    A library you load from a CDN is altered.

  2. Day 1

    Some visitors are redirected or see fake fields. Your own tests look fine.

  3. Day 5

    Conversion rate drops; customers report odd behaviour.

  4. Day 6

    Security notice published. You remove or pin the library.

How to check if your site has it

Audit your scripts and dependencies
  1. List every external script

    Open your page, view source and search for <script src=. In Chrome DevTools, the Network tab filtered to JS shows each domain. Note anything on a public CDN.

  2. Run a dependency auditCustom code

    In your project run npm audit (or yarn audit, pnpm audit). It lists known vulnerable or malicious package versions.

  3. Check your lockfileCustom code

    Review package-lock.json for versions that changed unexpectedly in recent commits.

  4. Read the advisories

    Search the package name in the GitHub Advisory Database and the library's issue tracker for reports of a hijack.

  5. Compare what is served with what you expect

    Download the live file with curl and compare its hash with a known good copy.

Check a CDN file's hash (SRI format)bash
curl -s https://cdn.example.com/lib@1.2.3/lib.min.js | openssl dgst -sha384 -binary | openssl base64 -A

How to fix it

Contain, replace, lock
  1. Remove or disable the affected script

    Take it off the page now. A missing widget costs less than a malicious one.

  2. Replace with a known good version

    Install a version published before the compromise, or switch to a trusted alternative. Rebuild and redeploy.

  3. Pin exact versionsCustom code

    Use exact versions in package.json (no ^ or ~) and commit the lockfile. Install in CI with npm ci.

  4. Add SRI or self-host

    Add an integrity hash to CDN scripts so the browser refuses a changed file, or host the file on your own domain.

  5. Rotate secrets

    If the script could reach tokens, API keys or admin sessions, rotate them and force logouts.

Script tag with Subresource Integrityhtml
<script src="https://cdn.example.com/lib@1.2.3/lib.min.js"
        integrity="sha384-REPLACE_WITH_YOUR_HASH"
        crossorigin="anonymous"></script>
Where to remove or replace the script by platform
  1. Find the source

    Check Plugins and your theme for the script. Update or deactivate the plugin that loads it. Look in functions.php for wp_enqueue_script calls with external URLs.

  2. Self-host

    Download a verified copy into your theme folder and enqueue it locally.

Example report-only CSP headertext
Content-Security-Policy-Report-Only: script-src 'self' https://www.googletagmanager.com https://cdn.example.com; report-uri /csp-report
Loading third-party code

Before

  • Script loaded from a CDN with a floating version like @latest
  • No integrity hash
  • Nobody reviews dependency updates

After

  • Exact version pinned and lockfile committed
  • SRI hash or self-hosted file
  • CSP allowlist and update reviews

How to confirm it's fixed

Confirmation checklist
  • The affected script no longer appears in view source or the DevTools Network tab
  • npm audit reports no known malicious or critical packages
  • CDN scripts carry integrity hashes and load without console errors
  • Forms and checkout work and show no unexpected fields or redirects
  • CSP report-only logs show no unknown script domains
  • Passwords, keys and tokens that could have been exposed are rotated

How to prevent it happening again

Prevention habits
  • Pin exact versions and commit lockfiles
  • Use npm ci in builds and review dependency update pull requests
  • Add SRI to every third-party script that supports it
  • Self-host critical libraries, especially on checkout pages
  • Keep a written list of approved script domains
  • Enable a CSP and review its reports
  • Remove libraries you no longer use
  • Turn on two-factor authentication for your npm, GitHub and CDN accounts

How FunnelGuard handles this

This issue is outside what FunnelGuard checks. FunnelGuard runs your real customer journeys on a schedule and checks each step, tracking hits and test lead delivery. It is not a code scanner and does not inspect the contents of your scripts or dependencies.

The kinds of tool that fit are a software composition analysis or dependency scanner (such as npm audit, GitHub Dependabot or similar) for packages, and a client-side script monitoring tool or CSP reporting for what runs in visitors' browsers. A broken form or checkout step caused by a bad script is a separate symptom that journey monitoring can show, but that is not the same as finding the compromise.

Questions people ask

How do I know if an npm package I use has been compromised?

Run npm audit and check the GitHub Advisory Database and the package's issue tracker. Also review your lockfile for versions that changed unexpectedly. Enable Dependabot or a similar scanner to get alerts automatically.

What is Subresource Integrity (SRI) and does it stop a compromised CDN?

SRI adds an integrity hash to a script tag. The browser compares the downloaded file with the hash and refuses to run it if it differs. It protects you if the CDN file is changed, but only for files that never change legitimately, so pin an exact version.

Why is my website redirecting visitors to another site?

A common cause is a third-party script or tag that has been changed to inject redirects, including a compromised CDN library. Check external scripts, Custom HTML tags in your tag manager and plugins, and remove anything unfamiliar.

Should I self-host JavaScript libraries instead of using a CDN?

Self-hosting removes the risk of the CDN file changing without your knowledge, as long as you download from a trusted source and update deliberately. It is a good choice for critical pages like checkout. You then own the job of applying security updates.

What should I do first if a library I use was compromised?

Remove or disable the script, then replace it with a known good version and redeploy. Rotate any keys or sessions it could reach and check whether visitor data was exposed. Review legal notification duties if forms or payments were affected.

Does a Content Security Policy prevent supply chain attacks?

It limits them. A CSP with a script-src allowlist blocks scripts from domains you did not approve, but a compromised script on an allowed domain can still run. Combine it with SRI, pinned versions and dependency scanning.

Can FunnelGuard detect a compromised library?

No. FunnelGuard checks journeys, tracking hits and lead delivery, not the contents of your scripts. Use a dependency scanner and a client-side script monitoring tool for this.

Does FunnelGuard catch this?

Other tool

Needs a different kind of tool.

Stop finding out from your customers

FunnelGuard runs your real forms and checkouts on a schedule, checks the tracking and the leads behind them, and alerts you once, with a screenshot. Try any paid plan free for 3 days.