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.

40.8 KB
Raw Blame
"""``POST /api/save_raw_config`` — the blocked-key guard, restored.

WHY THIS FILE EXISTS
--------------------
``/api/save_raw_config`` takes a user-supplied TOML blob and writes it to
``<data_dir>/config/config.toml``. Before writing, the handler parses the
blob and refuses it if any key matches ``_BLOCKED_KEY_PATTERNS``
(``src/local_deep_research/web/routers/research.py``)::

    _BLOCKED_KEY_PATTERNS = ("module_path", "class_name", "module", "class")

Those key names are how this codebase points config at a Python module or
class to import, so letting a user write them into the config file is a
code-execution vector. The guard is the only thing standing between the
endpoint and that.

``origin/main`` pinned it from three files
(``tests/web/routes/test_research_routes_config.py``,
``..._coverage.py``, ``..._extra_coverage.py``). The Flask -> FastAPI
migration deleted all three and wrote no replacement: before this file,
``grep -rn save_raw_config tests/`` matched nothing at all. This restores
the coverage against the FastAPI route and extends it, because the
original suite only ever tried three of the four patterns and only two
nesting shapes. Section 8 also covers the parsing limits the handler
applies before tomllib runs (``security/toml_limits.py``), and section 9
the bounds on the walk that follows the parse.

HOW THE SCANNER ACTUALLY WORKS (read off the handler, not assumed)
------------------------------------------------------------------
``_find_blocked_keys`` walks the parsed TOML at every depth — into nested
dicts AND into lists — and for every dict key tests
``pattern in key.lower()``. So the match is:

* recursive, not top-level-only (nested tables, arrays of tables, inline
  tables inside arrays, arrays of arrays are all reached);
* case-insensitive (``.lower()``); and
* a SUBSTRING test, not equality — ``module-path``, ``modulepath`` and
  ``subclass`` all match.

The tests below are built from that reading, and the expected
``blocked_keys`` path strings are asserted exactly so a future refactor
that stops walking one of those shapes fails loudly instead of silently
narrowing the guard.

TWO SEPARATE GATES — WHY ``allow_config_write`` IS PATCHED
-----------------------------------------------------------
Reaching the write at all requires ``system.allow_config_write`` to be
``True``. That setting is not defined anywhere in this repo, so
``write_file_verified`` default-denies and a *perfectly valid* config
comes back 500 (pinned below in
``test_pinned_write_gate_default_denies_with_a_500``).

That matters for this file's integrity: if the tests ran without the
patch, EVERY submission would fail and every "the config was not saved"
assertion would pass vacuously — including for a guard that had been
deleted. So the ``allow_config_write`` fixture opens the write gate, and
the rejection tests then prove the *key guard* is what stopped the write.
The positive controls run first in the file and prove the endpoint really
does write when the guard has no reason to fire.

HARNESS
-------
* ``TestClient(app, raise_server_exceptions=False)`` over the ``app``
  fixture from ``tests/conftest.py`` (function-scoped; points
  ``LDR_DATA_DIR`` at a throwaway dir before ``fastapi_app`` is
  imported), matching ``tests/security/test_auth_routes_fastapi.py``.
* CSRF is ASGI middleware with no off switch, and this endpoint is not
  exempt. The body is JSON, and the middleware only reads a
  ``csrf_token`` FORM field from urlencoded bodies — so a JSON POST must
  carry the token in the ``X-CSRFToken`` header.
* Each client gets its own ``X-Forwarded-For`` from a MONOTONIC counter.
  Random addresses collide across a session with many clients and
  produce a 429 that has nothing to do with the guard under test.
"""

import asyncio
import itertools
import json
import sys
import threading
import uuid
from pathlib import Path
from unittest.mock import AsyncMock, MagicMock

import pytest
from fastapi.testclient import TestClient

TEST_PASSWORD = "RawConfigPass123"  # noqa: S105

# Monotonic, never random: with this many clients in one session, random
# /8 addresses collide and the colliding client gets a 429 from a bucket
# some earlier test filled.
_FORWARDED_IP_SEQ = itertools.count(1)

CLEAN_CONFIG = '[search]\ntool = "searxng"\niterations = 5\n'

# A clean config that exercises EVERY container shape the evasion cases
# below use — nested tables, arrays of tables, inline tables in arrays,
# arrays of arrays. Its only difference from those cases is the key
# names. Used as the structural control.
CLEAN_ALL_SHAPES_CONFIG = (
    '[search]\ntool = "searxng"\n\n'
    "[search.advanced.rerank]\nenabled = true\n\n"
    '[[search.engines]]\nname = "a"\n\n'
    '[[search.engines]]\nname = "b"\n\n'
    "[extras]\n"
    'items = [{ name = "x" }, { name = "y" }]\n'
    'matrix = [[{ name = "z" }]]\n'
)


