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.

10.4 KB
Raw Blame
"""Symlink TOCTOU on the .db and .salt paths.

Research context (CAPEC-27 "HTTP Response Splitting"-family redirected-
file attacks; the 2025-26 symlink-race CVE class, e.g. CVE-2026-54370
and the filelock CVE-2025-68146): a local attacker who can plant a
symlink where the application will create or open a file can redirect
the write to a victim path. Planting requires data-dir write access,
which the 0700 owner-only directory already denies to other users --
these tests pin the residual posture for the same-user/compromised-
account case.

Probed and pinned against the real manager:

* AJ1 dangling .db symlink at create -- CLOSED. It was an open
  exposure pinned as an ``xfail(strict)``: ``Path.exists()`` follows
  symlinks and returns False for a dangling one, so the
  ``db_path.exists()`` guard passed, and ``sqlcipher3.connect`` (via
  ``create_sqlcipher_connection`` with creation_mode=True) followed the
  symlink -- the whole database written AT THE ATTACKER-CHOSEN VICTIM
  PATH outside the data dir, while the salt landed inside and the
  account looked perfectly normal. ``create_user_database`` now refuses
  a symlink at the .db path before it opens anything, so the test passes
  on its own terms and the marker is gone.
* AJ2 .db symlink to an EXISTING victim -- HOLD, and refused earlier:
  the symlink check names the link instead of the exists() guard seeing
  the victim through it; the victim's bytes are untouched either way.
* AJ3 .salt symlink -- HOLD via O_EXCL no-follow: ``create_database_
  salt`` opens with O_CREAT|O_EXCL, which never follows a symlink (the
  canonical mitigation); the FileExistsError flows into create_user_
  database's orphaned-salt recovery branch, whose unlink removes the
  LINK, and a real salt is created at the expected path. The victim is
  never written.
* AJ4 a symlinked data dir (deployer choice, e.g. /srv symlink) works
  transparently for the full create/open cycle.
"""

import shutil
import uuid

import pytest
from sqlalchemy import text

from local_deep_research.config.paths import get_user_database_filename
from local_deep_research.database.encrypted_db import DatabaseManager

# Module-level gate. Without a working SQLCipher build every test below
# is a no-op, and the dozens of in-body ``pytest.skip`` calls let a CI
# lane report this whole battery green having executed nothing. Skipping
# at import time makes the missing dependency one visible, uniform signal
# per module instead.
pytest.importorskip("sqlcipher3", reason="requires SQLCipher (encrypted mode)")


@pytest.fixture
def manager(tmp_path, monkeypatch):
    """A DatabaseManager writing to an isolated data directory.

    Same self-contained pattern as the round-1..6 files, with the
    sanctioned test-mode KDF knob.
    """
    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
    monkeypatch.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "1000")
    mgr = DatabaseManager()
    mgr.data_dir = tmp_path
    yield mgr
    mgr.close_all_databases()


