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.

8.7 KB
Raw Blame
# allow: no-sut-import — drives the REAL settings import surface
# (``SettingsManager.import_settings``) against a real SQLite session.
"""Regression tests for settings import policy and value validation.

Library storage paths are operator-only after #7152/#7224: existing saved
locations and environment overrides remain supported, including locations
outside LDR_DATA_DIR, but user imports cannot redirect them. These checks
exercise the real manager and download-service path with isolated roots.

Benchmark output is contained where it is written instead: run_benchmark
resolves its output directory inside LDR_DATA_DIR (test_settings_path_escape).
The import itself still stores an outside benchmark.output_dir value, which
remains a strict xfail. Select validation and valid user-editable imports are
ordinary controls.
"""

from pathlib import Path
from unittest.mock import MagicMock, patch

import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from local_deep_research.database.models import Base, Setting, SettingType
from local_deep_research.settings.manager import SettingsManager


_STORAGE_KEY = "research_library.storage_path"
_OUTPUT_KEY = "benchmark.output_dir"


def _metadata(key: str, value: str, **extra) -> dict[str, object]:
    data: dict[str, object] = {
        "value": value,
        "type": "APP",
        "name": key,
        "description": "security test setting",
        "ui_element": "text",
    }
    data.update(extra)
    return data


@pytest.fixture
def settings_manager(tmp_path, monkeypatch):
    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path / "ldr-data"))
    monkeypatch.delenv("LDR_RESEARCH_LIBRARY_STORAGE_PATH", raising=False)
    engine = create_engine("sqlite:///:memory:")
    Base.metadata.create_all(engine)
    db_session = sessionmaker(bind=engine)()
    for key, value in (
        (_STORAGE_KEY, str(tmp_path / "saved-library")),
        (_OUTPUT_KEY, "./benchmarks"),
    ):
        db_session.add(
            Setting(
                key=key,
                value=value,
                type=SettingType.APP,
                name=key,
                ui_element="text",
            )
        )
    db_session.commit()
    manager = SettingsManager(db_session=db_session)
    yield manager
    manager.close()
    db_session.close()
    engine.dispose()


def _import(manager: SettingsManager, key: str, value: str) -> None:
    manager.import_settings({key: _metadata(key, value)})


@pytest.mark.parametrize("value", ["$HOME/victim", "../victim", "collections"])
def test_import_cannot_redirect_saved_library_root(settings_manager, value):
    """Even a benign-looking user import must preserve the operator-only root."""
    before = settings_manager.get_setting(_STORAGE_KEY, check_env=False)

    _import(settings_manager, _STORAGE_KEY, value)

    assert settings_manager.get_setting(_STORAGE_KEY, check_env=False) == before


@pytest.mark.xfail(
    strict=True,
    reason=(
        "OPEN: the settings import still stores a benchmark.output_dir "
        "outside LDR_DATA_DIR; the library's operator-only policy does not "
        "cover it. No code reads this key, and run_benchmark contains the "
        "output directory it is given when writing. Remove this marker if "
        "the import gains a path policy for this key."
    ),
)
def test_import_rejects_benchmark_path_escape(settings_manager):
    before = settings_manager.get_setting(_OUTPUT_KEY, check_env=False)

    _import(settings_manager, _OUTPUT_KEY, "$HOME/victim")

    assert settings_manager.get_setting(_OUTPUT_KEY, check_env=False) == before


@pytest.mark.parametrize("value", ["./benchmarks", "./other-benchmarks"])
def test_import_accepts_editable_relative_path_controls(
    settings_manager, value
):
    """A valid user-editable setting must still be imported, including changes."""
    _import(settings_manager, _OUTPUT_KEY, value)

    assert settings_manager.get_setting(_OUTPUT_KEY, check_env=False) == value


