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.

20.6 KB
Raw Blame
"""Per-user library isolation and refusal of ambiguous legacy PDF reads."""

import hashlib
from unittest.mock import MagicMock

import pytest

from local_deep_research.research_library.services.pdf_storage_manager import (
    PDFStorageManager,
)
from local_deep_research.research_library.utils import apply_user_subdir

# Retired flag: old deployment configuration must not restore the fallback.
LEGACY_READ_FALLBACK_ENV = "LDR_RESEARCH_LIBRARY_ALLOW_LEGACY_READ_FALLBACK"


class TestApplyUserSubdir:
    def test_per_user_subdir_when_not_shared(self, tmp_path):
        assert (
            apply_user_subdir(tmp_path, "alice", False)
            == (tmp_path / "alice").resolve()
        )

    def test_shared_mode_returns_base(self, tmp_path, monkeypatch):
        # Shared mode only takes effect when the operator env gate is on.
        monkeypatch.setattr(
            "local_deep_research.research_library.utils."
            "_shared_library_allowed",
            lambda: True,
        )
        assert apply_user_subdir(tmp_path, "alice", True) == tmp_path.resolve()

    def test_shared_mode_gated_off_enforces_per_user(self, tmp_path):
        # SECURITY: with the operator gate OFF (default), a user's own
        # shared_library=true must NOT drop per-user isolation — otherwise a
        # multi-tenant user could set shared_library=true and point
        # storage_path at another user's directory to read their PDFs.
        assert (
            apply_user_subdir(tmp_path, "alice", True)
            == (tmp_path / "alice").resolve()
        )

    def test_no_username_returns_base(self, tmp_path):
        assert apply_user_subdir(tmp_path, None, False) == tmp_path.resolve()

    def test_two_users_get_distinct_dirs(self, tmp_path):
        alice = apply_user_subdir(tmp_path, "alice", False)
        bob = apply_user_subdir(tmp_path, "bob", False)
        assert alice != bob

    def test_valid_registration_usernames_are_accepted(self, tmp_path):
        # Values a valid registration can produce ([A-Za-z0-9_-]) must join.
        for name in ("alice", "Bob-2", "user_name", "abc123"):
            assert (
                apply_user_subdir(tmp_path, name, False)
                == (tmp_path / name).resolve()
            )

    def test_non_ascii_username_round_trips(self, tmp_path):
        # Registration's ``username.replace("_","").replace("-","").isalnum()``
        # check is Unicode-aware, so accented/Cyrillic/CJK usernames register
        # successfully. The per-user path guard must accept the exact same
        # usernames rather than raising for an already-provisioned account.
        for name in ("álice", "аlice", "Müller", "田中太郎"):
            result = apply_user_subdir(tmp_path, name, False)
            assert result == (tmp_path / name).resolve()
            assert result.name == name


class TestUsernameSanitization:
    """A traversal/separator username must be rejected at the chokepoint, not
    join into a path that a downstream ``.resolve()`` can escape the base
    with (defense-in-depth for future non-alnum username provisioning)."""

    @pytest.mark.parametrize(
        "bad",
        [
            # Path traversal / separators.
            "../evil",
            "../../etc",
            "a/b",
            "a\\b",
            "..\\..\\x",
            "..",
            ".",
            "foo/..",
            "/etc/passwd",
            "\\\\server\\share",
            "a\x00b",
            "..%2f..",
            # Windows drive-relative ':' (base / "D:evil" discards base) and
            # NTFS alternate-data-stream 'name:stream'.
            "D:evil",
            "a:b",
            "alice:hidden",
            # Trailing dot / space canonicalization.
            "alice.",
            "alice ",
            " alice",
            "a b",
            # Reserved device name style punctuation (not actually a reserved
            # device name check — just still non-alnum after stripping).
            "CON.",
            # All-punctuation usernames: empty after stripping '_'/'-'.
            "-",
            "__",
        ],
    )
    def test_traversal_username_is_rejected(self, tmp_path, bad):
        with pytest.raises(ValueError):
            apply_user_subdir(tmp_path, bad, False)

    def test_empty_username_short_circuits_to_base(self, tmp_path):
        # Pre-existing shared-mode / no-username short-circuit is preserved:
        # an empty username returns the base rather than raising.
        assert apply_user_subdir(tmp_path, "", False) == tmp_path.resolve()

    def test_traversal_username_cannot_escape_base(self, tmp_path):
        # Even after the join+resolve that a consumer performs, no accepted
        # username escapes the base: rejection happens before the join.
        base = (tmp_path / "library").resolve()
        base.mkdir()
        with pytest.raises(ValueError):
            apply_user_subdir(base, "../../../../tmp", False)

    def test_shared_mode_skips_username_check(self, tmp_path, monkeypatch):
        # With the operator gate on, shared mode never joins the username, so
        # it is not validated.
        monkeypatch.setattr(
            "local_deep_research.research_library.utils."
            "_shared_library_allowed",
            lambda: True,
        )
        assert (
            apply_user_subdir(tmp_path, "../../etc", True) == tmp_path.resolve()
        )


class TestDownloadWritesToPerUserPath:
    """Simulates the download write path: a PDFStorageManager built with the
    per-user root writes PDFs under ``<base>/<username>/pdfs/``."""

    def test_write_lands_in_per_user_dir(self, tmp_path, mock_pdf_content):
        base = tmp_path
        per_user = apply_user_subdir(base, "alice", False)
        manager = PDFStorageManager(per_user, "filesystem")
        doc = MagicMock()
        doc.id = "doc-1"
        rel_path, _ = manager.save_pdf(
            mock_pdf_content, doc, MagicMock(), "5.pdf"
        )

        expected_name = f"5_{hashlib.sha256(mock_pdf_content).hexdigest()}.pdf"
        assert rel_path == f"pdfs/{expected_name}"
        written = base / "alice" / rel_path
        assert written.is_file()
        assert written.read_bytes() == mock_pdf_content
        # Nothing was written to the shared base directly.
        assert not (base / "pdfs").exists()

    @pytest.mark.parametrize("same_content", [False, True])
    def test_two_users_same_resource_id_do_not_collide(
        self, tmp_path, mock_pdf_content, same_content
    ):
        base = tmp_path
        alice_mgr = PDFStorageManager(
            apply_user_subdir(base, "alice", False), "filesystem"
        )
        bob_mgr = PDFStorageManager(
            apply_user_subdir(base, "bob", False), "filesystem"
        )
        alice_bytes = mock_pdf_content + b"-alice"
        bob_bytes = alice_bytes if same_content else mock_pdf_content + b"-bob"

        a_rel, _ = alice_mgr.save_pdf(
            alice_bytes, MagicMock(id="a"), MagicMock(), "5.pdf"
        )
        b_rel, _ = bob_mgr.save_pdf(
            bob_bytes, MagicMock(id="b"), MagicMock(), "5.pdf"
        )

        assert a_rel == f"pdfs/5_{hashlib.sha256(alice_bytes).hexdigest()}.pdf"
        assert b_rel == f"pdfs/5_{hashlib.sha256(bob_bytes).hexdigest()}.pdf"
        # Equal bytes produce equal snapshot names, so content hashes alone
        # cannot provide per-user isolation.
        assert (a_rel == b_rel) is same_content
        a_file = base / "alice" / a_rel
        b_file = base / "bob" / b_rel
        assert a_file != b_file
        assert a_file.read_bytes() == alice_bytes
        assert b_file.read_bytes() == bob_bytes
        assert not (base / "pdfs").exists()


class TestNoLegacyReadFallback:
    """Shared-root filenames cannot establish ownership, even with the old flag."""

    def _pdf_doc(self, rel_path="pdfs/5.pdf"):
        doc = MagicMock()
        doc.id = "doc-1"
        doc.file_type = "pdf"
        doc.file_path = rel_path
        return doc

    def _session_without_blob(self):
        session = MagicMock()
        session.query.return_value.filter_by.return_value.first.return_value = (
            None
        )
        return session

    @pytest.mark.parametrize("old_flag", ["false", "true"])
    def test_legacy_file_never_loads(
        self, tmp_path, mock_pdf_content, monkeypatch, old_flag
    ):
        monkeypatch.setenv(LEGACY_READ_FALLBACK_ENV, old_flag)
        per_user = apply_user_subdir(tmp_path, "alice", False)
        legacy_pdf = tmp_path / "pdfs" / "5.pdf"
        legacy_pdf.parent.mkdir(parents=True)
        legacy_pdf.write_bytes(mock_pdf_content)

        manager = PDFStorageManager(per_user, "none")
        doc, session = self._pdf_doc(), self._session_without_blob()
        assert manager.load_pdf(doc, session) is None
        assert not manager.has_pdf(doc, session)
        assert not PDFStorageManager.pdf_exists(per_user, doc, session)
        assert legacy_pdf.read_bytes() == mock_pdf_content

    def test_per_user_file_preferred_over_legacy(
        self, tmp_path, mock_pdf_content
    ):
        base = tmp_path
        per_user = apply_user_subdir(base, "alice", False)
        # Same relative path exists in BOTH roots with different content.
        (per_user / "pdfs").mkdir(parents=True, exist_ok=True)
        (per_user / "pdfs" / "5.pdf").write_bytes(mock_pdf_content + b"-user")
        (base / "pdfs").mkdir(parents=True, exist_ok=True)
        (base / "pdfs" / "5.pdf").write_bytes(mock_pdf_content + b"-legacy")

        manager = PDFStorageManager(per_user, "none")
        loaded = manager.load_pdf(self._pdf_doc(), self._session_without_blob())
        assert loaded == mock_pdf_content + b"-user"

    def test_missing_everywhere_returns_none(self, tmp_path):
        base = tmp_path
        per_user = apply_user_subdir(base, "alice", False)
        per_user.mkdir(parents=True, exist_ok=True)
        manager = PDFStorageManager(per_user, "none")
        assert (
            manager.load_pdf(self._pdf_doc(), self._session_without_blob())
            is None
        )

    def test_no_legacy_root_does_not_fall_back(
        self, tmp_path, mock_pdf_content
    ):
        base = tmp_path
        per_user = apply_user_subdir(base, "alice", False)
        per_user.mkdir(parents=True, exist_ok=True)
        # File only in legacy location; manager has NO legacy_root.
        (base / "pdfs").mkdir(parents=True, exist_ok=True)
        (base / "pdfs" / "5.pdf").write_bytes(mock_pdf_content)

        manager = PDFStorageManager(per_user, "none")
        assert (
            manager.load_pdf(self._pdf_doc(), self._session_without_blob())
            is None
        )


class TestDestructiveDeleteDoesNotUseLegacyFallback:
    """Deleting a user's own document must never unlink a colliding file in
    the shared legacy root — per-user autoincrement ids collide by
    construction, so ``pdfs/5.pdf`` there can belong to a different tenant
    (#5521)."""

    def _pdf_doc(self, rel_path="pdfs/5.pdf"):
        doc = MagicMock()
        doc.id = "doc-1"
        doc.file_type = "pdf"
        doc.file_path = rel_path
        doc.storage_mode = "filesystem"
        return doc

    def test_delete_pdf_keeps_legacy_shared_file(
        self, tmp_path, mock_pdf_content
    ):
        base = tmp_path
        per_user = apply_user_subdir(base, "alice", False)
        per_user.mkdir(parents=True, exist_ok=True)
        # Another tenant's file at the same relative path in the shared root.
        legacy_pdf = base / "pdfs" / "5.pdf"
        legacy_pdf.parent.mkdir(parents=True, exist_ok=True)
        legacy_pdf.write_bytes(mock_pdf_content)

        manager = PDFStorageManager(per_user, "none", username="alice")
        manager.delete_pdf(self._pdf_doc(), MagicMock())

        # The shared-root file must survive — it is not alice's to delete.
        assert legacy_pdf.exists()

    def test_delete_pdf_removes_own_per_user_file(
        self, tmp_path, mock_pdf_content
    ):
        base = tmp_path
        per_user = apply_user_subdir(base, "alice", False)
        own_pdf = per_user / "pdfs" / "5.pdf"
        own_pdf.parent.mkdir(parents=True, exist_ok=True)
        own_pdf.write_bytes(mock_pdf_content)

        manager = PDFStorageManager(per_user, "none", username="alice")
        manager.delete_pdf(self._pdf_doc(), MagicMock())

        assert not own_pdf.exists()

    def test_delete_pdf_fails_closed_without_username(
        self, tmp_path, mock_pdf_content
    ):
        # Fail closed: an empty username means library_root may be the bare
        # shared root, so delete_pdf must refuse the filesystem unlink rather
        # than risk deleting another tenant's colliding file.
        base = tmp_path
        shared_pdf = base / "pdfs" / "5.pdf"
        shared_pdf.parent.mkdir(parents=True, exist_ok=True)
        shared_pdf.write_bytes(mock_pdf_content)

        # library_root IS the shared base (username was empty upstream).
        manager = PDFStorageManager(base, "none", username="")
        result = manager.delete_pdf(self._pdf_doc(), MagicMock())

        assert result is False
        # The file the manager could have unlinked survives.
        assert shared_pdf.exists()


class TestResolverDeleteGate:
    """Reads and deletes use the same per-user-only path resolution."""

    def _mock_settings(self, mocker, base):
        mock_settings = MagicMock()

        def _get_setting(key, default=None):
            if key == "research_library.storage_path":
                return str(base)
            if key == "research_library.shared_library":
                return False
            return default

        mock_settings.get_setting.side_effect = _get_setting
        mocker.patch(
            "local_deep_research.utilities.db_utils.get_settings_manager",
            return_value=mock_settings,
        )
        return mock_settings

    def test_read_and_delete_ignore_legacy_file(
        self, mocker, tmp_path, mock_pdf_content, monkeypatch
    ):
        from local_deep_research.research_library.utils import (
            get_absolute_path_from_settings,
        )

        # A stale deployment flag must not change read or delete resolution.
        monkeypatch.setenv(LEGACY_READ_FALLBACK_ENV, "true")
        base = tmp_path
        self._mock_settings(mocker, base)
        legacy_pdf = base / "pdfs" / "5.pdf"
        legacy_pdf.parent.mkdir(parents=True, exist_ok=True)
        legacy_pdf.write_bytes(mock_pdf_content)

        path = get_absolute_path_from_settings("pdfs/5.pdf", "alice")
        assert path == apply_user_subdir(base, "alice", False) / "pdfs/5.pdf"
        assert not path.exists()
        assert legacy_pdf.read_bytes() == mock_pdf_content

    def test_file_resolve_fails_closed_without_username(self, mocker, tmp_path):
        # Fail closed: a destructive resolve with no user context has no
        # per-user root to scope the unlink to, and apply_user_subdir would
        # otherwise return the bare shared root. Raise rather than hand a
        # shared-root path to unlink.
        from local_deep_research.research_library.utils import (
            get_absolute_path_from_settings,
        )

        base = tmp_path
        self._mock_settings(mocker, base)

        for empty in ("", None):
            with pytest.raises(ValueError):
                get_absolute_path_from_settings("pdfs/5.pdf", empty)

        # Trusted callers may still request only the configured root, e.g.
        # as a containment boundary for cleanup; this does not return a file.
        assert get_absolute_path_from_settings("") == base


class TestGetAbsolutePerUserPath:
    """Missing per-user files never change the resolution root."""

    def _patch_settings(self, monkeypatch, base):
        import local_deep_research.utilities.db_utils as db_utils

        sm = MagicMock()

        def _get_setting(key, default=None):
            if key == "research_library.storage_path":
                return str(base)
            if key == "research_library.shared_library":
                return False
            return default

        sm.get_setting.side_effect = _get_setting
        monkeypatch.setattr(
            db_utils, "get_settings_manager", lambda *a, **k: sm
        )

    def test_missing_file_stays_in_per_user_root(
        self, tmp_path, monkeypatch, mock_pdf_content
    ):
        from local_deep_research.research_library.utils import (
            get_absolute_path_from_settings,
        )

        # The retired flag cannot bring back implicit shared-root reads.
        monkeypatch.setenv(LEGACY_READ_FALLBACK_ENV, "true")
        base = tmp_path
        self._patch_settings(monkeypatch, base)

        # Legacy file only.
        (base / "pdfs").mkdir(parents=True, exist_ok=True)
        (base / "pdfs" / "5.pdf").write_bytes(mock_pdf_content)

        resolved = get_absolute_path_from_settings("pdfs/5.pdf", "alice")
        assert resolved == (base / "alice" / "pdfs" / "5.pdf")
        assert not resolved.is_file()

    def test_prefers_per_user_file(
        self, tmp_path, monkeypatch, mock_pdf_content
    ):
        from local_deep_research.research_library.utils import (
            get_absolute_path_from_settings,
        )

        base = tmp_path
        self._patch_settings(monkeypatch, base)

        (base / "alice" / "pdfs").mkdir(parents=True, exist_ok=True)
        (base / "alice" / "pdfs" / "5.pdf").write_bytes(mock_pdf_content)
        (base / "pdfs").mkdir(parents=True, exist_ok=True)
        (base / "pdfs" / "5.pdf").write_bytes(mock_pdf_content + b"-legacy")

        resolved = get_absolute_path_from_settings("pdfs/5.pdf", "alice")
        assert resolved == (base / "alice" / "pdfs" / "5.pdf")

    def test_nonempty_path_requires_username(
        self, tmp_path, monkeypatch, mock_pdf_content
    ):
        from local_deep_research.research_library.utils import (
            get_absolute_path_from_settings,
        )

        base = tmp_path
        self._patch_settings(monkeypatch, base)
        (base / "pdfs").mkdir(parents=True, exist_ok=True)
        (base / "pdfs" / "5.pdf").write_bytes(mock_pdf_content)

        with pytest.raises(ValueError, match="user context"):
            get_absolute_path_from_settings("pdfs/5.pdf")


class TestCrossTenantReadFallbackRemoved:
    """A missing own file cannot turn into another user's colliding PDF."""

    def _patch_settings(self, mocker, storage_path):
        sm = MagicMock()

        def _get_setting(key, default=None):
            if key == "research_library.storage_path":
                return str(storage_path)
            if key == "research_library.shared_library":
                return False
            return default

        sm.get_setting.side_effect = _get_setting
        mocker.patch(
            "local_deep_research.utilities.db_utils.get_settings_manager",
            return_value=sm,
        )

    def test_attacker_cannot_read_victim_pdf_when_gate_off(
        self, mocker, tmp_path, mock_pdf_content
    ):
        base = tmp_path
        # Victim's per-user library and PDF (victim used storage_path=<base>).
        victim_root = base / "victim"
        victim_pdf = victim_root / "pdfs" / "5.pdf"
        victim_pdf.parent.mkdir(parents=True, exist_ok=True)
        victim_pdf.write_bytes(mock_pdf_content + b"-VICTIM-SECRET")

        # Attacker points their own storage_path at the victim's directory.
        self._patch_settings(mocker, victim_root)

        # Gate OFF (default): the read must NOT resolve to the victim's file.
        from local_deep_research.research_library.utils import (
            get_absolute_path_from_settings,
        )

        resolved = get_absolute_path_from_settings("pdfs/5.pdf", "attacker")

        assert resolved != victim_pdf
        # Resolves strictly within the attacker's own per-user root...
        attacker_root = apply_user_subdir(victim_root, "attacker", False)
        assert attacker_root in resolved.parents
        # ...where the attacker has no such file (nothing leaked).
        assert not resolved.exists()

    def test_retired_flag_cannot_enable_cross_user_read(
        self, mocker, tmp_path, mock_pdf_content, monkeypatch
    ):
        # Previously documented opt-in behavior is no longer available.
        monkeypatch.setenv(LEGACY_READ_FALLBACK_ENV, "true")
        base = tmp_path
        victim_root = base / "victim"
        victim_pdf = victim_root / "pdfs" / "5.pdf"
        victim_pdf.parent.mkdir(parents=True, exist_ok=True)
        victim_pdf.write_bytes(mock_pdf_content)

        self._patch_settings(mocker, victim_root)

        from local_deep_research.research_library.utils import (
            get_absolute_path_from_settings,
        )

        resolved = get_absolute_path_from_settings("pdfs/5.pdf", "attacker")
        assert resolved == victim_root / "attacker" / "pdfs/5.pdf"
        assert not resolved.exists()
        assert victim_pdf.read_bytes() == mock_pdf_content