From d8eed62e08ff830a0d3fd35a9f812203a8197df9 Mon Sep 17 00:00:00 2001 From: David Sangrey Date: Fri, 26 Dec 2025 11:22:31 -0500 Subject: [PATCH] [#2567] Improve TOML Dump Safety --- config/windows.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/config/windows.py b/config/windows.py index ae727c33..947a71a0 100644 --- a/config/windows.py +++ b/config/windows.py @@ -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."""