cytel
cytel

Reputation: 1

Is it possbible to use node with admin or sudo privileges within electron?

Is it possbible to use node with admin or sudo privileges within electron?

I found sudo-prompt, but from my understanding, this could only be used to execute a shell command and not to use node functions (code from within the main.js-file for example).

Is this possible? If so, could you give me any advice on that?

Any help would be really appreciated, maybe I am just looking for the wrong things.

Thanks in advance!

Upvotes: 0

Views: 1790

Answers (2)

Kim T
Kim T

Reputation: 6454

It is not recommended to run the entire Electron app as admin administrator. As this gives the entire app blanket access to change anything on the users machine. If someone were to inject code, or if you had a bug you could do lots of bad things to a computer.

It's better to run a single function and always prompt the user to login as admin before the function is run.

You can see this approach in a popular Electron app: https://github.com/microsoft/vscode

In the package.json file they have two useful dependencies:

They check to see if permissions are elevated using native-is-elevated, and if not, prompt for an admin password using sudo-prompt.

You can read the source code for the process here: https://github.com/microsoft/vscode/blob/8845f89c1e4183b54126cd629cd45c8f0f7549f2/src/vs/platform/native/electron-main/nativeHostMainService.ts#L491

I have created an example Electron app using this approach here: https://github.com/kmturley/electron-runas-admin

Upvotes: 0

Hai Pham
Hai Pham

Reputation: 2225

Yes you can, take a look at electron-sudo module: https://github.com/automation-stack/electron-sudo

Run a subprocess with administrative privileges, prompting the user with a graphical OS dialog if necessary. Useful for background subprocesse which run native Electron apps that need sudo.

  • Windows, uses elevate utility with native User Account Control (UAC) prompt (no PowerShell required)
  • OS X, uses bundled applet (inspired by Joran Dirk Greef)
  • Linux, uses system pkexec or gksudo (system or bundled).

Upvotes: 0

Related Questions