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.

2.5 KB
Raw Blame
"""Limits on TOML text checked before tomllib parses it.

The key scanner's edge cases (strings, comments, escapes, CRLF, inline
tables) are exercised through files in
``tests/document_loaders/test_toml_loader.py``; these tests cover the
text entry point that ``POST /api/save_raw_config`` uses.
"""

import tomllib

import pytest

from local_deep_research.security import toml_limits

# One part more than MAX_TOML_KEY_PARTS (32).
_OVERLONG_KEY = ".".join(["k"] * 33)


def _forbid_parsing(monkeypatch):
    """Fail if tomllib runs, so a refusal must happen before parsing."""

    def parse_attempted(*args, **kwargs):
        raise AssertionError("tomllib ran on input that must be refused")

    monkeypatch.setattr(tomllib, "loads", parse_attempted)


def test_text_within_the_limits_parses():
    assert toml_limits.loads_toml_within_limits(
        '[search]\ntool = "searxng"\r\n'
    ) == {"search": {"tool": "searxng"}}


def test_text_over_the_size_limit_is_refused_before_parsing(monkeypatch):
    monkeypatch.setattr(toml_limits, "MAX_TOML_BYTES", 8)
    assert toml_limits.loads_toml_within_limits("x = 'ab'") == {"x": "ab"}

    _forbid_parsing(monkeypatch)
    with pytest.raises(toml_limits.TOMLLimitError, match="larger than 8"):
        toml_limits.loads_toml_within_limits("x = 'abc'")


def test_size_limit_counts_utf8_bytes_not_characters(monkeypatch):
    monkeypatch.setattr(toml_limits, "MAX_TOML_BYTES", 8)
    text = "x = 'éé'"
    assert len(text) == 8
    assert len(text.encode()) == 10

    _forbid_parsing(monkeypatch)
    with pytest.raises(toml_limits.TOMLLimitError, match="larger than 8"):
        toml_limits.loads_toml_within_limits(text)


@pytest.mark.parametrize(
    "text",
    [
        pytest.param(f"{_OVERLONG_KEY} = 1\n", id="dotted-key"),
        pytest.param(f"[{_OVERLONG_KEY}]\r\n", id="crlf-table-header"),
    ],
)
def test_overlong_key_text_is_refused_before_parsing(monkeypatch, text):
    _forbid_parsing(monkeypatch)
    with pytest.raises(
        toml_limits.TOMLLimitError, match="more than 32 dotted parts"
    ):
        toml_limits.loads_toml_within_limits(text)


@pytest.mark.parametrize("value", [b"x = 1", ["x = 1"], 1])
def test_non_string_input_is_rejected_before_parsing(monkeypatch, value):
    # Python 3.12's tomllib raises different errors for these inputs. Check
    # our stable contract and prove that invalid types never reach parsing.
    _forbid_parsing(monkeypatch)
    with pytest.raises(TypeError, match="Expected str object"):
        toml_limits.loads_toml_within_limits(value)