def test_dangling_db_symlink_is_refused_at_create(manager):
    """AJ1 (now ENFORCED): a planted dangling symlink at the .db path
    must not redirect the create. The database bytes must never land at
    an attacker-chosen path outside the data dir -- an existence guard
    that follows symlinks (``exists()``) is exactly the guard a dangling
    link defeats.

    This carried an ``xfail(strict)`` while the gap was open. The
    ``is_symlink()`` check in ``create_user_database`` closes it, so the
    marker is gone rather than inverted: the assertion below was always
    the correct one and now holds.

    Precondition is same-user / compromised-account (the data dir is
    0700), which is why this is a hardening gap rather than a
    cross-account break -- but the redirected write is unbounded: it goes
    wherever the link points, with the process's own privileges."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    username = f"aj1_{uuid.uuid4().hex[:8]}"
    password = "DanglingSymlinkPw1!"  # noqa: S105
    filename = get_user_database_filename(username)
    victim = manager.data_dir.parent / f"aj1_victim_{uuid.uuid4().hex[:8]}.db"
    link = manager.data_dir / filename
    link.symlink_to(victim)  # dangling: victim does not exist yet

    try:
        # The refusal shape is deliberately not pinned (a guard may raise
        # or may unlink the planted link and create normally inside the
        # data dir); what MUST hold is that nothing is written outside.
        try:
            manager.create_user_database(username, password)
        except (ValueError, OSError):
            pass
        assert not victim.exists(), (
            "the create followed the planted symlink -- the database "
            "landed at the attacker-chosen path outside the data dir"
        )
        # Refusal may leave the planted link untouched. The security
        # boundary is that the create never writes through it; deleting
        # an untrusted pre-existing path is not required for refusal.
    finally:
        manager.close_user_database(username)
        victim.unlink(missing_ok=True)


def test_db_symlink_to_existing_victim_is_refused(manager):
    """AJ2 (HOLD, and now for a better reason): a .db symlink pointing at
    an EXISTING victim file is refused and the victim's bytes are
    untouched.

    The refusal used to come from the ``db_path.exists()`` guard, which
    saw the victim THROUGH the link and reported "Database already
    exists" -- correct outcome, wrong reason, and the same guard let the
    dangling spelling in AJ1 straight through. The ``is_symlink()`` check
    refuses first and names what it found, so this pins the symlink
    refusal rather than an existence collision that happens to cover it.
    """
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    username = f"aj2_{uuid.uuid4().hex[:8]}"
    filename = get_user_database_filename(username)
    victim = manager.data_dir.parent / f"aj2_victim_{uuid.uuid4().hex[:8]}.db"
    victim.write_bytes(b"existing-precious")
    (manager.data_dir / filename).symlink_to(victim)

    # `victim` lives in the per-worker basetemp (tmp_path.parent), which
    # pytest only reclaims on its 3-run rotation -- so the unlink has to be
    # on the failure path too, not just the happy one.
    try:
        with pytest.raises(ValueError, match="is a symlink"):
            manager.create_user_database(username, "ExistingVictimPw1!")  # noqa: S105

        assert victim.read_bytes() == b"existing-precious", (
            "the refused create modified the victim file"
        )
        assert username not in manager.connections
    finally:
        victim.unlink(missing_ok=True)


def test_salt_symlink_is_never_followed_oexcl_wins(manager):
    """AJ3 (HOLD): a symlink at the .salt path cannot redirect the salt
    write -- create_database_salt's O_CREAT|O_EXCL open refuses to
    follow symlinks; the FileExistsError flows into the orphaned-salt
    recovery branch, which unlinks the LINK and creates a real salt at
    the expected path. The victim is never written; the create succeeds."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    username = f"aj3_{uuid.uuid4().hex[:8]}"
    password = "SaltSymlinkPw1!"  # noqa: S105
    filename = get_user_database_filename(username)
    victim = manager.data_dir.parent / f"aj3_victim_{uuid.uuid4().hex[:8]}.bin"
    salt_link = manager.data_dir / filename.replace(".db", ".db.salt")
    salt_link.symlink_to(victim)

    # `victim` is outside tmp_path; if the no-follow guard ever regresses it
    # gets CREATED there, so the cleanup belongs on the failure path.
    try:
        engine = manager.create_user_database(username, password)
        assert engine is not None, (
            "create failed entirely in the face of a planted salt symlink"
        )

        assert not victim.exists(), (
            "the salt write FOLLOWED the symlink to the victim -- O_EXCL "
            "no-follow was bypassed"
        )
        assert salt_link.exists() and not salt_link.is_symlink(), (
            "a real salt (not the symlink) must exist at the expected path"
        )
        with engine.connect() as conn:
            assert conn.execute(text("SELECT 1")).scalar() == 1
        manager.close_user_database(username)

        # And the account reopens normally with the recovered salt.
        reopened = manager.open_user_database(username, password)
        assert reopened is not None
    finally:
        victim.unlink(missing_ok=True)


def test_symlinked_data_dir_works_transparently(manager):
    """AJ4 (positive): a deployer-chosen symlinked data directory
    supports the full create/open cycle, with the artifacts landing in
    the real target directory."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    real_dir = manager.data_dir.parent / f"aj4_real_{uuid.uuid4().hex[:8]}"
    real_dir.mkdir()
    link_dir = manager.data_dir.parent / f"aj4_link_{uuid.uuid4().hex[:8]}"
    link_dir.symlink_to(real_dir, target_is_directory=True)

    manager.data_dir = link_dir  # the setter's mkdir tolerates a symlink-to-dir

    # Both live in the per-worker basetemp (tmp_path.parent), which pytest
    # only reclaims on its 3-run rotation -- and this one holds a whole
    # database tree, so remove it however the test ends.
    try:
        username = f"aj4_{uuid.uuid4().hex[:8]}"
        password = "SymlinkDirPw1!"  # noqa: S105
        engine = manager.create_user_database(username, password)
        assert engine is not None
        with engine.begin() as conn:
            conn.execute(
                text("CREATE TABLE gap_aj4 (id INTEGER PRIMARY KEY, v TEXT)")
            )
            conn.execute(text("INSERT INTO gap_aj4 VALUES (1, 'gap-AJ4')"))
        manager.close_user_database(username)

        assert (real_dir / get_user_database_filename(username)).exists(), (
            "artifacts did not land in the real directory behind the symlink"
        )
        reopened = manager.open_user_database(username, password)
        assert reopened is not None
        with reopened.connect() as conn:
            assert (
                conn.execute(text("SELECT v FROM gap_aj4")).scalar()
                == "gap-AJ4"
            )
    finally:
        manager.close_all_databases()
        link_dir.unlink(missing_ok=True)
        shutil.rmtree(real_dir, ignore_errors=True)