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(auth): disconnect sockets on inline expiry (#7144)

LearningCircuit2fefe0653eca
diff --git a/changelog.d/+inline-session-socket-expiry.security.md b/changelog.d/+inline-session-socket-expiry.security.md
new file mode 100644
index 0000000000..603bffd7ea
--- /dev/null
+++ b/changelog.d/+inline-session-socket-expiry.security.md
@@ -0,0 +1 @@
+Disconnect an expired session's live Socket.IO connection when an HTTP request expires that session inline, so the socket cannot keep receiving the user's events until the next cleanup sweep.
diff --git a/src/local_deep_research/web/auth/session_manager.py b/src/local_deep_research/web/auth/session_manager.py
index fa974d5344..573640c39b 100644
--- a/src/local_deep_research/web/auth/session_manager.py
+++ b/src/local_deep_research/web/auth/session_manager.py
@@ -70,7 +70,7 @@ class SessionManager:
def validate_session(self, session_id: str) -> Optional[str]:
"""
Validate a session and return username if valid.
- Updates last access time.
+ Updates last access time and disconnects sockets for an expired session.
"""
expired_username = None
with self._lock:
@@ -98,6 +98,10 @@ class SessionManager:
session_data["last_access"] = now
return str(session_data["username"])
+ # The scheduled sweep cannot find a session removed inline above.
+ # Disconnect only after releasing _lock: socket teardown can enter
+ # session code again, and this lock is not reentrant.
+ self._disconnect_expired_sockets([session_id])
logger.debug(
f"Session {session_id[:8]}... expired for {expired_username}"
)
diff --git a/tests/web/auth/test_session_manager.py b/tests/web/auth/test_session_manager.py
index e51d172c2a..8a4b38a365 100644
--- a/tests/web/auth/test_session_manager.py
+++ b/tests/web/auth/test_session_manager.py
@@ -441,7 +441,7 @@ _DISCONNECT_SESSION_PATCH = (
class TestExpiredSessionSocketTeardown:
- """cleanup_expired_sessions must also disconnect expired sessions' sockets.
+ """Both expiry paths must disconnect expired sessions' sockets.
A socket is authorised once at handshake and then frozen, so without an
explicit teardown an expired session's tab keeps receiving the user's
@@ -464,6 +464,44 @@ class TestExpiredSessionSocketTeardown:
assert valid_sid in manager.sessions
disconnect.assert_called_once_with(expired_sid)
+ def test_inline_validation_disconnects_only_the_expired_session(
+ self, manager
+ ):
+ expired_sid = manager.create_session("alice")
+ valid_sid = manager.create_session("bob")
+ manager.sessions[expired_sid]["last_access"] = datetime.datetime.now(
+ UTC
+ ) - datetime.timedelta(hours=MOCK_SESSION_TIMEOUT_HOURS + 1)
+
+ disconnected = []
+
+ def observe_disconnect(session_id):
+ disconnected.append((session_id, manager._lock.locked()))
+
+ with patch(_DISCONNECT_SESSION_PATCH, side_effect=observe_disconnect):
+ assert manager.validate_session(expired_sid) is None
+ assert manager.validate_session(valid_sid) == "bob"
+ manager.cleanup_expired_sessions()
+
+ assert expired_sid not in manager.sessions
+ assert valid_sid in manager.sessions
+ # Teardown is outside the non-reentrant session lock and happens only
+ # once; the later sweep cannot rediscover a session removed inline.
+ assert disconnected == [(expired_sid, False)]
+
+ def test_inline_expiry_survives_socket_teardown_failure(self, manager):
+ expired_sid = manager.create_session("alice")
+ manager.sessions[expired_sid]["last_access"] = datetime.datetime.now(
+ UTC
+ ) - datetime.timedelta(hours=MOCK_SESSION_TIMEOUT_HOURS + 1)
+
+ with patch(
+ _DISCONNECT_SESSION_PATCH, side_effect=RuntimeError("no loop")
+ ):
+ assert manager.validate_session(expired_sid) is None
+
+ assert expired_sid not in manager.sessions
+
def test_cleanup_survives_socket_service_uninitialized(self, manager):
"""If the socket service isn't initialised, cleanup still succeeds."""
expired_sid = manager.create_session("alice", remember_me=False)