I threat-model the build cache. Nobody else does, because the build cache is the friendliest thing in CI. It’s the one step that makes things faster. It asks for nothing and saves you four minutes. Every convenience is an attack surface with better marketing, and this one comes with a feature called restore-keys whose whole job is to hand you something other than what you asked for.
Here is the scenario. A security advisory lands for a package you depend on. You do the responsible thing: npm uninstall, commit the new lockfile, push. CI goes green. You close the ticket. Meanwhile the CI runner restored a node_modules from three weeks ago, skipped the install because “the cache hit,” and ran your tests against the package you just removed. It’s still there. It still loads. Your test suite has no idea anything changed.
SEVERITY: the green check you merged on
ATTACK VECTOR: restore-keys: ${{ runner.os }}-node-
EXPLOIT: nobody ran npm ci
The tip comes from it-journey.dev’s Workflow Optimization quest, which teaches caching to make pipelines fast. This is the security half of that lesson: what the cache restores when your key doesn’t match, and how to make sure the lockfile, not the cache, decides what’s installed.
Everything in the console blocks below ran on Node v24.22.3 and npm 11.19.1. One honest caveat up front: the GitHub cache service only exists inside a real Actions run, so I didn’t fake one. I reproduced what a prefix restore does. I built a project, saved its node_modules, removed a dependency, then dropped the old tree into a clean copy of the new project, which is the state a runner is in after a stale restore. The workflow YAML snippets are configuration I’m recommending, not something I ran here. The behaviour of restore-keys and cache-hit comes from the actions/cache README and GitHub’s dependency caching reference, quoted below.
What restore-keys actually promises
The README defines it in one sentence that deserves a second read:
restore-keys- An ordered multiline string listing the prefix-matched keys, that are used for restoring stale cache if no cache hit occurred for key.
“Restoring stale cache” is the documented purpose. Not a side effect. The lookup goes like this, per GitHub’s reference:
- Look for an exact match on
key. - If there isn’t one, look for caches whose key starts with
key. The primary key gets prefix-matched too, which surprises people. - Then try each
restore-keysentry in order, as a prefix. “If there are multiple partial matches for a restore key, the action returns the most recently created cache.” - If the current branch has nothing, repeat all of that against the default branch’s caches.
So restore-keys: Linux-node- restores the most recent cache whose name starts with Linux-node-. That’s whatever tree some earlier run built from whatever lockfile it had. It could be yesterday’s. It could be the one from before you removed the advisory package.
Then there’s the output everyone gates on. The README says cache-hit is 'true' for an exact match on key, 'false' when the restore came from restore-keys, and an empty string when nothing was restored at all. Three states, all of them strings. Hold on to that.
The reproduction
Build a tiny project with two dependencies, one of which we’re about to “remove for security reasons.” is-odd is playing the compromised package. It’s innocent. It drew the short straw.
$ npm init -y >/dev/null
$ npm install --silent left-pad@1.3.0 is-odd@3.0.1
$ cat index.js
const lp=require("left-pad");const o=require("is-odd");console.log(lp("7",3,"0"), o(7));
$ node index.js
007 true
$ cp -a node_modules /tmp/lhcache/cached_node_modules # what CI saved last month
$ npm uninstall --silent is-odd
$ node index.js
Error: Cannot find module 'is-odd'
(Node’s stack trace is trimmed to its first line here and below.) That last error is the correct outcome. index.js still requires a package the lockfile no longer contains, so the build should fail. Hold on to that one too. A build that fails here is a build telling you the truth.
Now play CI. Each run gets a fresh checkout (the new package.json, the new package-lock.json, the unchanged index.js) plus the stale node_modules a prefix restore would drop in. I tried it three ways: skip the install, npm install, and npm ci.
== mode: skip
is-number
is-odd
left-pad
node index.js exit=0
== mode: install
removed 2 packages in 204ms
left-pad
node index.js exit=1
== mode: ci
added 1 package in 224ms
left-pad
node index.js exit=1
The skipped install is the one that went green. is-odd and its dependency is-number are sitting in node_modules and nothing in the lockfile asked for them. node index.js exits 0 on code that should not run.
The part where it broke (my threat model, specifically)
I came into this expecting npm install to be the villain. The brief I worked from said the stale tree “lingers,” and older folklore says npm install only adds. On npm 11 that isn’t true: npm install read the lockfile and removed 2 packages without being asked. Both installers fixed the tree. I’m leaving that in, because the real lesson is narrower and worse: the stale tree survives only when the install doesn’t run at all. So the question is when your workflow skips the install, and the answer is “more often than you’d think”:
- The key isn’t a lockfile hash.
key: ${{ runner.os }}-node_modulesis an exact hit on every run forever.cache-hitis'true', the README’s own “skip the install on a hit” pattern skips it, and the tree you cached in March is still running in November. A key that doesn’t change is a time capsule. - The
if:treats'false'as false. It isn’t. Per GitHub’s expressions reference, the falsy values in a conditional arefalse,0,-0,"",''andnull, so the non-empty string'false'is truthy. A shortcut likeif: ${{ !steps.cache.outputs.cache-hit }}therefore runs the install only on a total miss and skips it on arestore-keyspartial hit, which is exactly the stale case. The README’s form,if: steps.cache.outputs.cache-hit != 'true', is the one that installs on a partial hit. I didn’t run that expression in a live workflow; the behaviour comes straight from the reference. - Someone “optimised” the install away. A step called
Install (only if needed), written by a person who has since left, guarded by a condition nobody reads.
There was a second failure, and it’s the one that scared me. I assumed npm ls would complain about a tree full of packages the lockfile doesn’t mention. It noticed them. It even said so. Then it exited 0:
$ npm ls --all
app@1.0.0 /tmp/lhcache/ci-skip
├── is-number@6.0.0 extraneous
├─┬ is-odd@3.0.1 extraneous
│ └── is-number@6.0.0 deduped
└── left-pad@1.3.0
npm ls exit=0
extraneous is a word, not an exit code. A CI step that runs npm ls as a sanity check passes on this tree.
Now the thriller
Escalate it properly. The package you removed wasn’t is-odd. It was the one in the advisory, the one with the postinstall that phones home. You removed it within the hour, like a professional. And your CI kept restoring a tree with it inside, from the default branch’s cache, into every pull request, for every contributor, for as long as the key stayed loose. Somewhere a nation-state analyst is reading your test logs and marvelling at your loyalty.
Then it gets less funny. Caches can be written by things you don’t expect: Adnan Khan’s cache-poisoning research shows how code running in a workflow with access to the default branch’s cache scope can plant entries that later runs restore. A stale cache is an accident. A poisoned one is the same mechanism with intent.
Walk it back. For most projects this is not an attack. It’s a slow, boring wrong answer: tests passing on a dependency set that doesn’t match your lockfile, and a removal that doesn’t really take effect until the cache key changes. The fix doesn’t involve giving up caching. It involves caching the right thing and never letting the cache decide what’s installed.
Three mitigations, ranked
1. Run npm ci on every run. Never skip the install. This is the one that matters. npm ci deletes node_modules and installs exactly what the lockfile says. You saw it above: with the stale tree restored, npm ci left only left-pad, and node index.js failed the way it should. It also refuses to run when package.json and the lockfile disagree, so a forgotten lockfile commit fails loudly instead of resolving something new. If your workflow has if: on the install step, delete the if:. The four minutes you saved were spent on the wrong node_modules.
2. Cache ~/.npm, not node_modules, and hash the lockfile into the key. The actions/cache npm example says it outright: “It is not recommended to cache node_modules, as it can break across Node versions and won’t work with npm ci.” Cache the download cache instead. A stale ~/.npm is harmless, because it’s a pile of tarballs that npm ci pulls from only if the lockfile asks for them. I tested exactly that: a cache directory still holding is-odd, a lockfile without it.
$ grep -rhoa 'registry.npmjs.org/[a-z-]*/-/[a-z0-9.-]*tgz' /tmp/lhcache/oldcache/_cacache/index-v5 | sort -u
registry.npmjs.org/is-number/-/is-number-6.0.0.tgz
registry.npmjs.org/is-odd/-/is-odd-3.0.1.tgz
registry.npmjs.org/left-pad/-/left-pad-1.3.0.tgz
$ npm ci --cache /tmp/lhcache/oldcache --prefer-offline --no-audit --no-fund
npm warn deprecated left-pad@1.3.0: use String.prototype.padStart()
added 1 package in 215ms
$ ls node_modules
left-pad
$ node -e 'require("is-odd")'
Error: Cannot find module 'is-odd'
The stale cache still held the removed package and nothing installed it. That’s what makes restore-keys safe here: a warm start that can only speed up the install, never change its answer. The key should still change whenever the lockfile does. I checked that removing one dependency changes the lockfile’s hash, so a hashFiles key on it will miss exactly when it should:
$ sha256sum package-lock.json
fb87ff9bd7b0c614f71b0225dd7c736a77f4e65bd56e2385a98aba71d4e23e58 package-lock.json
$ npm uninstall --silent is-odd
$ sha256sum package-lock.json
73401d640baf5daf9e02905cff8a6c115cb187ba3d71acc602791b410059a92b package-lock.json
The workflow shape I recommend (not run here, as I said):
- uses: actions/cache@v6
with:
path: ~/.npm
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-npm-
- run: npm ci # no if:, ever
Or let actions/setup-node do it with cache: npm. Its README notes that “the action does not cache node_modules.” That’s the feature, not a limitation.
3. Add a tripwire that fails on extraneous packages. For the day someone adds an if: back, or a different job skips the install. Since npm ls exits 0 on a stale tree, make it fail on the word it prints:
$ if npm ls --all 2>&1 | grep -q extraneous; then echo "stale node_modules: refusing to build"; false; fi; echo "tripwire exit=$?"
stale node_modules: refusing to build
tripwire exit=1
Run it after the install and before the tests. On a clean npm ci tree it prints nothing and passes. If you’d rather see what would be removed, npm prune --dry-run listed remove is-odd 3.0.1 and remove is-number 6.0.0 on the same tree. It exited 0 too, so it’s a report, not a gate.
What a normal person should do
Open your workflow. Search for node_modules in a path: and for if: on an install step. If you find either, switch to caching ~/.npm with a lockfile-hashed key and an unconditional npm ci. That’s a three-line change. Keep restore-keys if you like the speed; once the cache only holds downloads, the worst a stale restore can do is make a cold install a little warmer.
For the record, my own site’s build caches its theme under the theme’s exact commit SHA, with no restore-keys at all, and I still wrote a whole field note about trusting it too much. If you like this genre of self-inflicted paranoia, order your Dockerfile for the layer cache covers the same problem one layer down: a cache that’s fast because it isn’t checking.