# When your agent runs npm install: typosquats, dependency confusion and hallucinated packages

> Coding agents add and install dependencies on their own. The supply-chain attacks that exploit that, from install scripts to package names models invent, and the controls that limit the damage.

Coding agents add and install dependencies on their own. The supply-chain attacks that exploit that, from install scripts to package names models invent, and the controls that limit the damage.

November 23, 2026 · 4 min read · The Sandscale team

Ask a coding agent to add CSV parsing to a service, and there is a good chance its next step is `npm install some-csv-library` or `pip install something`. That one line does more than download code. It runs code: install scripts, build steps, and later everything the library does when imported. The agent chose the name, often from memory, and nobody checked it.

Package registries have been a supply-chain target for years. Agents make the existing attacks easier and add one new one. This post walks through the main patterns and the controls that actually help.

## Installing is executing

Start with the uncomfortable basics.

**npm runs lifecycle scripts.** A package can declare `preinstall`, `install` and `postinstall` scripts, and npm runs them during installation. The npm docs describe these in [the scripts reference](https://docs.npmjs.com/cli/v10/using-npm/scripts). A malicious package does not need you to import it; installing it is enough.

**pip can run a build.** When pip installs from a source distribution rather than a prebuilt wheel, it builds the package, and building means running the package’s build code. pip’s own [secure installs guide](https://pip.pypa.io/en/stable/topics/secure-installs/) suggests `--only-binary :all:` to disallow source distributions, and hash-checking mode with `--require-hashes` to pin exactly what gets installed.

So when an agent installs a package, the question is not “will we use this code?” but “what can this code reach right now, on this machine?”

## Attack 1: typosquatting

The oldest trick: publish a package whose name is one typo away from a popular one, and wait. Humans make typos occasionally. Models make a related mistake: they produce names that look right, blending real package names together or using a plausible but wrong spelling.

## Attack 2: dependency confusion

In February 2021, Alex Birsan published [Dependency Confusion](https://medium.com/@alex.birsan/dependency-confusion-4a5d60fec610), describing how he got code running inside dozens of large companies, with permission through their bug bounty programs. The idea: companies use internal package names that do not exist on the public registry. If someone publishes a public package with the same name, some build systems prefer the public one. His proof-of-concept packages used install scripts to report back when they ran.

Agents add a twist. An agent reading your internal code sees internal package names, and when something fails to resolve, a helpful agent may try installing it from the public registry, which is exactly the move the attack waits for.

## Attack 3: hallucinated packages

This is the new one. Models sometimes recommend packages that do not exist. A 2024 study, [We Have a Package for You!](https://arxiv.org/abs/2406.10279) by Spracklen and colleagues, generated 576,000 code samples with 16 code-generating models and found that the average share of hallucinated packages was at least 5.2% for commercial models and 21.7% for open-source models, with 205,474 unique hallucinated names.

A name that a model invents once, it may invent again for someone else. An attacker who registers those names gets their code installed by whichever agent repeats the mistake next. This pattern is sometimes called “slopsquatting”.

## Controls that help

No single control stops all three. Layer them.

### Decide where installs happen

The strongest control is architectural: do not install anything new on a machine that matters. If the agent’s code runs in a disposable sandbox with nothing valuable in it, a malicious install script finds an empty room. Secrets the task does not need are not there, your home directory is not there, and the sandbox is deleted when the run ends.

### Split install from execution

Install dependencies from a reviewed lockfile in a setup step, then run the agent’s code with the network off. If the agent wants a new dependency, that is a change to the lockfile, which goes through review like any other change, rather than an `npm install` in the middle of a run.

### Limit the network during install

If installs must happen in the run, allow only the registry hosts the ecosystem needs, as in our post on [egress allowlists](https://sandscale.dev/blog/egress-allowlist-design-for-agent-sandboxes). A postinstall script that tries to send your environment to its own server then fails. Note that a registry host that accepts publishes is also a way out if the run holds a publish token, so keep publish credentials out of agent runs entirely.

### Turn off what you do not need

- `npm ci --ignore-scripts` skips lifecycle scripts. Some packages genuinely need them (native builds), so expect a short allowlist of exceptions.
- `pip install --only-binary :all:` refuses source builds, and `--require-hashes` refuses anything not pinned in your requirements file.
- Scope internal packages so their names cannot be claimed on the public registry, and configure your package manager so internal scopes resolve only from your internal registry.

### Check names before installing

Give your agent a tool policy: a new package name triggers a check before install. Does it exist? How old is it? How many versions and maintainers does it have? Does the name match what the agent says it does? A package published last week with one version and a name nearly identical to a popular one deserves a human look. This fits naturally as an approval step; see [which agent actions should wait for a person](https://sandscale.dev/blog/human-approvals-for-agent-tool-calls).

### Review dependency changes as code

Treat a new entry in `package.json`, `requirements.txt` or `go.mod` from an agent as one of the riskiest lines in its PR, not boilerplate. Read the name carefully, and read the lockfile diff for unexpected transitive additions.

## Where Sandscale fits

Sandscale runs each agent run in its own Firecracker microVM, so install scripts execute inside a sandbox with its own kernel instead of on a laptop or a shared CI runner, and the sandbox goes away when you stop it. Its network is limited to what your plan and your [egress allowlist](https://sandscale.dev/security) allow, so you can keep a run’s outbound traffic to the registries it needs. Secrets reach a run only if you reference them from Vault for that run.

That limits what a malicious package can reach. It does not tell you whether a package is malicious, and code inside the sandbox can still read anything you put there. Name checks, lockfile review and keeping installs out of the execution phase are still yours to build.

**On this page**

1. Installing is executing
2. Attack 1: typosquatting
3. Attack 2: dependency confusion
4. Attack 3: hallucinated packages
5. Controls that help
6. Where Sandscale fits

**Run your agents in a box of their own**

Each sandbox is a Firecracker microVM. Free includes 100 awake vCPU-hours a month.

[Start free](https://cloud.sandscale.dev/auth/signup)

## Keep reading

- [November 23, 2026 · **A checklist for reviewing a pull request an AI agent wrote** · Agent-written PRs fail in different places than human ones. A practical review checklist: what to read first, the lines that deserve the most suspicion, and what to run before you approve.](https://sandscale.dev/blog/reviewing-an-agent-diff-checklist)
- [November 23, 2026 · **A threat model for AI coding agents: what can go wrong, and where to put the controls** · A practical threat model for coding agents: the assets at risk, the ways untrusted text gets in, the attacker goals, and which control belongs at which layer.](https://sandscale.dev/blog/threat-model-for-ai-coding-agents)

---
Source: https://sandscale.dev/blog/supply-chain-risk-when-agents-install-packages
