corepack: Safeguards for keyid mismatch
-
Issue https://github.com/nodejs/corepack/issues/612 records a disruption to the corepack service after npm registry keys were updated.
-
https://github.com/nodejs/corepack/issues/625 describes how a project with an existing
packageManagerrecord can be affected if a new package manager version is released, signed with a new key, and not used in the project. -
How can this be prevented in future? I’m not familiar with how the keys and systems for using them work, so this is a question for the experts.
In https://github.com/nodejs/corepack/issues/612#issuecomment-2616588603 @wraithgar wrote
https://registry.npmjs.org/-/npm/v1/keys should now be returning two keys. During key rotation the old one was omitted by mistake and has since been re-added.
Even if a mistake hadn’t been made, would there still have been an issue needing keys urgently updating in Corepack?
Does Corepack need to know in advanced if npm registry keys on the endpoint https://registry.npmjs.org/-/npm/v1/keys (see https://docs.npmjs.com/about-registry-signatures) are going to be changed?
If npm registry keys are changed, is there any advanced notice from the npm registry of a plan to change?
Is there any notification that npm registry keys have changed, at the time of change or does the endpoint need to be polled by Corepack regularly?
Can Corepack ensure that the version bundled in Node.js will always have a set of keys which can be used without error?
Also: the more fundamental question of whether Corepack should have a copy of the npm registry keys with the risk of them becoming out of date? Could Corepack simply use the live keys on https://registry.npmjs.org/-/npm/v1/keys instead of keeping a copy in the repo?
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 17
- Comments: 26 (18 by maintainers)
Commits related to this issue
- fix: workaround corepack/pnpm install issue See https://github.com/nodejs/corepack/issues/616#issuecomment-2622079955 Signed-off-by: Alexander Trost <galexrt@googlemail.com> — committed to fivenet-app/fivenet by galexrt 2 years ago
- fix: Corepack pnpm issue (#10840) So, a while back Circle CI builds and Heroku builds started to fail. From all the threads I read, it seems like the [npm registry rotated it's signing keys](https:... — committed to chatwoot/chatwoot by scmmishra 2 years ago
- Get NPM signing keys from @sigstore/tuf Instead of hardcoding NPM signing keys for verification we get them from sigstore’s TUF repository. This is in line with how npm implements signature verificat... — committed to geigerzaehler/corepack by geigerzaehler 2 years ago
- Get NPM signing keys from @sigstore/tuf Instead of hardcoding NPM signing keys for verification we get them from sigstore’s TUF repository. This is in line with how npm implements signature verificat... — committed to geigerzaehler/corepack by geigerzaehler 2 years ago
- resolve docker build issue https://github.com/bmrlab/musedam-website/actions/runs/13302944866/job/37148137160 https://github.com/nodejs/corepack/issues/616 — committed to bmrlab/musedam-website by web3nomad 2 years ago
- resolve docker build issue https://github.com/bmrlab/musedam-website/actions/runs/13302944866/job/37148137160 https://github.com/nodejs/corepack/issues/616 — committed to bmrlab/musedam-website by web3nomad 2 years ago
- Get NPM signing keys from @sigstore/tuf Instead of hardcoding NPM signing keys for verification we get them from sigstore’s TUF repository. This is in line with how npm implements signature verificat... — committed to geigerzaehler/corepack by geigerzaehler 2 years ago
- fix: Corepack pnpm issue (#10840) So, a while back Circle CI builds and Heroku builds started to fail. From all the threads I read, it seems like the [npm registry rotated it's signing keys](https:... — committed to conexaoazul/magicachat by scmmishra 2 years ago
- Get NPM signing keys from @sigstore/tuf Instead of hardcoding NPM signing keys for verification we get them from sigstore’s TUF repository. This is in line with how npm implements signature verificat... — committed to geigerzaehler/corepack by geigerzaehler 2 years ago
- Get NPM signing keys from @sigstore/tuf Instead of hardcoding NPM signing keys for verification we get them from sigstore’s TUF repository. This is in line with how npm implements signature verificat... — committed to geigerzaehler/corepack by geigerzaehler 2 years ago
Fair point. I wanted to emphasize that the old key is not revoked and that even if the old key is not expired the current system breaks.
Cool, I’ll take a look and will create a PR.
The npm cli uses TUF to fetch the keys. It also falls back to a direct request from the registry if that fails, and perhaps a warning should be logged when this happens.
Some shower thoughts on this topic: we technically could have a trusted public/private key pair that would allow Corepack maintainers to sign registry keys without needing to release a new version every time a registry rotates its keys. However this creates additional challenges such as:
tbh I don’t know if that would be worth the effort, I suspect most folks would be perfectly fine trusting only HTTPS (and therefor be confortable setting
COREPACK_INTEGRITY_KEYSto0in their env, the signature verification is only a redundancy for TLS), and while the transition period is annoying (from the time the new registry key is released, and the time the version of Corepack that integrates it is deployed everywhere), hopefully it’s rather rare.I don’t think that’s true, what is signed is the concatenation of
${packageName}@${version}:${integrity}, so it would only work if the package name and version matches.So, let’s not put any safeguards? I really don’t follow the logic here
The registry exposes both
dist.integrity(anddist.shasum) to validate the integrity, anddist.signaturesto validate the authenticity.This would be my recommendation. I am not a corepack maintainer though, so it’s not my decision.
There shouldn’t be. The values of the keys aren’t something corepack needs to know beforehand. If a local reference is needed they can be cached. The response has http caching headers that should inform clients how long to cache.
expiresis simply used to denote the latest date that a published package should have been signed by a given key.keyidis the id used to correspond a package’s signature with a key for signature verification. Both are present in registry results, based on when the package was published.cf how npm correlates
keyidand validatesexpires, and how corepack correlateskeyid(it does not appear that corepack validates theexpiresattribute of a key)The endpoint has always returned an array in anticipation of key rotation. Old keys will remain in the returned data set for as long as there are artifacts in the registry that are signed by those keys.
Trying to keep the contents of that endpoint in the source code in a way that’s decoupled from the source (i.e. not an http response cache) seems like it’s guaranteed to have problems when those contents change. The current setup also prevents other registries from being used, as it appears to be indexed by package name, not by registry hostname.
Worth noting that as long as the next key rotation happens after the fix for https://github.com/nodejs/corepack/issues/625 is released and deployed everywhere, the consequences should be not as dramatic next time around, i.e. only folks who need to use a latest/non-pinned version would be affected instead of everyone.
Easier said than done as we’ve been discussing in this issue.
(EDIT: some of this is wrong, see messages below!) I am not a security professional. However, I am not sure this attack vector (an attacker manages to MitM/impersonate
registry.npmjs.org) is relevant in this situation because:registry.npmjs.organd trusting TLS. (Compare with TLS certs or Debian/Arch/RedHat packages: they have a “chain of trust” or “root of trust” where a trust anchor has signed these keys. The local package manager can verify that new keys are trusted (signed) by a key it already trusts.)Even if the keys were pinned so that Corepack wasn’t fetching them via HTTPS, since the attacker controls what you receive they could simply send you data for another package entirely: by simply uploading a malicious package to NPM, they can get a package with a valid signature that Corepack would accept. The only way to guard against this would be to instead pin the hash of each package version.Even if you pin the hash yourself (like how @MikeMcC399 explained here: https://github.com/nodejs/corepack/issues/612#issuecomment-2622669686) so that you are sure that the package you receive is a trusted one, I think npm itself (and I assume other package managers) does not pin the registry’s keys and would trust what it gets over HTTPS and the attack would go through. Trying to have Corepack act as a bonus rogue-key-detector doesn’t make sense to me.registry.npmjs.org, we have bigger problems.TLDR: (I believe that) these signatures only guarantee integrity, and not authenticity. This is instead where projects like TUF and Sigstore fit:
For reference, NPM loads keys from Sigstore then falls back to the
/-/npm/v1/keysendpoint here: https://github.com/npm/cli/blob/593c84921b0df963cef2ca7b13e44acc20cbd558/lib/utils/verify-signatures.js#L174 (Note that I don’t know how to use TUF / Sigstore but I believe this is the relevant part. I also haven’t looked into how other package managers get their keys.)Given this, it makes sense to me that Corepack would retrieve the keys via HTTPS to verify download integrity. I believe that Sigstore is what you’re looking for if you want to make sure that the package managers that Corepack downloads are the ones released by a trusted source. Again though, I am not a security professional so hopefully someone more apt can chip in.
If you still want to pin the npm registry’s integrity keys, then I think:
package.json(I believe this is what this issue is for https://github.com/nodejs/corepack/issues/620).Looking back in the history, for instance of npm releases, the earliest release shown on https://www.npmjs.com/package/npm is https://www.npmjs.com/package/npm/v/1.1.25 published 12 years ago.
The same key
jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzAwas used all these years until npm@11.1.0 was published on Jan 29, 2025:If the signing keys on https://registry.npmjs.org/-/npm/v1/keys in future are only going to be rotated every 12 years, then this issue does not represent a practical risk for Corepack users.
If on the other hand there is a new process to rotate the keys more often, then this could represent a risk for Corepack users experiencing a time window where new keys have not propagated through a new Corepack release and distribution. During this time packages signed with a new key would fail unless additional steps are taken by users to disable integrity checks or add a hash in
corepackcommands.@wraithgar
Thank you so much for the details and explanation!
I realized that I can answer one of the questions myself.
The answer is yes, there is still an issue!
pnpm@10.0.0was signed with the older key (which expired earlier today2025-01-29T00:00:00.000Z, meaning it shouldn’t get used for signing any new packages)pnpm@10.1.0was signed with the new key. I assume that currently any future pnpm or Yarn releases will be using this key until it itself is rotated and set to expire.Even though the https://registry.npmjs.org/-/npm/v1/keys has been corrected, the latest version of Corepack (
0.30.0) distributed with Node.js fails to be able to installpnpm@10.1.0.The workarounds are to set the environment variable
COREPACK_INTEGRITY_KEYS=0, to manually update tocorepack@0.31.0or to install it using knowledge of the release’s hash.Any other pnpm or Yarn package published to the npm registry from today onwards is going to potentially hit this key mismatch issue.
If I have understood this correctly it would mean that Corepack would need to be enhanced to always use the live keys on https://registry.npmjs.org/-/npm/v1/keys and not use its own stored keys which could become out of date.
Closing, as this is not currently causing any problems, and it has been partially addressed in PR https://github.com/nodejs/corepack/pull/632
PR https://github.com/nodejs/corepack/pull/647 is attempting to address some remaining issues here, however it has been stalled for several months waiting for feedback.
CI builds using corepack breaking out of the blue after npm added a key (not even rotating) unfortunately generated a lot of work for me, all at the same time. I think it is super important to fix this so that key additions don’t require manual intervention and I’d like to contribute.
I think that corepack should follow npm here and use sigstore‘s TUF repository to fetch the keys for registry signature verification. This approach is simple, secure and stops installs from breaking when a new key is added .Sigstore is the right tool for this.
Conceptually, this sounds similar to what Sigstore and TUF do, only that you have to implement everything yourselves and you manage your own keys instead of establishing trust for NPM registry keys.
All other alternatives that were mentioned (temporarily setting
COREPACK_INTEGRITY_KEYS=0or loading the key from the file system decrease security and still require manual intervention.I’d be happy to create a prototype.
@wraithgar
Are you able to predict how often keys in https://registry.npmjs.org/-/npm/v1/keys are likely to be rotated and therefore what the risk of Corepack breaking again would be if Corepack does not address this issue?