-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Check UID unconditionally on non-Windows platforms, if os.getuid is available #10566
Conversation
When do they set UID to 0 when not running as root? We should detect the exact scenario instead of blindly excluding *BSD in the check. |
Hi @uranusjr, thanks for taking a look at the PR. Adding the additional OR conditions enables the calling of getuid() on the BSD's. Today with the code as it is, the getuid() call is only performed on Linux and Mac OS. I'm not sure the history of why Linux and Mac OS are explicitly checked -- maybe on some platforms getuid() returns a nonsensical value -- but on the BSDs getuid() works as expected and should be called. I guess one other change that could be done in addition to the one I've implemented here, is to print the warning only if getuid() was called to start with. Today the warning was being printed even without any getuid() call. In other words, it could be restructured as follows: if (sys.platform == "darwin" or sys.platform == "linux" or
sys.platform.startswith("openbsd") or sys.platform.startswith("freebsd") or
sys.platform.startswith("netbsd")):
if os.getuid() == 0:
logger.warning(
"Running pip as the 'root' user can result in broken permissions and "
"conflicting behaviour with the system package manager. "
"It is recommended to use a virtual environment instead: "
"https://pip.pypa.io/warnings/venv"
) This way, if some unknown platform is detected and we don't even call getuid(), it won't print the warning at least (unlike today where the warning is printed, even if the getuid() call is completely skipped). |
8e7b395
to
680576c
Compare
I actually strongly prefer the alternative approach I mentioned in my comment above, and have likewise updated the pull request. |
So this seems bizarre. If Rather than adding some extra platforms to the list here, why would we not just check |
680576c
to
ce860fe
Compare
Thanks for the feedback @pfmoore , I have updated the PR accordingly. Note that although the native Windows version of python does not implement os.getuid(), the cygwin version does. The old code was specifically excluding cases where sys.platform is win32 or cygwin from this root check, and for now I left this same exclusion in place. |
ce860fe
to
e9fed34
Compare
The version of pip included in OpenBSD 6.9 and FreeBSD 13.0 is 20.3.4, which did not include this warning message. On those platforms this issue would not be seen unless the user does a pip upgrade (ie. pip install pip --user --upgrade). Maybe that is why it was not reported earlier. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic looks good to me, a stylistic note that I feel surprisingly strongly about. :)
e9fed34
to
a01fabd
Compare
I'll note that BSD falls outside of our regular "supported platforms" matrix. I'm on board for merging this, specifically since this is actually simplifying the code, and it is a low cost fix for the users of those platforms. But, it is worth nothing none the less, should someone decide to argue that since we accepted PRs like this means that we support BSD upstream. We don't. It just works out well in this case. :) |
a01fabd
to
9ca9780
Compare
Updated the news file as requested. |
…vailable Check the uid using getuid() on platforms other than Linux and Mac OS before warning about running pip as root. Before this change, pip was emitting this warning even when it was run as a non-root user.
9ca9780
to
f231732
Compare
Changed the commit title also. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@n1000 This is a good proposal!
On BSD systems, pip warns about executing it as root, even though it is not being executed as root.
Fixes issue #10565.