Go straight to an AI summary of this blog post instead

  • Summarize this post with Perplexity
  • Summarize this post with Gemini

In modern software development, we have moved from a strictly supply-chain installation model to a hybrid model. Dependencies can now be obtained from two paths: private and public packages. This article is going to focus on dependency confusion and how it relates to deep transitive dependency attacks.

The Hybrid Supply Chain Model

 

Today, we have two different ways of installing packages via various tools like npm, pnpm, etc. The concept of a single way to install packages is a thing of the past. Years ago, we would just open our terminal up and type npm install <package>. It would get pulled from the npm registry, and that would be that. Private packages, or scoped packages, can be delivered from the npm registry and are secure by default. You can also host scoped packages in internal registries like Nexus, Artifactory, and Verdaccio.

A scoped package looks like this, @myorg/mypackage. It starts with the “@” symbol followed by the name of your org, and ends with a “/” and the package name. This is a secured, private package hosted within the npm registry. This also offers you the ability to group packages under your organization. You can still install public packages that are open source as well. You are almost certainly going to install a public package in order to build out functionality. Therefore, you will have a mix of both public and private packages.

The vulnerability creeps its way in when using public, open source packages or having an improper CI/CD configuration. Package managers were designed for convenience. That convenience has a cost. Those package managers will prioritize higher version numbers over source origin. Then things start to snowball.

 

What Is a Dependency Confusion Attack?

 

Dependency confusion attacks target the way modern package managers resolve dependencies across multiple sources. In environments where both private and public registries are used, this resolution process can introduce unexpected risk.

What appears to be a routine dependency install can instead become an entry point for malicious code – especially when internal packages are not properly isolated or scoped.

How Dependency Confusion Works

 

A dependency confusion attack (or namespace confusion) exploits a flaw in how package managers resolve dependencies across multiple registries. It occurs when a build system is configured to pull from both a private (internal) registry and a public registry (like npmjs.com).

If an attacker identifies the name of a private, unscoped package and publishes a malicious package with the exact same name to the public registry using a higher version number, the package manager will resolve the public, malicious version as the “best match,” silently injecting it into the supply chain. This happens because package managers prioritize higher versions across matching names.

In practice, package managers prioritize the highest version that satisfies the semver range, as stated above. If your package.json asks for ^1.2.0 of internal-tool, and the attacker publishes v99.0.0 to npmjs.com, the manager assumes the public version is a legitimate update and pulls it.

Another common scenario occurs when an organization does not use scoped packages. The package was published to the private registry as auth-package, but there is also a package with the same name in the public registry. The registry will pull the one with the highest version number unless specified in the .npmrc file.

Transitive Dependency Breakdown

 

A transitive dependency is a package that wasn’t intentionally installed by you, the developer, but comes along for the ride when you install a direct dependency for a project. Transitive dependencies can be hiding multiple levels deep in the dependency tree, making it difficult to pinpoint where security flaws stem from.

“Most cybersecurity incidents were linked to unapplied patches. 60.4% of incidents occurred while a patch was available but was not applied.”

 

Staying ahead of the game and applying patches when they are due is critical to combating transitive dependency security flaws in the software supply chain. An example of what a transitive dependency looks like is shown below.

rxjs is a direct dependency of Angular v20, but if you were to dig inside the node_modules folder and find the rxjs folder, you will see inside the package.json the tslib package. This package was not installed by you, but came along for the ride within the rxjs package, inside the Angular v20 project.

Anatomy of a Dependency Confusion Attack

 

Let’s take a basic user interface component that you want to install in a project, a simple button. This button is coming directly from your org’s design system in a component library.

Not much to it. Background, font with text, and rounded corners. Whatever is built into the design system is what this button is. The code is built with React and looks like this:

 

export const DemoButton = () => {

  return (

    <button style={{ backgroundColor: 'blue', color: 'white' }}>

      Secure Scoped Action

    </button>

  );

};

 

We install it via npm, import it in our project, and there we go. A working button in our UI. On the other hand, though, there is another fancy-button out there, and that button is either not scoped in the private registry, or it is scoped, but a developer installs the public-facing one. The public-facing one is the malicious one that triggers alerts on the homepage, intercepts credentials in the background, or inverts the colors of the site, either right away or after a specific time.

The code for this button looks like this, which you may not have reviewed:



import React, { useEffect } from 'react';



