Skip to content

Commit cd8d18a

Browse files
fix: list pending scripts in approve-scripts when ignore-scripts is set (#9482)
Backport of #9479 to `release/v11`. Co-authored-by: Jamie Magee <jamie.magee@gmail.com>
1 parent c14e87c commit cd8d18a

4 files changed

Lines changed: 37 additions & 3 deletions

File tree

lib/utils/allow-scripts-cmd.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ class AllowScriptsCmd extends BaseCommand {
6262
})
6363
await arb.loadActual()
6464

65-
const unreviewed = await checkAllowScripts({ arb, npm: this.npm })
65+
// Keep listing unreviewed packages even with ignore-scripts set, so
66+
// you can move from a blanket ignore-scripts to an allowlist. This
67+
// only lists; nothing runs.
68+
const unreviewed = await checkAllowScripts({ arb, npm: this.npm, includeWhenIgnored: true })
6669

6770
if (pending) {
6871
return this.runPending(unreviewed)

lib/utils/check-allow-scripts.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@ const getInstallScripts = require('@npmcli/arborist/lib/install-scripts.js')
88
// Returns an array of `{ node, scripts }` entries. `scripts` is an object
99
// describing the relevant lifecycle scripts that would run.
1010

11-
const checkAllowScripts = async ({ arb, npm, tree }) => {
11+
const checkAllowScripts = async ({ arb, npm, tree, includeWhenIgnored = false }) => {
1212
const ignoreScripts = !!arb.options?.ignoreScripts
1313
const dangerouslyAllowAll = !!npm?.flatOptions?.dangerouslyAllowAllScripts
1414

15-
if (ignoreScripts || dangerouslyAllowAll) {
15+
// With ignore-scripts set, no scripts run, so execution callers
16+
// (install, rebuild, strict preflight) bail out here. approve/deny pass
17+
// includeWhenIgnored so they keep listing unreviewed packages, which is
18+
// what you need to move from a blanket ignore-scripts to an allowlist.
19+
// Listing never runs anything.
20+
if ((ignoreScripts && !includeWhenIgnored) || dangerouslyAllowAll) {
1621
return []
1722
}
1823

test/lib/commands/approve-scripts.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ t.test('approve-scripts --pending lists unreviewed packages', async t => {
6060
t.match(out, /sharp@1\.0\.0/)
6161
})
6262

63+
t.test('approve-scripts --pending lists unreviewed packages even with ignore-scripts set', async t => {
64+
const { npm, joinedOutput } = await mockNpm(t, {
65+
prefixDir: setupProject({ withScripts: ['canvas', 'sharp'] }),
66+
config: { 'allow-scripts-pending': true, 'ignore-scripts': true },
67+
})
68+
await npm.exec('approve-scripts', [])
69+
const out = joinedOutput()
70+
t.match(out, /2 packages have install scripts not yet covered/)
71+
t.match(out, /canvas@1\.0\.0/)
72+
t.match(out, /sharp@1\.0\.0/)
73+
})
74+
6375
t.test('approve-scripts --pending with no unreviewed says so', async t => {
6476
const { npm, joinedOutput } = await mockNpm(t, {
6577
prefixDir: setupProject({

test/lib/utils/check-allow-scripts.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,20 @@ t.test('returns [] when ignoreScripts is set', async t => {
5757
t.strictSame(result, [])
5858
})
5959

60+
t.test('returns unreviewed nodes when ignoreScripts is set but includeWhenIgnored is true', async t => {
61+
const checkAllowScripts = mockCheck(t)
62+
const result = await checkAllowScripts({
63+
arb: arb({
64+
nodes: [node({ name: 'a', scripts: { install: 'do-stuff' } })],
65+
ignoreScripts: true,
66+
}),
67+
npm: { flatOptions: {} },
68+
includeWhenIgnored: true,
69+
})
70+
t.equal(result.length, 1)
71+
t.strictSame(result[0].scripts, { install: 'do-stuff' })
72+
})
73+
6074
t.test('returns [] when dangerouslyAllowAllScripts is set', async t => {
6175
const checkAllowScripts = mockCheck(t)
6276
const result = await checkAllowScripts({

0 commit comments

Comments
 (0)