Skip to content

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

fix: resolve the shim's node from the child PATH when bypassing cmd.exe (#12346)

[._.]/ Adam Eivy5e3616b710b6
diff --git a/server/lib/aiToolkit/internal/windowsSafeSpawn.js b/server/lib/aiToolkit/internal/windowsSafeSpawn.js
index d766106d49..be350c204e 100644
--- a/server/lib/aiToolkit/internal/windowsSafeSpawn.js
+++ b/server/lib/aiToolkit/internal/windowsSafeSpawn.js
@@ -53,9 +53,10 @@ function escapeCmdMetachars(value, carets) {
* The direct launch an npm-style cmd-shim performs — `[node.exe, script]` for
* a node shim, `[target.exe]` for a native one — or `null` when the file is
* unreadable or not a shim this recognizes. Mirrors the shim's own interpreter
- * choice: a `node.exe` beside it, else `node` on PATH, else this process's node.
+ * choice: a `node.exe` beside it, else `node` on the child's PATH, else this
+ * process's node.
*/
-function resolveCmdShimLaunch(shimPath) {
+function resolveCmdShimLaunch(shimPath, searchEnv) {
let source;
try { source = readFileSync(shimPath, 'utf8'); } catch { return null; }
const targets = [...source.matchAll(CMD_SHIM_TARGET_RE)].map((m) => m[1]);
@@ -66,7 +67,7 @@ function resolveCmdShimLaunch(shimPath) {
if (!existsSync(targetPath)) return null;
if (CMD_SHIM_NODE_RE.test(source)) {
const localNode = join(shimDir, 'node.exe');
- const pathNode = resolveWindowsExecutable('node', true);
+ const pathNode = resolveWindowsExecutable('node', true, searchEnv);
const nodeExe = existsSync(localNode) ? localNode
: (pathNode && /\.exe$/i.test(pathNode) ? pathNode : process.execPath);
return { command: nodeExe, args: [targetPath] };
@@ -87,12 +88,13 @@ function resolveCmdShimLaunch(shimPath) {
* cannot carry at all (CMD_UNCARRYABLE_RE — agent prompts on argv) bypasses
* cmd.exe by launching the shim's target directly; when the shim is not one
* that can be resolved, the launch is refused rather than handing untrusted
- * text to cmd.exe's parser.
+ * text to cmd.exe's parser. `searchEnv` is the child's env, so the bypass
+ * picks the same `node` the shim would have.
*/
-export function prepareWindowsSafeSpawn(command, args, isWin32 = process.platform === 'win32') {
+export function prepareWindowsSafeSpawn(command, args, isWin32 = process.platform === 'win32', searchEnv = process.env) {
if (!isWin32 || !WIN_BATCH_EXT_RE.test(command)) return { command, args };
if (args.some((arg) => CMD_UNCARRYABLE_RE.test(String(arg)))) {
- const direct = resolveCmdShimLaunch(command);
+ const direct = resolveCmdShimLaunch(command, searchEnv);
if (!direct) {
const error = new Error(`Cannot pass this argument safely through cmd.exe to ${command}: it contains a double quote, percent sign, or line break, and the batch file is not an npm shim that can be launched directly. Install the CLI with npm or as a native .exe.`);
error.code = 'WINDOWS_BATCH_ARG_UNSAFE';
diff --git a/server/lib/aiToolkit/runner.js b/server/lib/aiToolkit/runner.js
index 2de8d1c883..e85203238a 100644
--- a/server/lib/aiToolkit/runner.js
+++ b/server/lib/aiToolkit/runner.js
@@ -447,7 +447,7 @@ export function createRunnerService(config = {}) {
// Resolved against `childEnv` (not bare process.env) so a
// provider-configured PATH override is honored.
const resolvedCommand = resolveWindowsExecutable(provider.command, undefined, childEnv) || provider.command;
- const { command: spawnCommand, args: spawnArgs } = prepareWindowsSafeSpawn(resolvedCommand, args);
+ const { command: spawnCommand, args: spawnArgs } = prepareWindowsSafeSpawn(resolvedCommand, args, undefined, childEnv);
if (beforeExecute) {
try { await beforeExecute(); }
catch {
diff --git a/server/lib/bufferedSpawn.js b/server/lib/bufferedSpawn.js
index d6805595ab..439d41f752 100644
--- a/server/lib/bufferedSpawn.js
+++ b/server/lib/bufferedSpawn.js
@@ -68,7 +68,7 @@ export const needsShell = (cmd) => IS_WIN32 && WIN_CMD_SHIMS.has(cmd);
*/
export function prepareCliSpawn(command, args, searchEnv = process.env, isWin32 = IS_WIN32) {
const resolved = resolveWindowsExecutable(command, isWin32, searchEnv) || command;
- return prepareWindowsSafeSpawn(resolved, args, isWin32);
+ return prepareWindowsSafeSpawn(resolved, args, isWin32, searchEnv);
}
// Cap buffered stdout/stderr so a runaway child can't exhaust memory; we only
diff --git a/server/lib/bufferedSpawn.test.js b/server/lib/bufferedSpawn.test.js
index 749500b300..79c909ff24 100644
--- a/server/lib/bufferedSpawn.test.js
+++ b/server/lib/bufferedSpawn.test.js
@@ -3,7 +3,7 @@ import { EventEmitter } from 'events';
import { spawn as nodeSpawn } from 'node:child_process';
import { ChildProcess } from './childProcess.js';
import { mkdir, mkdtemp, rm, writeFile } from 'fs/promises';
-import { join } from 'path';
+import { delimiter, join } from 'path';
import { tmpdir } from 'os';
// Mock child_process.spawn so we can drive the buffered-spawn machinery without
@@ -490,6 +490,15 @@ describe('prepareWindowsSafeSpawn — argv cmd.exe cannot carry (#12346)', () =>
expect(result).toEqual({ command: join(shimDir, 'node.exe'), args: [script, '--print', HOSTILE_PROMPT] });
});
+ it('picks node from the child PATH, as the shim would, when none sits beside it', async () => {
+ await writeFile(join(shimDir, 'fake-cli.cmd'), NPM_CMD_SHIM);
+ const nodeDir = join(shimDir, 'provider-node');
+ await mkdir(nodeDir);
+ await writeFile(join(nodeDir, 'node.exe'), '');
+ const result = prepareCliSpawn('fake-cli', [HOSTILE_PROMPT], { PATH: [shimDir, nodeDir].join(delimiter) }, true);
+ expect(result).toEqual({ command: join(nodeDir, 'node.exe'), args: [script, HOSTILE_PROMPT] });
+ });
+
it('refuses a batch file it cannot bypass rather than passing the prompt to cmd.exe', async () => {
const shim = join(shimDir, 'custom.bat');
await writeFile(shim, '@echo off\r\necho %*\r\n');
diff --git a/server/lib/credentialBootstrap.js b/server/lib/credentialBootstrap.js
index 29fe418ce3..b1c66ca002 100644
--- a/server/lib/credentialBootstrap.js
+++ b/server/lib/credentialBootstrap.js
@@ -330,5 +330,5 @@ export function processGroupKillable(child, processGroup) {
export function resolveCliSpawn(provider, command, args, childEnv, options = {}) {
const bootstrapped = applyCredentialBootstrap(provider, command, args, options);
const resolvedCommand = resolveWindowsExecutable(bootstrapped.command, undefined, childEnv) || bootstrapped.command;
- return { ...prepareWindowsSafeSpawn(resolvedCommand, bootstrapped.args), wrapped: bootstrapped.wrapped };
+ return { ...prepareWindowsSafeSpawn(resolvedCommand, bootstrapped.args, undefined, childEnv), wrapped: bootstrapped.wrapped };
}