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.
This commit is contained in:
Mohamed Boudra
2026-11-25 17:37:33 +01:00
parent de5f139d33
commit 0258d6752e
3 changed files with 25 additions and 6 deletions
@@ -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"');
@@ -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("");
}
+10 -2
View File
@@ -168,6 +168,8 @@ const commandScriptArguments = [
"!bang!",
"C:\\path with space\\",
'mixed "a & b" quote',
"--profile=restricted",
"a,b;c",
];
async function writeCommandScriptLauncher(): Promise<string> {
@@ -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<nul set /p "=%~1|%~2|%~3|%~4|%~5"\r\nexit /b 0\r\n');
await writeFile(
script,
'@echo off\r\n<nul set /p "=%~1|%~2|%~3|%~4|%~5|%~6|%~7|%~8"\r\nexit /b 0\r\n',
);
const { stdout } = await execCommand(script, [
"plain",
"with space",
"feature&bugfix",
"100%",
"--profile=restricted",
"a,b",
"a;b",
"",
]);
expect(stdout).toBe("plain|with space|feature&bugfix|100%|");
expect(stdout).toBe("plain|with space|feature&bugfix|100%|--profile=restricted|a,b|a;b|");
},
);