Skip to content

Commit 26cbe99

Browse files
authored
fix: improve permission error for provenance (#6226)
Improves the error message returned when a user attempts to generate a provenance statement on publish but has not set the correct perissions in the GitHub Actions workflow. Signed-off-by: Brian DeHamer <[email protected]>
1 parent 8a78c6f commit 26cbe99

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

workspaces/libnpmpublish/lib/publish.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,23 @@ const buildMetadata = async (registry, manifest, tarballData, spec, opts) => {
141141
digest: { sha512: integrity.sha512[0].hexDigest() },
142142
}
143143

144-
// Ensure that we're running in GHA and an OIDC token is available,
145-
// currently the only supported build environment
146-
if (ciInfo.name !== 'GitHub Actions' || !process.env.ACTIONS_ID_TOKEN_REQUEST_URL) {
144+
// Ensure that we're running in GHA, currently the only supported build environment
145+
if (ciInfo.name !== 'GitHub Actions') {
147146
throw Object.assign(
148147
new Error('Automatic provenance generation not supported outside of GitHub Actions'),
149148
{ code: 'EUSAGE' }
150149
)
151150
}
152151

152+
// Ensure that the GHA OIDC token is available
153+
if (!process.env.ACTIONS_ID_TOKEN_REQUEST_URL) {
154+
throw Object.assign(
155+
/* eslint-disable-next-line max-len */
156+
new Error('Provenance generation in GitHub Actions requires "write" access to the "id-token" permission'),
157+
{ code: 'EUSAGE' }
158+
)
159+
}
160+
153161
const visibility =
154162
await npmFetch.json(`${registry}/-/package/${spec.escapedName}/visibility`, opts)
155163
if (!visibility.public && opts.provenance === true && opts.access !== 'public') {

workspaces/libnpmpublish/test/publish.js

+29-1
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ t.test('automatic provenance in unsupported environment', async t => {
784784
mockGlobals(t, {
785785
'process.env': {
786786
CI: false,
787-
GITHUB_ACTIONS: false,
787+
GITHUB_ACTIONS: undefined,
788788
},
789789
})
790790
const { publish } = t.mock('..', { 'ci-info': t.mock('ci-info') })
@@ -806,3 +806,31 @@ t.test('automatic provenance in unsupported environment', async t => {
806806
}
807807
)
808808
})
809+
810+
t.test('automatic provenance with incorrect permissions', async t => {
811+
mockGlobals(t, {
812+
'process.env': {
813+
CI: false,
814+
GITHUB_ACTIONS: true,
815+
ACTIONS_ID_TOKEN_REQUEST_URL: undefined,
816+
},
817+
})
818+
const { publish } = t.mock('..', { 'ci-info': t.mock('ci-info') })
819+
const manifest = {
820+
name: '@npmcli/libnpmpublish-test',
821+
version: '1.0.0',
822+
description: 'test libnpmpublish package',
823+
}
824+
825+
await t.rejects(
826+
publish(manifest, Buffer.from(''), {
827+
...opts,
828+
access: null,
829+
provenance: true,
830+
}),
831+
{
832+
message: /requires "write" access/,
833+
code: 'EUSAGE',
834+
}
835+
)
836+
})

0 commit comments

Comments
 (0)