Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dev for Ruby apps #4522

Merged
merged 4 commits into from
Dec 4, 2024
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
32 changes: 32 additions & 0 deletions packages/cli-kit/src/public/node/system.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as system from './system.js'
import {execa} from 'execa'
import {describe, expect, test, vi} from 'vitest'
import which from 'which'

vi.mock('which')
vi.mock('execa')

describe('captureOutput', () => {
test('runs the command when it is not found in the current directory', async () => {
// Given
vi.mocked(which.sync).mockReturnValueOnce('/system/command')
vi.mocked(execa).mockResolvedValueOnce({stdout: undefined} as any)

// When
const got = await system.captureOutput('command', [], {cwd: '/currentDirectory'})

// Then
expect(got).toEqual(undefined)
})

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.captureOutput('command', [], {cwd: '/currentDirectory'})

// Then
await expect(got).rejects.toThrowError('Skipped run of unsecure binary command found in the current directory.')
})
})
18 changes: 12 additions & 6 deletions packages/cli-kit/src/public/node/system.ts
gonzaloriestra marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {renderWarning} from './ui.js'
import {shouldDisplayColors, outputDebug} from '../../public/node/output.js'
import {execa, ExecaChildProcess} from 'execa'
import which from 'which'
import {delimiter} from 'pathe'
import type {Writable, Readable} from 'stream'

export interface ExecOptions {
Expand Down Expand Up @@ -99,10 +100,11 @@ function buildExec(command: string, args: string[], options?: ExecOptions): Exec
if (shouldDisplayColors()) {
env.FORCE_COLOR = '1'
}
checkCommandSafety(command)
const executionCwd = options?.cwd ?? cwd()
checkCommandSafety(command, {cwd: executionCwd})
const commandProcess = execa(command, args, {
env,
cwd: options?.cwd,
cwd: executionCwd,
input: options?.input,
stdio: options?.stdio,
stdin: options?.stdin,
Expand All @@ -115,14 +117,18 @@ function buildExec(command: string, args: string[], options?: ExecOptions): Exec
outputDebug(`
Running system process:
· Command: ${command} ${args.join(' ')}
· Working directory: ${options?.cwd ?? cwd()}
· Working directory: ${executionCwd}
`)
return commandProcess
}

function checkCommandSafety(command: string) {
const commandDirectory = dirname(which.sync(command))
if (commandDirectory === cwd()) {
function checkCommandSafety(command: string, _options: {cwd: string}): void {
gonzaloriestra marked this conversation as resolved.
Show resolved Hide resolved
const pathIncludingLocal = `${_options.cwd}${delimiter}${process.env.PATH}`
const commandPath = which.sync(command, {
nothrow: true,
path: pathIncludingLocal,
gonzaloriestra marked this conversation as resolved.
Show resolved Hide resolved
})
if (commandPath && dirname(commandPath) === _options.cwd) {
const headline = ['Skipped run of unsecure binary', {command}, 'found in the current directory.']
const body = 'Please remove that file or review your current PATH.'
renderWarning({headline, body})
Expand Down
Loading