mirror of
https://github.com/EDCD/EDMarketConnector.git
synced 2026-11-28 03:32:23 +03:00
[#2567] Improve TOML Dump Safety
This commit is contained in:
1 file changed
+19
-2
+19
-2
@@ -9,6 +9,7 @@ from __future__ import annotations
|
||||
|
||||
import pathlib
|
||||
import sys
|
||||
import os
|
||||
import uuid
|
||||
import winreg
|
||||
import datetime
|
||||
@@ -88,9 +89,25 @@ class WinConfigMinimal:
|
||||
|
||||
path_obj = pathlib.Path(toml_path)
|
||||
path_obj.parent.mkdir(parents=True, exist_ok=True)
|
||||
tmp_path = path_obj.with_suffix(path_obj.suffix + ".tmp")
|
||||
config_logger.debug("Writing Config File")
|
||||
with path_obj.open("wb") as f:
|
||||
tomli_w.dump(config_data, f)
|
||||
try:
|
||||
# Validate serialization BEFORE touching the real file
|
||||
tomli_w.dumps(config_data)
|
||||
with tmp_path.open("wb") as f:
|
||||
tomli_w.dump(config_data, f)
|
||||
f.flush()
|
||||
os.fsync(f.fileno())
|
||||
tmp_path.replace(path_obj)
|
||||
except Exception:
|
||||
config_logger.exception("Failed to write TOML config file")
|
||||
# Ensure temp file doesn't linger
|
||||
try:
|
||||
if tmp_path.exists():
|
||||
tmp_path.unlink()
|
||||
except Exception:
|
||||
pass
|
||||
raise
|
||||
|
||||
def close(self):
|
||||
"""Close registry handle."""
|
||||
|
||||
Reference in new issue
Block a user