-
Notifications
You must be signed in to change notification settings - Fork 4.7k
fix: provenance-file takes precedence over OIDC auto-generated provenance #9882
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
martinrrm
merged 7 commits into
npm:latest
from
yunseo-kim:fix/provenance-file-precedence
Sep 3, 2026
+326
−8
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7b2ab6d
fix: provenance-file takes precedence over OIDC auto-generated proven…
yunseo-kim 66710dc
fix(libnpmpublish): throw when provenance and provenanceFile are both…
yunseo-kim df4e300
docs: document provenance-file precedence over automatic provenance
yunseo-kim 71f3f48
fix(libnpmpublish): use EUSAGE for provenance and provenanceFile conf…
yunseo-kim 6907480
test: parameterize provenance-file precedence regression test
yunseo-kim b55f4f7
fix: scope OIDC automatic provenance to the current publish
yunseo-kim 4610389
test: add workspace regression test for shared-config provenance leak
yunseo-kim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,8 @@ const { loadNpmWithRegistry } = require('../../fixtures/mock-npm') | |
| const { cleanZlib } = require('../../fixtures/clean-snapshot') | ||
| const pacote = require('pacote') | ||
| const Arborist = require('@npmcli/arborist') | ||
| const npa = require('npm-package-arg') | ||
| const ssri = require('ssri') | ||
| const path = require('node:path') | ||
| const fs = require('node:fs') | ||
| const { circleciIdToken, githubIdToken, gitlabIdToken, oidcPublishTest, mockOidc } = require('../../fixtures/mock-oidc') | ||
|
|
@@ -1533,6 +1535,256 @@ t.test('oidc token exchange - provenance', (t) => { | |
| }, | ||
| })) | ||
|
|
||
| const provenanceFileSources = [ | ||
| { | ||
| name: 'CLI config', | ||
| options: provenanceBundlePath => ({ | ||
| config: { | ||
| 'provenance-file': provenanceBundlePath, | ||
| }, | ||
| }), | ||
| }, | ||
| { | ||
| // exercises Publish.#getManifest() and its flatten(filteredPublishConfig, opts) | ||
| // path: publishConfig must reach opts.provenanceFile before oidc() decides | ||
| // whether to enable automatic provenance | ||
| name: 'publishConfig', | ||
| options: provenanceBundlePath => ({ | ||
| packageJson: { | ||
| publishConfig: { | ||
| 'provenance-file': provenanceBundlePath, | ||
| }, | ||
| }, | ||
| }), | ||
| }, | ||
| ] | ||
|
|
||
| for (const { name, options } of provenanceFileSources) { | ||
| t.test(`${name} provenance-file takes precedence over OIDC auto-provenance`, async t => { | ||
| const bundleDir = t.testdir() | ||
| const provenanceBundlePath = path.join( | ||
| bundleDir, | ||
| 'provenance-bundle.json' | ||
| ) | ||
| // holder so the libnpmpack mock can return the tarball computed below | ||
| const packMock = { tarballData: null } | ||
|
|
||
| const sourceOptions = options(provenanceBundlePath) | ||
|
|
||
| const { npm, registry, prefix, joinedOutput } = await mockOidc(t, { | ||
| oidcOptions: { github: true }, | ||
| config: { | ||
| '//registry.npmjs.org/:_authToken': 'existing-fallback-token', | ||
| ...sourceOptions.config, | ||
| }, | ||
| packageJson: sourceOptions.packageJson, | ||
| mockGithubOidcOptions: { | ||
| audience: 'npm:registry.npmjs.org', | ||
| idToken: githubPublicIdToken, | ||
| }, | ||
| mockOidcTokenExchangeOptions: { | ||
| idToken: githubPublicIdToken, | ||
| body: { | ||
| token: 'exchange-token', | ||
| }, | ||
| }, | ||
| publishOptions: { | ||
| token: 'exchange-token', | ||
| noPut: true, | ||
| }, | ||
| load: { | ||
| mocks: { | ||
| libnpmaccess: { | ||
| getVisibility: async () => ({ public: true }), | ||
| }, | ||
| // publish a deterministic tarball so the bundle subject digest can match it | ||
| libnpmpack: async () => packMock.tarballData, | ||
| // libnpmpublish must be mocked as a module so its internal require of | ||
| // sigstore is intercepted: a user-supplied bundle is only verified, | ||
| // generation (attest) must never run | ||
| libnpmpublish: t.mock('libnpmpublish', { | ||
| 'libnpmpublish/lib/provenance': t.mock('libnpmpublish/lib/provenance', { | ||
| sigstore: { | ||
| verify: async () => {}, | ||
| attest: async () => { | ||
| throw new Error('sigstore.attest must not be called when provenance-file is configured') | ||
| }, | ||
| }, | ||
| }), | ||
| }), | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| // compute the tarball integrity the same way libnpmpublish does so the | ||
| // provenance bundle subject matches the packed tarball | ||
| packMock.tarballData = await pacote.tarball(prefix, { Arborist }) | ||
| const integrity = ssri.fromData(packMock.tarballData, { algorithms: ['sha512'] }) | ||
| const spec = npa.resolve(pkg, '1.0.0') | ||
| const provenanceBundle = { | ||
| mediaType: 'application/vnd.dev.sigstore.bundle+json;version=0.2', | ||
| verificationMaterial: { | ||
| x509CertificateChain: { | ||
| certificates: [{ rawBytes: 'dGVzdA==' }], | ||
| }, | ||
| tlogEntries: [], | ||
| }, | ||
| dsseEnvelope: { | ||
| payload: Buffer.from(JSON.stringify({ | ||
| _type: 'https://in-toto.io/Statement/v0.1', | ||
| subject: [ | ||
| { | ||
| name: npa.toPurl(spec), | ||
| digest: { sha512: integrity.sha512[0].hexDigest() }, | ||
| }, | ||
| ], | ||
| predicateType: 'https://slsa.dev/provenance/v0.2', | ||
| predicate: {}, | ||
| })).toString('base64'), | ||
| payloadType: 'application/vnd.in-toto+json', | ||
| signatures: [{ | ||
| /* eslint-disable-next-line max-len */ | ||
| sig: 'MEUCIQDqHtpkk1d0rMGLmf3qet9jLale3KVn8Pnywpwt7ln+9AIgG9CJvvUmyemhNYHz0DfJ4vMfKk1TMg+m3hR0mISXJos=', | ||
| keyid: '', | ||
| }], | ||
| }, | ||
| } | ||
| fs.writeFileSync(provenanceBundlePath, JSON.stringify(provenanceBundle, null, 2)) | ||
|
|
||
| let publishedBody | ||
| registry.nock | ||
| .put(`/${spec.escapedName}`, (body) => { | ||
| publishedBody = body | ||
| return true | ||
| }) | ||
| .matchHeader('authorization', 'Bearer exchange-token') | ||
| // optional so a failed publish does not leave a pending mock behind | ||
| .optionally() | ||
| .reply(200, {}) | ||
|
|
||
| // libnpmpublish checks package visibility itself before generating | ||
| // provenance; optional so it is only consumed if generation is attempted | ||
| registry.nock | ||
| .get(`/-/package/${spec.escapedName}/visibility`) | ||
| .optionally() | ||
| .reply(200, { public: true }) | ||
|
|
||
| await npm.exec('publish', []) | ||
|
|
||
| t.match(joinedOutput(), '+ @npmcli/[email protected]') | ||
|
|
||
| const attachment = | ||
| publishedBody?._attachments[`${pkg}-1.0.0.sigstore`] | ||
|
|
||
| t.ok(attachment, 'published packument includes supplied provenance') | ||
| t.strictSame( | ||
| JSON.parse(attachment.data), | ||
| provenanceBundle, | ||
| 'published sigstore bundle is the user-supplied provenance file' | ||
| ) | ||
| }) | ||
| } | ||
|
|
||
| t.test('automatic provenance does not leak between workspace publishes', async t => { | ||
| const provenanceBundlePath = path.join(t.testdir(), 'provenance-bundle.json') | ||
| const autoPackage = 'workspace-auto-provenance' | ||
| const filePackage = 'workspace-file-provenance' | ||
| const publishCalls = [] | ||
| const prefixDir = { | ||
| 'package.json': JSON.stringify({ | ||
| name: 'workspace-root', | ||
| version: '1.0.0', | ||
| workspaces: [autoPackage, filePackage], | ||
| }), | ||
| [autoPackage]: { | ||
| 'package.json': JSON.stringify({ | ||
| name: autoPackage, | ||
| version: '1.0.0', | ||
| }), | ||
| }, | ||
| [filePackage]: { | ||
| 'package.json': JSON.stringify({ | ||
| name: filePackage, | ||
| version: '1.0.0', | ||
| publishConfig: { | ||
| 'provenance-file': provenanceBundlePath, | ||
| }, | ||
| }), | ||
| }, | ||
| } | ||
|
|
||
| const { npm, registry } = await mockOidc(t, { | ||
| oidcOptions: { github: true }, | ||
| packageName: autoPackage, | ||
| config: { | ||
| '//registry.npmjs.org/:_authToken': 'existing-fallback-token', | ||
| workspaces: true, | ||
| }, | ||
| mockGithubOidcOptions: { | ||
| audience: 'npm:registry.npmjs.org', | ||
| idToken: githubPublicIdToken, | ||
| times: 2, | ||
| }, | ||
| mockOidcTokenExchangeOptions: { | ||
| idToken: githubPublicIdToken, | ||
| body: { | ||
| token: 'exchange-token', | ||
| }, | ||
| }, | ||
| publishOptions: { | ||
| noPut: true, | ||
| }, | ||
| load: { | ||
| prefixDir, | ||
| mocks: { | ||
| libnpmaccess: { | ||
| getVisibility: async () => ({ public: true }), | ||
| }, | ||
| // mocked as a plain module so the publish options each workspace | ||
| // receives can be recorded verbatim | ||
| libnpmpublish: { | ||
| publish: async (manifest, _tarballData, opts) => { | ||
| publishCalls.push({ | ||
| name: manifest.name, | ||
| provenance: opts.provenance, | ||
| provenanceFile: opts.provenanceFile, | ||
| }) | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| registry.mockOidcTokenExchange({ | ||
| packageName: filePackage, | ||
| idToken: githubPublicIdToken, | ||
| body: { | ||
| token: 'exchange-token', | ||
| }, | ||
| }) | ||
| registry.publish(filePackage, { noPut: true }) | ||
|
|
||
| await npm.exec('publish', []) | ||
|
|
||
| t.strictSame(publishCalls, [ | ||
| { | ||
| name: autoPackage, | ||
| provenance: true, | ||
| provenanceFile: null, | ||
| }, | ||
| { | ||
| name: filePackage, | ||
| provenance: false, | ||
| provenanceFile: provenanceBundlePath, | ||
| }, | ||
| ]) | ||
| t.equal( | ||
| npm.config.isDefault('provenance'), | ||
| true, | ||
| 'automatic provenance does not mutate shared config' | ||
| ) | ||
| }) | ||
|
|
||
| const brokenJwts = [ | ||
| 'x.invalid-jwt.x', | ||
| 'x.invalid-jwt.', | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.