Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(security): redact secrets in settings change events (#7006)

LearningCircuit1fd2004f6976
diff --git a/changelog.d/5626.security.md b/changelog.d/5626.security.md
new file mode 100644
index 0000000000..93cdd55a90
--- /dev/null
+++ b/changelog.d/5626.security.md
@@ -0,0 +1 @@
+Settings-change socket events now redact API keys, notification URLs, and custom password fields before sending updates to browser tabs, matching the HTTP settings read contract.
diff --git a/src/local_deep_research/settings/manager.py b/src/local_deep_research/settings/manager.py
index 03f501494c..923245c7c0 100644
--- a/src/local_deep_research/settings/manager.py
+++ b/src/local_deep_research/settings/manager.py
@@ -1945,11 +1945,11 @@ class SettingsManager(ISettingsManager):
try:
# Import here to avoid circular imports
from ..web.services.socketio_asgi import emit_to_user
+ from ..security.data_sanitizer import DataSanitizer
- # settings_changed carries raw setting values (including plaintext
- # API keys), so it must reach only the owning user's own browser
- # tabs — never every connected client on this shared, single-user-
- # agnostic Socket.IO server (#4437). A change made outside a
+ # Keep the event scoped to the owning user's browser tabs, never
+ # every connected client on this shared Socket.IO server (#4437).
+ # A change made outside a
# request context (app start-up defaults, background workers,
# migrations) has no user tab to notify, so we skip the emit
# entirely rather than fall back to a broadcast.
@@ -1974,7 +1974,31 @@ class SettingsManager(ISettingsManager):
for key in changed_keys:
setting_value = self.get_setting(key)
if setting_value is not None:
- settings_data[key] = {"value": setting_value}
+ # Match the HTTP read contract, including custom
+ # password controls whose key has no secret suffix.
+ # The exact DB row takes precedence over defaults.
+ exact_row = next(
+ (
+ row
+ for row in self.__query_settings(key)
+ if str(row.key) == key
+ ),
+ None,
+ )
+ if exact_row is not None:
+ ui_element = exact_row.ui_element
+ else:
+ default_meta = self.default_settings.get(key, {})
+ ui_element = (
+ default_meta.get("ui_element")
+ if isinstance(default_meta, dict)
+ else None
+ )
+ settings_data[key] = {
+ "value": DataSanitizer.redact_value(
+ key, ui_element, setting_value
+ )
+ }
emit_to_user(
"settings_changed",
diff --git a/src/local_deep_research/web/auth/session_manager.py b/src/local_deep_research/web/auth/session_manager.py
index 573640c39b..555b542587 100644
--- a/src/local_deep_research/web/auth/session_manager.py
+++ b/src/local_deep_research/web/auth/session_manager.py
@@ -194,8 +194,8 @@ class SessionManager:
layer. The old import raised ``ModuleNotFoundError`` into the bare
``except Exception`` below, so this path silently disconnected
nothing: an idle-expired session's sockets stayed live and kept
- receiving that user's events -- including ``settings_changed``, which
- carries plaintext secrets. It was masked whenever the idle-DB-close
+ receiving that user's events -- including ``settings_changed``. It
+ was masked whenever the idle-DB-close
sweep happened to tear the user down anyway, but not when they had
another live session or an in-flight research run.
"""
diff --git a/src/local_deep_research/web/routers/auth.py b/src/local_deep_research/web/routers/auth.py
index 5927a64c50..e2c8629723 100644
--- a/src/local_deep_research/web/routers/auth.py
+++ b/src/local_deep_research/web/routers/auth.py
@@ -104,8 +104,8 @@ def _disconnect_user_sockets(username: str) -> None:
"""Disconnect ALL of ``username``'s live sockets and drop subscriptions.
A Socket.IO connection is authorised once at handshake and then keeps
- delivering that user's events (including ``settings_changed``, which
- carries plaintext secrets) for its whole lifetime. Used by the
+ delivering that user's events (including ``settings_changed``) for its
+ whole lifetime. Used by the
password-change flow, which destroys every session for the user, so all
of their sockets must go. For single-session logout use
``_disconnect_session_sockets`` instead so other tabs / devices survive.
diff --git a/src/local_deep_research/web/services/socketio_asgi.py b/src/local_deep_research/web/services/socketio_asgi.py
index 42caf272f5..c4fd3b2d80 100644
--- a/src/local_deep_research/web/services/socketio_asgi.py
+++ b/src/local_deep_research/web/services/socketio_asgi.py
@@ -533,8 +533,8 @@ async def connect(sid, environ, auth=None):
# sockets by enumerating `_sid_sessions`; if that enumeration ran BEFORE
# the block above added this sid, the teardown never sees this socket. It
# would then sit registered with a dead session, still receiving that
- # user's events -- including `settings_changed`, which carries plaintext
- # secrets -- until some other session ends.
+ # user's events -- including `settings_changed` -- until some other
+ # session ends.
#
# Re-validate after registering and fail closed. Returning False rejects
# the handshake, so python-socketio never fires our `disconnect` handler
diff --git a/tests/web/services/test_settings_manager_extended.py b/tests/web/services/test_settings_manager_extended.py
index a036b119a6..9e2c26cfe2 100644
--- a/tests/web/services/test_settings_manager_extended.py
+++ b/tests/web/services/test_settings_manager_extended.py
@@ -725,9 +725,7 @@ class TestWebSocketEmission:
return SettingsManager(db_session=mock_session)
def test_emit_settings_changed_scoped_to_owning_user(self):
- """The event is emitted only to the changing user — never broadcast —
- so one user's setting values (incl. plaintext API keys) never reach
- other connected clients (#4437).
+ """The event is emitted only to the changing user (#4437).
Post-FastAPI-migration the owning user is resolved from the
request-context contextvar (set by DatabaseMiddleware) and the emit
@@ -752,6 +750,60 @@ class TestWebSocketEmission:
assert args[1] == "alice"
assert "app.debug" in args[2]["changed_keys"]
+ def test_emit_settings_changed_redacts_secrets_before_socket_delivery(
+ self,
+ ):
+ """A settings event exposes the same masked values as HTTP reads."""
+ manager = self._working_manager()
+ values = {
+ "llm.openai.api_key": "sk-socket-secret",
+ "notifications.service_url": "discord://id/socket-secret",
+ "custom.credential": "custom-socket-secret",
+ "llm.anthropic.api_key": "",
+ "app.debug": True,
+ }
+ rows = [
+ MockSetting(key=key, value=value, ui_element="password")
+ if key == "custom.credential"
+ else MockSetting(key=key, value=value)
+ for key, value in values.items()
+ ]
+
+ with (
+ patch.object(
+ manager,
+ "get_setting",
+ side_effect=lambda key, default=None, **_kwargs: values.get(
+ key, default
+ ),
+ ),
+ patch.object(
+ manager,
+ "_SettingsManager__query_settings",
+ side_effect=lambda key: [row for row in rows if row.key == key],
+ ),
+ patch(
+ "local_deep_research.utilities.request_context.get_current_username",
+ return_value="alice",
+ ),
+ patch(
+ "local_deep_research.web.services.socketio_asgi.emit_to_user"
+ ) as emit,
+ ):
+ manager._emit_settings_changed(list(values))
+
+ emit.assert_called_once()
+ payload = emit.call_args.args[2]
+ assert emit.call_args.args[:2] == ("settings_changed", "alice")
+ assert payload["settings"] == {
+ "llm.openai.api_key": {"value": "[REDACTED]"},
+ "notifications.service_url": {"value": "[REDACTED]"},
+ "custom.credential": {"value": "[REDACTED]"},
+ "llm.anthropic.api_key": {"value": ""},
+ "app.debug": {"value": True},
+ }
+ assert "socket-secret" not in json.dumps(payload)
+
def test_emit_settings_changed_skipped_without_request_username(self):
"""A settings change outside any request (start-up defaults, background
workers, migrations) or without an authenticated username has no user