Files
OmniRoute/bin/cli/utils/winShellArgs.mjs
Samar Nathani 0eaf0b3d32 fix(cli): gate cmd.exe arg escaping on the resolved spawn shell (#17178)
On Windows the launch call sites (launch, launch-codex, fcc-claude and run)
cmd.exe-escaped pass-through arguments unconditionally. That escaping is only
correct on the `shell: true` path (npm .cmd shims, #8837); the native-installer
path spawns a real claude.exe/codex.exe with `shell: undefined` (#9454), where
the escaped text reached the binary literally (e.g. `^^^"-p^^^"`) and every
launch was silently mangled with exit code 0.

Add one shared quoteShellArgsForSpawn(args, platform, useShell) helper and gate
every spawn/plan site on the resolved spec's `shell`, so the `run` dry-run plan
keeps matching what actually spawns.

Fixes #17177
2026-10-22 04:48:42 -03:00

66 lines
2.6 KiB
JavaScript

/**
* Argument escaping for child processes spawned with `shell: true` on Windows.
*
* The launchers (`omniroute launch`, `omniroute launch-codex`) must go through
* cmd.exe on win32 because the target binaries are npm `.cmd` shims that Node
* cannot exec directly (CVE-2024-27980). With `shell: true` Node joins argv with
* plain spaces and no escaping at all (the DEP0190 warning), so anything with a
* space, a quote or a cmd metacharacter reaches the child mangled.
*/
/** cmd.exe metacharacters that stay live inside a quoted argument. */
const WIN_META_CHARS = /([()\][%!^"`<>&|;, *?])/g;
/**
* Escape one argument for a cmd.exe command line built by `shell: true`.
*
* Two layers, in order:
* 1. the CRT argv rules the target binary parses (double the backslashes that
* precede a quote, escape embedded quotes, wrap in quotes);
* 2. cmd.exe's metacharacters, caret-escaped — applied TWICE because the
* target is an npm `.cmd` shim that forwards `%*` to node, so the line is
* parsed by cmd a second time. Single-escaping truncated any argument at
* the first `&` or `|`. (Same rule as cross-spawn's doubleEscapeMetaChars.)
*
* @param {unknown} arg
* @returns {string}
*/
export function escapeWindowsShellArg(arg) {
const s = String(arg);
if (s === "") return '""';
let out = s.replace(/(\\*)"/g, '$1$1\\"').replace(/(\\*)$/, "$1$1");
out = `"${out}"`;
return out.replace(WIN_META_CHARS, "^$1").replace(WIN_META_CHARS, "^$1");
}
/**
* Escape a whole argv for the `shell: true` path. Off Windows there is no shell,
* so argv is passed through untouched.
*
* @param {string[]} args
* @param {NodeJS.Platform|string} platform
* @returns {string[]}
*/
export function quoteShellArgs(args, platform) {
const list = [...(args ?? [])];
return platform === "win32" ? list.map(escapeWindowsShellArg) : list;
}
/**
* Quote argv for a spawn ONLY when the child actually goes through a shell.
*
* `quoteShellArgs` is mandatory on the `shell: true` path (cmd.exe reparses the
* joined command line — #8837). The native-installer path spawns a real `.exe`
* with `shell: undefined` (#9454), where the cmd.exe escaping reaches the
* binary literally and mangles every pass-through argument (#17177) — gate on
* the resolved spawn spec's `shell` before handing argv to spawn().
*
* @param {string[]} args
* @param {NodeJS.Platform|string} platform
* @param {boolean|undefined} useShell the resolved spawn spec's `shell`
* @returns {string[]}
*/
export function quoteShellArgsForSpawn(args, platform, useShell) {
return useShell ? quoteShellArgs(args, platform) : [...(args ?? [])];
}