Skip to content

Commit 64d4b4b

Browse files
fix(config): avoid exporting persistent allow-scripts (#9937)
Backport of #9913 to `release/v11`. Co-authored-by: Fnine59 <[email protected]>
1 parent afdc5bd commit 64d4b4b

4 files changed

Lines changed: 54 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ Each name is matched against a dependency's resolved identity, not against
327327
the package's self-reported name. \`--ignore-scripts\` and
328328
\`--dangerously-allow-all-scripts\` both override this setting.
329329
330-
330+
This value is not exported to the environment for child processes.
331331
332332
#### \`allow-scripts-pending\`
333333

‎test/lib/utils/resolve-allow-scripts.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,22 @@ t.test('--allow-scripts CLI flag is rejected in project-scoped installs', async
8686
)
8787
})
8888

89+
t.test('allow-scripts environment policy is rejected in project-scoped installs', async t => {
90+
const mock = await mockNpm(t, {
91+
prefixDir: {
92+
'package.json': JSON.stringify({ name: 'p' }),
93+
},
94+
globals: {
95+
'process.env.npm_config_allow_scripts': 'canvas',
96+
},
97+
})
98+
const resolveAllowScripts = loadResolver(t)
99+
await t.rejects(
100+
resolveAllowScripts(mock.npm),
101+
{ code: 'EALLOWSCRIPTS', message: /--allow-scripts is not allowed/ }
102+
)
103+
})
104+
89105
t.test('--allow-scripts CLI flag is accepted in global installs (RFC layer 1 wins)', async t => {
90106
const mock = await mockNpm(t, {
91107
prefixDir: {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ const definitions = {
258258
default: '',
259259
type: [String, Array],
260260
hint: '<package-list>',
261+
envExport: false,
261262
description: `
262263
Comma-separated list of packages whose install-time lifecycle scripts
263264
(\`preinstall\`, \`install\`, \`postinstall\`, and \`prepare\` for

‎workspaces/config/test/set-envs.js‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,39 @@ t.test('dont set configs marked as envExport:false', t => {
241241
t.strictSame(env, { ...extras }, 'not exported, because envExport=false')
242242
t.end()
243243
})
244+
245+
t.test('does not export persistent allow-scripts config', t => {
246+
const { definitions, defaults } = mockDefinitions(t)
247+
const userConf = Object.create(defaults)
248+
userConf['allow-scripts'] = 'canvas'
249+
const envConf = Object.create(userConf)
250+
const cliConf = Object.create(envConf)
251+
const env = {}
252+
const config = {
253+
list: [cliConf, envConf],
254+
env,
255+
defaults,
256+
definitions,
257+
execPath,
258+
globalPrefix,
259+
localPrefix,
260+
npmPath,
261+
npmBin,
262+
}
263+
264+
setEnvs(config)
265+
t.equal(
266+
env.npm_config_allow_scripts,
267+
undefined,
268+
'persistent policy is reloaded instead of exported to lifecycle scripts'
269+
)
270+
envConf['allow-scripts'] = 'sharp'
271+
env.npm_config_allow_scripts = 'sharp'
272+
setEnvs(config)
273+
t.equal(
274+
env.npm_config_allow_scripts,
275+
'sharp',
276+
'an explicit environment policy remains inherited'
277+
)
278+
t.end()
279+
})

0 commit comments

Comments
 (0)