Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

fix(utils): load_json_config hands back a copy, not the caller default

Juan Denisea7129b4d75d
diff --git a/backend/app/utils/config_store.py b/backend/app/utils/config_store.py
index 8ca181de..4106d2c0 100644
--- a/backend/app/utils/config_store.py
+++ b/backend/app/utils/config_store.py
@@ -13,6 +13,7 @@ locking, no atomic rename — none of the originals had them, and adding them
here would be a behaviour change smuggled in with a consolidation. If atomic
writes are ever wanted, they land here, once.
"""
+import copy
import json
import os
@@ -21,7 +22,9 @@ def load_json_config(path, default):
"""JSON file → dict; *default* on missing or corrupt.
A corrupt config reads as the default, never raises — the same contract
- the hand-rolled copies had, and the one callers rely on at boot.
+ the hand-rolled copies had, and the one callers rely on at boot. The
+ fallback is a deep copy: callers mutate what they get back, and a shared
+ default object would leak those mutations into every later call.
"""
if os.path.exists(path):
try:
@@ -29,7 +32,7 @@ def load_json_config(path, default):
return json.load(f)
except Exception: # noqa: BLE001
pass
- return default
+ return copy.deepcopy(default)
def save_json_config(path, config):
diff --git a/backend/tests/test_config_store.py b/backend/tests/test_config_store.py
index ed4c5e7e..0c0611da 100644
--- a/backend/tests/test_config_store.py
+++ b/backend/tests/test_config_store.py
@@ -18,11 +18,17 @@ def test_missing_file_reads_as_the_default(tmp_path):
def test_the_default_is_not_shared_between_calls(tmp_path):
- """Callers mutate what they get back; a shared default would leak."""
- default = {'items': []}
- first = load_json_config(str(tmp_path / 'cfg.json'), default)
+ """Callers mutate what they get back; a shared default would leak —
+ including a default the caller keeps on a class/module, not just a
+ literal (PR review)."""
+ shared = {'items': [], 'nested': {'flag': False}}
+ first = load_json_config(str(tmp_path / 'cfg.json'), shared)
first['items'].append('x')
- assert load_json_config(str(tmp_path / 'cfg.json'), default) == default
+ first['nested']['flag'] = True
+
+ second = load_json_config(str(tmp_path / 'cfg.json'), shared)
+ assert second == {'items': [], 'nested': {'flag': False}}
+ assert shared == {'items': [], 'nested': {'flag': False}} # and the caller's own copy is untouched
def test_corrupt_file_reads_as_the_default_never_raises(tmp_path):