X Tutup
The Wayback Machine - https://web.archive.org/web/20201025233407/https://github.com/nodejs/node/issues/35765
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for executing Node.js fs functions as sudo #35765

Open
aleksey-hoffman opened this issue Oct 22, 2020 · 4 comments
Open

Support for executing Node.js fs functions as sudo #35765

aleksey-hoffman opened this issue Oct 22, 2020 · 4 comments

Comments

@aleksey-hoffman
Copy link

@aleksey-hoffman aleksey-hoffman commented Oct 22, 2020

Is your feature request related to a problem? Please describe.
The inability to execute Node.js fs functions with sudo (as admin) limits the applications built on Node.js. Nowadays, a lot of applications are built with Electron (which comes with Node.js), This feature would allow us to build more advanced apps with Node.js.

Describe the solution you'd like
Example;

  1. A function executed with option asAdmin: true requests the system password dialog:
fs.readdirSync('/media/sharedDrive/', { asAdmin: true, adminAccessTimeout: 15 * 1000 * 60 })
  1. User enters their password
  2. On success, execute the function with admin rights.

Describe alternatives you've considered
N/A

@jasnell
Copy link
Member

@jasnell jasnell commented Oct 22, 2020

This is not likely as there are a number of usability and security concerns with doing this. It's already possible to run the node process with sudo so I'm not sure what additional benefit this would offer.

@aleksey-hoffman
Copy link
Author

@aleksey-hoffman aleksey-hoffman commented Oct 22, 2020

@jasnell I guess it's theoretically possible with child_process, but how would that even work?

You know how the file manager on Linux prompts you with the password dialog when you're trying to open a protected directory? Let's say the user of your Node.js file manager app wants to do the same, they want to see the contents of their protected directory '/media/sharedDrive/' on Linux and 'C:/test/' on Windows.

How do we implement this? Do you have to create a dummy readdir.js file in your project directory that logs the results of fs.readdirSync() and then get it from the stdout parameter from the result of child_process.exec?

Something like this, with the help of sudo-prompt module, maybe?

const sudo = require('sudo-prompt')
const options = {
  name: 'Electron'
}
const command = 'node "PATH_TO_THE_APP/readdir.js path/to/dir"'
sudo.exec(command,  options, function(error, stdout, stderr) {
  if (error) { throw error }
  console.log(stdout)
})

On Windows, you can't even pass sudo as an argument, you would have to invoke a new cmd or powershell process via powershell with admin rights -Verb RunAs and then pipe stdout from that process back to powershell and then back to the app.

If you know how to do it, please suggest a working solution for this, using existing Node.js methods

@devsnek
Copy link
Member

@devsnek devsnek commented Oct 22, 2020

Strong -1.

  • This would likely require a sudoer's password being within node's address space in plain text, even if temporarily.
  • This would likely be a platform compat nightmare.
  • If your program needs elevated permissions, run it with elevated permissions.
@aleksey-hoffman
Copy link
Author

@aleksey-hoffman aleksey-hoffman commented Oct 23, 2020

@devsnek unfortunately you cannot just run Electron apps as root without --no-sandbox parameter, which makes the app dangerous to use.

The only solution I can see is to use Node's child_process to create a new sudo Node process which would somehow request the user password (perhaps using sudo-prompt module) and then run the needed fs function e.g. fs.readdirSync(path) for the specified protected directory and then return the result back into the app via stdout . But as I mentioned in the comment above, I don't really know how to do it properly.

If someone knows how to do it, please share a little code snippet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.
X Tutup