fix: provenance-file takes precedence over OIDC auto-generated provenance
Why Needed
When publishing with an externally generated provenance bundle under OIDC trusted publishing
(npm publish <tarball> --provenance-file=<bundle>), npm silently discarded the supplied bundle and published
its own auto-generated provenance instead. Three layers interacted:
lib/utils/oidc.jsauto-enabled provenance (opts.provenance = true) whenever theprovenanceconfig was at
its default, without checking whether aprovenance-filewas supplied.- In
libnpmpublish'sbuildMetadata(), the innerprovenance === truebranch then rangenerateProvenance(),
so theverifyProvenance(subject, provenanceFile)branch, the only code path that reads the supplied file,
never executed. - Every documented way to disable automatic provenance was unusable in combination with
--provenance-file
(config-layer mutual exclusivity error, env carve-out, or publishConfig flatten timing).
What Changes
lib/utils/oidc.js: skip auto-enabling provenance when a provenance file is configured (opts.provenanceFile).
optsalready carriesprovenanceFilefrom every config source (CLI/env/npmrc/publishConfig) by the time the
OIDC flow runs, so this covers all entry paths. The supplied bundle is now verified viaverifyProvenance()and
published, as documented.
Automatic provenance is also no longer written to the shared config; it is set only on the current publish's
opts(which reaches libnpmpublish viaotplease). Previously theuser-scoped config write leaked
provenance: trueinto later workspace publishes duringnpm publish --workspaces, where a workspace with
publishConfig["provenance-file"]would then hit the conflict check below.libnpmpublish:buildMetadata()now throwsEPROVENANCECONFLICTEUSAGE(updated per review) when
bothprovenance: trueandprovenanceFileare provided.- Docs: config descriptions for
provenance/provenance-fileand the libnpmpublish README now state the
precedence rule.
⚠️ New error code (feedback requested) Resolved: reuses EUSAGE
This PR introduces EPROVENANCECONFLICT in libnpmpublish, thrown when both provenance: true and
provenanceFile are provided programmatically.
Rationale: the README already documents the two as mutually exclusive, and silently preferring either direction
discards a cryptographically meaningful artifact. Note the CLI's config layer reports the same conflict as a
TypeError without an error code (pre-existing). Happy to align on EUSAGE or another convention per
review.
Update: per review, the conflict reuses the existing EUSAGE code instead of introducing a new one
(Object.assign(new Error('provenance and provenanceFile cannot be used together'), { code: 'EUSAGE' })),
and the README documents EUSAGE accordingly.
Testing
- New CLI regression test: OIDC trusted publishing +
provenance-fileconfig asserts the published packument's
sigstore attachment deep-equals the supplied bundle (and that sigstore generation is never invoked).
Update: parameterized per review to cover bothprovenance-filesources, CLI config andpublishConfig;
the latter provespublishConfig["provenance-file"]is flattened intoopts.provenanceFilevia
Publish.#getManifest()beforeoidc()decides whether to enable automatic provenance. - New libnpmpublish test: both options set → rejects with
EPROVENANCECONFLICTEUSAGE, no registry PUT,
generation not invoked. - New workspace regression test (per review): publishes two public workspaces in order (one where OIDC
auto-enables provenance, then one withpublishConfig["provenance-file"]), and asserts each publish receives
exactly its own options ({ provenance: true, provenanceFile: null }then
{ provenance: false, provenanceFile }) and that the shared config stays at its default
(npm.config.isDefault('provenance') === true). Verified to fail on the pre-fix code for exactly the leak
reason, and to pass after the fix.mock-oidcgained atimesoption on the GitHub id-token mock to serve
one token request per workspace. - Full root suite green with 100% coverage; libnpmpublish workspace suite green; lint clean.
References
Fixes #9879
Out of scope (noted for follow-up)
publishConfig.provenance: falsedoes not block the OIDC auto-enable (publishConfig flattens intooptsonly,
soconfig.isDefault('provenance')stays true). A separate behavioral question aboutisDefaultsemantics.- Hardening
config.setitself against bypassing load-time exclusivity is an@npmcli/configsemver-major
conversation; this flow no longer writesprovenanceto the config at all.