-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
gh-135815: skip netrc
security checks if os.getuid
is missing
#135816
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,19 @@ | |
__all__ = ["netrc", "NetrcParseError"] | ||
|
||
|
||
def _can_security_check(): | ||
# On WASI, getuid() is indicated as a stub but it may also be missing. | ||
return os.name == 'posix' and hasattr(os, 'getuid') | ||
|
||
|
||
def _getpwuid(uid): | ||
try: | ||
import pwd | ||
return pwd.getpwuid(uid)[0] | ||
except (ImportError, LookupError): | ||
return f'uid {uid}' | ||
|
||
|
||
class NetrcParseError(Exception): | ||
"""Exception raised on syntax errors in the .netrc file.""" | ||
def __init__(self, msg, filename=None, lineno=None): | ||
|
@@ -142,18 +155,11 @@ def _parse(self, file, fp, default_netrc): | |
self._security_check(fp, default_netrc, self.hosts[entryname][0]) | ||
|
||
def _security_check(self, fp, default_netrc, login): | ||
if os.name == 'posix' and default_netrc and login != "anonymous": | ||
if _can_security_check() and default_netrc and login != "anonymous": | ||
prop = os.fstat(fp.fileno()) | ||
if prop.st_uid != os.getuid(): | ||
import pwd | ||
try: | ||
fowner = pwd.getpwuid(prop.st_uid)[0] | ||
except KeyError: | ||
fowner = 'uid %s' % prop.st_uid | ||
try: | ||
user = pwd.getpwuid(os.getuid())[0] | ||
except KeyError: | ||
user = 'uid %s' % os.getuid() | ||
fowner = _getpwuid(prop.st_uid) | ||
user = _getpwuid(os.getuid()) | ||
raise NetrcParseError( | ||
(f"~/.netrc file owner ({fowner}, {user}) does not match" | ||
" current user")) | ||
Comment on lines
165
to
166
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The message looks weird. Would not be better to rewrite in in the following way? (f"~/.netrc file owner ({fowner}) does not match"
f" current user ({user})")) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll change it in a follow-up PR so that only unstable branches are affected. |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
:mod:`netrc`: skip security checks if :func:`os.getuid` is missing. | ||
Patch by Bénédikt Tran. |
Uh oh!
There was an error while loading. Please reload this page.