export const DemoButton = () => {

  useEffect(() => {

    document.body.style.filter = 'invert(100%)';

    alert("Hacked via Unscoped Dependency!");

  }, []);




  return <button>Click for Update</button>;

};

 

Chances are, this button wouldn’t come along as a direct dependency. In the example code, I am showing it as a direct because I don’t have a full design system built out with multiple components. If you install an unverified UI library or a package that serves as a dependency for your design system, a malicious button could be introduced transitively.

Whether introduced directly or transitively, these dependencies become part of your application’s trust boundary – and a potential attack surface if left unchecked.

pre and postinstall Attacks

 

Attackers use pre and postinstall scripts in order to execute malicious payloads before a developer even has time to run the code. The way a preinstall script works is that before any package files are put into your folder, the script may perform environment checks to determine whether to execute malicious behavior. If the script detects a hardened or monitored environment, it phones home silently and stays dormant to avoid detection.

The postinstall script waits for the door to be unlocked and then a whole plethora of things can happen. A postinstall script can steal secrets or, in the words of E.T., phone home and let the server know the door is wide open and anyone can come in. You can relate this to vampire lore. Once you invite a vampire in, it is open season.

Once the script is inside your node_modules, it’s too late – it executes immediately. The script runs a command like curl or wget to send your .env files, SSH keys, or AWS tokens to the attacker’s site. Once they have these credentials, there isn’t much you can do besides delete your keys/tokens and regenerate new ones.

Dependency Confusion Remediation

 

There are a few different ways we can stay ahead of these issues and move to a secure, patched project. 

Scoped Packages

The most clear way to stay away from a dependency confusion attack is by registering your organization with the package manager and using scoped packages, @myorg/mypackage. This provides a secure, private package that can be used within your own projects across the org.

Proper .npmrc Configs

When creating your .npmrc file, make sure to strictly map specific scopes to private registries.  By ensuring they are strictly mapped, it will stop them from being “leaked’ to the public registry. An example of an .npmrc file set up to do this is as follows, and is the most effective way of combating issues within your project:

 

# Set the default registry for public packages

registry=https://registry.npmjs.org/




# Force all @org scoped packages to resolve ONLY via the private registry

@org:registry=https://npm.pkg.github.com/

always-auth=true

Using –ignore-scripts

Another clever way to get away from the preinstall and postinstall scripts is to use –ignore-scripts when installing packages. Using that flag looks like this: npm install <new-package> –ignore-scripts. This isn’t just something that is used once; you want to bake this into your automation and workflows as well.

Lockfile Integrity

Your package-lock.json or yarn.lock does more than just pin versions; it stores a hash, a cryptographic hash. If the hash changes and the build fails, that tells you an attacker swapped the package out on the registry. Using npm ci enforces the lockfile. With npm install, this protection can be bypassed.

Secure the Stack

Taking all of these things into account, make sure that you follow a strict security checklist. These things are only a part of it.

 

  1. Use scoped packages and make sure you register your org with the registry
  2. Configure your .npmrc file to strictly map your packages to the private registry
  3. Use –ignore-scripts in your CI/CD pipelines to disable preinstall and postinstall scripts by default

 

Supply chain security isn’t a ‘set it and forget it’ task like a Ronco rotisserie oven; it requires constant vigilance as your project evolves. It is something that has to be taken care of from the beginning and well into the future as the project evolves.

Lock down your registries, verify your scopes, and monitor what’s happening deep in your dependency tree. The biggest risks aren’t always visible – but they’re often already in your stack. In the same vein as the vampire not being invited inside metaphor, I leave you with this quote from The Lost Boys, “One thing about living in Santa Carla I never could stomach; all the damn vampires.” Santa Carla is the software, and the vampires are the security flaws. Stay ahead of the game and don’t let them in.

 

Summary
Dependency Confusion Attack – How It Works & How to Prevent It
Article Name
Dependency Confusion Attack – How It Works & How to Prevent It
Description
Learn how dependency confusion attacks work, how they exploit npm and transitive dependencies, and how to secure your software supply chain.
Author
Publisher Name
TuxCare
Publisher Logo

Join 4,500+ Linux & Open Source Professionals!

2x a month. No spam.

Table of Contents
Get the open-source security answers you need

Join 4,500+ Linux & Open Source Professionals!

2x a month. No spam.