# ---------------------------------------------------------------------------
# Harness
# ---------------------------------------------------------------------------


@pytest.fixture(autouse=True)
def _slowapi_off():
    """Take the per-IP HTTP rate limiter out of the picture.

    Nothing here is about rate limiting (that is
    ``tests/security/test_rate_limiter_fastapi.py``'s job), and each test
    registers a user — ``/auth/register`` is rate limited per IP.
    A 429 leaking in would mask the status code each test asserts. The
    distinct ``X-Forwarded-For`` per client below is kept anyway so this
    file does not silently depend on the flag.
    """
    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.

    ``write_file_verified`` resolves that setting through
    ``config.search_config.get_setting_from_snapshot`` (a function-level
    import, so patching the module attribute is seen at call time). The
    setting does not exist in this repo, so the real resolver raises and
    the verifier default-denies — see
    ``test_pinned_write_gate_default_denies_with_a_500``.

    Only that one key is answered; every other key falls through to the
    real resolver so nothing else in the request is disturbed.
    """
    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 config_file(temp_data_dir) -> Path:
    """Where a successful save lands (``LDR_DATA_DIR/config/config.toml``).

    ``get_config_directory()`` is only called on the write path, so after
    a rejection neither this file nor its parent directory exists.
    """
    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}.7"}
    )
    return client


def _csrf(client: TestClient) -> str:
    """Stamp the session with a CSRF token and hand it back."""
    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"rawcfg_{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 _save(client: TestClient, raw_config):
    """POST the raw TOML with a live CSRF token in the header."""
    return client.post(
        "/api/save_raw_config",
        json={"raw_config": raw_config},
        headers={"X-CSRFToken": _csrf(client)},
    )


def _assert_not_saved(config_file: Path, why: str, before: str | None = None):
    """Assert the config file was not created or not modified."""
    if before is None:
        assert not config_file.exists(), (
            f"{why} — but config.toml was written anyway: "
            f"{config_file.read_text()[:300]!r}"
        )
    else:
        assert config_file.read_text() == before, (
            f"{why} — but the existing config.toml was modified: "
            f"{config_file.read_text()[:300]!r}"
        )


# ---------------------------------------------------------------------------
# 0. Positive controls.
#
# These run FIRST, deliberately. Every rejection assertion below is of the
# form "4xx and nothing was written", which a handler that 500s on the
# path, 404s, or is unreachable behind CSRF/rate limiting satisfies for
# free. These two tests prove the endpoint is reachable, authenticates,
# passes CSRF, and genuinely writes the file — so the rejections that
# follow mean something.
# ---------------------------------------------------------------------------


def test_positive_control_valid_config_is_accepted_and_written(
    app, allow_config_write, config_file
):
    """A legitimate config with no blocked keys saves end to end.

    Asserts on the file on disk, not just the 200: a handler that
    returned success without writing would make every "nothing was
    persisted" assertion below meaningless.
    """
    client = _authenticated_client(app)

    resp = _save(client, CLEAN_CONFIG)

    assert resp.status_code == 200, (
        "a config with no blocked keys must be accepted — if this fails, "
        "every rejection test in this file is vacuous: "
        f"{resp.status_code} {resp.text[:300]}"
    )
    assert resp.json() == {"success": True}
    assert config_file.exists(), (
        "the endpoint reported success but wrote no config file"
    )
    assert config_file.read_text() == CLEAN_CONFIG


def test_positive_control_every_container_shape_is_accepted_when_clean(
    app, allow_config_write, config_file
):
    """The structural control for the evasion sweep.

    The evasion cases nest blocked keys inside nested tables, arrays of
    tables, inline tables inside arrays and arrays of arrays. If the
    handler simply choked on those shapes, all of them would 403 (or
    500) for reasons unrelated to the guard. This submits the same
    shapes with innocuous key names and requires a clean save, so a 403
    over there is attributable to the KEY and not to the structure.
    """
    client = _authenticated_client(app)

    resp = _save(client, CLEAN_ALL_SHAPES_CONFIG)

    assert resp.status_code == 200, (
        "nested tables / arrays of tables / inline tables are legal TOML "
        "and must save when no key is blocked: "
        f"{resp.status_code} {resp.text[:300]}"
    )
    assert config_file.read_text() == CLEAN_ALL_SHAPES_CONFIG


# ---------------------------------------------------------------------------
# 1. Every blocked pattern is refused at the route.
# ---------------------------------------------------------------------------


_EACH_PATTERN = [
    pytest.param(
        '[custom]\nmodule_path = "evil.mod"\n',
        ["custom.module_path"],
        id="module_path",
    ),
    pytest.param(
        '[providers]\nclass_name = "EvilClass"\n',
        ["providers.class_name"],
        id="class_name",
    ),
    pytest.param(
        '[custom]\nmodule = "evil.mod"\n',
        ["custom.module"],
        id="module",
    ),
    pytest.param(
        '[custom]\nclass = "EvilClass"\n',
        ["custom.class"],
        id="class",
    ),
]


@pytest.mark.parametrize("raw_config,expected_paths", _EACH_PATTERN)
def test_each_blocked_pattern_is_rejected(
    app, allow_config_write, config_file, raw_config, expected_paths
):
    """Each entry of ``_BLOCKED_KEY_PATTERNS`` is refused with 403 and
    nothing is written.

    The write gate is OPEN for this test (``allow_config_write``), so the
    only thing that can stop the write is the key guard itself.
    """
    client = _authenticated_client(app)

    resp = _save(client, raw_config)

    assert resp.status_code == 403, (
        f"{expected_paths} names a code-execution key and must be "
        f"refused: {resp.status_code} {resp.text[:300]}"
    )
    body = resp.json()
    assert body["success"] is False
    assert body["blocked_keys"] == expected_paths, (
        "the response must name the offending key so the user can fix it"
    )
    assert "protected keys" in body["error"]
    _assert_not_saved(config_file, "the config named a blocked key")


# ---------------------------------------------------------------------------
# 2. The rejection survives evasion.
#
# Every shape the scanner walks, plus the ways a key name can be spelled.
# Expected ``blocked_keys`` paths are asserted exactly: a refactor that
# stopped descending into (say) arrays of tables would still 403 on the
# other cases, and only the exact-path assertion catches the narrowing.
# ---------------------------------------------------------------------------


_EVASIONS = [
    pytest.param(
        'module_path = "evil.mod"\n',
        ["module_path"],
        id="top-level-bare-key",
    ),
    pytest.param(
        '[a.b.c]\nmodule = "evil"\n',
        ["a.b.c.module"],
        id="deeply-nested-table",
    ),
    pytest.param(
        '[a]\nb.c.module_path = "evil"\n',
        ["a.b.c.module_path"],
        id="dotted-key",
    ),
    pytest.param(
        '[custom]\nMODULE_PATH = "evil"\n',
        ["custom.MODULE_PATH"],
        id="uppercase",
    ),
    pytest.param(
        '[custom]\nClAsS_nAmE = "evil"\n',
        ["custom.ClAsS_nAmE"],
        id="mixed-case",
    ),
    pytest.param(
        '[[plugins]]\nmodule = "evil"\n',
        ["plugins[0].module"],
        id="array-of-tables",
    ),
    pytest.param(
        '[[plugins]]\nname = "ok"\n\n[[plugins]]\nclass_name = "Evil"\n',
        ["plugins[1].class_name"],
        id="array-of-tables-second-element",
    ),
    pytest.param(
        '[[a]]\nname = "ok"\n[a.b]\nmodule = "evil"\n',
        ["a[0].b.module"],
        id="table-under-array-of-tables",
    ),
    pytest.param(
        'plugins = [{ name = "ok" }, { module = "evil" }]\n',
        ["plugins[1].module"],
        id="inline-table-inside-array",
    ),
    pytest.param(
        'x = [[{ class_name = "Evil" }]]\n',
        ["x[0][0].class_name"],
        id="array-of-arrays-of-inline-tables",
    ),
    pytest.param(
        '[custom]\n"module_path" = "evil"\n',
        ["custom.module_path"],
        id="quoted-key",
    ),
    pytest.param(
        "[custom]\n'class_name' = 'evil'\n",
        ["custom.class_name"],
        id="literal-quoted-key",
    ),
    pytest.param(
        '[custom]\n" module " = "evil"\n',
        ["custom. module "],
        id="quoted-key-padded-with-whitespace",
    ),
    pytest.param(
        '[custom]\n   module_path    =    "evil"\n',
        ["custom.module_path"],
        id="whitespace-around-key-and-equals",
    ),
    pytest.param(
        "[module]\nfoo = 1\n",
        ["module"],
        id="table-header-is-the-blocked-key",
    ),
    pytest.param(
        "[[class]]\nfoo = 1\n",
        ["class"],
        id="array-of-tables-header-is-the-blocked-key",
    ),
    pytest.param(
        '[custom]\nmodule-path = "evil"\n',
        ["custom.module-path"],
        id="hyphenated-spelling",
    ),
    pytest.param(
        '[custom]\nmodulepath = "evil"\n',
        ["custom.modulepath"],
        id="run-together-spelling",
    ),
]


@pytest.mark.parametrize("raw_config,expected_paths", _EVASIONS)
def test_blocked_keys_cannot_be_hidden_from_the_scanner(
    app, allow_config_write, config_file, raw_config, expected_paths
):
    """Nesting, casing, quoting and whitespace must not smuggle a
    blocked key past the guard, and nothing is written when one is
    found.

    The clean counterpart of these shapes is
    ``test_positive_control_every_container_shape_is_accepted_when_clean``
    — without it a handler that rejected all nested TOML would pass this
    whole sweep.
    """
    client = _authenticated_client(app)

    resp = _save(client, raw_config)

    assert resp.status_code == 403, (
        f"a blocked key hidden as {expected_paths} got past the guard: "
        f"{resp.status_code} {resp.text[:300]}"
    )
    assert resp.json()["blocked_keys"] == expected_paths, (
        "the scanner found the key but reported the wrong path — the walk "
        "has changed shape"
    )
    _assert_not_saved(config_file, "the config hid a blocked key")


# ---------------------------------------------------------------------------
# 3. A rejection is never partially applied.
# ---------------------------------------------------------------------------


def test_one_blocked_key_rejects_the_whole_config(
    app, allow_config_write, config_file
):
    """A config that is 95% legitimate and carries ONE blocked key deep
    inside an array of tables must save NOTHING.

    The handler writes ``raw_config`` verbatim, so a partial application
    would mean the whole blob (blocked key included) landing on disk.
    """
    client = _authenticated_client(app)
    raw_config = (
        '[search]\ntool = "searxng"\niterations = 5\n\n'
        "[search.advanced]\nrerank = true\n\n"
        '[[search.engines]]\nname = "ok"\n\n'
        '[[search.engines]]\nname = "bad"\nmodule_path = "evil.mod"\n'
    )

    resp = _save(client, raw_config)

    assert resp.status_code == 403, f"{resp.status_code} {resp.text[:300]}"
    assert resp.json()["blocked_keys"] == ["search.engines[1].module_path"]
    _assert_not_saved(
        config_file, "one blocked key must reject the entire submission"
    )


def test_rejection_does_not_overwrite_an_existing_config(
    app, allow_config_write, config_file
):
    """An existing config.toml must survive a rejected submission byte
    for byte.

    The second half of the test overwrites the same file with a clean
    config. That is the anti-vacuity half: it proves the seeded file was
    writable all along, so "unchanged" after the rejection is the guard's
    doing and not a permissions accident.
    """
    client = _authenticated_client(app)
    seeded = '[search]\ntool = "duckduckgo"\n# operator-managed\n'
    config_file.parent.mkdir(parents=True, exist_ok=True)
    config_file.write_text(seeded)

    resp = _save(client, '[custom]\nclass_name = "Evil"\n')

    assert resp.status_code == 403, f"{resp.status_code} {resp.text[:300]}"
    _assert_not_saved(
        config_file,
        "a rejected submission clobbered the existing config",
        before=seeded,
    )

    accepted = _save(client, CLEAN_CONFIG)
    assert accepted.status_code == 200, (
        "the seeded file was not writable, so 'unchanged' above proved "
        f"nothing: {accepted.status_code} {accepted.text[:300]}"
    )
    assert config_file.read_text() == CLEAN_CONFIG


# ---------------------------------------------------------------------------
# 4. Malformed TOML is a clean 4xx, never a 500.
# ---------------------------------------------------------------------------


_MALFORMED = [
    pytest.param("this is [not valid toml", id="unclosed-table-header"),
    pytest.param("invalid = [unclosed\n", id="unclosed-array"),
    pytest.param("just some bare words\n", id="bare-words"),
    pytest.param("[a]\nx = 1\nx = 2\n", id="duplicate-key"),
    pytest.param('[a]\nx = "unterminated\n', id="unterminated-string"),
    pytest.param('[a]\nx = "bad \\q escape"\n', id="invalid-escape"),
    pytest.param("<html><body>hi</body></html>", id="html-not-toml"),
    pytest.param('{"module_path": "evil.mod"}', id="json-not-toml"),
]


@pytest.mark.parametrize("raw_config", _MALFORMED)
def test_malformed_toml_is_a_clean_400(
    app, allow_config_write, config_file, raw_config
):
    """Unparseable input must be a 400 that says "TOML" — not a 500, and
    not a leaked ``tomllib`` traceback (CWE-209).

    The ``json-not-toml`` case doubles as a guard-ordering check: that
    body contains ``module_path``, but it is refused as bad syntax (400)
    rather than as a blocked key (403), because the scanner only ever
    sees successfully parsed input.
    """
    client = _authenticated_client(app)

    resp = _save(client, raw_config)

    assert resp.status_code == 400, (
        f"malformed TOML must be a clean 4xx: {resp.status_code} "
        f"{resp.text[:300]}"
    )
    body = resp.json()
    assert body["success"] is False
    assert "TOML" in body["error"]
    for leak in ("Traceback", "tomllib", "line 1, column", "Expected"):
        assert leak not in resp.text, (
            f"the parse error leaks internal detail ({leak!r}): "
            f"{resp.text[:300]}"
        )
    _assert_not_saved(config_file, "the submission did not parse")


# ---------------------------------------------------------------------------
# 5. Request-body shapes.
# ---------------------------------------------------------------------------


def test_empty_raw_config_is_rejected(app, allow_config_write, config_file):
    client = _authenticated_client(app)

    resp = _save(client, "")

    assert resp.status_code == 400, f"{resp.status_code} {resp.text[:300]}"
    assert resp.json() == {
        "success": False,
        "error": "Raw configuration is required",
    }
    _assert_not_saved(config_file, "raw_config was empty")


def test_missing_raw_config_key_is_rejected(
    app, allow_config_write, config_file
):
    client = _authenticated_client(app)

    resp = client.post(
        "/api/save_raw_config",
        json={},
        headers={"X-CSRFToken": _csrf(client)},
    )

    assert resp.status_code == 400, f"{resp.status_code} {resp.text[:300]}"
    assert resp.json()["success"] is False
    _assert_not_saved(config_file, "raw_config was absent")


def test_non_object_json_body_is_rejected(app, allow_config_write, config_file):
    """A JSON array (not an object) must not reach ``.get()``."""
    client = _authenticated_client(app)

    resp = client.post(
        "/api/save_raw_config",
        json=['[custom]\nmodule_path = "evil"\n'],
        headers={"X-CSRFToken": _csrf(client)},
    )

    assert resp.status_code == 400, f"{resp.status_code} {resp.text[:300]}"
    assert resp.json()["success"] is False
    _assert_not_saved(config_file, "the body was not a JSON object")


def test_non_json_body_is_a_400_not_a_500(app, allow_config_write, config_file):
    """A body that is not JSON at all must not surface as a 500.

    ``save_raw_config`` calls ``await request.json()`` with no try/except
    of its own, so this pins that something upstream turns the decode
    failure into a 400.
    """
    client = _authenticated_client(app)

    resp = client.post(
        "/api/save_raw_config",
        content=b"not json at all",
        headers={
            "X-CSRFToken": _csrf(client),
            "Content-Type": "application/json",
        },
    )

    assert resp.status_code == 400, (
        f"a non-JSON body must be a clean 4xx: {resp.status_code} "
        f"{resp.text[:300]}"
    )
    _assert_not_saved(config_file, "the body was not JSON")


# ---------------------------------------------------------------------------
# 6. The other two gates in front of the guard.
#
# Both of these also return a 4xx with no file written, i.e. they look
# exactly like a blocked-key rejection from the outside. They are pinned
# here so a regression that made the endpoint unreachable — the classic
# way this whole file could go vacuous — fails LOUDLY rather than turning
# every rejection above green for the wrong reason.
# ---------------------------------------------------------------------------


def test_unauthenticated_caller_cannot_write_config(
    app, allow_config_write, config_file
):
    client = _client(app)

    resp = client.post(
        "/api/save_raw_config",
        json={"raw_config": CLEAN_CONFIG},
        headers={"X-CSRFToken": _csrf(client)},
    )

    assert resp.status_code == 401, f"{resp.status_code} {resp.text[:300]}"
    _assert_not_saved(config_file, "the caller was not authenticated")


def test_csrf_403_is_distinguishable_from_a_blocked_key_403(
    app, allow_config_write, config_file
):
    """An authenticated POST with no CSRF token is also a 403 — but a
    different one.

    This is the trap this file has to stay clear of: if CSRF started
    rejecting every request, the blocked-key tests above would all still
    see a 403 and pass while proving nothing. Pinning that the CSRF 403
    carries no ``blocked_keys`` makes the two distinguishable, and the
    positive controls at the top of the file confirm a correctly-formed
    request gets through.
    """
    client = _authenticated_client(app)

    resp = client.post(
        "/api/save_raw_config",
        json={"raw_config": CLEAN_CONFIG},
    )

    assert resp.status_code == 403
    body = resp.json()
    assert "blocked_keys" not in body, (
        "a CSRF rejection is masquerading as a blocked-key rejection"
    )
    assert "CSRF" in body["error"]
    _assert_not_saved(config_file, "the request carried no CSRF token")


# ---------------------------------------------------------------------------
# 7. Current behaviour pinned, NOT fixed.
#
# Each of these documents a real limit of the guard as written. They are
# pinned rather than corrected because changing ``src/`` is out of scope
# for this file, and because a silent change in any of them is exactly
# what a regression would look like.
# ---------------------------------------------------------------------------


def test_pinned_substring_match_also_blocks_innocuous_keys(
    app, allow_config_write, config_file
):
    """LIMITATION (over-blocking): the guard tests ``pattern in
    key.lower()``, so any key that merely CONTAINS "module" or "class"
    is refused even when it names nothing importable.

    ``classification``, ``module_count`` and ``subclass`` are all
    ordinary setting names and all get a 403. This is the cost of the
    substring rule and it is deliberate (a stricter equality test would
    be trivially evaded by ``module-path``, which is covered above), but
    it means the endpoint cannot express those settings at all. Pinned,
    not fixed.
    """
    client = _authenticated_client(app)
    raw_config = (
        '[search]\nclassification = "topic"\nmodule_count = 3\nsubclass = "x"\n'
    )

    resp = _save(client, raw_config)

    assert resp.status_code == 403
    assert resp.json()["blocked_keys"] == [
        "search.classification",
        "search.module_count",
        "search.subclass",
    ]
    _assert_not_saved(config_file, "substring match rejected the config")


def test_pinned_guard_is_a_key_denylist_and_ignores_values(
    app, allow_config_write, config_file
):
    """LIMITATION (under-blocking): only KEY NAMES are inspected. Any
    other name for an import pointer, and any dotted import target
    sitting in a VALUE, is written to disk unchallenged.

    ``entry_point``, ``factory``, ``plugin_path`` and a ``provider``
    whose value is ``"evil.module.EvilClass"`` all save with a 200. The
    guard is therefore only as good as the denylist: it protects the key
    names this codebase happens to use for dynamic imports today, and any
    consumer that grows a differently-named import key inherits no
    protection from it. Pinned as current behaviour — closing it needs a
    schema/allowlist in ``src/``, not a test.
    """
    client = _authenticated_client(app)
    raw_config = (
        "[custom]\n"
        'entry_point = "evil.mod:Evil"\n'
        'factory = "os.system"\n'
        'plugin_path = "/tmp/evil.py"\n'
        'provider = "evil.module.EvilClass"\n'
    )

    resp = _save(client, raw_config)

    assert resp.status_code == 200, (
        "current behaviour changed — if the guard now inspects values or "
        "grew these key names, this pin should be updated, not deleted: "
        f"{resp.status_code} {resp.text[:300]}"
    )
    assert config_file.read_text() == raw_config


def test_pinned_persisted_bytes_are_the_raw_submission(
    app, allow_config_write, config_file
):
    """LIMITATION (validate-one-thing, persist-another): the handler
    validates the ``tomllib`` PARSE of ``raw_config`` but writes the
    ORIGINAL STRING to disk.

    Demonstrated here with a comment naming a blocked key: ``tomllib``
    discards it, so the guard never sees it, and it lands in config.toml
    verbatim. Harmless as a comment — the point is the general shape.
    Anything the validating parser and the eventual consuming parser
    disagree about is an evasion, and re-serialising the accepted parse
    tree instead of echoing the submission would close that class
    entirely. Pinned as current behaviour.
    """
    client = _authenticated_client(app)
    raw_config = '# module_path = "evil.mod"\n[search]\ntool = "searxng"\n'

    resp = _save(client, raw_config)

    assert resp.status_code == 200, f"{resp.status_code} {resp.text[:300]}"
    assert config_file.read_text() == raw_config, (
        "the file on disk is not a byte-for-byte echo of the submission"
    )
    assert 'module_path = "evil.mod"' in config_file.read_text()


def test_pinned_write_gate_default_denies_with_a_500(app, config_file):
    """LIMITATION (operability): with ``system.allow_config_write``
    undefined — which is its state everywhere in this repo — a perfectly
    valid config comes back 500 "Failed to process request".

    ``write_file_verified`` raises ``FileWriteSecurityError``, which the
    handler's blanket ``except Exception`` turns into a 500. So the
    endpoint is closed by default (good) but reports its own policy
    decision as a server fault, with no way for the caller to tell
    "config writes are disabled here" from "the disk is full". Pinned as
    current behaviour; it is also why every other test in this file takes
    the ``allow_config_write`` fixture, since without it no submission
    could ever be saved and every "nothing was persisted" assertion would
    hold vacuously.
    """
    client = _authenticated_client(app)

    resp = _save(client, CLEAN_CONFIG)

    assert resp.status_code == 500, (
        "the write gate no longer default-denies — if config writes are "
        "now allowed out of the box that is a security change, and if "
        "the denial now has its own status code this pin should be "
        f"updated: {resp.status_code} {resp.text[:300]}"
    )
    assert resp.json() == {
        "success": False,
        "error": "Failed to process request",
    }
    _assert_not_saved(config_file, "the write gate was closed")


# ---------------------------------------------------------------------------
# 8. Parsing limits.
#
# The body may be 16 MB, and tomllib's cost for one long dotted key grows
# with the square of its parts, so the handler refuses text over
# MAX_TOML_BYTES, or with a key or table header of more than
# MAX_TOML_KEY_PARTS dotted parts, before tomllib parses it
# (security/toml_limits.py). Each refusal is checked against tomllib's own
# entry point: no tomllib.loads call may receive the submitted text.
# ---------------------------------------------------------------------------


def _record_parses_of(monkeypatch, raw_config: str) -> list[str]:
    """Record every ``tomllib.loads`` call that receives *raw_config*.

    Other parses in the request (theme front matter on the pages the
    harness loads for CSRF) pass through unrecorded.
    """
    import tomllib

    parses: list[str] = []
    real_loads = tomllib.loads
    submitted = raw_config.replace("\r\n", "\n")

    def recording_loads(text, *args, **kwargs):
        if text in (raw_config, submitted):
            parses.append(text)
        return real_loads(text, *args, **kwargs)

    monkeypatch.setattr(tomllib, "loads", recording_loads)
    return parses


def test_overlong_dotted_key_is_refused_before_tomllib_parses_it(
    app, allow_config_write, config_file, monkeypatch
):
    client = _authenticated_client(app)
    raw_config = "[search]\n" + ".".join(["k"] * 33) + " = 1\n"
    parses = _record_parses_of(monkeypatch, raw_config)

    resp = _save(client, raw_config)

    assert resp.status_code == 400, f"{resp.status_code} {resp.text[:300]}"
    body = resp.json()
    assert body["success"] is False
    assert body["error"].startswith("Configuration exceeds the parsing limits")
    assert parses == [], "tomllib parsed a key over the dotted-part limit"
    _assert_not_saved(config_file, "a key had more than 32 dotted parts")


def test_dotted_key_at_the_part_limit_is_accepted_and_written(
    app, allow_config_write, config_file
):
    client = _authenticated_client(app)
    raw_config = "[search]\n" + ".".join(["k"] * 32) + " = 1\n"

    resp = _save(client, raw_config)

    assert resp.status_code == 200, f"{resp.status_code} {resp.text[:300]}"
    assert resp.json() == {"success": True}
    assert config_file.read_text() == raw_config


def test_config_over_the_size_limit_is_refused_before_tomllib_parses_it(
    app, allow_config_write, config_file, monkeypatch
):
    from local_deep_research.security import toml_limits

    monkeypatch.setattr(
        toml_limits, "MAX_TOML_BYTES", len(CLEAN_CONFIG.encode())
    )
    client = _authenticated_client(app)

    resp = _save(client, CLEAN_CONFIG)
    assert resp.status_code == 200, f"{resp.status_code} {resp.text[:300]}"
    assert config_file.read_text() == CLEAN_CONFIG

    over_limit = CLEAN_CONFIG + "#"
    parses = _record_parses_of(monkeypatch, over_limit)
    resp = _save(client, over_limit)

    assert resp.status_code == 400, f"{resp.status_code} {resp.text[:300]}"
    body = resp.json()
    assert body["success"] is False
    assert body["error"].startswith("Configuration exceeds the parsing limits")
    assert parses == [], "tomllib parsed a config over the size limit"
    _assert_not_saved(
        config_file, "the config was one byte over the limit", CLEAN_CONFIG
    )


# ---------------------------------------------------------------------------
# 9. The protected-key walk.
#
# The walk runs on what the parse returned. It is offloaded like the parse,
# and its report is bounded: at most _MAX_REPORTED_BLOCKED_KEYS paths, each
# cut to _MAX_BLOCKED_KEY_PATH_CHARS characters. It used to build the full
# path of every key on the event loop and keep the path of every protected
# one, so each key under one long table name repeated that name. These tests
# lower those limits, so their inputs stay small. The ones that need no app
# call the handler directly, as
# tests/web/routers/test_research_model_log_preview_bound.py does: its 403
# returns before any DB or file access.
# ---------------------------------------------------------------------------

_ELLIPSIS = "\N{HORIZONTAL ELLIPSIS}"


def _call_save_raw_config(raw_config: str):
    """Run the handler on an event loop on this thread, without the app."""
    from local_deep_research.web.routers import research

    request = MagicMock()
    request.json = AsyncMock(return_value={"raw_config": raw_config})
    response = asyncio.run(research.save_raw_config(request, "testuser"))
    return response.status_code, json.loads(response.body)


def test_the_protected_key_walk_runs_off_the_event_loop(monkeypatch):
    """The parse is replaced by one returning tables that record the thread
    each walk of them runs on. ``asyncio.run`` runs the event loop on this
    thread, so no table may be walked here."""
    from local_deep_research.security import toml_limits

    walked_on: list[int] = []

    class RecordingTable(dict):
        def items(self):
            walked_on.append(threading.get_ident())
            return super().items()

    monkeypatch.setattr(
        toml_limits,
        "loads_toml_within_limits",
        lambda text: RecordingTable(custom=RecordingTable(module_path="x")),
    )

    status, body = _call_save_raw_config('[custom]\nmodule_path = "x"\n')

    assert status == 403, body
    assert body["blocked_keys"] == ["custom.module_path"]
    assert len(walked_on) == 2, walked_on
    assert threading.get_ident() not in walked_on, (
        "the protected-key walk ran on the event loop"
    )


def test_the_walk_stops_at_the_first_protected_key_past_the_limit(
    monkeypatch,
):
    from local_deep_research.security import toml_limits
    from local_deep_research.web.routers import research

    later_walked: list[bool] = []

    class LaterTable(dict):
        def items(self):
            later_walked.append(True)
            return super().items()

    monkeypatch.setattr(research, "_MAX_REPORTED_BLOCKED_KEYS", 1)
    monkeypatch.setattr(
        toml_limits,
        "loads_toml_within_limits",
        lambda text: {"module": 1, "class": 2, "later": LaterTable(module=3)},
    )

    status, body = _call_save_raw_config(
        "module = 1\nclass = 2\n[later]\nmodule = 3\n"
    )

    assert status == 403, body
    assert body["blocked_keys"] == ["module"]
    assert body["blocked_keys_truncated"] is True
    assert later_walked == [], (
        "the walk went on past the first protected key over the limit"
    )


@pytest.mark.parametrize(
    ("raw_config", "expected_path"),
    [
        pytest.param("[abcde]\nmodule = 1\n", "abcde.module", id="at-limit"),
        pytest.param(
            "[abcdef]\nmodule = 1\n",
            "abcdef.modu" + _ELLIPSIS,
            id="one-over",
        ),
        pytest.param(
            "[[ab]]\nmodule_path = 1\n",
            "ab[0].modul" + _ELLIPSIS,
            id="after-an-array-index",
        ),
        pytest.param(
            "[" + "t" * 40 + "]\nmodule = 1\n",
            "t" * 11 + _ELLIPSIS,
            id="long-table-name",
        ),
        pytest.param(
            "module" + "x" * 40 + " = 1\n",
            "module" + "x" * 5 + _ELLIPSIS,
            id="long-protected-key",
        ),
    ],
)
def test_a_protected_key_path_is_cut_to_the_limit(
    monkeypatch, raw_config, expected_path
):
    """With the limit lowered to 12 characters. A cut path ends with an
    ellipsis, so it cannot pass for a whole one."""
    from local_deep_research.web.routers import research

    monkeypatch.setattr(research, "_MAX_BLOCKED_KEY_PATH_CHARS", 12)

    status, body = _call_save_raw_config(raw_config)

    assert status == 403, body
    assert body["blocked_keys"] == [expected_path]
    assert len(expected_path) <= 12


def test_more_protected_keys_than_are_listed_still_refuses_the_save(
    app, allow_config_write, config_file, monkeypatch
):
    """Fail closed: past the limit the list is cut, and the configuration is
    refused all the same, with a response that says the list was cut."""
    from local_deep_research.web.routers import research

    monkeypatch.setattr(research, "_MAX_REPORTED_BLOCKED_KEYS", 3)
    client = _authenticated_client(app)
    three = '[a]\nmodule = 1\nname = "x"\nclass = 2\n[b]\nmodule_path = 3\n'
    listed = ["a.module", "a.class", "b.module_path"]

    resp = _save(client, three)

    assert resp.status_code == 403, f"{resp.status_code} {resp.text[:300]}"
    body = resp.json()
    assert body["blocked_keys"] == listed
    assert body["blocked_keys_truncated"] is False
    assert body["error"] == (
        "Configuration contains protected keys that cannot be modified"
    )

    resp = _save(client, three + "class_name = 4\n")

    assert resp.status_code == 403, f"{resp.status_code} {resp.text[:300]}"
    body = resp.json()
    assert body["blocked_keys"] == listed
    assert body["blocked_keys_truncated"] is True
    assert body["error"] == (
        "Configuration contains protected keys that cannot be modified; "
        "only the first 3 are listed"
    )
    _assert_not_saved(
        config_file, "the config had more protected keys than are listed"
    )


def test_a_configuration_nested_past_the_recursion_limit_is_walked(
    app, allow_config_write, config_file
):
    """A dotted key in an inline table nests up to 32 tables for a few
    frames of tomllib's own recursion, so this configuration of about 2 KB
    parses within the limits yet nests deeper than Python's recursion
    limit. The walk recursed once per level of nesting, so it raised
    RecursionError, a 500; it keeps its own stack."""
    from local_deep_research.web.routers import research

    levels = sys.getrecursionlimit() // 32 + 1
    key = ".".join(["k"] * 32)
    raw_config = (
        "x = "
        + ("{" + key + " = ") * levels
        + "{ module = 1 }"
        + "}" * levels
        + "\n"
    )
    client = _authenticated_client(app)

    resp = _save(client, raw_config)

    assert resp.status_code == 403, f"{resp.status_code} {resp.text[:300]}"
    limit = research._MAX_BLOCKED_KEY_PATH_CHARS
    path = "x" + ".k" * (32 * levels) + ".module"
    assert resp.json()["blocked_keys"] == [path[: limit - 1] + _ELLIPSIS]
    _assert_not_saved(config_file, "the config nested a protected key deep")