~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""CROSS-USER ISOLATION regression tests (Flask -> FastAPI migration branch).
The primary contract is that ``utilities/db_utils.py`` resolves the username
before cache lookup and calls
``_get_cached_user_session(username, namespace)``. The cache key therefore
contains both identity and namespace. This file also protects adjacent engine,
thread-local session, settings-context, and contextvar isolation invariants.
The application runs uvicorn with ``workers=1`` and offloads synchronous route
handlers to a reused AnyIO thread pool. Each invariant is therefore asserted at
the layer where it is enforced, so a future regression fails CI.
Because thread affinity cannot be assumed (any of the ~40 pooled threads may
serve any user's request, back to back), each test below drives the real
mechanism with genuine concurrency -- real OS threads or real asyncio tasks
synchronized with barriers/events, never a sleep-based race -- rather than
asserting on a single-threaded call.
Invariants pinned here:
1. ``DatabaseManager``/``db_manager`` never hands the same SQLAlchemy Engine
to two different usernames (database/encrypted_db.py).
2. ``ThreadLocalSessionManager.get_session`` self-heals a username mismatch
on a reused thread instead of returning the previous user's session
(database/thread_local_session.py).
3. ``thread_specific_cache``'s cache key includes the full call signature
(in particular ``username``), so ``_get_cached_user_session`` never
collides two different users on the same thread (utilities/
threading_utils.py + utilities/db_utils.py).
4. ``get_setting_from_snapshot`` refuses to serve a thread-local
``settings_context`` built for a different user than the current request
(config/thread_settings.py).
5. The ``set_request_user``/``get_current_username`` contextvar does not
leak between concurrently-running asyncio tasks, even across a hop
through a shared, reused thread pool (utilities/request_context.py).
"""
import asyncio
import threading
import uuid
from concurrent.futures import ThreadPoolExecutor
import pytest
from cachetools import LRUCache
from local_deep_research.config.thread_settings import (
clear_settings_context,
get_setting_from_snapshot,
set_settings_context,
)
from local_deep_research.database.encrypted_db import DatabaseManager
from local_deep_research.database.thread_local_session import (
ThreadLocalSessionManager,
)
from local_deep_research.settings.manager import SnapshotSettingsContext
from local_deep_research.utilities.db_utils import _get_cached_user_session
from local_deep_research.utilities.request_context import (
get_current_username,
reset_request_user,
set_request_user,
)
from local_deep_research.utilities.threading_utils import thread_specific_cache
# ---------------------------------------------------------------------------
# Shared fixture: two REAL, on-disk, SQLCipher-encrypted per-user databases.
# ---------------------------------------------------------------------------
# Module-scoped (not per-test) because DatabaseManager.create_user_database()
# runs the full Alembic migration chain against a fresh SQLCipher database --
# multiple seconds even with a reduced KDF iteration count. The two users
# below are created once and then reused (opened/read, never destructively
# mutated) by every test in this file, which keeps real-encryption coverage
# without blowing the CI time budget.
_ALICE = f"cui_alice_{uuid.uuid4().hex[:8]}"
_BOB = f"cui_bob_{uuid.uuid4().hex[:8]}"
_ALICE_PW = "AliceCrossUser123!"
_BOB_PW = "BobCrossUser123!"
@pytest.fixture(scope="module")
def real_db_manager(tmp_path_factory):
"""A real ``DatabaseManager`` with two real encrypted user databases.
Not a mock: this is the actual ``DatabaseManager`` class from
database/encrypted_db.py, pointed at an isolated temp directory instead
of the production data directory, with two real SQLCipher databases
created on disk via the real ``create_user_database()`` code path.
"""
mp = pytest.MonkeyPatch()
# Test-infrastructure speed only (PBKDF2 iteration count), not a change
# to the code path under test -- mirrors tests/conftest.py's `app`
# fixture, which sets this for the same reason.
mp.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "1000")
# Keep the real production QueuePool (not the test-only StaticPool) so
# the concurrency tests below exercise the pool uvicorn actually uses.
mp.delenv("TESTING", raising=False)
tmp_dir = tmp_path_factory.mktemp("cross_user_isolation_dbs")
manager = DatabaseManager()
# The manager resolves its default directory on every access, so a
# constructor-only path patch cannot keep module-scoped databases stable
# across the function-scoped platform-default fixtures.
manager.data_dir = tmp_dir / "encrypted_databases"
manager.create_user_database(_ALICE, _ALICE_PW)
manager.create_user_database(_BOB, _BOB_PW)
try:
yield manager
finally:
manager.close_all_databases()
mp.undo()
# ---------------------------------------------------------------------------
# 1. Per-user engine isolation (database/encrypted_db.py)
# ---------------------------------------------------------------------------
class TestPerUserEngineIsolation:
def test_concurrent_open_user_database_never_shares_engine_across_users(
self, real_db_manager
):
"""PIN: DatabaseManager.open_user_database() must never return the
same SQLAlchemy Engine object for two different usernames, even
under real concurrent access from many threads.
WHY IT MATTERS: with uvicorn workers=1 and sync handlers offloaded
to the AnyIO threadpool, ``db_manager.connections`` (database/
encrypted_db.py, ``DatabaseManager.connections`` /
``open_user_database``) is read and written by many concurrently
running requests for DIFFERENT users at once. If a race in that
dict ever let one user's Engine leak into another user's cache
slot, every query subsequently issued for the "wrong" user would
run against a stranger's encrypted SQLCipher database -- exactly
the class of cross-user engine reuse this test prevents. This
test hammers the real per-user cold-open + cache path with real
threads and real encrypted databases and asserts engine identity
never crosses a username boundary.
Mechanism: src/local_deep_research/database/encrypted_db.py,
``DatabaseManager.open_user_database`` / ``.connections``.
"""
manager = real_db_manager
users = [(_ALICE, _ALICE_PW), (_BOB, _BOB_PW)]
# Evict the cached connections first so the concurrent opens below
# race through the real cold-open + per-user-lock path, not just a
# dict hit on an already-open connection.
for username, _ in users:
manager.close_user_database(username)
n_per_user = 6
jobs = [
(username, password)
for username, password in users
for _ in range(n_per_user)
]
barrier = threading.Barrier(len(jobs))
results_lock = threading.Lock()
results: list[tuple[str, object]] = []
errors: list[BaseException] = []
def worker(username, password):
barrier.wait(timeout=30)
try:
engine = manager.open_user_database(username, password)
except BaseException as exc: # noqa: BLE001 - surfaced via `errors`
with results_lock:
errors.append(exc)
return
with results_lock:
results.append((username, engine))
threads = [
threading.Thread(target=worker, args=(u, p)) for u, p in jobs
]
for t in threads:
t.start()
for t in threads:
t.join(timeout=30)
assert not errors, f"concurrent open_user_database() raised: {errors!r}"
assert len(results) == len(jobs)
# No Engine object may ever be recorded against two different
# usernames -- the core cross-user-leak assertion.
engine_owner: dict[object, str] = {}
for username, engine in results:
assert engine is not None
if engine in engine_owner:
assert engine_owner[engine] == username, (
f"Engine handed to both {engine_owner[engine]!r} and "
f"{username!r} -- cross-user engine leak"
)
else:
engine_owner[engine] = username
# Every user's concurrent callers converge on exactly one engine...
for username, _ in users:
engines_for_user = {
engine for u, engine in results if u == username
}
assert len(engines_for_user) == 1, (
f"{username} received {len(engines_for_user)} distinct "
"engines across concurrent callers (cache is not stable)"
)
# ...and the two users' engines are never the same object.
alice_engine = next(e for u, e in results if u == _ALICE)
bob_engine = next(e for u, e in results if u == _BOB)
assert alice_engine is not bob_engine
# ---------------------------------------------------------------------------
# 2. Thread-local session self-heal (database/thread_local_session.py)
# ---------------------------------------------------------------------------
class TestThreadLocalSessionSelfHeal:
def test_get_session_rebuilds_on_username_mismatch_same_thread(
self, real_db_manager, monkeypatch, loguru_caplog
):
"""PIN: ThreadLocalSessionManager.get_session() detects a
cached-session/username mismatch on a REUSED thread and rebuilds --
it must never silently return the previous user's session.
WHY IT MATTERS: ThreadLocalSessionManager caches one DB session PER
OS THREAD (not per-request), keyed by ``threading.local()``. uvicorn's
AnyIO threadpool reuses OS threads across different users' requests,
so a thread that just served alice's request can be handed to bob's
request next. Without the ``!= username`` self-heal check, bob would
silently receive alice's already-open SQLCipher session object and
every read/write issued for "bob" would actually hit alice's
encrypted database. This drives the REAL class (not a mock) with two real
per-user encrypted databases, on one physical thread.
Mechanism: src/local_deep_research/database/thread_local_session.py,
``ThreadLocalSessionManager.get_session``, the
``getattr(self._local, "username", None) != username`` guard.
"""
monkeypatch.setattr(
"local_deep_research.database.thread_local_session.db_manager",
real_db_manager,
)
manager = ThreadLocalSessionManager()
try:
session_alice = manager.get_session(_ALICE, _ALICE_PW)
assert session_alice is not None
assert manager._local.username == _ALICE
assert session_alice.bind is real_db_manager.connections[_ALICE]
with loguru_caplog.at_level("WARNING"):
session_bob = manager.get_session(_BOB, _BOB_PW)
assert session_bob is not None
assert session_bob is not session_alice, (
"get_session() returned alice's cached session for bob's "
"request on the reused thread"
)
assert manager._local.username == _BOB
assert session_bob.bind is real_db_manager.connections[_BOB]
# The self-heal must be logged, not silent -- so the mechanism
# is auditable/alertable in production, not just "happens to
# work". Pin the actual message from the source.
assert "Session username mismatch" in loguru_caplog.text
assert "clearing stale cross-user session" in loguru_caplog.text
finally:
manager._cleanup_thread_session()
# ---------------------------------------------------------------------------
# 3. thread_specific_cache key includes the username
# (utilities/threading_utils.py + utilities/db_utils.py)
# ---------------------------------------------------------------------------
class TestThreadSpecificCacheUsernameKey:
def test_thread_specific_cache_does_not_collide_across_usernames_same_thread(
self,
):
"""PIN: thread_specific_cache()'s key function folds the FULL call
signature of the decorated function (not just the thread id) into
the cache key, so two different usernames called on the SAME thread
land in different cache slots.
WHY IT MATTERS: thread_specific_cache() (utilities/threading_utils.py)
is the caching primitive that backs ``_get_cached_user_session``
(utilities/db_utils.py) -- the DB-session cache consulted on every
request served by a pooled AnyIO worker thread. Its key is
``(thread_id,) + keys.hashkey(*args, **kwargs)``. This is a
regression pin: if a future change ever computed the key from ONLY
the thread id (e.g. "cache the one most-recent session on this
thread" instead of "cache per (thread, username)"), two different
users whose requests land on the same reused thread would silently
be served each other's cached return value.
Mechanism: src/local_deep_research/utilities/threading_utils.py,
``thread_specific_cache()._key_func``.
"""
calls = {"n": 0}
@thread_specific_cache(cache=LRUCache(maxsize=10))
def build_marker(username: str) -> object:
calls["n"] += 1
return object() # a fresh, unique sentinel per real call
alice_marker_1 = build_marker("cache_alice")
bob_marker = build_marker("cache_bob")
alice_marker_2 = build_marker("cache_alice")
assert calls["n"] == 2, (
"expected exactly 2 real (uncached) calls, one per username -- "
"a 3rd call means alice's cache entry was evicted or "
"overwritten by bob's call"
)
assert alice_marker_1 is alice_marker_2, (
"alice's second call did not hit her own cache entry"
)
assert bob_marker is not alice_marker_1, (
"bob's call returned alice's cached value -- cross-user cache "
"collision on a shared thread"
)
def test_get_cached_user_session_no_cross_user_collision_same_thread(
self, real_db_manager, monkeypatch
):
"""PIN: db_utils._get_cached_user_session -- the actual production
caller of thread_specific_cache -- never returns one user's cached
DB session for a different user's request on the same thread.
WHY IT MATTERS: ``get_db_session()`` must resolve
``username`` from the request-context contextvar and then call
``_get_cached_user_session(username, _namespace)``; its own
docstring warns that caching ABOVE username resolution would key
two different users' requests, served by the same threadpool
worker, to one entry. This drives the REAL decorated function
(not a re-implementation) with two real usernames and two real
per-user SQLCipher engines, on one thread.
Mechanism: src/local_deep_research/utilities/db_utils.py,
``_get_cached_user_session``; src/local_deep_research/utilities/
threading_utils.py, ``thread_specific_cache``.
"""
monkeypatch.setattr(
"local_deep_research.utilities.db_utils.db_manager",
real_db_manager,
)
ns = f"invariant3_{uuid.uuid4().hex[:8]}"
session_alice_1 = _get_cached_user_session(_ALICE, ns)
try:
session_bob = _get_cached_user_session(_BOB, ns)
try:
session_alice_2 = _get_cached_user_session(_ALICE, ns)
assert session_alice_2 is session_alice_1, (
"alice's second lookup did not hit her cached session"
)
assert session_bob is not session_alice_1, (
"bob's lookup returned alice's cached session object"
)
assert (
session_alice_1.bind is real_db_manager.connections[_ALICE]
)
assert session_bob.bind is real_db_manager.connections[_BOB]
finally:
session_bob.close()
finally:
session_alice_1.close()
# ---------------------------------------------------------------------------
# 4. settings_context identity guard (config/thread_settings.py)
# ---------------------------------------------------------------------------
class TestSettingsContextIdentityGuard:
def test_get_setting_from_snapshot_rejects_stale_cross_user_context(
self, loguru_caplog
):
"""PIN: get_setting_from_snapshot() must not return a value from a
thread-local settings_context built for a different user than the
CURRENT request. On a username mismatch it falls through to the
caller's default and logs a warning; on a match it serves the
context value normally. Both branches are exercised here.
WHY IT MATTERS: config/thread_settings.py's ``_thread_local`` is a
bare ``threading.local()``, NOT a contextvar -- so on a pooled
AnyIO worker thread it outlives whichever request set it via
``set_settings_context()``/``settings_context()``. If a future call
site ever sets a settings context without a guaranteed
``clear_settings_context()`` in a ``finally`` (or if this identity
check regressed), the NEXT request reusing this OS thread for a
DIFFERENT user would read the previous user's settings. The identity
check prevents that reuse. This test leaves a stale context in place (as it
would be if cleanup were skipped) and only the request-scoped
username changes.
Mechanism: src/local_deep_research/config/thread_settings.py,
``get_setting_from_snapshot``'s ``ctx_username != current_username``
check.
"""
alice_ctx = SnapshotSettingsContext(
{"llm.model": "alice-private-model"}, username="alice_ctx_user"
)
tokens = set_request_user("alice_ctx_user")
try:
set_settings_context(alice_ctx)
try:
# --- Matching branch: same user -> context value is served.
value = get_setting_from_snapshot(
"llm.model", default="DEFAULT_MODEL"
)
assert value == "alice-private-model"
# --- Mismatched branch: the thread is handed to a
# different user's request WITHOUT clear_settings_context()
# having run (the mismatched-context scenario) -- only the
# request-scoped contextvar changes; the stale
# threading.local() context is untouched.
bob_tokens = set_request_user("bob_ctx_user")
try:
assert get_current_username() == "bob_ctx_user"
with loguru_caplog.at_level("WARNING"):
leaked_value = get_setting_from_snapshot(
"llm.model", default="DEFAULT_MODEL"
)
assert leaked_value == "DEFAULT_MODEL", (
"bob's read returned alice's stale context value "
f"({leaked_value!r}) instead of falling through to "
"the default"
)
assert (
"Discarding stale thread-local settings context"
in loguru_caplog.text
)
assert "alice_ctx_user" in loguru_caplog.text
assert "bob_ctx_user" in loguru_caplog.text
finally:
reset_request_user(bob_tokens)
finally:
clear_settings_context()
finally:
reset_request_user(tokens)
# ---------------------------------------------------------------------------
# 5. Contextvar isolation across the AnyIO/asyncio threadpool
# (utilities/request_context.py)
# ---------------------------------------------------------------------------
class TestContextvarIsolationAcrossThreadpool:
@pytest.mark.asyncio
async def test_request_user_contextvar_survives_concurrent_threadpool_reuse(
self,
):
"""PIN: set_request_user()/get_current_username() must not leak
between concurrently-running "requests" (asyncio tasks) even when
their sync work is offloaded onto a SHARED, REUSED thread pool --
the exact execution model uvicorn uses on this branch (workers=1,
sync route handlers dispatched to the AnyIO threadpool).
WHY IT MATTERS: request_context.py deliberately uses ``contextvars``
(not ``threading.local``) specifically because contextvars are
captured per-asyncio-Task and correctly propagated by
``asyncio.to_thread``/AnyIO's threadpool dispatch even when the
underlying OS thread is reused for a different task's work. This
test forces genuine contention on a DELIBERATELY small thread pool
(more concurrent tasks than worker threads, synchronized with an
``asyncio.Barrier`` so every task's thread-pool hop is in flight at
the same time -- no sleep-based race) and asserts every one of many
concurrent "requests" reads back its OWN username: immediately
after an ``await``, after a hop through ``asyncio.to_thread`` onto a
thread some other task already used, and again back on the event
loop afterward.
Mechanism: src/local_deep_research/utilities/request_context.py,
``set_request_user`` / ``get_current_username``
(``contextvars.ContextVar``).
"""
n_tasks = 24
pool_workers = 4
barrier = asyncio.Barrier(n_tasks)
executor = ThreadPoolExecutor(max_workers=pool_workers)
loop = asyncio.get_running_loop()
loop.set_default_executor(executor)
mismatches: list[tuple[str, str, object]] = []
mismatches_lock = threading.Lock()
async def run_as_user(i: int) -> None:
username = f"threadpool_user_{i}"
tokens = set_request_user(username)
try:
# Every task waits here until ALL n_tasks have set their own
# username -- guarantees the thread-pool hops below are
# genuinely concurrent, deterministically, without sleeps.
await barrier.wait()
if get_current_username() != username:
with mismatches_lock:
mismatches.append(
("pre-thread", username, get_current_username())
)
# Real hop onto the shared pool. pool_workers << n_tasks
# guarantees this OS thread has already run another task's
# work (or will, for a task still waiting).
seen_in_thread = await asyncio.to_thread(get_current_username)
if seen_in_thread != username:
with mismatches_lock:
mismatches.append(
("in-thread", username, seen_in_thread)
)
if get_current_username() != username:
with mismatches_lock:
mismatches.append(
("post-thread", username, get_current_username())
)
finally:
reset_request_user(tokens)
try:
await asyncio.gather(*(run_as_user(i) for i in range(n_tasks)))
finally:
executor.shutdown(wait=True)
assert mismatches == [], (
f"contextvar leaked across the shared threadpool: {mismatches!r}"
)