Skip to content
This repository has been archived by the owner on Sep 23, 2021. It is now read-only.

Commit

Permalink
Copy cmd.exe if the path contains any of thes characters: ` ' " & | <…
Browse files Browse the repository at this point in the history
… > ^
  • Loading branch information
zvin committed Nov 20, 2020
1 parent 81cab70 commit 687e194
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,16 @@ function Windows(instance, callback) {
);
}

function WindowsNeedsCopyCmd(instance) {
const specialChars = /&`'"<>\|\^/;
return specialChars.test(instance.path);
}

function WindowsCopyCmd(instance, end) {
if (!WindowsNeedsCopyCmd(instance)) {
end();
return;
}
// Work around https://github.com/jorangreef/sudo-prompt/issues/97
// Powershell can't properly escape amperstands in paths.
// We work around this by copying cmd.exe in our temporary folder and running
Expand All @@ -525,7 +534,7 @@ function WindowsCopyCmd(instance, end) {
Node.fs.copyFile(
Node.path.join(Node.process.env.SystemRoot, 'System32', 'cmd.exe'),
Node.path.join(instance.path, 'cmd.exe'),
end
end,
);
}

Expand All @@ -536,14 +545,24 @@ function WindowsElevate(instance, end) {
command.push('powershell.exe');
command.push('Start-Process');
command.push('-FilePath');
// Node.path.join('.', 'cmd.exe') would return 'cmd.exe'
command.push(['.', 'cmd.exe'].join(Node.path.sep));
command.push('-ArgumentList');
command.push('"/C","execute.bat"');
var options = { encoding: 'utf8' };
if (WindowsNeedsCopyCmd(instance)) {
// Node.path.join('.', 'cmd.exe') would return 'cmd.exe'
command.push(['.', 'cmd.exe'].join(Node.path.sep));
command.push('-ArgumentList');
command.push('"/C","execute.bat"');
options.cwd = instance.path;
} else {
// Escape characters for cmd using double quotes:
// Escape characters for PowerShell using single quotes:
// Escape single quotes for PowerShell using backtick:
// See: https://ss64.com/ps/syntax-esc.html
command.push('"\'' + instance.pathExecute.replace(/'/g, "`'") + '\'"');
}
command.push('-WindowStyle hidden');
command.push('-Verb runAs');
command = command.join(' ');
var child = Node.child.exec(command, { encoding: 'utf-8', cwd: instance.path },
var child = Node.child.exec(command, options,
function(error, stdout, stderr) {
// We used to return PERMISSION_DENIED only for error messages containing
// the string 'canceled by the user'. However, Windows internationalizes
Expand Down

0 comments on commit 687e194

Please sign in to comment.