JavaScript supply chain attacks in ecommerce: risks and defenses
You did not add malicious code to your checkout page. Your developers did not either. The code was added by a script you trusted, loaded from a domain you approved, maintained by a company that got compromised six months ago. This is a JavaScript supply chain attack.
These attacks are one of the most effective ways to steal payment data from ecommerce stores. They are hard to detect, run silently for months and exploit the fundamental trust model of the modern web: scripts from third-party vendors run with the same permissions as your own code.
What a JavaScript supply chain attack is
A supply chain attack targets the dependencies your application trusts rather than your application directly. In the JavaScript context, this means an attacker compromises a library, script or service that your site includes. When your site loads that compromised script, the attacker gains the ability to execute arbitrary code in your users' browsers.
For ecommerce stores, this is particularly dangerous on checkout and payment pages. A compromised script on a payment page can capture credit card numbers, CVVs, billing addresses and authentication credentials as users type them. The data is exfiltrated to an attacker-controlled server. The payment page looks and functions normally. Customers have no indication anything is wrong.
Real-world examples
Supply chain attacks on JavaScript dependencies are well-documented and escalating in frequency:
- event-stream (2018). A malicious contributor took over maintenance of the popular event-stream npm package and added code that targeted a specific Bitcoin wallet application. The package had 2 million weekly downloads. The attack was specific, but it demonstrated how easily a widely-used npm package could be weaponized.
- ua-parser-js (2021). The npm package ua-parser-js, which parses User Agent strings and had over 7 million weekly downloads, was compromised when an attacker used credentials obtained through another breach to publish malicious versions. The compromised versions installed cryptominers and credential-stealing malware. The package was used by Facebook, Microsoft and hundreds of thousands of other projects.
- Codecov (2021). Attackers modified Codecov's Bash uploader script hosted on the company's servers. Any CI/CD pipeline that downloaded and ran the script exfiltrated environment variables, including secrets and API keys. Codecov had over 29,000 customers. Affected companies included Twilio, HashiCorp, Rapid7 and Confluent.
- Polyfill.io (2024). The polyfill.io domain and its CDN infrastructure was sold to a Chinese company that promptly began injecting malicious JavaScript into the polyfill bundle. Over 100,000 websites that loaded scripts from polyfill.io were affected. The malicious code redirected mobile users to scam sites and collected data. Many affected sites did not know they were loading from polyfill.io, having inherited the dependency from a theme or plugin.
- Magecart group attacks. The Magecart collective is a loose group of attackers specializing in ecommerce skimming. Their attacks have compromised British Airways (380,000 cards), Ticketmaster (9 million customers), Newegg and thousands of smaller stores. They typically compromise third-party analytics or support scripts that load on checkout pages.
How these attacks target ecommerce specifically
Ecommerce sites are particularly attractive targets for supply chain attacks because they are the point where financial data is entered. The attack chain has several common patterns:
- Compromised analytics scripts. Google Tag Manager, Segment, Mixpanel and similar analytics tools load on virtually every page including checkout. If any of these are compromised or if a tag within GTM is modified, the attacker has access to the entire page including payment form inputs.
- Chat and support widgets. Live chat tools like Intercom, Zendesk, Drift and Freshchat are common on ecommerce sites. Many merchants leave these scripts loading on checkout pages even though customers rarely need live chat support during payment. Each of these is a potential attack surface.
- A/B testing and personalization tools. Tools like Optimizely, VWO and Dynamic Yield have broad access to page DOM and are designed to manipulate page content. A compromised version of these scripts could overlay a fake payment form, add keyloggers or replace the legitimate payment processor iframe with a fake one.
- Session replay tools. Hotjar, FullStory, Microsoft Clarity and similar tools record user sessions. They run on checkout pages with deep access to form interactions. Some have had vulnerabilities that exposed form data or allowed injection attacks.
- CDN-hosted scripts. When you include a script from a third-party CDN (cdn.example.com/analytics.js), you are trusting not just the vendor but also that specific URL, that CDN infrastructure and that vendor's update process. If any of those are compromised, your site loads the malicious version automatically.
The attack chain walkthrough
Here is how a typical supply chain attack on an ecommerce checkout plays out:
An attacker identifies a popular analytics company used by thousands of ecommerce stores. They target the analytics company's build infrastructure or CDN. They compromise a developer account at the analytics company via credential stuffing using previously leaked credentials. Using that access, they add a small payload to the analytics script: a function that detects payment form fields (looking for inputs with names like "cardnumber," "cvv," "expiry" or iframes from known payment processors) and captures their values on form submission or input change events.
The payload exfiltrates captured data via a fetch() call to a domain the attacker controls, typically mimicking a legitimate domain name (analytcs-cdn.com instead of analytics-cdn.com). The payload is obfuscated to avoid detection. It only activates on pages that contain payment fields, reducing the chance of detection during routine testing on non-payment pages.
The compromised script is deployed via the analytics company's normal update pipeline. Thousands of ecommerce sites that load the script from the analytics company's CDN automatically receive the malicious version. No action is required on their part. The attack runs until someone notices anomalous traffic to the exfiltration domain, until a card fraud pattern is detected by a card brand, or until the analytics company discovers and remediates the compromise.
Detection often takes 90 to 180 days. By then, hundreds of thousands of cards may have been compromised.
Vendor risk assessment
Not all third-party scripts carry equal risk. Assessing vendor risk helps prioritize where to focus monitoring and controls:
- Frequency of updates. A script that updates daily is a more active attack surface than one that is static. Frequent updates mean more opportunities for a malicious update to be introduced and less time to detect it before it reaches your users.
- Size of vendor organization. Larger organizations typically have more robust security practices. A script from a one-person side project hosted on a personal server carries more risk than a script from a SOC 2-certified company.
- Access level. Scripts that manipulate DOM elements, intercept events or have access to form data are higher risk than scripts that only send network requests to analytics endpoints.
- Delivery mechanism. Scripts served from the vendor's own CDN with no integrity checks are higher risk than scripts pinned to a specific version hash using Subresource Integrity.
- Incident history. Check whether the vendor has disclosed security incidents in the past. A vendor that has experienced a breach and handled it well is often more trustworthy than one with no disclosed incidents but no transparency about their security program.
Script inventory management
You cannot protect what you cannot see. The first defensive step is knowing every script that loads on your payment pages.
Build and maintain a script inventory for your checkout and payment pages. For each script, document:
- The script URL and source domain.
- The business purpose (why is this script on the payment page specifically?).
- Who approved it and when.
- The vendor's PCI DSS or SOC 2 compliance status.
- Whether the script is pinned to a specific version or always loads the latest.
- The script hash at time of last review.
Review this inventory monthly. Any script that cannot justify its presence on a payment page should be moved to other pages or removed.
Content Security Policy as a defense
Content Security Policy (CSP) is an HTTP header that restricts which resources your pages can load. A well-configured CSP is one of the most effective defenses against supply chain attacks.
A CSP with a strict script-src directive will block any script that loads from a domain not on your whitelist. If an attacker substitutes a script from a new domain, the browser blocks it before it executes.
The challenge is that CSP is difficult to implement on pages with many third-party scripts. Common blockers include:
- Google Tag Manager requires wildcard directives that significantly weaken CSP protections.
- Third-party scripts frequently load sub-resources from additional domains not explicitly approved.
- Inline scripts (JavaScript directly in HTML) require either unsafe-inline or nonce-based approaches.
Despite these challenges, even a partial CSP is better than none. Start with report-only mode to capture violations without breaking functionality. Then progressively tighten the policy over several weeks.
Subresource Integrity
Subresource Integrity (SRI) allows you to specify an expected cryptographic hash for a script. If the script's content does not match the hash, the browser refuses to execute it.
SRI is effective against attacks where an attacker modifies a script file at its source URL. If the analytics company's CDN serves a modified script, the hash will not match and your site will not execute it.
The limitation of SRI is that it breaks when scripts update legitimately. Scripts pinned to a specific version via SRI will break when the vendor releases a new version. This creates a maintenance burden: someone must update SRI hashes when vendors release updates.
SRI is most practical for scripts that are version-pinned (loading from a URL like /analytics/v2.3.1/script.js rather than /analytics/latest/script.js). For scripts that auto-update via a "latest" URL, SRI is not operationally feasible without automation.
Monitoring tools
Automated monitoring is essential for detecting supply chain compromises quickly. Tools that provide browser-based monitoring of your payment pages can detect:
- New scripts that appear on monitored pages.
- Changes to existing script content (hash drift).
- New network requests made by scripts (potential exfiltration endpoints).
- Changes to HTTP security headers like CSP and HSTS.
- Hidden iframes or dynamically injected elements.
PCI DSS 4.0 requirement 11.6.1 mandates this kind of monitoring for payment pages, checked at least weekly. Commercial solutions include c/side, Reflectiz and Feroot. Building this capability in-house requires a headless browser setup that regularly crawls your payment pages and diffs the results.
Incident response
If you detect a supply chain compromise on your payment pages, the response must be fast. Every hour the compromised script runs, more card data is exfiltrated.
- Immediate removal. Remove or block the compromised script immediately. If you cannot remove it cleanly, take your payment page offline temporarily. A brief checkout outage is far preferable to an ongoing card breach.
- Preserve evidence. Capture the malicious script content, network logs and timeline before removing it. You will need this for forensic investigation and regulatory reporting.
- Notify your acquirer. Under PCI DSS, you must notify your acquiring bank of a suspected breach. They will initiate the incident response process and may require a forensic investigation by a PCI Forensic Investigator (PFI).
- Assess scope. Work with your payment processor to identify which transactions occurred during the window the malicious script was present. All cards from those transactions should be considered compromised.
- Notify affected customers. State breach notification laws typically require notification within 30 to 72 hours of discovering a breach. Check your obligations in each state where affected customers reside.
- Notify the vendor. If the compromise was via a third-party vendor's script, notify that vendor immediately. They need to remediate on their end and notify other affected customers.
Practical recommendations
The most effective defense is reducing the number of third-party scripts on your payment pages. Each script removed is an attack surface eliminated. Question every script that loads on checkout: does it genuinely need to be there during payment?
Most analytics data can be captured on non-payment pages. Marketing pixels do not need to fire on checkout completion. Chat widgets can be disabled on payment pages without meaningful customer service impact.
The combination of a minimal script footprint, CSP enforcement, SRI for version-pinned scripts and weekly automated monitoring provides strong protection against the majority of supply chain attack scenarios. Our payment page security guide covers the full architecture of a secure checkout.
Frequently asked questions
- What is a JavaScript supply chain attack?
- A supply chain attack compromises a third-party library, script or service that your site includes. When your site loads the compromised script, the attacker can execute arbitrary code in your users' browsers, including capturing payment card data on checkout pages.
- How long do JavaScript supply chain attacks go undetected?
- Detection often takes 90 to 180 days. During that window, every customer who completes checkout on an affected site may have their card data compromised.
- What is the best defense against JavaScript supply chain attacks?
- Reduce the number of third-party scripts on payment pages, implement Content Security Policy in enforce mode, add Subresource Integrity hashes to version-pinned scripts and deploy automated weekly monitoring for payment page changes.
- Does PCI DSS 4.0 address supply chain attacks?
- Yes. Requirement 6.4.3 requires a script inventory and integrity verification for all payment page scripts. Requirement 11.6.1 requires automated tamper detection. Both became mandatory in March 2025.
Complyko scans your payment pages for JavaScript supply chain risks: unauthorized scripts, missing SRI, weak CSP and suspicious third-party load points. Get a full inventory of what is running on your checkout.
Get your free scan →