def test_import_enforces_same_value_validation_as_save_routes(
    settings_manager,
):
    """GUARANTEE: the import applies the same value validation as the save routes.

    ``import_settings`` (settings/manager.py) runs
    ``validate_imported_setting_value`` over every value that comes from
    the imported file and skips the row when the value is invalid under
    the CURRENT defaults schema. That closes what used to be a genuine
    import-specific bypass: the import validated only the KEY shape and
    persisted the VALUE verbatim, so a select-typed setting could be
    driven out of its allowed options -- something ``validate_setting``
    (web/services/settings_service.py) refuses on every per-key save
    route.

    ``app.timezone`` is used because select is one of the classes the
    validator can actually judge. Library paths have a separate operator-only
    write policy; benchmark output paths are contained by run_benchmark, and
    the import's acceptance of them remains a separate expected failure.

    Note the two validators read their constraints from different places:
    the save route judges the stored ``Setting`` row, while the import
    judges the shipped defaults metadata for the key. Both reject
    ``Mars/Phobos`` for ``app.timezone``, and the assertions below pin
    both halves so neither can regress unnoticed.
    """
    from local_deep_research.web.services.settings_service import (
        validate_setting,
    )

    options = ["UTC", "America/New_York"]
    row = Setting(
        key="app.timezone",
        value="UTC",
        type=SettingType.APP,
        name="app.timezone",
        ui_element="select",
    )
    row.options = list(options)
    settings_manager.db_session.add(row)
    settings_manager.db_session.commit()

    is_valid, _error = validate_setting(row, "Mars/Phobos")
    assert not is_valid  # control: the save-route validator refuses it

    settings_manager.import_settings(
        {
            "app.timezone": _metadata(
                "app.timezone", "Mars/Phobos", ui_element="select"
            )
        }
    )

    assert (
        settings_manager.get_setting("app.timezone", check_env=False) == "UTC"
    ), (
        "import_settings persisted a value that validate_setting rejects "
        "on every save route \u2014 the import path skips the save-path "
        "validators entirely"
    )


def test_import_still_accepts_in_options_select_values(settings_manager):
    """POSITIVE CONTROL for the test above: a legal select value still imports.

    Without this, an ``import_settings`` that rejected *everything* (or
    that never reached the persist step at all) would satisfy the
    regression assertion vacuously.
    """
    row = Setting(
        key="app.timezone",
        value="UTC",
        type=SettingType.APP,
        name="app.timezone",
        ui_element="select",
    )
    row.options = ["UTC", "America/New_York"]
    settings_manager.db_session.add(row)
    settings_manager.db_session.commit()

    settings_manager.import_settings(
        {
            "app.timezone": _metadata(
                "app.timezone", "America/New_York", ui_element="select"
            )
        }
    )

    assert (
        settings_manager.get_setting("app.timezone", check_env=False)
        == "America/New_York"
    )


@pytest.mark.parametrize("operator_override", [False, True])
def test_imported_storage_escape_cannot_redirect_download_service(
    settings_manager, tmp_path, monkeypatch, operator_override
):
    """A refused import preserves saved/operator roots and still creates a library."""
    from local_deep_research.research_library.services.download_service import (
        DownloadService,
    )

    home = tmp_path / "home"
    home.mkdir()
    monkeypatch.setenv("HOME", str(home))
    saved_root = settings_manager.get_setting(_STORAGE_KEY, check_env=False)
    expected_root = Path(saved_root)
    if operator_override:
        expected_root = tmp_path / "operator-library"
        monkeypatch.setenv(
            "LDR_RESEARCH_LIBRARY_STORAGE_PATH", str(expected_root)
        )
    _import(settings_manager, _STORAGE_KEY, "$HOME/victim")

    with (
        patch(
            "local_deep_research.research_library.services.download_service.get_settings_manager",
            return_value=settings_manager,
        ),
        patch(
            "local_deep_research.research_library.services.download_service.RetryManager",
            return_value=MagicMock(),
        ),
    ):
        service = DownloadService("alice")
        try:
            assert Path(service.library_root) == expected_root / "alice"
            assert (expected_root / "alice" / "pdfs").is_dir()
            assert (
                settings_manager.get_setting(_STORAGE_KEY, check_env=False)
                == saved_root
            )
        finally:
            service.close()

    assert not (home / "victim").exists()