Skip to content

Commit b2e6338

Browse files
fix(arborist): don't flag inert optional deps in strict-allow-scripts (#9602)
Backport of #9597 to `release/v11`. Co-authored-by: Jamie Magee <[email protected]>
1 parent 6ad5715 commit b2e6338

6 files changed

Lines changed: 58 additions & 0 deletions

File tree

‎tap-snapshots/test/lib/docs.js.test.cjs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,6 +1942,10 @@ silently skipped; this setting only affects unreviewed entries.
19421942
\`--ignore-scripts\` and \`--dangerously-allow-all-scripts\` both override this
19431943
setting.
19441944
1945+
Optional dependencies that cannot be installed on the current platform or
1946+
engine (a non-matching \`os\`, \`cpu\`, or \`libc\`) are not flagged, because
1947+
their install scripts never run.
1948+
19451949
19461950
19471951
#### \`strict-peer-deps\`

‎test/lib/utils/strict-allow-scripts-preflight.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@ t.test('throws when unreviewed install scripts exist (idealTree path)', async t
7676
)
7777
})
7878

79+
t.test('passes when the only unreviewed node is inert (platform-incompatible optional dep)', async t => {
80+
// An inert dep is in the ideal tree but removed before any script runs, so
81+
// strict mode must not reject it (npm/cli#9562).
82+
const inertNode = { ...node({ name: 'fsevents' }), inert: true }
83+
const arb = makeArb({ ideal: tree([inertNode]) })
84+
await preflight({
85+
arb,
86+
npm: { flatOptions: { strictAllowScripts: true } },
87+
idealTreeOpts: {},
88+
})
89+
t.pass('no error thrown for inert node')
90+
})
91+
7992
t.test('passes when all install-script nodes are explicitly approved', async t => {
8093
const arb = makeArb({
8194
ideal: tree([node({ name: 'canvas' })]),

‎workspaces/arborist/lib/unreviewed-scripts.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ const collectUnreviewedScripts = async ({
5050
// its own lifecycle scripts.
5151
continue
5252
}
53+
if (node.inert) {
54+
// Inert = an optional dep that can't be installed here (failed the
55+
// os/cpu/libc or engine check, or failed to load). reify drops it
56+
// before any script runs, so its install scripts never execute and it
57+
// must not be flagged (npm/cli#9562).
58+
continue
59+
}
5360

5461
const verdict = isScriptAllowed(node, resolvedPolicy)
5562
if (verdict === true || verdict === false) {

‎workspaces/arborist/test/unreviewed-scripts.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const node = ({
2828
isWorkspace = false,
2929
isLink = false,
3030
inBundle = false,
31+
inert = false,
3132
resolved,
3233
} = {}) => ({
3334
name,
@@ -39,6 +40,7 @@ const node = ({
3940
isWorkspace,
4041
isLink,
4142
inBundle,
43+
inert,
4244
isRegistryDependency: true,
4345
package: { name, version, scripts },
4446
})
@@ -82,6 +84,14 @@ t.test('collectUnreviewedScripts', async t => {
8284
t.strictSame(result, [])
8385
})
8486

87+
t.test('skips inert (platform/engine-incompatible) optional nodes', async t => {
88+
const result = await collectUnreviewedScripts({
89+
tree: tree([node({ name: 'fsevents', scripts: { install: 'x' }, inert: true })]),
90+
policy: null,
91+
})
92+
t.strictSame(result, [])
93+
})
94+
8595
t.test('skips nodes with no install-relevant scripts', async t => {
8696
const result = await collectUnreviewedScripts({
8797
tree: tree([node({ scripts: { test: 'jest' } })]),

‎workspaces/config/lib/definitions/definitions.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2371,6 +2371,10 @@ const definitions = {
23712371
always silently skipped; this setting only affects unreviewed entries.
23722372
\`--ignore-scripts\` and \`--dangerously-allow-all-scripts\` both
23732373
override this setting.
2374+
2375+
Optional dependencies that cannot be installed on the current platform
2376+
or engine (a non-matching \`os\`, \`cpu\`, or \`libc\`) are not flagged,
2377+
because their install scripts never run.
23742378
`,
23752379
flatten,
23762380
}),

‎workspaces/libnpmexec/test/strict-allow-scripts-preflight.js‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,26 @@ t.test('throws when unreviewed scripts are present', async t => {
6464
)
6565
})
6666

67+
t.test('resolves when the only unreviewed node is inert', async t => {
68+
// Inert deps (platform/engine-incompatible) are removed before any script
69+
// runs, so strict mode must not reject them (npm/cli#9562).
70+
const inertTree = {
71+
inventory: new Map([
72+
['node_modules/has-scripts', {
73+
name: 'has-scripts',
74+
location: 'node_modules/has-scripts',
75+
inert: true,
76+
package: { scripts: { install: 'node-gyp rebuild' } },
77+
}],
78+
]),
79+
}
80+
const arb = fakeArb(inertTree)
81+
await t.resolves(
82+
strictAllowScriptsPreflight(arb, { strictAllowScripts: true }),
83+
'no error when the unreviewed node is inert'
84+
)
85+
})
86+
6787
t.test('resolves when no unreviewed scripts are present', async t => {
6888
const cleanTree = {
6989
inventory: new Map([

0 commit comments

Comments
 (0)