From 2b138f742b74a9f8a81f7b0bdd3768019f01854c Mon Sep 17 00:00:00 2001 From: Sylvain Defresne Date: Thu, 12 Jul 2018 08:34:48 +0000 Subject: [PATCH] Avoid creating an empty netrc file in $TEMP The code to load .netrc file copies it to a temporary file named $TEMP/XXXXXXXXXgerrit_util/netrc before loading it to workaround crbug.com/664664. However, using a netrc file is deprecated (in favor of a gitcookies file instead) so most of the time, the file is empty. Instead of creating an empty file, load data from /dev/null (named os.devnull). This avoid leaving an empty file lying around (since the temporary file is not deleted), and avoid race-conditions on Windows when multiple processes/threads try to create the same file. Bug: none Change-Id: I873b72402038ca3f0d32387353dc89b731aaa927 Reviewed-on: https://chromium-review.googlesource.com/1132998 Commit-Queue: Sylvain Defresne Reviewed-by: Ryan Tseng --- gerrit_util.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/gerrit_util.py b/gerrit_util.py index 81b28e728..d5780adce 100644 --- a/gerrit_util.py +++ b/gerrit_util.py @@ -134,16 +134,17 @@ class CookiesAuthenticator(Authenticator): def _get_netrc(cls): # Buffer the '.netrc' path. Use an empty file if it doesn't exist. path = cls.get_netrc_path() - content = '' - if os.path.exists(path): - st = os.stat(path) - if st.st_mode & (stat.S_IRWXG | stat.S_IRWXO): - print >> sys.stderr, ( - 'WARNING: netrc file %s cannot be used because its file ' - 'permissions are insecure. netrc file permissions should be ' - '600.' % path) - with open(path) as fd: - content = fd.read() + if not os.path.exists(path): + return netrc.netrc(os.devnull) + + st = os.stat(path) + if st.st_mode & (stat.S_IRWXG | stat.S_IRWXO): + print >> sys.stderr, ( + 'WARNING: netrc file %s cannot be used because its file ' + 'permissions are insecure. netrc file permissions should be ' + '600.' % path) + with open(path) as fd: + content = fd.read() # Load the '.netrc' file. We strip comments from it because processing them # can trigger a bug in Windows. See crbug.com/664664.