Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
address review (codex): caret-escape cmd.exe metacharacters in unquoted args
[._.]/ Adam Eivycb7a41266142
diff --git a/server/lib/aiToolkit/providers.js b/server/lib/aiToolkit/providers.jsindex 6819002c5b..2789ddaa09 100644--- a/server/lib/aiToolkit/providers.js+++ b/server/lib/aiToolkit/providers.js@@ -59,11 +59,30 @@ const WIN_BATCH_EXT_RE = /\.(cmd|bat)$/i;*/function prepareWindowsSafeSpawn(command, args, isWin32 = process.platform === 'win32') {if (isWin32 && WIN_BATCH_EXT_RE.test(command)) {- return { command: 'cmd.exe', args: ['/c', command, ...args] };+ return { command: 'cmd.exe', args: ['/c', command, ...args.map(escapeCmdMetacharsIfUnquoted)] };}return { command, args };}+// cmd.exe metacharacters that act as command separators / redirection /+// grouping on its raw command line.+const CMD_METACHAR_RE = /[&|<>^()]/g;+// Node's argv→command-line quoting wraps an argument in literal double+// quotes only when it contains whitespace or a `"`; characters inside that+// quoted span are not re-interpreted by cmd.exe.+const NEEDS_NODE_QUOTING_RE = /[\s"]/;++/**+ * Caret-escape cmd.exe metacharacters, but ONLY when Node's own quoting+ * would otherwise leave the argument unquoted on cmd.exe's raw command line.+ * Mirrors server/lib/bufferedSpawn.js for self-containment.+ */+function escapeCmdMetacharsIfUnquoted(value) {+ const str = String(value);+ if (NEEDS_NODE_QUOTING_RE.test(str)) return str;+ return str.replace(CMD_METACHAR_RE, '^$&');+}+const execFileAsync = promisify(execFile);// Tool-use (function-calling) capable model families. Inlined here because thediff --git a/server/lib/aiToolkit/runner.js b/server/lib/aiToolkit/runner.jsindex 91ec881401..a0a85280aa 100644--- a/server/lib/aiToolkit/runner.js+++ b/server/lib/aiToolkit/runner.js@@ -55,11 +55,32 @@ const WIN_BATCH_EXT_RE = /\.(cmd|bat)$/i;*/function prepareWindowsSafeSpawn(command, args, isWin32 = IS_WIN32) {if (isWin32 && WIN_BATCH_EXT_RE.test(command)) {- return { command: 'cmd.exe', args: ['/c', command, ...args] };+ return { command: 'cmd.exe', args: ['/c', command, ...args.map(escapeCmdMetacharsIfUnquoted)] };}return { command, args };}+// cmd.exe metacharacters that act as command separators / redirection /+// grouping on its raw command line.+const CMD_METACHAR_RE = /[&|<>^()]/g;+// Node's argv→command-line quoting wraps an argument in literal double+// quotes only when it contains whitespace or a `"`; characters inside that+// quoted span are not re-interpreted by cmd.exe.+const NEEDS_NODE_QUOTING_RE = /[\s"]/;++/**+ * Caret-escape cmd.exe metacharacters, but ONLY when Node's own quoting+ * would otherwise leave the argument unquoted on cmd.exe's raw command line+ * (an argument WITH whitespace is already double-quoted by Node, and+ * caret-escaping it too would inject literal `^` into the value the target+ * program receives). Mirrors server/lib/bufferedSpawn.js for self-containment.+ */+function escapeCmdMetacharsIfUnquoted(value) {+ const str = String(value);+ if (NEEDS_NODE_QUOTING_RE.test(str)) return str;+ return str.replace(CMD_METACHAR_RE, '^$&');+}+// On Windows, taskkill (used below) runs in a separate detached process that// never touches the original ChildProcess object — `.killed` is set// synchronously here (mirroring what Node's own `child.kill()` does on thediff --git a/server/lib/bufferedSpawn.js b/server/lib/bufferedSpawn.jsindex 0681a9bfec..49c44c3157 100644--- a/server/lib/bufferedSpawn.js+++ b/server/lib/bufferedSpawn.js@@ -96,6 +96,13 @@ const WIN_BATCH_EXT_RE = /\.(cmd|bat)$/i;* A resolved native `.exe`/`.com` target needs no wrapping at all — it's* directly launchable, so it's returned unchanged.*+ * Each arg is also passed through `escapeCmdMetacharsIfUnquoted` (see its+ * docstring) — Node's own argv→command-line quoting only wraps an arg in+ * literal double quotes when it contains whitespace/a quote; an arg with+ * none of those reaches cmd.exe's raw command line UNQUOTED, so a bare+ * metacharacter like `&` in it would still be interpreted by cmd.exe as a+ * command separator despite `shell:false`.+ ** @param {string} command - bare name, or a resolveWindowsExecutable result* @param {string[]} args* @param {boolean} [isWin32] - injectable for tests; defaults to the real platform@@ -103,11 +110,37 @@ const WIN_BATCH_EXT_RE = /\.(cmd|bat)$/i;*/export function prepareWindowsSafeSpawn(command, args, isWin32 = IS_WIN32) {if (isWin32 && WIN_BATCH_EXT_RE.test(command)) {- return { command: 'cmd.exe', args: ['/c', command, ...args] };+ return { command: 'cmd.exe', args: ['/c', command, ...args.map(escapeCmdMetacharsIfUnquoted)] };}return { command, args };}+// cmd.exe metacharacters that act as command separators / redirection /+// grouping on its raw command line.+const CMD_METACHAR_RE = /[&|<>^()]/g;+// Node's argv→command-line quoting (CommandLineToArgvW rules, used because+// cmd.exe is a normal executable target from Node's point of view) wraps an+// argument in literal double quotes only when it contains whitespace or a+// `"` — characters inside that quoted span are not re-interpreted by cmd.exe.+const NEEDS_NODE_QUOTING_RE = /[\s"]/;++/**+ * Caret-escape cmd.exe metacharacters in an argument, but ONLY when Node's+ * own quoting (see NEEDS_NODE_QUOTING_RE above) would otherwise leave it+ * unquoted on cmd.exe's raw command line. An argument containing whitespace+ * is deliberately left untouched here — it's already wrapped in literal+ * double quotes by Node, and caret-escaping it too would inject literal `^`+ * characters into the value the target program receives, corrupting it.+ * This is the narrower, conservative fix for the specific gap: an argument+ * with NO whitespace but a metacharacter (e.g. `foo&calc`) reaches cmd.exe+ * unquoted and unprotected without this.+ */+function escapeCmdMetacharsIfUnquoted(value) {+ const str = String(value);+ if (NEEDS_NODE_QUOTING_RE.test(str)) return str;+ return str.replace(CMD_METACHAR_RE, '^$&');+}+// Cap buffered stdout/stderr so a runaway child can't exhaust memory; we only// ever surface a tail of the output anyway.export const MAX_OUTPUT_BYTES = 64 * 1024;diff --git a/server/lib/bufferedSpawn.test.js b/server/lib/bufferedSpawn.test.jsindex 0b301b56b7..7f58fee8da 100644--- a/server/lib/bufferedSpawn.test.js+++ b/server/lib/bufferedSpawn.test.js@@ -195,6 +195,32 @@ describe('prepareWindowsSafeSpawn', () => {const result = prepareWindowsSafeSpawn('opencode', ['exec', '-'], true);expect(result).toEqual({ command: 'opencode', args: ['exec', '-'] });});++ it('caret-escapes a cmd.exe metacharacter in an arg with NO whitespace (Node would leave it unquoted)', () => {+ // foo&calc has no whitespace, so Node's own argv quoting would NOT wrap+ // it in literal quotes — the bare `&` would reach cmd.exe's raw command+ // line and be interpreted as a command separator without this.+ const result = prepareWindowsSafeSpawn('C:\\npm\\opencode.cmd', ['foo&calc'], true);+ expect(result.args).toEqual(['/c', 'C:\\npm\\opencode.cmd', 'foo^&calc']);+ });++ it('does NOT escape metacharacters in an arg that already contains whitespace (Node will quote it)', () => {+ // An arg with a space is already wrapped in literal double quotes by+ // Node's own argv escaping — caret-escaping it too would inject literal+ // `^` characters into the value the target program receives.+ const result = prepareWindowsSafeSpawn('C:\\npm\\codex.cmd', ['describe this & list colors'], true);+ expect(result.args).toEqual(['/c', 'C:\\npm\\codex.cmd', 'describe this & list colors']);+ });++ it('escapes multiple metacharacters in an unquoted arg', () => {+ const result = prepareWindowsSafeSpawn('C:\\npm\\tool.cmd', ['a|b>c<d'], true);+ expect(result.args).toEqual(['/c', 'C:\\npm\\tool.cmd', 'a^|b^>c^<d']);+ });++ it('never escapes off Windows, even for an unquoted metacharacter arg', () => {+ const result = prepareWindowsSafeSpawn('/usr/local/bin/tool.cmd', ['foo&bar'], false);+ expect(result.args).toEqual(['foo&bar']);+ });});describe('bufferedSpawn — structured result', () => {