~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Owner-only permissions for the central auth database (round-9, C1).
Attack class: sensitive-file permission gap / local information
disclosure. ``ldr_auth.db`` is created by ``init_auth_database``
(``database/auth_db.py``) through a bare ``create_engine`` — SQLite
creates the file with umask-default permissions (0644 under the common
022), while every other database artifact in the data directory is
tightened to owner-only (encrypted per-user DBs and salts 0600; the
per-user encrypted-database, backup and RAG-cache directories 0700 —
the top-level data directory itself is left at its umask default). The
file holds usernames plus
account creation / last-login metadata: not credentials, but an account
enumeration and user-census surface for any local same-host reader.
These tests pin that the auth DB file lands exactly 0600 under a
realistic umask — both on the direct ``init_auth_database()`` path and
the ensure-through-engine path (``_get_auth_engine`` initializes the
file when missing) — and that auth flows keep working against the
tightened file (user insert + lookup roundtrip through
``auth_db_session``, the same query shape ``DatabaseManager.user_exists``
runs).
Whole module ``serial`` (mutates the process-global umask) and
``nonroot`` (permission bits are not enforced for root), matching
``tests/security/test_db_connection_umask_perms.py`` conventions.
"""
import os
import stat
from pathlib import Path
from unittest.mock import Mock, patch
import pytest
pytestmark = [pytest.mark.serial, pytest.mark.nonroot]
@pytest.fixture
def auth_db_at(tmp_path):
"""Point the auth DB at an isolated path; returns the file path."""
def _point() -> Path:
db_path = tmp_path / "ldr_auth.db"
from local_deep_research.database import auth_db
# Drop any cached engine so each call re-ensures the file.
auth_db.dispose_auth_engine()
return db_path
return _point
def _auth_roundtrip(db_path: Path) -> bool:
"""Insert and re-query a user through the real auth session path."""
from local_deep_research.database import auth_db
from local_deep_research.database.models.auth import User
with patch.object(auth_db, "get_auth_db_path", return_value=db_path):
auth_db.dispose_auth_engine()
with auth_db.auth_db_session() as session:
session.add(User(username="owner_only_probe"))
session.commit()
found = (
session.query(User)
.filter_by(username="owner_only_probe")
.first()
)
auth_db.dispose_auth_engine()
return found is not None and found.username == "owner_only_probe"
class TestAuthDbOwnerOnlyPermissions:
def test_init_auth_database_file_is_owner_only_under_umask_022(
self, auth_db_at
):
"""Fresh creation under umask 022 lands exactly 0600 (SQLite
creates the file umask-default — 0644 — so an explicit chmod is
required), and auth flows still work on the tightened file."""
if os.geteuid() == 0:
pytest.skip("permission bits are not enforced for root")
from local_deep_research.database import auth_db
db_path = auth_db_at()
original = os.umask(0o022)
try:
with patch.object(
auth_db, "get_auth_db_path", return_value=db_path
):
auth_db.init_auth_database()
finally:
os.umask(original)
assert stat.S_IMODE(os.stat(db_path).st_mode) == 0o600, (
"ldr_auth.db is not owner-only after init — local readers can "
"enumerate usernames and login metadata"
)
assert _auth_roundtrip(db_path), "auth roundtrip broke on the 0600 file"
def test_get_auth_engine_ensures_owner_only_file(self, auth_db_at):
"""The engine path (``_get_auth_engine`` initializes a missing
file) also lands exactly 0600 under umask 022."""
if os.geteuid() == 0:
pytest.skip("permission bits are not enforced for root")
from local_deep_research.database import auth_db
db_path = auth_db_at()
assert not db_path.exists()
original = os.umask(0o022)
try:
with patch.object(
auth_db, "get_auth_db_path", return_value=db_path
):
engine = auth_db._get_auth_engine()
assert engine is not None
finally:
os.umask(original)
auth_db.dispose_auth_engine()
assert stat.S_IMODE(os.stat(db_path).st_mode) == 0o600
def test_existing_0644_file_tightened_when_engine_created(self, auth_db_at):
"""A pre-existing 0644 ``ldr_auth.db`` — carried over from an
install that predates owner-only creation — is tightened to 0600
the first time the auth engine is (re)created for that path, with
no migration step required (``_get_auth_engine`` re-asserts the
permission whenever it (re)creates the engine, not only when it
has to call ``init_auth_database`` for a missing file)."""
if os.geteuid() == 0:
pytest.skip("permission bits are not enforced for root")
from local_deep_research.database import auth_db
db_path = auth_db_at()
db_path.touch()
os.chmod(db_path, 0o644)
assert stat.S_IMODE(os.stat(db_path).st_mode) == 0o644
try:
with patch.object(
auth_db, "get_auth_db_path", return_value=db_path
):
engine = auth_db._get_auth_engine()
assert engine is not None
finally:
auth_db.dispose_auth_engine()
assert stat.S_IMODE(os.stat(db_path).st_mode) == 0o600, (
"a pre-existing 0644 ldr_auth.db from an older install is not "
"tightened on upgrade — it stays readable by any local account "
"forever"
)
def test_chmod_failure_does_not_raise(self, auth_db_at, monkeypatch):
"""Permission hardening must never break auth database creation
or engine setup. On filesystems where chmod raises even for the
owner (some Docker bind mounts and network/FUSE volumes), the
engine must still be created and usable, at whatever permissions
the filesystem left the file.
With chmod patched to always fail, the only thing that can still
land the file at 0600 is the O_EXCL owner-only pre-create in
``_create_private_file`` — so this pins that path under a
realistic umask 022 rather than only checking the file exists."""
if os.geteuid() == 0:
pytest.skip("permission bits are not enforced for root")
from local_deep_research.database import auth_db
db_path = auth_db_at()
assert not db_path.exists()
monkeypatch.setattr(
auth_db.os,
"chmod",
Mock(side_effect=OSError(13, "Permission denied")),
)
original = os.umask(0o022)
try:
with patch.object(
auth_db, "get_auth_db_path", return_value=db_path
):
engine = auth_db._get_auth_engine()
assert engine is not None
assert db_path.exists(), (
"chmod failing must not prevent the database file "
"from being created"
)
finally:
os.umask(original)
auth_db.dispose_auth_engine()
assert stat.S_IMODE(os.stat(db_path).st_mode) == 0o600, (
"chmod is patched to fail, so only the O_EXCL owner-only "
"pre-create can have produced 0600 here; anything else means "
"the file was left readable at the umask default (0644 "
"under 022)"
)
def test_dangling_symlink_is_refused_on_first_creation(self, auth_db_at):
"""A dangling ``ldr_auth.db`` symlink — nothing exists at its
target yet — must be refused rather than silently followed:
creating the database there would otherwise materialize a new
file at whatever the link points to, planted by whoever controls
that symlink."""
from local_deep_research.database import auth_db
db_path = auth_db_at()
target = db_path.parent / "planted-target.db"
assert not target.exists()
db_path.symlink_to(target)
assert db_path.is_symlink()
assert not target.exists()
try:
with patch.object(
auth_db, "get_auth_db_path", return_value=db_path
):
with pytest.raises(ValueError):
auth_db._get_auth_engine()
finally:
auth_db.dispose_auth_engine()
assert not target.exists(), (
"the dangling symlink's target must not be created — the "
"refusal has to happen before SQLite (or the owner-only "
"pre-create) ever materializes a file there"
)