Reputation: 1214
How can I temporarily drop root privileges in Node.js?
I can see process.setuid
in the standard library, but without seteuid
, it cannot be temporary. Trying to get root privileges again fails.
Upvotes: 1
Views: 600
Reputation: 550
You can use the node-posix module's seteuid
method. For such a simple task as creating a single file, I can imagine that you use writeFileSync
surrounded by calls to seteuid
, as your original idea probably was.
Upvotes: 1
Reputation: 104080
I don't think changing privileges in a Node.js program really makes sense because you don't know when the different IO requests are going to be handled. You might change privilege, submit the IO, change back, and at some point in the future the IO will be performed -- presumably under the privileges it runs with at the time.
I could imagine a mechanism to tie the euid, egid, and supplemental groups to specific IO requests, but it would probably be a drastic complication compared to their current design for not much benefit.
An approach you could take would spawn off new processes with Cluster
, change permissions, and then exit the process once you're finished. You could also have it be long-lived and handle requests over an internal queue, but fork(2)
is fast, and hopefully the Node.js shims over fork(2)
aren't too expensive.
Upvotes: 2