From 0258d6752eabba17fe71202e95c1e4349f85af28 Mon Sep 17 00:00:00 2001 From: Mohamed Boudra Date: Wed, 25 Nov 2026 16:38:29 +0100 Subject: [PATCH] fix(plugins): quote =, comma and semicolon for a Windows .cmd launch A batch script splits its arguments at =, comma and semicolon outside quotes, so a launcher that reads %~1 got --profile=restricted as two arguments. Quote them for a script; %* forwarders still pass the argument through unchanged. --- .../server/process-internal/windows-command.test.ts | 7 +++++++ .../src/server/process-internal/windows-command.ts | 12 ++++++++---- packages/plugin/src/server/process.test.ts | 12 ++++++++++-- 3 files changed, 25 insertions(+), 6 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 d72beb59b3..1a38f02208 100644 --- a/packages/plugin/src/server/process-internal/windows-command.test.ts +++ b/packages/plugin/src/server/process-internal/windows-command.test.ts @@ -66,6 +66,13 @@ describe("quoteWindowsArgument", () => { expect(quoteWindowsArgument("feature^bugfix", script)).toBe('"feature^bugfix"'); }); + test("quotes =, comma and semicolon for a script, which splits its arguments at them", () => { + for (const argument of ["--profile=restricted", "a,b", "a;b"]) { + expect(quoteWindowsArgument(argument, script)).toBe(`"${argument}"`); + expect(quoteWindowsArgument(argument, windows)).toBe(argument); + } + }); + 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"'); diff --git a/packages/plugin/src/server/process-internal/windows-command.ts b/packages/plugin/src/server/process-internal/windows-command.ts index 9921a19b79..8737765623 100644 --- a/packages/plugin/src/server/process-internal/windows-command.ts +++ b/packages/plugin/src/server/process-internal/windows-command.ts @@ -30,6 +30,8 @@ export function quoteWindowsCommand( // 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; +// A script splits its arguments at `=`, `,` and `;` outside quotes, and reads the syntax again. +const SPLIT_BY_SCRIPT = /[&|<>()^=,;]/u; function escapeOutsideQuotes(character: string, commandScript: boolean): string { if (EXPANDED_ONCE.test(character)) return `^${character}`; @@ -69,9 +71,11 @@ function escapeQuotedForCmd(quoted: string, commandScript: boolean): string { * * 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. + * cmd.exe keeps the text inside the quotes and `%~1` removes them. A `.cmd` or `.bat` script + * splits its arguments at `=`, `,` and `;` as well, and reads them a second time when it forwards + * them with `%*`. So for `commandScript` an argument with one of those or with cmd.exe syntax is + * quoted too, and syntax outside quotes is escaped for both reads. A script then gets each one as + * a single `%~1`, and `%*` forwards it unchanged. * * Inside quotes cmd.exe still expands a `%NAME%` that names a set variable. */ @@ -81,7 +85,7 @@ export function quoteWindowsArgument( ): string { if (platform !== "win32") return argument; const quote = - argument === "" || /[\s"]/u.test(argument) || (commandScript && /[&|<>()^]/u.test(argument)); + argument === "" || /[\s"]/u.test(argument) || (commandScript && SPLIT_BY_SCRIPT.test(argument)); if (!quote) { return [...argument].map((character) => escapeOutsideQuotes(character, false)).join(""); } diff --git a/packages/plugin/src/server/process.test.ts b/packages/plugin/src/server/process.test.ts index 251f75abfd..16256de566 100644 --- a/packages/plugin/src/server/process.test.ts +++ b/packages/plugin/src/server/process.test.ts @@ -168,6 +168,8 @@ const commandScriptArguments = [ "!bang!", "C:\\path with space\\", 'mixed "a & b" quote', + "--profile=restricted", + "a,b;c", ]; async function writeCommandScriptLauncher(): Promise { @@ -232,15 +234,21 @@ test.runIf(process.platform === "win32")( roots.push(directory); const script = path.join(directory, "read.cmd"); // `set /p` without input exits 1, so the script exits 0 on its own. - await writeFile(script, '@echo off\r\n