~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
"""Security tripwire battery: TOML parse -> scan -> raw-write roundtrip
smuggling against /api/save_raw_config.
Route semantics (src/local_deep_research/web/routers/research.py, around
line 1757 ``save_raw_config``): the raw TOML text is parsed with
``tomllib.loads``, the parsed tree is walked by ``find_blocked_keys``
rejecting any key whose lowercased name contains
``module_path``/``class_name``/``module``/``class`` (BLOCKED_KEY_PATTERNS),
and then the RAW string is written verbatim to
``<config_dir>/config.toml`` via ``write_file_verified``
(security/file_write_verifier.py).
Loader semantics: config.toml has NO dedicated reader in src/ — the only
occurrence of the filename is the write itself. Every TOML consumer in the
app parses with tomllib (the route itself, ``web/themes/loader.py``), and
the real dynamic-import sink for module_path/class_name reads from the
settings DB, never this file
(``web_search_engines/search_engines_config.py``,
``web/routers/settings.py``), gated by the ``get_safe_module_class``
whitelist (``security/module_whitelist.py``). The route's blocked-key
guard is therefore defense-in-depth, and the invariant this battery pins
is:
any payload the route ACCEPTS must, when the written bytes are re-parsed
with tomllib (the loader interpretation the app actually performs),
contain no key whose lowercased name matches a blocked pattern.
Because the scanner and any re-parse share one parser, a smuggle requires a
scanner-vs-disk divergence. The payloads below cover every fold/encoding
trick that could make the parsed view look clean while the raw bytes carry
a dangerous key: dotted and quoted keys, ``\\u`` escapes in quoted keys,
whitespace/case variants, bare and nested table headers (including arrays
of tables), inline tables (bare and inside arrays), TOML-looking decoy text
inside multi-line string values, homoglyph key names, a UTF-8 BOM prefix,
CRLF line endings, and duplicate keys. A payload that lands verbatim AND
re-parses to a blocked key == RED tripwire (smuggle worked).
The regular settings API is out of scope: it persists to the settings DB and
never writes config.toml (verified: no other reference to config.toml
anywhere in src/).
Harness: the real FastAPI route through ``TestClient(app)`` — an
authenticated, CSRF-carrying request, with ``system.allow_config_write``
patched open — mirroring
``tests/security/test_save_raw_config_blocked_keys_fastapi.py``. The
Flask ``app.test_request_context()`` + unwrapped-handler harness this file
used before the FastAPI migration (#3299) deleted
``web.routes.research_routes`` no longer applies. Offline, tmp_path-only,
no LLM/network.
"""
import itertools
import sys
import tomllib
import uuid
from pathlib import Path
import pytest
from fastapi.testclient import TestClient
# Mirror of BLOCKED_KEY_PATTERNS in the route
# (src/local_deep_research/web/routers/research.py, ``save_raw_config``).
# If the route's list ever changes, this mirror — and the whole battery —
# must change with it.
BLOCKED_KEY_PATTERNS = ["module_path", "class_name", "module", "class"]
TEST_PASSWORD = "TomlSmugglePass123" # noqa: S105
# Monotonic, never random: many clients register in one session and random
# /8 addresses can collide, producing a 429 from a bucket some earlier test
# filled (see the sibling FastAPI file for the same reasoning).
_FORWARDED_IP_SEQ = itertools.count(1)
def _blocked_keys_in_parsed(obj, path=""):
"""Walk a parsed TOML tree exactly like the route's find_blocked_keys:
substring match on lowercased key names, recursing into nested dicts
and lists. Applied to the RE-PARSED file bytes, this is the
loader-side view of "did a dangerous key survive".
"""
blocked = []
if isinstance(obj, dict):
for key, value in obj.items():
current_path = f"{path}.{key}" if path else key
key_lower = key.lower()
for pattern in BLOCKED_KEY_PATTERNS:
if pattern in key_lower:
blocked.append(current_path)
break
blocked.extend(_blocked_keys_in_parsed(value, current_path))
elif isinstance(obj, list):
for i, item in enumerate(obj):
blocked.extend(_blocked_keys_in_parsed(item, f"{path}[{i}]"))
return blocked
# ---------------------------------------------------------------------------
# Harness
# ---------------------------------------------------------------------------
@pytest.fixture(autouse=True)
def _slowapi_off():
"""Take the per-IP HTTP rate limiter out of the picture.
Every payload in this battery registers a fresh user to get a CSRF'd,
authenticated client, and ``/auth/register`` is rate limited per IP. A
429 leaking in would mask the status code each test asserts.
"""
from local_deep_research.web.dependencies.rate_limit import limiter
original = limiter.enabled
limiter.enabled = False
try:
yield
finally:
limiter.enabled = original
@pytest.fixture
def allow_config_write(monkeypatch):
"""Open the ``system.allow_config_write`` gate for this test.
Without this, ``write_file_verified`` default-denies (the setting is
undefined anywhere in this repo) and every payload would 500 before
the scanner is even relevant — see
``test_save_raw_config_blocked_keys_fastapi.py::test_pinned_write_gate_default_denies_with_a_500``.
"""
import local_deep_research.config.search_config as search_config
real = search_config.get_setting_from_snapshot
def _resolve(key, default=None, *args, **kwargs):
if key == "system.allow_config_write":
return True
return real(key, default, *args, **kwargs)
monkeypatch.setattr(search_config, "get_setting_from_snapshot", _resolve)
@pytest.fixture
def saved_config_path(allow_config_write, temp_data_dir) -> Path:
"""Where a successful save lands (``LDR_DATA_DIR/config/config.toml``).
``get_config_directory()`` resolves against ``LDR_DATA_DIR``, which the
``app`` fixture points at ``temp_data_dir`` before ``fastapi_app`` is
imported.
"""
return temp_data_dir / "config" / "config.toml"
def _client(app) -> TestClient:
client = TestClient(app, raise_server_exceptions=False)
n = next(_FORWARDED_IP_SEQ)
client.headers.update(
{"X-Forwarded-For": f"10.{(n // 250) % 250 + 1}.{n % 250 + 1}.9"}
)
return client
def _csrf(client: TestClient) -> str:
"""Stamp the session with a CSRF token, then fetch it as a string."""
client.get("/auth/login")
resp = client.get("/auth/csrf-token")
return resp.json().get("csrf_token", "") if resp.status_code == 200 else ""
def _authenticated_client(app) -> TestClient:
"""Register a fresh user and return their logged-in client."""
client = _client(app)
username = f"tomlsmg_{uuid.uuid4().hex[:10]}"
resp = client.post(
"/auth/register",
data={
"username": username,
"password": TEST_PASSWORD,
"confirm_password": TEST_PASSWORD,
"acknowledge": "true",
"csrf_token": _csrf(client),
},
follow_redirects=False,
)
assert resp.status_code == 302, (
"harness broken — could not register a user to drive the endpoint "
f"with: {resp.status_code} {resp.text[:300]}"
)
return client
def _call_save_raw_config(app, raw_config):
"""POST the raw TOML through the real FastAPI route with a live CSRF
token, mirroring ``tests/security/test_save_raw_config_blocked_keys_fastapi.py``.
"""
client = _authenticated_client(app)
resp = client.post(
"/api/save_raw_config",
json={"raw_config": raw_config},
headers={"X-CSRFToken": _csrf(client)},
)
return resp.json(), resp.status_code
# Each payload is a smuggle attempt. Dispositions verified against this
# venv's tomllib:
# - fold/table/inline/case/escape variants -> the scanner sees the blocked
# key in the PARSED tree -> 403, nothing written;
# - multiline-decoy / quoted-homoglyph / CRLF-clean -> accepted (200); the
# written bytes must re-parse to a tree with NO blocked key;
# - bare Cyrillic homoglyph (invalid bare key), BOM prefix, and duplicate
# keys -> tomllib itself rejects -> 400.
SMUGGLE_PAYLOADS = [
# --- tomllib folds these key syntaxes to a blocked name: scanner must 403
pytest.param('search.module_path = "evil.module"', id="dotted_key"),
pytest.param('"\\u006dodule_path" = "evil"', id="quoted_unicode_escape"),
pytest.param(
'custom."\\u006dodule" = "evil"',
id="dotted_quoted_unicode_escape",
),
pytest.param('"module_path " = "evil"', id="quoted_trailing_space"),
pytest.param('ClAsS_nAmE = "Evil"', id="bare_mixed_case"),
# --- blocked names via table syntax (bare header, nested header, array
# of tables — plain and with a dotted module header)
pytest.param('[module]\nname = "evil"', id="bare_table_header"),
pytest.param('[custom.module]\nname = "evil"', id="nested_table_header"),
pytest.param(
'[[engines]]\nname = "safe"\nclass_name = "Evil"',
id="array_of_tables",
),
pytest.param(
'[[custom.module]]\nname = "evil"',
id="array_of_tables_module_header",
),
pytest.param('engine = { module_path = "evil" }', id="inline_table"),
pytest.param(
'engines = [{ class_name = "Evil" }]', id="inline_table_in_array"
),
# --- designed to be ACCEPTED: dangerous text may only ever live in a
# string VALUE or a non-ASCII key, never as a real key on disk
pytest.param(
"[search]\n"
'tool = "google"\n'
'note = """\n'
"[custom]\n"
'module_path = "evil.module"\n'
'class_name = "Evil"\n'
'"""\n',
id="multiline_string_decoy_value",
),
# Greek omicron (U+03BF) in a QUOTED key: tomllib performs no NFKC
# normalization, so the on-disk key is NOT the ASCII "module_path" the
# dynamic-import sink looks for. If the app ever adds a
# unicode-normalizing config loader, this case is the divergence surface.
pytest.param(
'[custom]\n"mοdule_path" = "evil"',
id="homoglyph_quoted_omicron",
),
# Cyrillic o (U+043E) in a BARE key: invalid TOML bare-key charset ->
# parser rejection before the scanner ever runs.
pytest.param(
'mоdule_path = "evil.module"',
id="homoglyph_bare_cyrillic",
),
# --- encoding / parser-divergence edges
pytest.param(
'\ufeff[custom]\nmodule_path = "evil"',
id="utf8_bom_prefix",
),
pytest.param(
'[search]\r\ntool = "searxng"\r\niterations = 3\r\n',
id="crlf_clean_accepted",
),
pytest.param(
'[custom]\r\nmodule_path = "evil"\r\n',
id="crlf_blocked",
),
pytest.param(
'[custom]\nmodule_path = "a"\nmodule_path = "b"',
id="duplicate_blocked_key",
),
]
def _assert_rejected_or_semantically_safe(app, raw_config, config_file):
"""Universal invariant per payload:
- accepted (2xx) -> config.toml exists, holds the payload verbatim, and
re-parses (tomllib — the app's loader interpretation) to a tree with
NO blocked key. Any violation == smuggle worked == RED;
- rejected (400 invalid TOML / 403 blocked key) -> clean pass, provided
nothing landed on disk.
"""
data, status = _call_save_raw_config(app, raw_config)
if 200 <= status < 300:
assert data["success"] is True
assert config_file.is_file()
written = config_file.read_bytes()
# The route's threat model is the verbatim raw write; pin that the
# scanned text is exactly what landed (POSIX text-mode writes are
# byte-exact; Windows translates newlines).
if sys.platform != "win32":
assert written == raw_config.encode("utf-8")
# The loader interpretation: any consumer of config.toml in this app
# parses it with tomllib. A parse failure here would mean the bytes
# on disk diverged from what the route scanned.
parsed = tomllib.loads(written.decode("utf-8"))
assert _blocked_keys_in_parsed(parsed) == []
else:
assert status in (400, 403)
assert data["success"] is False
assert not config_file.exists()
@pytest.mark.parametrize("raw_config", SMUGGLE_PAYLOADS)
def test_raw_config_smuggling_never_lands_dangerous_key(
app, raw_config, saved_config_path
):
_assert_rejected_or_semantically_safe(app, raw_config, saved_config_path)
def test_positive_control_clean_config_accepted(app, saved_config_path):
"""A clean config must be accepted and retain its text on every platform."""
raw = '[search]\ntool = "google"\niterations = 5\n'
_assert_rejected_or_semantically_safe(app, raw, saved_config_path)
assert saved_config_path.read_text(encoding="utf-8") == raw
def test_positive_control_blocked_config_rejected(app, saved_config_path):
"""An obviously blocked config must be refused BEFORE any write."""
data, status = _call_save_raw_config(
app, '[custom]\nmodule_path = "evil.module"\n'
)
assert status == 403
assert data["success"] is False
assert "custom.module_path" in data["blocked_keys"]
assert not saved_config_path.exists()