~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
"""CWE-209 regression coverage for the RAG embedding-test error boundary.
Exception messages may contain response bodies, credentials, filesystem
paths, or SQL. The public formatter therefore returns fixed category messages
for internal, provider (connect-failure or answered-with-an-error), and
unexpected failures. Every test below pairs its leak checks with a positive
assertion naming the category it expects, so a formatter that started
returning an empty or unrelated string could not pass by having "no leak" in
it.
"""
import pytest
from local_deep_research.web.routers.rag import _format_test_embedding_error
# Server-side details of exactly the shapes the catch-all used to echo.
# The API key literal is split so the file does not itself contain a
# credential-shaped blob, and is long enough to trip the
# ``sk-[A-Za-z0-9-]{20,}`` pattern in security/log_sanitizer.py.
_FAKE_API_KEY = "sk-proj-" + "A1b2C3d4E5f6G7h8" * 3
_SERVER_PATH = "/srv/ldr/data/encrypted_databases/victim_user.db"
_SQL_FRAGMENT = "SELECT settings.value FROM settings WHERE settings.key = ?"
def _upstream_exception(message):
"""A genuine ``openai``-module exception (``_UPSTREAM_MODULE_PREFIXES``)."""
import httpx
import openai
return openai.AuthenticationError(
message,
response=httpx.Response(
401, request=httpx.Request("GET", "http://provider.invalid/v1")
),
body=None,
)
def _sqlalchemy_exception(message):
"""A ``sqlalchemy.exc.*`` exception rendering SQL text and a DB path.
``str(OperationalError)`` appends ``[SQL: ...]``, ``[parameters: ...]``
and the versioned ``sqlalche.me/e/NN/...`` link to the driver message.
"""
from sqlalchemy.exc import OperationalError
return OperationalError(
_SQL_FRAGMENT, {"key": "llm.provider"}, Exception(message)
)
class TestUnrecognisedModulesDoNotReflectExceptionText:
"""The leak path. ``type(exc).__module__`` outside the allowlist must
yield a fixed message without exception text or type metadata.
"""
def test_oserror_server_path_is_not_reflected(self):
message = _format_test_embedding_error(
FileNotFoundError(
f"[Errno 2] No such file or directory: {_SERVER_PATH}"
),
"nomic-embed-text",
)
# Positive control: the caller still gets a useful category message.
assert "Embedding test failed for model 'nomic-embed-text'" in message
assert "unexpected error" in message
# The disclosure itself.
assert _SERVER_PATH not in message
assert "encrypted_databases" not in message
assert "victim_user" not in message
assert "No such file or directory" not in message
assert "Errno" not in message
# Not miscategorised as an LDR bug -- the #4208 regression.
assert "bug in LDR" not in message
assert "report it on GitHub" not in message
def test_sqlalchemy_sql_and_parameters_are_not_reflected(self):
exc = _sqlalchemy_exception(
f"unable to open database file {_SERVER_PATH}"
)
rendered = str(exc)
# Non-vacuity: prove the exception really does carry the SQL and the
# path, so the absence assertions below are meaningful.
assert _SQL_FRAGMENT in rendered
assert _SERVER_PATH in rendered
message = _format_test_embedding_error(exc, "m")
assert "Embedding test failed for model 'm'" in message
assert "unexpected error" in message
assert _SQL_FRAGMENT not in message
assert "SELECT" not in message
assert "[SQL:" not in message
assert "parameters:" not in message
assert "sqlalche.me" not in message
assert _SERVER_PATH not in message
def test_stdlib_runtimeerror_text_is_not_reflected(self):
message = _format_test_embedding_error(
RuntimeError("Connection refused by embedding backend"), "m"
)
assert "Embedding test failed for model 'm'" in message
assert "unexpected error" in message
assert "Connection refused" not in message
assert "embedding backend" not in message
def test_unrecognised_branch_message_is_constant_across_payloads(self):
"""The strongest form of the check: two exceptions of the same class
carrying wildly different text must produce byte-identical output, so
nothing attacker- or server-controlled from ``str(exc)`` survives.
"""
first = _format_test_embedding_error(
OSError(f"permission denied: {_SERVER_PATH}"), "m"
)
second = _format_test_embedding_error(
RuntimeError(f"totally different text {_SQL_FRAGMENT}"), "m"
)
assert first == second
assert "unexpected error" in first
assert "OSError" not in first
assert "RuntimeError" not in first
def test_empty_exception_message_still_yields_a_useful_message(self):
message = _format_test_embedding_error(ValueError(""), "m")
assert "Embedding test failed for model 'm'" in message
assert "unexpected error" in message
assert "ValueError" not in message
class TestRecognisedProvidersWithholdTheirDetail:
"""Provider failures retain actionable guidance, not exception detail."""
def test_openai_provider_detail_is_withheld(self):
message = _format_test_embedding_error(
_upstream_exception(f"Incorrect API key provided: {_FAKE_API_KEY}"),
"text-embedding-3-small",
)
assert "The request to the provider failed" in message
assert "text-embedding-3-small" in message
assert "Incorrect API key provided" not in message
assert _FAKE_API_KEY not in message
assert "[REDACTED_KEY]" not in message
def test_httpx_connect_error_says_connect_not_rejected(self):
"""A connect failure means nothing was rejected. Sending the user to
check credentials for an unreachable host is the misleading-guidance
class #4208 removed, so this branch has its own fixed message.
"""
import httpx
message = _format_test_embedding_error(
httpx.ConnectError("All connection attempts failed"), "m"
)
assert "Could not connect to the provider" in message
assert "reachable" in message
assert "rejected" not in message
assert "credentials" not in message
assert "All connection attempts failed" not in message
def test_provider_message_is_constant_across_payloads(self):
first = _format_test_embedding_error(
_upstream_exception("x" * 5000), "m"
)
second = _format_test_embedding_error(
_upstream_exception("different private detail"), "m"
)
assert first == second
# Category assertion, not just "the two are equal": an
# AuthenticationError is an answered request, so it must select the
# provider branch rather than the connect-failure or default-deny
# wording.
assert "The request to the provider failed" in first
assert "unexpected error" not in first
assert len(first) < 300
class TestInternalExceptionsAreStillSuppressed:
"""Unchanged branch, re-pinned here so a future refactor of the
categorisation cannot silently drop it.
"""
def test_internal_ldr_exception_detail_is_withheld(self):
from local_deep_research.config.thread_settings import (
NoSettingsContextError,
)
message = _format_test_embedding_error(
NoSettingsContextError(f"could not open {_SERVER_PATH}"), "m"
)
assert "internal LDR error" in message
assert "NoSettingsContextError" not in message
assert "report it on GitHub" in message
assert _SERVER_PATH not in message
assert "could not open" not in message
@pytest.mark.parametrize(
"exc",
[
FileNotFoundError(f"open {_SERVER_PATH}"),
RuntimeError(f"open {_SERVER_PATH}"),
KeyError(_SERVER_PATH),
TypeError(f"'NoneType' at {_SERVER_PATH}"),
],
)
def test_no_unrecognised_exception_family_reflects_a_server_path(exc):
"""Sweep across the families CodeQL's dataflow reaches, so hardening one
class of exception while leaving a sibling open is caught.
"""
message = _format_test_embedding_error(exc, "m")
# Positive control, category included: each of these classes is outside
# both allowlists, so the default-deny branch is the one that must run.
assert "Embedding test failed for model 'm'" in message
assert "unexpected error" in message
assert "bug in LDR" not in message
assert _SERVER_PATH not in message
assert type(exc).__name__ not in message