De-escalating privileges

There is a hack, however, which allows you to initiate the process as root using sudo, but de-escalate the privileges later by setting the user and group identity of the process to the user/group who issued the sudo command. We do this by using the environment variables SUDO_UID and SUDO_GID, and setting them using process.setgid and process.setuid:

app.listen(process.env.SERVER_PORT, async () => {  const sudoGid = parseInt(process.env.SUDO_GID);  const sudoUid = parseInt(process.env.SUDO_UID);  if (sudoGid) { process.setuid(sudoGid) }  if (sudoUid) { process.setuid(sudoUid) }  ...});

Get Building Enterprise JavaScript Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.