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.

fix(security): restrict ldr_auth.db to owner-only permissions on creation (#6682)

LearningCircuitc548b24cd70a
diff --git a/.github/workflows/docker-tests.yml b/.github/workflows/docker-tests.yml
index 09f591d086..e5e37b7582 100644
--- a/.github/workflows/docker-tests.yml
+++ b/.github/workflows/docker-tests.yml
@@ -360,10 +360,12 @@ jobs:
# Modules this lane collects at the time of writing:
# serial tests/security/test_db_connection_umask_perms.py
# tests/security/test_db_adjacent_backup_posture.py
+ # tests/security/test_db_adjacent_auth_owner_only.py
# nonroot tests/security/test_db_connection_umask_perms.py
# tests/security/test_db_connection_create_hostile_dir.py
# tests/security/test_db_connection_readonly_hostile_fs.py
# tests/security/test_db_adjacent_backup_posture.py
+ # tests/security/test_db_adjacent_auth_owner_only.py
# DELETING ALL OF THEM EMPTIES THE LANE: pytest exits 5 ("no tests
# collected") and this step fails hard -- the same class of breakage
# as #4411, which is why the security-tests.yml comment stopped
diff --git a/changelog.d/+auth-db-owner-only.security.md b/changelog.d/+auth-db-owner-only.security.md
new file mode 100644
index 0000000000..d5c45411bc
--- /dev/null
+++ b/changelog.d/+auth-db-owner-only.security.md
@@ -0,0 +1 @@
+Security: the central `ldr_auth.db` (usernames, account creation and last-login metadata) is now created owner-only (0600) — when the database is first created, the file is pre-created owner-only and no-follow before SQLite ever touches it, so there is no window where it is briefly world-readable, and a dangling symlink planted at that path is refused. A dangling `ldr_auth.db` symlink now stops account creation with an error instead of creating the link's target. An existing `ldr_auth.db` — including one reached through a symlink — is opened as before and tightened to 0600 the next time the application opens it, whether it was left at umask-default permissions (typically 0644, readable by any local same-host account) by an older install or is reached through a symlink; no migration step is needed either way. Encrypted user databases and salt files already enforce owner-only permissions (0600) and per-user directories (0700), and additionally refuse a symlink on every open, not just at first creation — the auth database's symlink refusal is narrower, applying only when the file does not exist yet. Every chmod stays best-effort (warning on chmod-hostile filesystems) so it can never break database creation or startup.
diff --git a/src/local_deep_research/database/auth_db.py b/src/local_deep_research/database/auth_db.py
index 6a90bdfb8a..9b9cd770d9 100644
--- a/src/local_deep_research/database/auth_db.py
+++ b/src/local_deep_research/database/auth_db.py
@@ -3,6 +3,8 @@ Authentication database initialization and management.
This manages the central ldr_auth.db which only stores usernames.
"""
+import errno
+import os
import threading
from contextlib import contextmanager
from pathlib import Path
@@ -33,6 +35,66 @@ def get_auth_db_path() -> Path:
return get_data_directory() / "ldr_auth.db"
+def _best_effort_chmod(path: Path, mode: int) -> None:
+ """Tighten permissions on the auth DB file to ``mode``, never raising.
+
+ Mirrors ``encrypted_db._best_effort_chmod``: permission hardening
+ must NOT be able to break auth database creation. On filesystems
+ where chmod raises even for the owner (some Docker bind mounts and
+ network/FUSE volumes), the file stays at its umask default and the
+ downgrade is surfaced at warning level — ``ldr_auth.db`` holds
+ usernames and login metadata, so a silent group/world-readable
+ state deserves visibility.
+ """
+ try:
+ os.chmod(str(path), mode)
+ except OSError as exc:
+ # Copy out before logging: the check-sensitive-logging pre-commit
+ # hook flags any reference to the ``except`` variable itself inside
+ # a non-debug logger call (including its attributes, since e.g.
+ # e.strerror carries the same detail as str(e)) as a potential
+ # secret leak. errno/strerror are plain OS error codes/text, not
+ # sensitive, but the hook can't tell that apart -- so, like
+ # encrypted_db._db_path_is_symlinked's ``target = f"...{exc
+ # .strerror}>"``, the detail is copied into ordinary local
+ # variables first and only those are interpolated below.
+ chmod_errno = exc.errno
+ chmod_strerror = exc.strerror
+ logger.warning(
+ f"Could not set permissions {oct(mode)} on auth database "
+ f"{path}; left at filesystem default "
+ f"(errno={chmod_errno}, {chmod_strerror})"
+ )
+
+
+def _create_private_file(path: Path) -> None:
+ """Create ``path`` as an empty, owner-only regular file, never via a link.
+
+ Mirrors ``encrypted_db._create_private_file``: ``O_CREAT | O_EXCL``
+ fails when anything exists at ``path`` — including a symlink, dangling
+ or not — and ``O_NOFOLLOW`` is added where the platform has it, so a
+ planted symlink is refused rather than followed. This closes the
+ creation-race window where SQLite would otherwise create the file at
+ umask-default permissions (typically 0644) before the post-DDL chmod
+ ever runs. Callers tolerate ``FileExistsError`` — the file may already
+ exist from a previous run, in which case SQLite simply opens it and the
+ existing-file chmod paths (here and in ``_get_auth_engine``) tighten it.
+ """
+ flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, "O_NOFOLLOW", 0)
+ try:
+ fd = os.open(str(path), flags, 0o600)
+ except OSError as exc:
+ # A platform may report the symlink through O_NOFOLLOW (ELOOP)
+ # before O_EXCL gets to say EEXIST; both mean "something is
+ # already at this path", so callers see one exception type.
+ if exc.errno == errno.ELOOP:
+ raise FileExistsError(
+ errno.EEXIST, "Path is a symlink", str(path)
+ ) from exc
+ raise
+ os.close(fd)
+
+
def _get_auth_engine() -> Engine:
"""
Get or create a cached engine for the auth database.
@@ -72,6 +134,16 @@ def _get_auth_engine() -> Engine:
if not auth_db_path.exists():
init_auth_database()
+ # SECURITY: tighten permissions whenever the engine is (re)created
+ # for this path. init_auth_database() already does this for a
+ # fresh database, but this call also covers an existing file from
+ # an older install that predates owner-only creation (still 0644)
+ # — upgrading and restarting is then enough to tighten it, with no
+ # migration step. Best-effort and idempotent; this block runs at
+ # most once per process per path since the cached-engine check
+ # above short-circuits every later call for the same path.
+ _best_effort_chmod(auth_db_path, 0o600)
+
# Moderate pool — auth DB is unencrypted SQLite used by every
# authenticated request via before_request middleware.
# See ADR-0004 for pool sizing rationale.
@@ -118,6 +190,32 @@ def init_auth_database():
logger.debug(f"Ensuring auth database at {auth_db_path}")
+ # SECURITY: materialize the file ourselves, owner-only and no-follow,
+ # before handing the path to SQLite. Without this, SQLite's own lazy
+ # create below lands the file at umask-default permissions (typically
+ # 0644) for the window between that create and the post-DDL chmod
+ # further down — a same-host reader with an fd opened in that window
+ # keeps read access even after the chmod. This call is idempotent and
+ # is what init_auth_database's callers rely on for repeated/concurrent
+ # invocations (see the "IF NOT EXISTS" DDL below), so a FileExistsError
+ # from an already-present file is expected and tolerated — but only
+ # when it is a regular file. init_auth_database() only runs when
+ # _get_auth_engine() sees auth_db_path.exists() is False, which is
+ # also true for a dangling symlink, so a dangling symlink at this
+ # path is caught right here and refused. An existing database file —
+ # including one reached through a symlink to an existing target —
+ # never reaches this function at all: _get_auth_engine() opens it as
+ # before via create_engine() and tightens it with its own chmod,
+ # following the link the same way SQLite's open always has.
+ try:
+ _create_private_file(auth_db_path)
+ except FileExistsError:
+ if auth_db_path.is_symlink():
+ raise ValueError(
+ f"Auth database path {auth_db_path} is a symlink; "
+ "refusing to create or open the database there"
+ )
+
# Create the database with a temporary engine
engine = create_engine(f"sqlite:///{auth_db_path}")
@@ -131,6 +229,12 @@ def init_auth_database():
# Dispose the temporary engine
engine.dispose()
+ # SECURITY: SQLite creates the file with umask-default permissions
+ # (typically 0644), while every other DB artifact is owner-only.
+ # Tighten to owner read/write — best-effort so a chmod-hostile
+ # filesystem cannot break auth database creation.
+ _best_effort_chmod(auth_db_path, 0o600)
+
logger.debug("Auth database initialized successfully")
diff --git a/tests/security/test_db_adjacent_auth_owner_only.py b/tests/security/test_db_adjacent_auth_owner_only.py
new file mode 100644
index 0000000000..94baf0f90e
--- /dev/null
+++ b/tests/security/test_db_adjacent_auth_owner_only.py
@@ -0,0 +1,230 @@
+"""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]
+
+
+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"
+ )