summaryrefslogtreecommitdiffstats
path: root/Lib/netrc.py
diff options
context:
space:
mode:
authorR David Murray <rdmurray@bitdance.com>2013-09-18 01:28:17 (GMT)
committerR David Murray <rdmurray@bitdance.com>2013-09-18 01:28:17 (GMT)
commit4750fa8369407a2a77d40f5aa3e681e80ca08f42 (patch)
treefc36fa0a52f36670eece4bf9deb32c4e83066ec4 /Lib/netrc.py
parent935349406aeb9d43fecea447f0309ce63ed3a406 (diff)
parentfb9dc0b3ae1e048c89988fcf4c570cd73da2f455 (diff)
downloadcpython-4750fa8369407a2a77d40f5aa3e681e80ca08f42.zip
cpython-4750fa8369407a2a77d40f5aa3e681e80ca08f42.tar.gz
cpython-4750fa8369407a2a77d40f5aa3e681e80ca08f42.tar.bz2
Merge #14984: On POSIX, enforce permissions when reading default .netrc.
Diffstat (limited to 'Lib/netrc.py')
-rw-r--r--Lib/netrc.py27
1 files changed, 24 insertions, 3 deletions
diff --git a/Lib/netrc.py b/Lib/netrc.py
index 7fe69ee..050a174 100644
--- a/Lib/netrc.py
+++ b/Lib/netrc.py
@@ -2,7 +2,7 @@
# Module and documentation by Eric S. Raymond, 21 Dec 1998
-import io, os, shlex
+import io, os, shlex, stat, pwd
__all__ = ["netrc", "NetrcParseError"]
@@ -21,6 +21,7 @@ class NetrcParseError(Exception):
class netrc:
def __init__(self, file=None):
+ default_netrc = file is None
if file is None:
try:
file = os.path.join(os.environ['HOME'], ".netrc")
@@ -29,9 +30,9 @@ class netrc:
self.hosts = {}
self.macros = {}
with open(file) as fp:
- self._parse(file, fp)
+ self._parse(file, fp, default_netrc)
- def _parse(self, file, fp):
+ def _parse(self, file, fp, default_netrc):
lexer = shlex.shlex(fp)
lexer.wordchars += r"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
lexer.commenters = lexer.commenters.replace('#', '')
@@ -86,6 +87,26 @@ class netrc:
elif tt == 'account':
account = lexer.get_token()
elif tt == 'password':
+ if os.name == 'posix' and default_netrc:
+ prop = os.fstat(fp.fileno())
+ if prop.st_uid != os.getuid():
+ 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()
+ raise NetrcParseError(
+ ("~/.netrc file owner (%s) does not match"
+ " current user (%s)") % (fowner, user),
+ file, lexer.lineno)
+ if (prop.st_mode & (stat.S_IRWXG | stat.S_IRWXO)):
+ raise NetrcParseError(
+ "~/.netrc access too permissive: access"
+ " permissions must restrict access to only"
+ " the owner", file, lexer.lineno)
password = lexer.get_token()
else:
raise NetrcParseError("bad follower token %r" % tt,