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.

15.1 KB
Raw Blame
import itertools
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import create_autospec

import pytest
from fastapi.testclient import TestClient

from local_deep_research.database.backup import backup_service
from local_deep_research.database.backup.backup_service import (
    BackupResult,
    BackupService,
)
from local_deep_research.document_loaders import bytes_loader
from local_deep_research.exporters.base import ExportOptions
from local_deep_research.exporters import odt_exporter
from local_deep_research.research_library.utils import apply_user_subdir


@pytest.mark.parametrize(
    "username",
    [
        "../outside",
        "/absolute/path",
        r"C:\\outside",
        "name\u2215..\u2215outside",
    ],
)
def test_user_library_path_rejects_hostile_username_components(
    tmp_path, username
):
    library_root = tmp_path / "library"
    library_root.mkdir()
    outside_canary = tmp_path / "outside"

    with pytest.raises(ValueError, match="Unsafe username"):
        apply_user_subdir(library_root, username)

    assert not outside_canary.exists()
    assert list(library_root.iterdir()) == []


# Monotonic, never random \u2014 see test_save_raw_config_blocked_keys_fastapi.py
# for why: random /8 addresses collide across a session with many clients.
_FORWARDED_IP_SEQ = itertools.count(1)


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

    Each parametrized case below registers a fresh user; nothing here is
    about rate limiting.
    """
    from local_deep_research.web.dependencies.rate_limit import limiter

    original = limiter.enabled
    limiter.enabled = False
    try:
        yield
    finally:
        limiter.enabled = original


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 ""


@pytest.fixture
def registration_client(app) -> TestClient:
    """A TestClient over the real FastAPI app for driving ``/auth/register``.

    Flask's ``web.app_factory.create_app()`` + a bare ``.test_client()``
    used to build this; the FastAPI migration (#3299) deleted that
    factory. The ``app`` fixture (``tests/conftest.py``) already points
    ``LDR_DATA_DIR`` at a throwaway ``temp_data_dir`` and initializes the
    auth database before ``fastapi_app`` is imported, so this just wraps
    it in a ``TestClient`` with its own forwarded IP.
    """
    n = next(_FORWARDED_IP_SEQ)
    client = TestClient(app, raise_server_exceptions=False)
    client.headers.update(
        {"X-Forwarded-For": f"10.{(n // 250) % 250 + 1}.{n % 250 + 1}.29"}
    )
    return client


@pytest.mark.parametrize(
    "username", ["../outside", "/absolute/path", "user\u202e.."]
)
def test_registration_rejects_path_like_usernames(
    registration_client, username
):
    response = registration_client.post(
        "/auth/register",
        data={
            "username": username,
            "password": "TestPass123",
            "confirm_password": "TestPass123",
            "acknowledge": "true",
            "csrf_token": _csrf(registration_client),
        },
    )

    assert response.status_code == 400
    assert b"Username can only contain" in response.data


@pytest.mark.parametrize(
    "title,author,date",
    [
        ("--output=/tmp/owned", "Author", "2026-01-01"),
        ("title\n--metadata=author:attacker", "Author\x00", "2026-01-01"),
        ("\u2010output=/tmp/owned", "Author", "2026-01-01"),
        ("x" * 20_000, "y" * 20_000, "z" * 20_000),
    ],
)
def test_odt_metadata_cannot_add_pandoc_arguments(
    monkeypatch, title, author, date
):
    captured = []

    def fake_run(command, **kwargs):
        captured.append((command, kwargs))
        return SimpleNamespace(stdout=b"odt")

    monkeypatch.setattr(odt_exporter, "PYPANDOC_AVAILABLE", True)
    monkeypatch.setattr(
        odt_exporter,
        "pypandoc",
        SimpleNamespace(get_pandoc_path=lambda: "pandoc"),
    )
    monkeypatch.setattr(odt_exporter.subprocess, "run", fake_run)
    monkeypatch.setattr(
        odt_exporter.ODTExporter,
        "_pandoc_supports_sandbox",
        staticmethod(lambda: True),
    )

    odt_exporter.ODTExporter(max_heap_mb=128).export(
        "body",
        ExportOptions(title=title, metadata={"author": author, "date": date}),
    )

    command, kwargs = captured[0]
    assert isinstance(command, list)
    assert command[:11] == [
        "pandoc",
        "+RTS",
        "-M128m",
        "-RTS",
        "--sandbox",
        "-f",
        "markdown-raw_attribute-raw_html-raw_tex"
        "-native_divs-native_spans-yaml_metadata_block-multiline_tables",
        "-t",
        "odt",
        "-o",
        "-",
    ]
    assert all(token.startswith("--metadata=") for token in command[11:])
    assert kwargs["check"] is True
    assert "shell" not in kwargs
    for token in command[11:]:
        _key, separator, value = token.partition(":")
        # Fail with the offending token rather than an opaque IndexError if
        # a --metadata= argument ever loses its ``key:value`` shape.
        assert separator, f"metadata token is not key:value form: {token!r}"
        # argv-list invocation (asserted above: list command, no shell=)
        # is the complete mitigation -- a value can never split into a
        # separate pandoc argument whatever bytes it contains. These two
        # assertions only pin the belt-and-braces scrubbing that
        # _sanitize_metadata (odt_exporter.py:187-188) already performs.
        assert "--" not in value
        assert "\n" not in value


def _configure_temp_loader(monkeypatch, tmp_path, loader_class):
    monkeypatch.setattr(bytes_loader.tempfile, "tempdir", str(tmp_path))
    monkeypatch.setattr(
        bytes_loader, "is_extension_supported", lambda _ext: True
    )
    monkeypatch.setattr(
        bytes_loader,
        "get_loader_class_for_extension",
        lambda _ext: (loader_class, {}),
    )


def test_generic_upload_spool_stays_in_configured_tempdir_and_is_removed(
    tmp_path, monkeypatch
):
    observed_paths = []

    class Loader:
        def __init__(self, path):
            observed_paths.append(Path(path))

        def load(self):
            return []

    _configure_temp_loader(monkeypatch, tmp_path, Loader)
    # PDFs now use the bounded in-memory extractor; text exercises the
    # generic loader's temporary-file lifecycle.
    bytes_loader.load_from_bytes(b"content", ".txt")

    assert len(observed_paths) == 1
    assert observed_paths[0].parent == tmp_path
    assert observed_paths[0].name.startswith("ldr_upload_")
    assert observed_paths[0].suffix == ".txt"
    assert not observed_paths[0].exists()


def test_generic_upload_spool_is_removed_when_parser_raises(
    tmp_path, monkeypatch
):
    observed_paths = []

    class ExplodingLoader:
        def __init__(self, path):
            observed_paths.append(Path(path))

        def load(self):
            raise RuntimeError("parser failed")

    _configure_temp_loader(monkeypatch, tmp_path, ExplodingLoader)

    with pytest.raises(RuntimeError, match="parser failed"):
        bytes_loader.load_from_bytes(b"content", ".txt")

    assert observed_paths[0].parent == tmp_path
    assert not observed_paths[0].exists()


@pytest.mark.parametrize(
    "extension", [".pdf/../../x", "\u2010.pdf", "", "." + "x" * 20_000]
)
def test_upload_rejects_hostile_extensions_before_spooling(
    tmp_path, monkeypatch, extension
):
    monkeypatch.setattr(bytes_loader.tempfile, "tempdir", str(tmp_path))
    monkeypatch.setattr(
        bytes_loader, "is_extension_supported", lambda _ext: False
    )

    with pytest.raises(ValueError, match="Unsupported file extension"):
        bytes_loader.load_from_bytes(b"content", extension)

    assert list(tmp_path.iterdir()) == []


@pytest.fixture
def backup_service_in_tmpdir(tmp_path, monkeypatch):
    backup_dir = tmp_path / "backups"
    backup_dir.mkdir()
    monkeypatch.setattr(
        backup_service, "get_user_database_filename", lambda _u: "user.db"
    )
    monkeypatch.setattr(
        backup_service, "get_encrypted_database_path", lambda: tmp_path
    )
    monkeypatch.setattr(
        backup_service, "get_user_backup_directory", lambda _u: backup_dir
    )
    service = BackupService("testuser", "password")
    monkeypatch.setattr(
        service,
        "_create_backup_impl",
        create_autospec(
            service._create_backup_impl,
            return_value=BackupResult(success=True),
        ),
    )
    return service


@pytest.mark.parametrize("forged_path", ["../outside.db", "/tmp/not-used"])
def test_purge_does_not_delete_backup_entries_outside_root(
    tmp_path, monkeypatch, backup_service_in_tmpdir, forged_path
):
    """Defence in depth: a listing entry outside backup_dir is not deleted.

    list_backup_files only reports entries of backup_dir, so this is not a
    reachable attack today; the test monkeypatches list_backup_files to
    inject the hostile path. purge_and_refresh skips any entry that is not
    directly in backup_dir (_is_purgeable) and re-checks containment with
    is_safe_glob_result before each unlink. This was a strict xfail until
    that check landed.
    """
    service = backup_service_in_tmpdir
    outside_canary = tmp_path / "outside.db"
    outside_canary.write_bytes(b"do not delete")
    if forged_path.startswith("/"):
        forged_path = str(outside_canary)
    forged_entry = service.backup_dir / forged_path
    monkeypatch.setattr(
        backup_service,
        "list_backup_files",
        lambda directory, *, include_auxiliary: [forged_entry],
    )

    result = service.purge_and_refresh()

    assert result.success is True
    assert outside_canary.read_bytes() == b"do not delete"
    # An entry outside the backup directory is not a backup the purge could
    # remove, so it must not hold the purge warning up forever (#7443 R2).
    service._create_backup_impl.assert_called_once_with(purge_incomplete=False)


@pytest.mark.skipif(
    __import__("sys").platform == "win32", reason="Unix-specific test"
)
def test_purge_does_not_follow_forged_backup_symlink(
    tmp_path, backup_service_in_tmpdir
):
    service = backup_service_in_tmpdir
    outside_canary = tmp_path / "outside.db"
    outside_canary.write_bytes(b"do not delete")
    symlink = service.backup_dir / "ldr_backup_forged.db"
    try:
        symlink.symlink_to(outside_canary)
    except OSError:
        pytest.skip("Cannot create symlinks in this environment")
    # Let the real raising enumeration see a matching backup name. A mock
    # of the legacy list_backups API would no longer exercise this path.
    result = service.purge_and_refresh()

    assert result.success is True
    assert outside_canary.read_bytes() == b"do not delete"
    assert symlink.is_symlink()
    # The purge never deletes a symlink's target, so the link itself is not a
    # remaining old-password backup and cannot keep the warning up (#7443 R2).
    service._create_backup_impl.assert_called_once_with(purge_incomplete=False)


# Issue #7853: every purge loop skips a forged listing entry and still
# deletes ordinary backups. The superseded-key purge consumes
# _managed_backup_paths (which re-checks list_backups entries); purge_and_refresh
# enumerates with list_backup_files and re-checks each entry itself, so the
# forged listing is injected through both.

_ORDINARY_BACKUP = "ldr_backup_20260101_000000.db"


def _forge_listing(service, monkeypatch, tmp_path, kind):
    """Plant an outside canary and a listing that also names a real backup.

    Returns (canary, ordinary_backup). ``kind`` picks how the forged entry
    reaches the canary: a relative ``..`` name, an absolute path, or an
    in-root symlink with a backup name. ``dot`` names backup_dir itself,
    whose salt path is a sibling outside it, and ``nul`` is a name with an
    embedded NUL byte that cannot be resolved at all.
    """
    canary = tmp_path / "outside.db"
    canary.write_bytes(b"do not delete")
    ordinary = service.backup_dir / _ORDINARY_BACKUP
    ordinary.write_bytes(b"old-key backup")
    if kind == "relative":
        forged = "../outside.db"
    elif kind == "absolute":
        forged = str(canary)
    elif kind == "dot":
        forged = "."
    elif kind == "nul":
        forged = "ldr_backup_20260101_000002.db\x00"
    else:
        if __import__("sys").platform == "win32":
            pytest.skip("Unix-specific test")
        link = service.backup_dir / "ldr_backup_20260101_000001.db"
        try:
            link.symlink_to(canary)
        except OSError:
            pytest.skip("Cannot create symlinks in this environment")
        forged = link.name
    monkeypatch.setattr(
        service,
        "list_backups",
        lambda: [
            {"filename": "forged.db", "path": forged},
            {"filename": _ORDINARY_BACKUP, "path": _ORDINARY_BACKUP},
        ],
    )
    monkeypatch.setattr(
        backup_service,
        "list_backup_files",
        lambda directory, *, include_auxiliary=False: [
            directory / forged,
            directory / _ORDINARY_BACKUP,
        ],
    )
    return canary, ordinary


_FORGED_KINDS = ["relative", "absolute", "symlink", "dot", "nul"]


@pytest.mark.parametrize("kind", _FORGED_KINDS)
def test_managed_backup_paths_drops_forged_entries(
    tmp_path, monkeypatch, backup_service_in_tmpdir, loguru_caplog, kind
):
    service = backup_service_in_tmpdir
    _canary, ordinary = _forge_listing(service, monkeypatch, tmp_path, kind)

    with loguru_caplog.at_level("WARNING"):
        paths = service._managed_backup_paths()

    assert paths == [ordinary]
    assert "directly inside the backup directory" in loguru_caplog.text
    # The refusal is logged without the entry's name.
    assert "outside.db" not in loguru_caplog.text
    assert "ldr_backup_20260101_000001" not in loguru_caplog.text
    assert "ldr_backup_20260101_000002" not in loguru_caplog.text


def _run_superseded_purge(service, monkeypatch):
    # Every backup counts as superseded and no salt is required, so the
    # only thing that can keep a forged entry alive is the containment check.
    monkeypatch.setattr(
        backup_service, "has_per_database_salt", lambda _p: False
    )
    monkeypatch.setattr(
        service, "_classify_backup_key", lambda _p: "superseded"
    )
    return service.purge_backups_under_superseded_key()


@pytest.mark.parametrize("kind", _FORGED_KINDS)
@pytest.mark.parametrize("loop", ["purge_and_refresh", "superseded_key"])
def test_every_purge_loop_skips_forged_entries(
    tmp_path, monkeypatch, backup_service_in_tmpdir, loop, kind
):
    if loop == "purge_and_refresh" and kind == "nul":
        pytest.skip(
            "list_backup_files enumerates with iterdir(), which cannot "
            "yield a name with a NUL byte"
        )
    service = backup_service_in_tmpdir
    canary, ordinary = _forge_listing(service, monkeypatch, tmp_path, kind)

    if loop == "purge_and_refresh":
        service.purge_and_refresh()
    else:
        assert _run_superseded_purge(service, monkeypatch) == 1

    assert canary.read_bytes() == b"do not delete"
    assert not ordinary.exists()