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

Windows error: Apostrophe in user name breaks sudo-prompt #119

Open
sigurdcarlsen opened this issue Jan 3, 2020 · 4 comments
Open

Windows error: Apostrophe in user name breaks sudo-prompt #119

sigurdcarlsen opened this issue Jan 3, 2020 · 4 comments

Comments

@sigurdcarlsen
Copy link

Frist of all thank you so much for this amazing repository!

Unfortunately I have learned that some users use apostrophe in their user names on windows machines which causes an error.

tested on:
Windows 10 home and pro
sudo-prompt 9.1.1
windows username: test's

Code:

var sudo = require("sudo-prompt");

sudo.exec("echo hello", { name: "hello" }, function(error, stdout, stderr) {
  if (error) throw error;
  console.log("stdout: " + stdout);
});

Error:

Error: User did not grant permission.

Logging the error inside the WindowsElevate function shows this:

Error: Command failed: powershell.exe Start-Process -FilePath "'C:\Users\test`'s\AppData\Local\Temp\ac48d60ff25e02d50153099887115cbf\execute.bat'" -WindowStyle hidden -Verb runAs
The string is missing the terminator: '.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

    at ChildProcess.exithandler (child_process.js:295:12)
    at ChildProcess.emit (events.js:210:5)
    at maybeClose (internal/child_process.js:1021:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:283:5) {
  killed: false,
  code: 1,
  signal: null,
  cmd: 'powershell.exe Start-Process -FilePath "\'C:\\Users\\test`\'s\\AppData\\Local\\Temp\\ac48d60ff25e02d50153099887115cbf\\execute.bat\'" -WindowStyle hidden -Verb runAs'
}

The problem is with the path to execute.bat.
My solution was changing line 523 from :

command.push('"\'' + instance.pathExecute.replace(/'/g, "`'") + '\'"');

to

command.push('"' + instance.pathExecute.replace(/'/g, "`'") + '"');

the first produces a string like:

"'C:\\Users\\test`'s\\execute.bat'"

whereas the second produces:

"C:\\Users\\test`'s\\execute.bat"

It seems that wrapping the string in both double and single quotes, interfers with the escape character ` before the apostrophe. This results in the apostrophe being read as a delimiter instead of a special character.
I am unsure if this is a good workaround as I am not sure of the reason behind using both double and single quotes.

@jorangreef
Copy link
Owner

Thanks @sigurdcarlsen for the detailed report. This is really helpful.

@jorangreef
Copy link
Owner

jorangreef commented Sep 15, 2020

@sigurdcarlsen we should have a fix for this one way or the other soon.

@jorangreef
Copy link
Owner

Fixing escaping issues like this is extremely tricky because one fix could introduce other issues.

@jorangreef
Copy link
Owner

... and Windows has it's own escaping bugs, as you will see in #97. But hopefully your issue will be fixed soon!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants