Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion workspaces/libnpmexec/lib/run-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ const run = async ({
// necessary for preventing bash/cmd keywords from overriding
if (!isWindowsShell) {
if (args.length > 0) {
args[0] = '"' + args[0] + '"'
// single-quote so shell metacharacters in the executable name are taken
// literally; double quotes still expand $(), backticks, $var and "
args[0] = `'${args[0].replace(/'/g, `'\\''`)}'`
}
}

Expand Down
14 changes: 14 additions & 0 deletions workspaces/libnpmexec/test/run-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,20 @@ t.test('isWindows', async t => {
await runScript()
})

t.test('escapes executable name to neutralize shell metacharacters', async t => {
let pkg
const { runScript } = await mockRunScript(t, {
'ci-info': { isCI: true },
'@npmcli/run-script': async (opts) => {
pkg = opts.pkg
},
'../lib/is-windows.js': false,
})

await runScript({ args: [`evil'; touch pwned #`] })
t.equal(pkg.scripts.npx, `'evil'\\''; touch pwned #'`)
})

t.test('isNotWindows', async t => {
const { runScript } = await mockRunScript(t, {
'ci-info': { isCI: true },
Expand Down
Loading