Topic 5 of 5 · 5 min read
Defences against phantom dependencies
Goal: Design a set of checks that stops hallucinated APIs and packages from reaching CI, and say which mistake each check catches.
No single check stops phantom dependencies, so stack cheap checks where each one catches a different mistake: made-up APIs, made-up packages, old versions and unsafe installs. The goal is that a model's wrong guess fails early, in a place a person or a machine will see it.
Check yourself · 1 of 3
Which check best catches an invented method on a typed library?
Why it matters
You cannot make a model stop guessing. You can make guesses visible and expensive to ship. A layered setup means a hallucinated method is caught by the compiler, a hallucinated package by the dependency rules, and drift by CI warnings, with no one relying on a tired reviewer spotting a plausible name.
The checks, and what each one catches
| Check | Catches |
|---|---|
Strict type checking, no new any or type: ignore |
Invented methods, parameters and option keys |
| Tests that assert the effect of a call | Options that are silently ignored |
| Lockfile diff reviewed in every pull request | New direct and transitive packages |
npm ci or hash-checked pip installs in CI |
Installs that differ from the reviewed lockfile |
| Allow-list or private registry proxy | Packages nobody on the team has approved |
| Package age and maintainer checks in CI | Newly registered names that match a model's guess |
| Deprecation warnings as errors | Version drift |
| Pinned versions and docs in the agent's instructions | Guessing at the root |
ignore-scripts where builds allow it |
Install-time code execution |
| Install permission rules for the agent | Unreviewed installs during a run |
How to roll it out
- Start with the free ones: strict types, lockfile review,
npm ciin CI. - Add a rule that any pull request touching a manifest or lockfile needs a named human approver.
- Add an allow-list or a proxy that only serves approved packages, with a short path for requesting a new one so people do not bypass it.
- Add a CI job that fails when a new package is younger than a set number of days or has no source link.
- Put the pinned versions, docs links and "ask before installing" in the agent's project instructions.
Before and after
# Before: install whatever resolves
- run: npm install
- run: npm test
The CI job resolves new versions on every run and trusts whatever the agent put in the manifest. After:
- run: npm ci --ignore-scripts
- run: npm run typecheck
- run: npm test -- --deprecation-errors
The install must match the lockfile, scripts do not run, and types and tests run before merge.
What goes wrong
- One layer only. A type checker does nothing about a real-looking malicious package.
- A policy nobody can use. If requesting a package takes a week, developers work around the block.
- Checking only at merge time. An agent on a laptop has already run the install.
- No owner. Allow-lists rot unless someone reviews them.
Examples
A small team's minimum setup
A four-person team adds three things in one afternoon: strict TypeScript with no new any, npm ci --ignore-scripts in CI, and a rule that lockfile changes need a second reviewer. None of it needs a new tool, and it covers invented APIs, unreviewed packages and surprise install scripts.
The registry proxy
A larger company routes installs through a proxy that only serves approved packages. When an agent suggests an invented name, the install returns not found instead of reaching the public registry. A form asks a security engineer to approve new names, with a one-day target.
Key points
- Layer cheap checks, each aimed at a different mistake: invented APIs, invented packages, old versions, unsafe installs.
- Types and effect-asserting tests catch made-up APIs; lockfile review and allow-lists catch made-up packages.
- CI should install exactly what the lockfile says, using
npm cior hash-checked pip installs. - Give developers a fast way to request a new package, or they will bypass the block.
- Checks at merge time are too late for an install an agent already ran on a laptop, so also set agent permission rules.
Common mistakes
- Relying on a single check, such as a reviewer's eye, to stop every phantom dependency.
- Blocking new packages without offering a quick request path.
- Running the checks only in CI, after the agent has already installed on a developer machine.
- Setting an allow-list once and never reviewing it.
Check yourself · 2 of 3
Why does CI use npm ci instead of npm install?
Check yourself · 3 of 3
A team blocks all new packages, and requests take two weeks. What is the likely result?
Practice items, not counted towards your mastery.
Start here
- We Have a Package for You! Package hallucinations by code-generating LLMs (opens in a new tab)PaperFree
The study that measured how often code models invent package names, across languages and models.
- Slopsquatting: how AI hallucinations are fueling a new class of supply chain attacks (Socket) (opens in a new tab)ArticleFree
Explains the attack in plain terms and what defenders can do about it.
- Concise Guide for Evaluating Open Source Software (OpenSSF) (opens in a new tab)ArticleFree
A short checklist for judging whether a package is maintained, trusted and the one you meant.
More for this topic
- npm ci (npm docs) (opens in a new tab)DocsFree
Installs exactly what the lockfile says and fails on a mismatch, which CI should use.
- Secure installs (pip docs) (opens in a new tab)DocsFree
Hash-checking and pinned installs for Python projects.