Skip to content

Improper argument escaping on Windows #99

@JP-Ellis

Description

@JP-Ellis

It seems like argument parsing on Windows is not working correctly. Specifically, when using Git Bash on Windows (maybe the mix of Bash and Windows?).

I strongly suspect there's an issue with the argument parsing:

function quoteCmdArg(arg: string) {
return `"${arg.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`;
}
function quotePwshArg(arg: string) {
return `'${arg.replace(/'/g, "''")}'`;
}
function quotePosixShArg(arg: string) {
return `'${arg.replace(/'/g, "'\\''")}'`;
}
function testWindowsExe(cmd: string, file: string) {
return new RegExp(`^(?:.*\\\\)?${cmd}(?:\\.exe)?$`, 'i').test(file);
}
function parseArgs(unparsed_args: string[]) {
if (isWindows === true) {
const file = process.env['comspec'] || 'cmd.exe';
if (testWindowsExe('cmd', file) === true) {
return unparsed_args.map((i) => quoteCmdArg(i));
}
if (testWindowsExe('(powershell|pwsh)', file) || file.endsWith('/pwsh')) {
return unparsed_args.map((i) => quotePwshArg(i));
}
return unparsed_args;
}
return unparsed_args.map((i) => quotePosixShArg(i));
}

In particular, I suspect it is not required at all:

  1. On the input side, process.argv has all arguments properly escaped into individual strings

  2. On the invokation side, functions like spawn will ensure arguments are escaping correctly:

    spawn('ls', ['-lh', '/path/with/a space'])

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIndicates an unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions