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
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: latest
node-version: lts/*
- uses: salesforcecli/github-workflows/.github/actions/yarnInstallWithRetries@main
- run: yarn build
- name: Remove package managers
Expand Down
5 changes: 5 additions & 0 deletions src/spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ const debug = makeDebug('@oclif/plugin-plugins:spawn')

export async function spawn(modulePath: string, args: string[] = [], {cwd, logLevel}: ExecOptions): Promise<Output> {
return new Promise((resolve, reject) => {
let argv0: string | undefined
if (modulePath.endsWith('.js')) {
const quote = process.platform === 'win32' ? `"${modulePath}"` : modulePath
args.unshift(quote)
modulePath = process.execPath
if (process.platform === 'win32' && modulePath.includes(' ')) {
argv0 = `"${modulePath}"`
}
}

debug('modulePath', modulePath)
Expand All @@ -37,6 +41,7 @@ export async function spawn(modulePath: string, args: string[] = [], {cwd, logLe
},
stdio: 'pipe',
windowsVerbatimArguments: true,
...(argv0 && {argv0}),
...(process.platform === 'win32' && modulePath.toLowerCase().endsWith('.cmd') && {shell: true}),
})

Expand Down
51 changes: 48 additions & 3 deletions test/spawn.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {expect} from 'chai'
import {chmodSync, mkdirSync, mkdtempSync, rmSync, writeFileSync} from 'node:fs'
import {chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync} from 'node:fs'
import {tmpdir} from 'node:os'
import {join} from 'node:path'
import {dirname, join} from 'node:path'
import {fileURLToPath} from 'node:url'

import {spawn} from '../src/spawn.js'

Expand Down Expand Up @@ -48,7 +49,51 @@ describe('spawn', () => {
expect(result.stdout).to.include('spaces-ok')
})

it('should not modify non-.js module paths', async () => {
it('should handle process.execPath containing spaces', async function () {
if (process.platform === 'win32') return this.skip()
const nodeDir = join(tempDir, 'path with spaces', 'bin')
mkdirSync(nodeDir, {recursive: true})
const nodeLink = join(nodeDir, 'node')
symlinkSync(process.execPath, nodeLink)

const script = join(tempDir, 'exec-path-test.js')
writeFileSync(script, 'console.log("execpath-ok")\n')
chmodSync(script, '755')

const originalExecPath = process.execPath
try {
Object.defineProperty(process, 'execPath', {configurable: true, value: nodeLink, writable: true})
const result = await spawn(script, [], {cwd: tempDir, logLevel: 'silent'})
expect(result.stdout).to.include('execpath-ok')
} finally {
Object.defineProperty(process, 'execPath', {configurable: true, value: originalExecPath, writable: true})
}
})

it('must use windowsVerbatimArguments to prevent argument injection (security)', () => {
const spawnSrc = readFileSync(join(dirname(fileURLToPath(import.meta.url)), '..', 'src', 'spawn.ts'), 'utf8')
expect(spawnSrc).to.include('windowsVerbatimArguments: true')
})

it('should not interpret shell metacharacters in arguments', async () => {
const script = join(tempDir, 'echo-args.js')
writeFileSync(script, 'console.log(JSON.stringify(process.argv.slice(2)))\n')
chmodSync(script, '755')

const result = await spawn(script, ['$(whoami)', '`whoami`', '%PATH%', '|calc.exe'], {
cwd: tempDir,
logLevel: 'silent',
})
const output = result.stdout.join(' ')

expect(output).to.include('$(whoami)')
expect(output).to.include('`whoami`')
expect(output).to.include('%PATH%')
expect(output).to.include('|calc.exe')
})

it('should not modify non-.js module paths', async function () {
if (process.platform === 'win32') return this.skip()
const script = join(tempDir, 'test-bin')
writeFileSync(script, `#!/usr/bin/env bash\necho "bin-ok"\n`)
chmodSync(script, '755')
Expand Down
Loading