/** * @file Child process spawning utilities with cross-platform support. Provides * enhanced spawn functionality with stdio handling and error management. * SECURITY: Array-Based Arguments Prevent Command Injection This module uses * array-based arguments for all command execution, which is the PRIMARY * DEFENSE against command injection attacks. When you pass arguments as an * array to spawn(): spawn('npx', ['sfw', tool, ...args], { shell: true }) * Node.js handles escaping automatically. Each argument is passed directly to * the OS without shell interpretation. Shell metacharacters like ; | & $ ( ) * ` are treated as LITERAL STRINGS, not as commands. This approach is secure * even when shell: true is used on Windows for .cmd/.bat file resolution. * UNSAFE ALTERNATIVE (not used in this codebase): spawn(`npx sfw ${tool} * ${args.join(' ')}`, { shell: true }) // ✖ VULNERABLE String concatenation * allows injection. For example, if tool = "foo; rm -rf /", the shell would * execute both commands. Array-based arguments prevent this. References: * * - https://nodejs.org/api/child_process.html#child_processspawncommand-args-options * - https://cheatsheetseries.owasp.org/cheatsheets/Nodejs_Security_Cheat_Sheet.html */ import type { BufferEncoding, SpawnExtra, SpawnOptions, SpawnResult, SpawnStdioResult, SpawnSyncOptions, SpawnSyncReturns } from './types.js'; /** * Spawn a child process and return a promise that resolves when it completes, * with error handling, output capture, and cross-platform support. * * SECURITY: array-based arguments are the PRIMARY DEFENSE against command * injection. Everything in `args` reaches the OS without shell interpretation, * so shell metacharacters stay literal rather than becoming operators. That * holds even when `shell: true` is set for Windows .cmd/.bat execution, * because Node escapes each argument before handing it to the shell. * * @example * // DO: array arguments stay safe however hostile userMessage is. * spawn('git', ['commit', '-m', userMessage]) * * @example * // NEVER: concatenating into one string lets a crafted userMessage close * // the quote and append a command of its own. * spawn(`git commit -m "${userMessage}"`, { shell: true }) * * @example * // stdin is exposed for interactive processes. * const result = spawn('cat', []) * result.stdin?.write('Hello\n') * result.stdin?.end() * const { stdout } = await result * * @example * // throws: false resolves with the non-zero code instead of rejecting, * // so callers branch on `code` rather than try/catch. * const { code } = await spawn('git', ['grep', '-q', needle], { * throws: false, * }) * * @throws {SpawnError} When the process exits non-zero or a signal kills it — * unless `throws: false`, which resolves with that result instead. A launch * failure (`'ENOENT'`) always rejects. */ export declare function spawn(cmd: string, args?: string[] | readonly string[] | undefined): SpawnResult; export declare function spawn(cmd: string, args: string[] | readonly string[] | undefined, options: SpawnOptions & { stdioString?: true | undefined; }, extra?: SpawnExtra | undefined): SpawnResult; export declare function spawn(cmd: string, args: string[] | readonly string[] | undefined, options: SpawnOptions & { stdioString: false; }, extra?: SpawnExtra | undefined): SpawnResult; export declare function spawn(cmd: string, args?: string[] | readonly string[] | undefined, options?: SpawnOptions | undefined, extra?: SpawnExtra | undefined): SpawnResult; /** * The resolved-result shape for a spawn rejection whose child actually RAN and * exited — a numeric exit code, or a terminating signal. This is what * `throws: false` resolves with instead of rejecting: the same * `cmd`/`args`/`code`/`signal`/`stdout`/`stderr` fields the success path * carries, plus the `exitCode` alias the success path also sets. A LAUNCH * failure — the command was never found or never started, so `.code` is a * string like `'ENOENT'` and no signal terminated it — returns undefined, * because there is no exit code to report and the rejection must stand. */ export declare function spawnExitResult(error: unknown): (SpawnStdioResult & { exitCode: number; }) | undefined; /** * Synchronously spawn a child process and wait for it to complete, returning * all output and exit information. * * WARNING: this blocks the event loop. Use {@link spawn} for async work. * * @example * // Basic synchronous spawn * const result = spawnSync('git', ['status']) * console.log(result.stdout) * console.log(result.status) // exit code * * @example * // With options * const result = spawnSync('npm', ['install'], { * cwd: '/path/to/project', * stdioString: true, * }) * if (result.status !== 0) { * console.error(result.stderr) * } * * @example * // Get raw buffer output * const result = spawnSync('cat', ['binary-file'], { * stdioString: false, * }) * console.log(result.stdout) // Buffer * * @example * // Handle process errors * const result = spawnSync('nonexistent-command') * if (result.error) { * console.error('Failed to spawn:', result.error) * } * * @param {string} cmd - Command to execute. * @param {string[] | readonly string[] | undefined} args - Array of arguments. * @param {SpawnSyncOptions | undefined} options - Spawn options for process * configuration. * * @returns {SpawnSyncReturns} Process result with exit code * and captured output. */ export declare function spawnSync(cmd: string, args?: string[] | readonly string[] | undefined): SpawnSyncReturns; export declare function spawnSync(cmd: string, args: string[] | readonly string[] | undefined, options: SpawnSyncOptions & { stdioString?: true | undefined; encoding?: Exclude | undefined; }): SpawnSyncReturns; export declare function spawnSync(cmd: string, args: string[] | readonly string[] | undefined, options: SpawnSyncOptions & { stdioString: false; }): SpawnSyncReturns; export declare function spawnSync(cmd: string, args: string[] | readonly string[] | undefined, options: SpawnSyncOptions & { encoding: 'buffer' | null; }): SpawnSyncReturns; export declare function spawnSync(cmd: string, args?: string[] | readonly string[] | undefined, options?: SpawnSyncOptions | undefined): SpawnSyncReturns;