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

Issue about os.homedir() #5582

Closed
XadillaX opened this issue Mar 7, 2016 · 8 comments
Closed

Issue about os.homedir() #5582

XadillaX opened this issue Mar 7, 2016 · 8 comments
Labels
os Issues and PRs related to the os subsystem.

Comments

@XadillaX
Copy link
Contributor

XadillaX commented Mar 7, 2016

I saw os.homedir() is directly using uv_os_homedir and it has an strange feature.

See here for the feature.

It will check the environment variable first and then check for the real home directory.

And someone discussed here for the reason.

I wonder if this should check $HOME first? It's something of a UNIX tradition to be able to change your home directory on the fly. A problem with that is that getenv("HOME") is not MT-safe.

-- @bnoordhuis

But I think there's still a problem.


Consider one situation:

If I wrote a CLI program and it will write some log file on user's home directory.

Now I run the CLI like this:

$ sudo -u foo node mycli.js

It should write log file on /home/foo. But the problem is - os.homedir() still got process.env.HOME.

One solution is to edit /etc/sudoers:

# env_keep+="HOME MAIL"
# comment env_keep += "HOME"

And another method is using -i argument.

_For further reading you can go to this article. (It was written in Chinese by @Amunu)_


But I think why don't we wrote a os.realHomedir() or removing checking HOME in realhomedir()? Not all of us (normal developers) have the permission to access configuration files on our online servers.

@mscdex mscdex added the os Issues and PRs related to the os subsystem. label Mar 7, 2016
@parox2014
Copy link

+1

@cjihrig
Copy link
Contributor

cjihrig commented Mar 7, 2016

There is work going on in libuv/libuv#742 to get the current user's password file entry. This would bypass the HOME check.

@XadillaX
Copy link
Contributor Author

XadillaX commented Mar 7, 2016

I wrote a package called real-homedir.

Actrully it's a copy of uv_os_homedir() but remove checking getenv("HOME") for getting real home directory.

So what I mean is that to replace os.homedir() or write a new function like os.realHomedir() for the situation above.

@cjihrig

@yorkie
Copy link
Contributor

yorkie commented Mar 7, 2016

@XadillaX I don't think adding the new function os.realHomedir is a flawless solution, which would cause more confusion at user-land.

@Fishrock123
Copy link
Contributor

IIRC there was actually requests to use $HOME if it existed..

@XadillaX
Copy link
Contributor Author

XadillaX commented Mar 8, 2016

@Fishrock123 Yeah I agree with $HOME.

But I'm talking about the situation above like sudo -u foo node mycli.js. It's the situation which mutually exclusive with $HOME.

How to solve it in Node.js and let developers be comfortable with it?

@ngot
Copy link

ngot commented Mar 8, 2016

+1. provide warning when sudo -u to get the os.homedir() maybe nice.

@jasnell jasnell mentioned this issue Apr 12, 2016
4 tasks
cjihrig added a commit to cjihrig/node that referenced this issue Apr 12, 2016
os.userInfo() calls libuv's uv_os_get_passwd() function. It returns
an object containing the current effective user's username, uid,
gid, shell, and home directory. On Windows, the uid and gid are
-1, and the shell is null.

Refs: nodejs#5582
PR-URL: nodejs#6104
Reviewed-By: James M Snell <jasnell@gmail.com>
@cjihrig
Copy link
Contributor

cjihrig commented Apr 12, 2016

Moving forward, you can use os.userInfo().homedir. It skips the environment variable check, and returns the information for the effective user.

@cjihrig cjihrig closed this as completed Apr 12, 2016
jasnell pushed a commit that referenced this issue Apr 26, 2016
os.userInfo() calls libuv's uv_os_get_passwd() function. It returns
an object containing the current effective user's username, uid,
gid, shell, and home directory. On Windows, the uid and gid are
-1, and the shell is null.

Refs: #5582
PR-URL: #6104
Reviewed-By: James M Snell <jasnell@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
os Issues and PRs related to the os subsystem.
Projects
None yet
Development

No branches or pull requests

7 participants