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): roll back partial session so a post-creation failure isn't a silent login (#5006)

LearningCircuit873903fd104b
diff --git a/changelog.d/5006.bugfix.md b/changelog.d/5006.bugfix.md
new file mode 100644
index 0000000000..3c5aeab932
--- /dev/null
+++ b/changelog.d/5006.bugfix.md
@@ -0,0 +1 @@
+Registration/login no longer leave a user silently logged in when post-authentication setup fails partway: the partial session is now rolled back, so a reported failure matches reality (you are not logged in).
diff --git a/src/local_deep_research/web/auth/routes.py b/src/local_deep_research/web/auth/routes.py
index 6bf862b0e2..b99d4346c4 100644
--- a/src/local_deep_research/web/auth/routes.py
+++ b/src/local_deep_research/web/auth/routes.py
@@ -59,24 +59,41 @@ def _create_user_session(session, username: str, password: str, remember: bool):
# Prevent session fixation by clearing old session data before creating new
session.clear()
- # Create session
- session_id = session_manager.create_session(username, remember)
- session["session_id"] = session_id
- session["username"] = username
- session.permanent = remember
+ session_id = None
+ try:
+ # Create session
+ session_id = session_manager.create_session(username, remember)
+ session["session_id"] = session_id
+ session["username"] = username
+ session.permanent = remember
- # Store password temporarily for post-login database access
- from ...database.temp_auth import temp_auth_store
+ # Store password temporarily for post-login database access
+ from ...database.temp_auth import temp_auth_store
- auth_token = temp_auth_store.store_auth(username, password)
- session["temp_auth_token"] = auth_token
+ auth_token = temp_auth_store.store_auth(username, password)
+ session["temp_auth_token"] = auth_token
- # Also store in session password store for metrics access
- from ...database.session_passwords import session_password_store
+ # Also store in session password store for metrics access
+ from ...database.session_passwords import session_password_store
- session_password_store.store_session_password(
- username, session_id, password
- )
+ session_password_store.store_session_password(
+ username, session_id, password
+ )
+ except Exception:
+ # Roll back a partially-created session so the caller's error response
+ # doesn't leave the user half-logged-in: a partially-set Flask cookie
+ # (session_id/username already assigned) would otherwise persist on the
+ # caller's 500 and effectively log them in despite the reported
+ # failure. Clear the cookie and best-effort tear down the server-side
+ # session + password-store entry, then re-raise. Cleanup must not mask
+ # the original error.
+ session.clear()
+ if session_id is not None:
+ try:
+ _cleanup_user_session(username, session_id=session_id)
+ except Exception:
+ logger.exception("Failed to roll back a partial user session")
+ raise
return session_id
diff --git a/tests/auth_tests/test_auth_routes.py b/tests/auth_tests/test_auth_routes.py
index 6893751ac4..57d5f82f90 100644
--- a/tests/auth_tests/test_auth_routes.py
+++ b/tests/auth_tests/test_auth_routes.py
@@ -565,6 +565,78 @@ class TestAuthRoutes:
assert db_manager.user_exists(username)
assert db_path.exists()
+ def test_post_creation_session_failure_does_not_leave_user_logged_in(
+ self, client, monkeypatch
+ ):
+ """If the post-creation session setup fails, the user must not be left
+ half-logged-in.
+
+ create_user_database() succeeds (the account is legitimately created),
+ but a later step in _create_user_session — storing the session password
+ — fails. Registration returns 500, and the Flask session must NOT retain
+ the auth keys: otherwise the partially-set cookie would effectively log
+ the user in on their next request despite the reported failure.
+ """
+ from local_deep_research.database.session_passwords import (
+ session_password_store,
+ )
+ from local_deep_research.web.auth.session_manager import (
+ session_manager,
+ )
+
+ username = "testuser_sessfail"
+ captured = {}
+
+ def _boom(*args, **kwargs):
+ # store_session_password(username, session_id, password) — capture
+ # the server-side session_id so we can assert it was torn down.
+ captured["session_id"] = (
+ args[1] if len(args) > 1 else kwargs.get("session_id")
+ )
+ raise RuntimeError("simulated post-creation session failure")
+
+ # Fails AFTER _create_user_session has already set session_id/username/
+ # temp_auth_token in the cookie AND created the server-side session —
+ # exactly the half-logged-in window.
+ monkeypatch.setattr(
+ session_password_store, "store_session_password", _boom
+ )
+
+ response = client.post(
+ "/auth/register",
+ data={
+ "username": username,
+ "password": "TestPass123",
+ "confirm_password": "TestPass123",
+ "acknowledge": "true",
+ },
+ )
+ assert response.status_code == 500
+ assert captured.get("session_id") # the failing step actually ran
+
+ # 1. Cookie rolled back: no auth material survives (session_id/username
+ # gate login; temp_auth_token is credential-lookup material).
+ with client.session_transaction() as sess:
+ assert "session_id" not in sess
+ assert "username" not in sess
+ assert "temp_auth_token" not in sess
+
+ # 2. Server-side state torn down too (not just the cookie): the session
+ # and its password-store entry must be gone, or they leak.
+ assert session_manager.validate_session(captured["session_id"]) is None
+ assert (
+ session_password_store.get_session_password(
+ username, captured["session_id"]
+ )
+ is None
+ )
+
+ # 3. End-to-end: the user is genuinely not authenticated — a protected
+ # route redirects to login.
+ protected = client.get("/")
+ assert protected.status_code == 302
+ assert "/auth/login" in protected.location
+
def test_successful_login(self, client):
"""Test successful login."""
# Register user first