Fix insecure temporary file creation (#1890)

* Fix insecure temporary file creation

* minor error fix

tmp_path.name (NamedTemporaryFile().name) is required to return the filename string.
old-commit-hash: a34b9f5cb5
commatwo_master
Mufeed VH 5 years ago committed by GitHub
parent 370a68623e
commit 9b94e0ae8b
  1. 6
      common/params.py

@ -319,14 +319,14 @@ def write_db(params_path, key, value):
lock.acquire() lock.acquire()
try: try:
tmp_path = tempfile.mktemp(prefix=".tmp", dir=params_path) tmp_path = tempfile.NamedTemporaryFile(mode="wb", prefix=".tmp", dir=params_path, delete=False)
with open(tmp_path, "wb") as f: with tmp_path as f:
f.write(value) f.write(value)
f.flush() f.flush()
os.fsync(f.fileno()) os.fsync(f.fileno())
path = "%s/d/%s" % (params_path, key) path = "%s/d/%s" % (params_path, key)
os.rename(tmp_path, path) os.rename(tmp_path.name, path)
fsync_dir(os.path.dirname(path)) fsync_dir(os.path.dirname(path))
finally: finally:
os.umask(prev_umask) os.umask(prev_umask)

Loading…
Cancel
Save