Skip to content
Closed
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
11 changes: 11 additions & 0 deletions packages/cli-kit/src/public/node/system.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,17 @@ describe('execCommand', () => {
// Then
expect(execaCommand).toHaveBeenCalledWith('cat', expect.objectContaining({stdin: 'inherit'}))
})

test('raises an error if the command to run is found in the current directory', async () => {
// Given
vi.mocked(which.sync).mockReturnValueOnce('/currentDirectory/command')

// When
const got = system.execCommand('command', {cwd: '/currentDirectory'})

// Then
await expect(got).rejects.toThrowError('Skipped run of unsecure binary command found in the current directory.')
})
})

describe('isStdinPiped', () => {
Expand Down
13 changes: 13 additions & 0 deletions packages/cli-kit/src/public/node/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ export async function execCommand(command: string, options?: ExecOptions): Promi
env.FORCE_COLOR = '1'
}
const executionCwd = options?.cwd ?? cwd()
const [cmd] = parseCommand(command)
if (cmd) {
// Security: Prevent execution of binaries from the current working directory to avoid PATH hijacking.
checkCommandSafety(cmd, {cwd: executionCwd})
}
try {
await execaCommand(command, {
env,
Expand Down Expand Up @@ -321,6 +326,14 @@ function buildExec(
return commandProcess
}

/**
* Check if the command is safe to run.
* It prevents the execution of binaries found in the current working directory,
* which is a security risk (PATH hijacking).
*
* @param command - Command to check.
* @param _options - Options containing the current working directory.
*/
function checkCommandSafety(command: string, _options: {cwd: string}): void {
const pathIncludingLocal = `${_options.cwd}${delimiter}${process.env.PATH}`
const commandPath = which.sync(command, {
Expand Down
Loading