Skip to content

Commit 774875b

Browse files
authored
fix(arborist): keep bin links for allowScripts-denied packages (#9686)
A package denied via `allowScripts` now keeps its `.bin` links. Fixes #9681.
1 parent abd8c6b commit 774875b

2 files changed

Lines changed: 56 additions & 18 deletions

File tree

‎workspaces/arborist/lib/arborist/rebuild.js‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,21 @@ module.exports = cls => class Builder extends cls {
199199
const { package: { bin, scripts = {} } } = node.target
200200
const { preinstall, install, postinstall, prepare } = scripts
201201
const tests = { bin, preinstall, install, postinstall, prepare }
202+
// A denied package (allowScripts resolves to `false`) still gets its
203+
// bins linked; only its lifecycle scripts are skipped, matching
204+
// --ignore-scripts (npm/cli#9681). --dangerously-allow-all-scripts
205+
// bypasses the gate.
206+
const scriptsDenied =
207+
!this.options.dangerouslyAllowAllScripts &&
208+
isScriptAllowed(node, this.options.allowScripts) === false
202209
for (const [key, has] of Object.entries(tests)) {
203-
if (has) {
204-
this.#queues[key].push(node)
210+
if (!has) {
211+
continue
205212
}
213+
if (key !== 'bin' && scriptsDenied) {
214+
continue
215+
}
216+
this.#queues[key].push(node)
206217
}
207218
}
208219
timeEnd()
@@ -226,18 +237,6 @@ module.exports = cls => class Builder extends cls {
226237
return
227238
}
228239

229-
// Phase 1 allowScripts gate: a `false` verdict from the policy matcher
230-
// means the user explicitly denied install scripts for this node, so skip
231-
// it. `true` and `null` (unreviewed) both fall through to the existing
232-
// detection logic — unreviewed nodes still run their scripts in Phase 1
233-
// and are surfaced via the post-reify advisory warning. The global
234-
// --ignore-scripts kill switch in #build() still takes precedence, and
235-
// --dangerously-allow-all-scripts bypasses this gate entirely.
236-
if (!this.options.dangerouslyAllowAllScripts &&
237-
isScriptAllowed(node, this.options.allowScripts) === false) {
238-
return
239-
}
240-
241240
if (this.#oldMeta === null) {
242241
const { root: { meta } } = node
243242
this.#oldMeta = meta && meta.loadedFromDisk &&

‎workspaces/arborist/test/arborist/rebuild.js‎

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,9 @@ t.test('do not run scripts if ignoreScripts=true', async t => {
114114
t.throws(() => fs.statSync(file), 'bundle build script not run')
115115
})
116116

117-
t.test('allowScripts deny entry skips the build set entry for that node', async t => {
118-
// Verifies the deny gate in #addToBuildSet: when `allowScripts` resolves
119-
// a node's policy to `false`, that node's scripts are skipped while
120-
// others continue to run.
117+
t.test('allowScripts deny entry skips scripts for the denied node', async t => {
118+
// Deny gate in #buildQueues: a `false` policy skips that node's
119+
// scripts while others still run.
121120
const path = fixture(t, 'testing-rebuild-script-env-flags')
122121
const arb = newArb({
123122
path,
@@ -154,6 +153,46 @@ t.test('dangerouslyAllowAllScripts bypasses the deny gate', async t => {
154153
)
155154
})
156155

156+
t.test('allowScripts deny keeps the bin link (npm/cli#9681)', async t => {
157+
// Denying a package's scripts must not drop its bin links: the script
158+
// is skipped but `.bin/<name>` is still created, like --ignore-scripts.
159+
const path = t.testdir({
160+
'package.json': JSON.stringify({
161+
name: 'project',
162+
version: '1.0.0',
163+
dependencies: { 'denied-dep': '1.0.0' },
164+
}),
165+
node_modules: {
166+
'denied-dep': {
167+
'package.json': JSON.stringify({
168+
name: 'denied-dep',
169+
version: '1.0.0',
170+
bin: { 'denied-bin': 'index.js' },
171+
scripts: {
172+
postinstall: `node -e "require('fs').writeFileSync('ran', '1')"`,
173+
},
174+
}),
175+
'index.js': '#!/usr/bin/env node\nconsole.log("hi")\n',
176+
},
177+
},
178+
})
179+
const arb = newArb({
180+
path,
181+
allowScripts: { 'denied-dep': false },
182+
})
183+
await arb.rebuild()
184+
185+
t.throws(
186+
() => fs.statSync(resolve(path, 'node_modules/denied-dep/ran')),
187+
'denied postinstall did not run'
188+
)
189+
t.equal(
190+
fs.statSync(resolve(path, 'node_modules/.bin/denied-bin')).isFile(),
191+
true,
192+
'denied package bin link is still created'
193+
)
194+
})
195+
157196
t.test('do nothing if ignoreScripts=true and binLinks=false', async t => {
158197
const path = fixture(t, 'testing-rebuild-bundle-reified')
159198
const file = resolve(path, 'node_modules/@isaacs/testing-rebuild-bundle-a/node_modules/@isaacs/testing-rebuild-bundle-b/cwd')

0 commit comments

Comments
 (0)