-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
[BUG] cannot run npm scripts as root as long as the directory's owner is a regular user #4095
Comments
sudo doesn’t run as a specific super user, and doesn’t change who’s logged in. It just runs the command as your regular user but with super user privileges. |
@ljharb So why this problem doesn't exist with npm v6.x.x? Furthermore, if I log in as root directly, the problem still exists. $su --login root |
This problem seems to exist with npm v 7.0.2 with node v15.0.0 and later. (My npm and node are also installed from n on Ubuntu 20.04 amd64.) |
I think I'm seeing the same issue. It starts with npm 7.0.0. I don't think node version matters. |
This can be reproduced in Docker. Create a Dockerfile with: FROM node:14-bullseye-slim
RUN echo 'Confirm we are root uid=0'; id
RUN echo 'Prints 16.14.15'; npm -v
# switch to user node
USER node
RUN mkdir /home/node/src/
WORKDIR /home/node/src/
RUN echo '{ "scripts": { "test": "node --eval \\"console.log(process.getuid())\\"" } }' > package.json
RUN echo 'Confirm we are user node uid=1000'; id
RUN echo 'Correctly prints 1000'; npm test
# switch to root
USER root
RUN echo 'Confirm we are root uid=0'; id
RUN echo 'Correctly prints 0'; npm test
# install [email protected]
RUN npm install -g [email protected]
RUN echo 'Prints 7.0.0'; npm -v
RUN echo 'Confirm we are root uid=0'; id
RUN echo 'INCORRECTLY prints 1000, BUG???'; npm test Building produces output as described:
This can just as well be reproduced with newest npm (8.3.0). I'm only showing 7.0.0, because it's the earliest version where it occurs. Also, Node version does not matter. Same thing happens with Node 12 ( Update: Actually the earliest version with the issue is 7.0.0-beta.6 (unfortunately, the diff is still quite substantial: v6.14.15...v7.0.0-beta.6). On the other hand all earlier betas (0-5) crash when running that last
|
Changelog for 7.0.0-beta.0 says:
And in docs:
So this is a feature?! |
This issue is closed because it is an expected behavior as @ArturDorochowicz said. |
I have created #4811 please go to upvote it so maybe it get noticed by maintainers |
See npm/rfcs#546 |
With NPM v7 and v8, when running as root, NPM uses the UID and GID of the current working directory owner to execute scripts (see [npm/cli#4095][]). This can cause a problem if `npm install` is run as root in a directory owned by another user because module installation scripts will instead use that user's UID and GID but may still try to write to root's home directory. For example, Puppeteer will try to download Chromium to `/root/.cache/puppeteer/chrome`, raising "EACCES: permission denied". Similarly, this can cause a problem if `npm install` is run as root in a directory owned by root, and then that directory is later `chown`ed to another user. Subsequent NPM commands (e.g. `npm run build`) run as root will instead use that user's UID and GID, and will raise "EACCES: permission denied" when touching anything in the root-owned `node_modules` directory (e.g. `node_modules/.cache`). NPM v9 removed this behavior (see [npm/rfcs#546][]), but v9 was first released on 2022-10-24, and v8 is still widespread. Therefore, as a workaround, instead of `chown`ing the application directory to a non-root user, this commit `chmod`s the application directory to grant owner-equivalent permissions to non-root users. Additionally, this commit `chown`s and `chmod`s the `PACKAGE_JSON_DIR` directory in the same way. [npm/cli#4095]: npm/cli#4095 [npm/rfcs#546]: npm/rfcs#546
Is there an existing issue for this?
This issue exists in the latest npm version
Current Behavior
If a project directory belongs to a linux regular user, all npm scripts in it cannot be run as root.
This problem doesn't exist on npm v6.x
Expected Behavior
When logged in as root, or running npm script with
sudo
, the scripts should be run as root.Steps To Reproduce
~/haha/package.json
:zim
is a regular user.Environment
My npm and node are installed from n
The text was updated successfully, but these errors were encountered: