Dependency prefixes are a supply chain risk: let’s fix them
Dependency prefixes like ^ and ~ make updates easy, but the version ranges they create widen the path a compromised package can take into production.
What’s the Problem with Version Ranges?
Most npm, pnpm, and Yarn projects treat dependency prefixes as routine syntax. A caret in ^1.2.3, a tilde in ~1.2.3, or a wildcard in 1.x often gets added without debate. The dependency installs, tests pass, and the pull request moves on.
But that syntax is policy. It controls which code your package manager fetches—often in CI, during builds, or on developer machines you don’t manage.
Version ranges have valid uses. They let teams get bug fixes without updating package.json every time. The issue is the broad trust boundary. ^1.2.3 accepts newer compatible versions. * accepts almost anything.
If a maintainer account is compromised, a package is hijacked, or a malicious update lands in a transitive dependency, a permissive range can turn the next install into a delivery mechanism.
Treat version ranges like any other production policy. They deserve the same scrutiny as CI secrets or deployment credentials. In a recent PyPI incident, pinned versions were the key to staying safe.
How to Reduce Risk: Start with Lockfiles
Commit your lockfile. For npm, that’s usually package-lock.json. In CI/CD, prefer npm ci over npm install.
npm ci requires an existing lockfile. It fails if the lockfile and package.json disagree. It removes existing node_modules and installs from a frozen dependency graph.
pnpm and Yarn have equivalents: commit pnpm-lock.yaml and use pnpm install –frozen-lockfile, or commit yarn.lock and use yarn install –immutable.
Lockfiles don’t make the dependency tree safe forever. But they make installs repeatable—essential for audits or incident response.
If a malicious version appears upstream, the lockfile stops the next install from pulling it in silently.
It also makes version changes visible. Because the lockfile records exact versions and lives in version control, any change shows up as a reviewable diff.
Pin High-Risk Dependencies
For high-risk packages, remove prefixes and pin exact versions:

"react": "19.2.6""react-dom": "19.2.6"
Exact pins shift update responsibility from npm to you. That increases your workload. So use them only where the blast radius justifies the cost.
Save exact pins for: build tools, install-time packages, authentication libraries, deployment tooling, packages with native bindings, and dependencies that run in privileged CI contexts.
The React ecosystem is a good example. When a critical React Server Components vulnerability affects many packages, exact pins show which apps are already updated and which need action.
Reduce Accidental Range Creation
Prevent new installs from adding permissive ranges by default:
npm config set save-exact truepnpm config set save-exact trueyarn config set defaultSemverRangePrefix ""
For one-off installs, use:
npm install --save-exactpnpm add --save-exactyarn add --exact
This stops future package additions from writing ^ ranges by default.
Disable Install Scripts When Possible
Malicious packages often use lifecycle scripts because install time is a convenient execution point.
Disable scripts during locked installs—where your build can tolerate it:
npm ci --ignore-scriptspnpm install --frozen-lockfile --ignore-scriptsyarn install --immutable --mode=skip-builds
Some packages need scripts to compile native modules or download assets. Treat those as allowlisted cases.
The goal is not to break builds. It’s to ensure any code execution during installation is a deliberate choice.
Audit the Actual Dependency Tree
Run npm audit against the lockfile-backed dependency graph—not a hypothetical tree.
In CI, use npm audit –audit-level=high or a threshold that matches your risk model. pnpm and Yarn offer the same check via pnpm audit and yarn npm audit.
These audits catch known vulnerabilities and give a repeatable signal. They don’t catch every malicious package.
Applying policy across many repositories is hard. Fixing one repo by hand is easy. Two hundred repos need a rollout.

Scale the Policy Across Many Repositories
Use Batch Changes to pin versions across repositories.
Sourcegraph Batch Changes is designed for this: define a change once, run it across all matching repos, preview the diffs, and open merge requests for each.
Applied to dependency prefixes, this means you can find risky ranges, update package.json, refresh the lockfile, and track review status from one place.
For demo purposes, this package.json shows what the batch spec updates:
{
"name": "react-demo-app",
"private": true,
"dependencies": {
"react": "^19.0.0",
"react-dom": "~19.0.0"
},
"devDependencies": {
"react-server-dom-webpack": "19.x"
}
}
Here’s a batch spec that replaces those ranges with approved exact versions:
version: 2
name: pin-risky-npm-dependencies
description: Pin selected npm dependencies by removing SemVer range prefixes.
on:
- repositoriesMatchingQuery: repo:github.com/YOUR-ORG/.* repohasfile:package.json
workspaces:
- rootAtLocationOf: package.json
in: '*'
onlyFetchWorkspace: true
steps:
- run: |
bash <<'BASH'
set -euo pipefail
node <<'JS'
const fs = require('fs')
const packageJsonPath = 'package.json'
const manifest = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
const approvedPins = {
'react': '19.2.6',
'react-dom': '19.2.6',
'react-server-dom-webpack': '19.2.6',
'react-server-dom-turbopack': '19.2.6',
'react-server-dom-parcel': '19.2.6',
}
let changed = false
for (const section of ['dependencies', 'devDependencies', 'optionalDependencies']) {
const deps = manifest[section] || {}
for (const [name, pinnedVersion] of Object.entries(approvedPins)) {
if (!Object.prototype.hasOwnProperty.call(deps, name)) continue
if (deps[name] !== pinnedVersion) {
deps[name] = pinnedVersion
changed = true
}
}
}
if (changed) {
fs.writeFileSync(packageJsonPath, `${JSON.stringify(manifest, null, 2)}\n`)
}
JS
if [[ -f package-lock.json ]]; then npm install --package-lock-only --ignore-scripts; fi
BASH
container: node:22
changesetTemplate:
title: Pin high-risk npm dependency versions
body: |
This pins selected high-risk npm dependencies by removing permissive SemVer prefixes.
The lockfile was refreshed without running install scripts.
branch: security/pin-risky-npm-dependencies-${{ replace steps.path "/" "-" }}
commit:
message: Pin high-risk npm dependency versions
The preview makes the policy visible as a small, reviewable diff.
The same pattern works for other controls: replace npm install with npm ci in CI files, add exact-version settings for npm, pnpm, or Yarn, or add –ignore-scripts to trusted build steps.
The key is making the policy concrete in code, then applying it everywhere the risk exists.
Final thoughts
Dependency ranges will always have a place. But they should be a deliberate choice.
With lockfiles, exact pins for sensitive packages, and Batch Changes for rollout, teams can keep version drift visible and controlled.
For future updates on Batch Changes, subscribe to our changelog.
A special thanks to Peter Guy, Robert Lin, and Stephanie Jarmak for their contributions to this blog post.
Sources & further reading
Featured image: Dependency prefixes are a supply chain risk: let's fix them. Via original publisher. Image source

