From 372c1ba949bb3830b1db0410b3915f2c9503ed41 Mon Sep 17 00:00:00 2001 From: Mohamed Boudra Date: Wed, 25 Nov 2026 16:23:35 +0100 Subject: [PATCH] fix(plugins): keep plain Windows arguments as given for cmd.exe builtins and %1 readers Quoting and escaping every argument broke three existing Windows contracts in server tests. `echo hello` printed `"hello"`, a build script that writes `%1` wrote the quotes, and a port script that reads `%~1` got `^"` escapes it cannot remove. A plain argument now stays as given. An argument with whitespace, a quote, or no text sits inside real cmd.exe quotes, which `%~1` and the program both remove. A .cmd or .bat target also quotes an argument with cmd.exe syntax, because the script reads it again for `%*`. Only text that literal quotes leave outside a quoted region is escaped. --- .../process-internal/windows-command.test.ts | 55 ++++++++++------ .../process-internal/windows-command.ts | 65 ++++++++++++++----- packages/plugin/src/server/process.test.ts | 31 ++++++--- 3 files changed, 106 insertions(+), 45 deletions(-) diff --git a/packages/plugin/src/server/process-internal/windows-command.test.ts b/packages/plugin/src/server/process-internal/windows-command.test.ts index a7c7c45eda..d72beb59b3 100644 --- a/packages/plugin/src/server/process-internal/windows-command.test.ts +++ b/packages/plugin/src/server/process-internal/windows-command.test.ts @@ -34,34 +34,47 @@ describe("quoteWindowsCommand", () => { }); describe("quoteWindowsArgument", () => { - test("quotes the argument and escapes the quotes so cmd.exe sees no quoted region", () => { - expect(quoteWindowsArgument("C:\\Program Files\\cli.js", { platform: "win32" })).toBe( - '^"C:\\Program Files\\cli.js^"', + const windows = { platform: "win32" } as const; + const script = { platform: "win32", commandScript: true } as const; + + test("leaves a plain argument as given, for cmd.exe builtins and scripts that read %1", () => { + expect(quoteWindowsArgument("hello", windows)).toBe("hello"); + expect(quoteWindowsArgument("feature/allocator-context", script)).toBe( + "feature/allocator-context", ); }); - test("escapes cmd.exe syntax, including a git --format atom", () => { - expect(quoteWindowsArgument("a & b|(c)!^", { platform: "win32" })).toBe( - '^"a ^& b^|^(c^)^!^^^^"', + test("escapes cmd.exe syntax in an argument without spaces", () => { + expect(quoteWindowsArgument("feature&bugfix", windows)).toBe("feature^&bugfix"); + expect(quoteWindowsArgument("%PATH%", script)).toBe("^%PATH^%"); + expect(quoteWindowsArgument("--format=%(refname)%09", windows)).toBe( + "--format=^%^(refname^)^%09", ); - expect( - quoteWindowsArgument("--format=%(refname)%09%(committerdate:unix)", { platform: "win32" }), - ).toBe('^"--format=^%^(refname^)^%09^%^(committerdate:unix^)^"'); }); - test("escapes literal quotes and the backslashes before them for the program", () => { - expect(quoteWindowsArgument('say "hi"', { platform: "win32" })).toBe('^"say \\^"hi\\^"^"'); - expect(quoteWindowsArgument("C:\\dir\\", { platform: "win32" })).toBe('^"C:\\dir\\\\^"'); - }); - - test("keeps an empty argument", () => { - expect(quoteWindowsArgument("", { platform: "win32" })).toBe('^"^"'); - }); - - test("escapes twice for a .cmd or .bat script, which reads its arguments again", () => { - expect(quoteWindowsArgument("a & b", { platform: "win32", commandScript: true })).toBe( - '^^^"a ^^^& b^^^"', + test("quotes whitespace and empty arguments, and keeps cmd.exe syntax inside the quotes", () => { + expect(quoteWindowsArgument("work profile", windows)).toBe('"work profile"'); + expect(quoteWindowsArgument("a & b", script)).toBe('"a & b"'); + expect(quoteWindowsArgument("C:\\path with space\\", windows)).toBe( + '"C:\\path with space\\\\"', ); + expect(quoteWindowsArgument("", script)).toBe('""'); + }); + + test("quotes cmd.exe syntax for a script, which reads it again when it forwards %*", () => { + expect(quoteWindowsArgument("feature&bugfix", script)).toBe('"feature&bugfix"'); + expect(quoteWindowsArgument("feature^bugfix", script)).toBe('"feature^bugfix"'); + }); + + test("escapes syntax that literal quotes leave outside a quoted region", () => { + expect(quoteWindowsArgument('say "hi"', windows)).toBe('"say \\"hi\\""'); + expect(quoteWindowsArgument('mixed "a & b" quote', windows)).toBe('"mixed \\"a ^& b\\" quote"'); + expect(quoteWindowsArgument('mixed "a & b" quote', script)).toBe( + '"mixed \\"a ^^^& b\\" quote"', + ); + // One literal quote would leave cmd.exe inside a region, so the closing quote is escaped. + expect(quoteWindowsArgument('a"b', windows)).toBe('"a\\"b^"'); + expect(quoteWindowsArgument('a"b', script)).toBe('"a\\"b^^^"'); }); test("returns the argument unchanged on non-Windows platforms", () => { diff --git a/packages/plugin/src/server/process-internal/windows-command.ts b/packages/plugin/src/server/process-internal/windows-command.ts index a6156134c4..9921a19b79 100644 --- a/packages/plugin/src/server/process-internal/windows-command.ts +++ b/packages/plugin/src/server/process-internal/windows-command.ts @@ -8,13 +8,6 @@ export function isWindowsCommandScript( return platform === "win32" && (extension === ".cmd" || extension === ".bat"); } -// cmd.exe reads these as syntax outside a quoted region. A `^` before each one makes it literal. -const CMD_METACHARACTERS = /([()%!^"<>&|])/g; - -function escapeCmdMetacharacters(value: string): string { - return value.replace(CMD_METACHARACTERS, "^$1"); -} - /** * When spawning with `shell: true` on Windows, the command is passed to * `cmd.exe /d /s /c "command args"`. The `/s` strips outer quotes, so a @@ -33,22 +26,64 @@ export function quoteWindowsCommand( return command; } +// A `^` makes the next character literal outside a quoted region. `%` and `!` expand only once, +// because cmd.exe does not expand the text a script's `%*` produces; the others are read again. +const EXPANDED_ONCE = /[%!]/u; +const READ_AS_SYNTAX = /[&|<>()^"]/u; + +function escapeOutsideQuotes(character: string, commandScript: boolean): string { + if (EXPANDED_ONCE.test(character)) return `^${character}`; + if (READ_AS_SYNTAX.test(character)) return commandScript ? `^^^${character}` : `^${character}`; + return character; +} + +/** Quote the argument the way programs split their command line. */ +function quoteForProgram(argument: string): string { + return `"${argument.replace(/(\\*)"/g, '$1$1\\"').replace(/(\\*)$/, "$1$1")}"`; +} + +/** + * Escape a quoted argument for cmd.exe. Each `"` switches cmd.exe in or out of a quoted region, + * where everything is text, so only the parts outside a region are escaped. A closing quote that + * would open a region is escaped instead, so the next argument starts outside one. + */ +function escapeQuotedForCmd(quoted: string, commandScript: boolean): string { + const characters = [...quoted]; + let inside = false; + let escaped = ""; + for (const [index, character] of characters.entries()) { + const closing = index === characters.length - 1; + if (character === '"' && (inside || !closing)) { + inside = !inside; + escaped += character; + } else { + escaped += inside ? character : escapeOutsideQuotes(character, commandScript); + } + } + return escaped; +} + /** * Quote one argument for the `cmd.exe /d /s /c "..."` line that `spawn(..., { shell: true })` * builds on Windows, so the program receives it unchanged. * - * The argument is quoted the way programs split their command line (backslashes before a quote - * doubled, the quote escaped), then every cmd.exe metacharacter is escaped with `^`, including - * those quotes. cmd.exe then sees no quoted region and keeps `&`, `%` and the others as text. - * A `.cmd` or `.bat` script reads its arguments a second time when it forwards them with `%*`, - * so `commandScript` escapes them twice. See https://qntm.org/cmd. + * A plain argument stays as it is, so cmd.exe builtins and scripts that read `%1` get it as + * given. An argument with whitespace or a quote, or an empty one, is quoted for the program; + * cmd.exe keeps the text inside the quotes and `%~1` removes them. A `.cmd` or `.bat` script reads + * its arguments a second time when it forwards them with `%*`, so for `commandScript` an argument + * with cmd.exe syntax is quoted too, and syntax outside quotes is escaped for both reads. + * + * Inside quotes cmd.exe still expands a `%NAME%` that names a set variable. */ export function quoteWindowsArgument( argument: string, { platform = process.platform, commandScript = false } = {}, ): string { if (platform !== "win32") return argument; - const quoted = `"${argument.replace(/(\\*)"/g, '$1$1\\"').replace(/(\\*)$/, "$1$1")}"`; - const escaped = escapeCmdMetacharacters(quoted); - return commandScript ? escapeCmdMetacharacters(escaped) : escaped; + const quote = + argument === "" || /[\s"]/u.test(argument) || (commandScript && /[&|<>()^]/u.test(argument)); + if (!quote) { + return [...argument].map((character) => escapeOutsideQuotes(character, false)).join(""); + } + return escapeQuotedForCmd(quoteForProgram(argument), commandScript); } diff --git a/packages/plugin/src/server/process.test.ts b/packages/plugin/src/server/process.test.ts index 95616d613f..abd8e54b50 100644 --- a/packages/plugin/src/server/process.test.ts +++ b/packages/plugin/src/server/process.test.ts @@ -54,7 +54,7 @@ test("spawnProcess delivers shell arguments to the real child unchanged", async }); // A launch wrap prepends a Windows script wrapper to the provider launch; spawnProcess runs it -// through cmd.exe with every argument escaped twice, because the script forwards them with `%*`. +// through cmd.exe and quotes each argument that the script would read as more than one. test("a Windows script launch wrap starts through cmd.exe with each argument quoted", () => { const wrapped = [ "C:\\Program Files\\Sandbox\\run.cmd", @@ -66,13 +66,7 @@ test("a Windows script launch wrap starts through cmd.exe with each argument quo ]; expect(resolveSpawnInvocation(wrapped[0]!, wrapped.slice(1), undefined, "win32")).toEqual({ command: '"C:\\Program Files\\Sandbox\\run.cmd"', - args: [ - '^^^"--profile^^^"', - '^^^"work profile^^^"', - '^^^"^^^"', - '^^^"C:\\tools\\agy.cmd^^^"', - '^^^"acp^^^"', - ], + args: ["--profile", '"work profile"', '""', "C:\\tools\\agy.cmd", "acp"], shell: true, }); expect(resolveSpawnInvocation(wrapped[0]!, wrapped.slice(1), undefined, "linux")).toEqual({ @@ -103,7 +97,7 @@ test("a Windows cmd.exe launch refuses an argument with a line break before it s // Only cmd.exe reads cmd.exe quoting. A shell the caller names parses the line by its own rules. test("a Windows launch quotes for cmd.exe only when cmd.exe parses the command line", () => { for (const shell of [true, "cmd.exe", "C:\\Windows\\System32\\CMD.EXE"]) { - expect(resolveSpawnInvocation("echo", ["a & b"], shell, "win32").args).toEqual(['^"a ^& b^"']); + expect(resolveSpawnInvocation("echo", ["a & b"], shell, "win32").args).toEqual(['"a & b"']); } for (const shell of ["bash.exe", "C:\\Program Files\\Git\\bin\\bash.exe", "pwsh"]) { expect(resolveSpawnInvocation("echo", ["'a & b'", "line\nbreak"], shell, "win32")).toEqual({ @@ -230,6 +224,25 @@ test.runIf(process.platform === "win32")( }, ); +// Real cmd.exe: a script that reads `%~1` gets each argument as given, without escapes. +test.runIf(process.platform === "win32")( + "a .cmd script that reads its arguments one by one gets each one as given on Windows", + async () => { + const directory = await mkdtemp(path.join(os.tmpdir(), "plugin script reader ")); + roots.push(directory); + const script = path.join(directory, "read.cmd"); + await writeFile(script, '@echo off\r\n {