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: refuse an unsafe Windows batch launch as a failing child, not a throw (#12346)

[._.]/ Adam Eivy9ec721b2dff4
diff --git a/server/lib/aiToolkit/internal/windowsSafeSpawn.js b/server/lib/aiToolkit/internal/windowsSafeSpawn.js
index be350c204e..86fe871eec 100644
--- a/server/lib/aiToolkit/internal/windowsSafeSpawn.js
+++ b/server/lib/aiToolkit/internal/windowsSafeSpawn.js
@@ -42,6 +42,7 @@ const CMD_UNCARRYABLE_RE = /["%\r\n]/;
// (`"%dp0%\…" %*`) and legacy (`"%~dp0\…" %*`) cmd-shim layouts.
const CMD_SHIM_TARGET_RE = /"%~?dp0%?\\([^"]+)"\s+%\*/g;
const CMD_SHIM_NODE_RE = /%~?dp0%?\\node\.exe"/i;
+const WINDOWS_BATCH_ARG_UNSAFE = 'WINDOWS_BATCH_ARG_UNSAFE';
function escapeCmdMetachars(value, carets) {
const str = String(value);
@@ -87,20 +88,23 @@ function resolveCmdShimLaunch(shimPath, searchEnv) {
* command path is not re-parsed and gets one level. An argument cmd.exe
* 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. `searchEnv` is the child's env, so the bypass
+ * that can be resolved, the launch is refused (a child that prints
+ * WINDOWS_BATCH_ARG_UNSAFE and exits 1) rather than handing untrusted 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', 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, 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';
- throw error;
- }
- return { command: direct.command, args: [...direct.args, ...args] };
+ if (direct) return { command: direct.command, args: [...direct.args, ...args] };
+ // Refuse as a launch that fails, not a throw: callers prepare outside their
+ // spawn-failure boundaries, and a child that exits 1 with this on stderr
+ // reaches every caller's existing failure, result and cleanup path.
+ return {
+ command: process.execPath,
+ args: ['-e', 'process.stderr.write(process.argv[1] + "\\n"); process.exit(1)', `${WINDOWS_BATCH_ARG_UNSAFE}: cannot pass an argument containing a double quote, percent sign or line break through cmd.exe to ${command}, which is not an npm shim PortOS can launch directly. Install the CLI with npm or as a native .exe.`],
+ };
}
return {
command: 'cmd.exe',
diff --git a/server/lib/bufferedSpawn.test.js b/server/lib/bufferedSpawn.test.js
index 79c909ff24..b6c5b7c78e 100644
--- a/server/lib/bufferedSpawn.test.js
+++ b/server/lib/bufferedSpawn.test.js
@@ -502,8 +502,10 @@ describe('prepareWindowsSafeSpawn — argv cmd.exe cannot carry (#12346)', () =>
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');
- expect(() => prepareWindowsSafeSpawn(shim, ['--prompt', HOSTILE_PROMPT], true))
- .toThrow(expect.objectContaining({ code: 'WINDOWS_BATCH_ARG_UNSAFE' }));
+ const result = prepareWindowsSafeSpawn(shim, ['--prompt', HOSTILE_PROMPT], true);
+ expect(result.command).toBe(process.execPath);
+ expect(result.args.join(' ')).not.toContain(HOSTILE_PROMPT);
+ expect(result.args.at(-1)).toMatch(/^WINDOWS_BATCH_ARG_UNSAFE: /);
});
it.runIf(process.platform === 'win32')('delivers hostile arguments verbatim through a real npm shim', async () => {
diff --git a/server/lib/cliProviderRun.test.js b/server/lib/cliProviderRun.test.js
index 416e072990..22bbd0e8bc 100644
--- a/server/lib/cliProviderRun.test.js
+++ b/server/lib/cliProviderRun.test.js
@@ -282,6 +282,30 @@ describe('runCliProviderPrompt', () => {
expect(result.exitCode).toBe(0);
});
+ it('settles a refused Windows batch launch as an error result, never a rejection (#12346)', async () => {
+ const { mkdtemp, rm, writeFile } = await import('node:fs/promises');
+ const { join } = await import('node:path');
+ const { tmpdir } = await import('node:os');
+ const { pinPlatform } = await import('./testHelper.js');
+ const dir = await mkdtemp(join(tmpdir(), 'cli-run-batch-'));
+ const batch = join(dir, 'custom.bat');
+ await writeFile(batch, '@echo off\r\necho %*\r\n');
+ vi.mocked(resolveWindowsExecutable).mockReturnValueOnce(batch);
+ const restorePlatform = pinPlatform('win32');
+ try {
+ const result = await runCliProviderPrompt({
+ provider: { id: 'gemini-cli', type: 'cli', command: 'custom', args: [] },
+ prompt: 'ignored',
+ extraArgs: ['--note', 'a " & calc & " b'],
+ });
+ expect(result.error).toContain('WINDOWS_BATCH_ARG_UNSAFE');
+ expect(result.exitCode).toBe(1);
+ } finally {
+ restorePlatform();
+ await rm(dir, { recursive: true, force: true });
+ }
+ });
+
// #5302: a non-zero exit that still printed is returned for parsing, but the
// text may be truncated — callers gate destructive use on `partial === false`.
describe('non-zero exit agreement', () => {