~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
"""Pins a KNOWN, documented gap: startup validation of the rate-limit
storage backend was LOST in the Flask -> FastAPI migration.
On main, ``src/local_deep_research/security/rate_limiter.py`` exposed
``validate_rate_limit_storage()`` (backed by ``_validated_storage_uri()``),
called by ``app_factory`` right before ``limiter.init_app`` so a broken
``RATELIMIT_STORAGE_URL`` (e.g. ``redis://`` with the ``redis`` client
package not installed, or an unreachable host) aborted server startup
with an actionable message:
"RATELIMIT_STORAGE_URL is configured, but the rate-limit storage
backend could not be initialised (<ExceptionType>). Install the
required client package (e.g. `pip install redis` for redis:// URLs)
or unset RATELIMIT_STORAGE_URL to use per-process in-memory limits."
That module does not exist at all on this branch (see the FastAPI
rewrite of rate limiting, ``web/dependencies/rate_limit.py`` — a
different module, different env var name (``RATE_LIMIT_STORAGE_URI`` vs
``RATELIMIT_STORAGE_URL``), and NO equivalent
``validate_rate_limit_storage`` / ``_validated_storage_uri`` function).
There is no dedicated, friendly startup check for the storage backend
today.
Nuance worth recording precisely (so the gap isn't overstated): slowapi's
``Limiter.__init__`` happens to call ``limits.storage.storage_from_string``
eagerly, so a genuinely malformed/unusable URI still blows up at import
time rather than being silently swallowed — see
``test_malformed_storage_uri_raises_bare_configurationerror`` below. What
was actually lost is the FRIENDLY wrapping: naming the offending env var
and telling the operator how to fix it, instead of a bare
``limits.errors.ConfigurationError`` surfacing from deep inside slowapi.
What would restore this: add a startup check — either inside
``web/dependencies/rate_limit.py`` itself (wrap the ``Limiter(...)``
construction in a try/except that catches
``limits.errors.ConfigurationError`` and re-raises a ``RuntimeError``
naming ``RATE_LIMIT_STORAGE_URI`` and the remedy) or as a dedicated
``validate_rate_limit_storage()`` called from
``fastapi_app.lifespan``/``_setup_rate_limiting`` before the module's
module-level ``Limiter(**_limiter_kwargs)`` runs. Once that lands, the
absence-pinning tests below (marked accordingly) should be deleted and
replaced with tests that exercise the new function directly — they are
written narrowly enough that doing so is a small diff, not a rewrite.
Do NOT implement the feature here — this file only documents/pins the
gap and the current configuration.
"""
import importlib
from contextlib import contextmanager
import pytest
from limits.errors import ConfigurationError
from slowapi import Limiter
def _reload_rate_limit():
"""Reload ``web/dependencies/rate_limit.py`` so it re-reads env vars.
Mirrors the helper in ``tests/web/dependencies/test_rate_limit_storage.py``
(that file owns the "does storage_uri thread through to Limiter"
behaviour; this file owns the "there is no validation of it" gap —
kept as separate, differently-scoped test modules per subject rather
than merged).
"""
from local_deep_research.web.dependencies import rate_limit
importlib.reload(rate_limit)
return rate_limit
@contextmanager
def _rate_limit_module_restored():
"""Snapshot the module namespace and put the ORIGINAL objects back.
``importlib.reload`` rebinds ``limiter`` to a brand-new ``Limiter``
with EMPTY ``_Limiter__marked_for_limiting`` / ``_route_limits``
registries, while every router keeps the object it was decorated
against at first import. Reloading again in teardown (what this file
used to do) only produces yet another unmarked instance, so the
leaked state outlived the file and broke later registry-inspecting
tests in the same worker — see the full explanation and the identity
fence in the sibling ``test_rate_limit_storage.py``.
"""
from local_deep_research.web.dependencies import rate_limit
snapshot = dict(rate_limit.__dict__)
try:
yield rate_limit
finally:
# Wholesale replace, not update: the ConfigurationError path below
# can abort a reload part-way, leaving a half-rewritten namespace.
rate_limit.__dict__.clear()
rate_limit.__dict__.update(snapshot)
# Defined FIRST so it is set up before (and torn down after) the restore
# fixture below, making it a real check that the restore happened rather
# than a tautology (same idiom as the limiter state guard in
# ``tests/web/routes/test_research_log_export_rate_limit.py``).
@pytest.fixture(autouse=True)
def _rate_limit_state_guard():
from local_deep_research.web.dependencies import rate_limit
original = dict(rate_limit.__dict__)
yield
assert rate_limit.__dict__ == original, (
"rate_limit module globals leaked out of this test — a reload was "
"not restored; later tests would see a fresh, unmarked Limiter."
)
@pytest.fixture(autouse=True)
def _clear_rate_limit_env(monkeypatch):
"""Isolate every test in this file from ambient rate-limit env vars
(CI sets LDR_DISABLE_RATE_LIMITING container-wide; a dev shell might
have RATE_LIMIT_STORAGE_URI exported) so each test controls its own
inputs explicitly."""
for name in (
"LDR_DISABLE_RATE_LIMITING",
"DISABLE_RATE_LIMITING",
"RATE_LIMIT_STORAGE_URI",
):
monkeypatch.delenv(name, raising=False)
@pytest.fixture(autouse=True)
def _restore_rate_limit_module_after_test():
"""Restore the module's ORIGINAL globals post-test, so a module left
mid-reload by a raised exception (see the ConfigurationError test) —
or simply re-executed by a passing one — doesn't poison later
tests/files that import
``local_deep_research.web.dependencies.rate_limit``.
A post-test reload (what this used to do) is NOT a restore: it swaps
in a third, equally unmarked ``Limiter`` while the routers stay bound
to the first one.
"""
with _rate_limit_module_restored():
yield
# ---------------------------------------------------------------------------
# The feature is gone, not just relocated.
# ---------------------------------------------------------------------------
def test_security_rate_limiter_module_no_longer_exists():
"""main's home for this feature (``security/rate_limiter.py``,
Flask-Limiter based) must not exist on this branch — confirms the
validation logic wasn't simply moved under a different name/path."""
with pytest.raises(ModuleNotFoundError):
importlib.import_module("local_deep_research.security.rate_limiter")
def test_rate_limit_module_has_no_validate_storage_helper():
"""The FastAPI replacement module exposes no equivalent of
``validate_rate_limit_storage`` / ``_validated_storage_uri`` under
any of the obvious names. If one of these starts existing, this
assertion should be replaced with a positive test of that function's
behaviour (raises on bad URI, no-op on a good/absent one) rather than
just relaxed."""
mod = _reload_rate_limit()
for candidate in (
"validate_rate_limit_storage",
"_validated_storage_uri",
"validate_storage",
):
assert not hasattr(mod, candidate), (
f"found {candidate!r} on rate_limit module — the startup "
"validation gap this test pins may have been closed; replace "
"this test with a real behavioural one instead of deleting it."
)
# ---------------------------------------------------------------------------
# Current configuration, pinned so the "storage backend, limits" this gap
# is about are known quantities (per task: assert what we think they are).
# ---------------------------------------------------------------------------
def test_current_storage_backend_is_unvalidated_inmemory_by_default():
"""With RATE_LIMIT_STORAGE_URI unset (the common case — most
deployments are single-worker), the effective backend is slowapi's
in-memory storage, threaded through with no validation call of any
kind (there's nothing to validate: no URI was given)."""
mod = _reload_rate_limit()
assert mod._RATE_LIMIT_STORAGE_URI == ""
assert "storage_uri" not in mod._limiter_kwargs
storage = getattr(mod.limiter, "_storage", None)
assert storage is not None
assert "memory" in type(storage).__module__.lower()
def test_current_default_rate_limit_values():
"""Pins the limit VALUES this gap concerns itself with (the missing
validation is about the storage *backend*, not the limit numbers, but
the task asks to record both so a future reader knows exactly what
configuration existed at the time this gap was documented)."""
from local_deep_research.web import server_config
mod = _reload_rate_limit()
assert (
mod.DEFAULT_RATE_LIMIT == server_config._DEFAULTS["rate_limit_default"]
)
assert mod.LOGIN_RATE_LIMIT == server_config._DEFAULTS["rate_limit_login"]
assert (
mod.REGISTRATION_RATE_LIMIT
== server_config._DEFAULTS["rate_limit_registration"]
)
assert (
mod.SETTINGS_RATE_LIMIT
== server_config._DEFAULTS["rate_limit_settings"]
)
assert (
mod.UPLOAD_RATE_LIMIT_USER
== server_config._DEFAULTS["rate_limit_upload_user"]
)
assert (
mod.UPLOAD_RATE_LIMIT_IP
== server_config._DEFAULTS["rate_limit_upload_ip"]
)
# Not env/config-driven at all — a plain module constant.
assert mod.API_RATE_LIMIT_DEFAULT == 60
# ---------------------------------------------------------------------------
# The friendliness gap, demonstrated without touching the live singleton
# module (a throwaway Limiter is enough — it goes through the exact same
# slowapi/limits code path rate_limit.py's module-level
# `Limiter(**_limiter_kwargs)` does).
# ---------------------------------------------------------------------------
def test_malformed_storage_uri_raises_bare_configurationerror():
"""TODAY, an unusable storage URI still crashes — but as a bare
``limits.errors.ConfigurationError`` from deep inside slowapi/limits,
naming neither ``RATE_LIMIT_STORAGE_URI`` nor any remedy. This is
exactly the failure mode main's ``_validated_storage_uri()`` docstring
describes wrapping into an actionable ``RuntimeError``. Once a
replacement validation helper exists, this test should be replaced
with one asserting ITS friendly message instead.
"""
with pytest.raises(ConfigurationError) as exc_info:
Limiter(
key_func=lambda request: "test",
storage_uri="totally-bogus-scheme://unusable",
enabled=True,
)
message = str(exc_info.value)
# The actual gap: nothing here points the operator at the setting
# they need to fix, unlike main's wrapped RuntimeError.
assert "RATE_LIMIT_STORAGE_URI" not in message
assert "pip install" not in message.lower()