npm Vulnerability
Two mechanisms keep a vulnerable version out, answering two different questions. **The audit
answers what is known to be bad. The quarantine answers what nobody has looked at yet.**
Neither covers the other's ground.
The order of work when `npm audit` reports
Take the steps in this order. A later step is reached only for what the earlier ones left.
1. Raise the declared dependencies within their minor range
Raise each declared entry to the newest minor or patch release its own range permits, as the
dependency-raising convention does, and never across a major. Read the audit after its
single install, never before. Part of what the audit reported may already be fixed by a newer
release.
- Measured: raising one declared entry by a single patch release took a report from four
vulnerable copies to two, with nothing else changed.
2. Raise the transitive dependency with `overrides`
An advisory usually names a package the project never asked for. Where the vulnerable
package is a dependency of a dependency, raise it through overrides in package.json.
"overrides": {
"alpha-package": "^3.4.4",
"beta-package@2": "^2.3.2"
}
- Write it in
overrides, never leave it tonpm audit fix. That command raises the
version in package-lock.json alone, and the record of why disappears with it. A fact held
only in a generated file cannot be read by anyone. overrides states it in the manifest.
- The direct dependency stays where it is. Its version becomes a separate decision, taken
later on its own merits.
- A key scoped to a major line (
name@major) touches only that line. An unscoped key
moves every copy, at every depth, whatever each dependant declared.
- Go to the dependant's source and read what it calls. A range says which version was
asked for; only the call says whether the version delivered answers. Where the call site
survives, leave the key unscoped; where it does not, scope the key to the major line the
advisory names. Neither the package's reputation nor the size of the jump settles it.
- Safety outweighs staying within a major. Where the fix lies in a major release that the
declared range excludes, take it: raise the declared range for a direct dependency, or write
the override for one reached through another package. Whether a major was crossed does not
decide this; whether the raise makes the project safe does. This is the exception to the
dependency-raising convention's rule that a major is a decision of its own, and it holds for
the fix of an advisory only.
- The raise holds only while the tests and the lint pass. Run both after it. Where either
fails, the fix cannot be taken as it stands, and the report goes to step 4.
3. A fix exists but is younger than the quarantine
An override names a version, and that version is subject to the quarantine like any other.
Read when the fix was published from the registry before writing the override, not after an
install fails.
- Where the fix is younger than the quarantine, waiting is not an option. The audit keeps
failing until the fix is taken, and a pull request cannot merge while it fails. Severity
does not decide this; whether the pull request can merge does.
- **Confirm that the diff of that package is safe, and take the fix as an exception with the
--min-release-age-exclude route /hoc-npm-install defines.** The quarantine stays on
every other package.
--min-release-age=0is never used, for any reason. See/hoc-npm-install.
4. No release fixes the advisory
An override needs a version to raise to. **Where the advisory names no patched release, or
the fix cannot be taken — not even through the exception of step 3, or because the tests or
the lint fail after the raise — the report is not resolved. It is decided, and the user
decides it.** Report the advisory and the manifest field its package is reached through, and
present the two ways: listing it, or reworking the code.
- Answer from the paths, not from the package's name.
npm explain <package>lists every
route by which the package is in the tree, and one route through dependencies or
peerDependencies is enough for the package to count as shipped.
| The vulnerable package is reached through | Way forward |
| :-- | :-- |
| devDependencies only | List it |
| dependencies or peerDependencies, directly or through another package | The user chooses between listing it and reworking the code |
- Severity does not decide any of this. What decides it is that the pull request cannot
merge while the audit fails.
- Listing is the only way to merge without reworking the code. It is a stopgap and not a
resolution. In a shipped dependency it leaves the vulnerable code in what is deployed, so the
report to the user says that in plain words.
- Reworking the code always needs the user's explicit go. It costs much, so the agent
never starts it on its own. Where the package is reached only through another package, the
application code cannot be reworked to avoid it, and listing is the way left.
- List it in
scripts/stopgap-audit/IGNORED_ADVISORIES.js. That takes the advisory out of
what fails the audit, for the time being. Each entry carries its reason and the condition
for removing it.
- Always report it to the user and obtain approval before listing an entry. This is never
decided alone.
- A listing is revisited later, and says so in the open. Propose an issue with no parent
issue that names the advisory, the package, why it was listed and the condition for
removing it, and file it once the user has approved the text. A listing without such an
issue is forgotten.
- The list cannot outlive its reason. An entry whose advisory is no longer reported fails
the audit. A patched release ends the entry: from then on the report is an ordinary one,
resolved by step 2. The entry is removed rather than renewed, and the issue is closed.
Why the audit alone is not enough
npm audit reports registered advisories, and nothing else. An advisory exists after
somebody notices, files it, and it is accepted, so every compromised package has an interval
in which the audit called it clean. "No vulnerabilities" means "no advisory today", one
check that passed and not a conclusion.
What the audit misses by construction — a version too new for anyone to have looked at — is
what the quarantine covers: a release-age setting refuses any package published more
recently than a given number of days, so that somebody else meets a bad publish first. The
setting is a line of .npmrc, written as /hoc-npm-npmrc says.
The quarantine applies to a resolving install and not to a lockfile install.
| Command | Subject to the quarantine |
| :-- | :-- |
| A resolving install | Yes — every candidate's age is checked |
| A lockfile install (npm ci) | No — the versions are already pinned |
- Once a version is pinned into the lockfile, every later lockfile install accepts it. The
quarantine is enforced at the moment of resolution, by whoever runs it. Review the
resolution, not the installs after.
Measure an audit by installing, never from the lockfile alone
An audit run against the committed lockfile reuses the pins that lockfile holds, so it
reports the vulnerabilities of the old resolution, and can report clean where a fresh
resolution would not. A real install re-resolves and can give a different answer entirely.
A measurement of an audit that did not install is not a measurement. Install, then read
the report.