~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
"""Artifact permissions under hostile umasks.
Incident context (PR #5596): these files hold the user's entire encrypted
data life -- the .db (ciphertext), the .salt (key material), the WAL
sidecars (plaintext-ordering writes). The connection layer enforces
permissions through TWO different mechanisms with DIFFERENT umask
sensitivity:
* explicit ``chmod`` after the fact (``_ensure_data_dir`` -> 0o700 for
the directory; ``_best_effort_chmod(db_path, 0o600)`` after structure
creation) -- chmod is NOT umask-masked, so these hold under any umask;
* explicit modes at creation (``create_database_salt``'s
``os.open(..., 0o600)``) -- open(2) modes ARE masked by the process
umask, so a hostile umask can degrade the salt below 0o600.
This file pins what actually happens at each umask:
* Z1 realistic umasks (0o022 / 0o077 / 0o002): directory 0o700, .db and
.salt exactly 0o600, WAL/SHM sidecars owner-only, create/open/close all
clean;
* Z2 extreme umask 0o777 (probed): the salt lands at 0o000, the
subsequent salt READ inside creation raises PermissionError, and the
failure-cleanup path removes every artifact -- a clean, loud failure
with ZERO orphans (see FINDING in the round-6 summary);
* Z3 under the runner's own (fixture-default) umask: every artifact is
at least owner-only.
The whole module is marked ``serial``: ``os.umask`` is process-global, so
these tests must not share a worker process with anything else.
The whole module is ALSO marked ``nonroot``: every test below starts with
``if os.geteuid() == 0: pytest.skip(...)`` because permission bits are not
enforced for root, so under root every one of them silently no-ops. This
must be carried on the test itself -- it is not safe to rely on a CI step
happening to invoke pytest with ``--user`` or an equivalent unprivileged
runner; that coupling is invisible here and silently regresses to a no-op
the moment a lane runs ``-m serial`` (or this file directly) as root. See
the ``serial`` and ``nonroot`` marker docstrings in pyproject.toml -- a
module carrying both only needs to run once: docker-tests.yml's merged
``serial``/``nonroot`` step selects ``-m 'serial or nonroot'`` under
``--user ldruser``, so this file runs exactly once there regardless of
which marker matched it.
Round-1's cold-open file fingerprinted FAILED opens; round-5 W covered
lifecycle -- neither pinned creation-time permissions vs umask.
"""
import os
import stat
import uuid
import pytest
from sqlalchemy import text
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)")
REALISTIC_UMASKS = [0o022, 0o077, 0o002]
# ``os.umask`` is PROCESS-global, not thread- or test-local: while the
# 0o777 window below is open, ANY file created anywhere in this process
# lands at mode 0o000. That is fine in a dedicated serial run and a
# landmine under '-n auto', so the whole module opts out of the parallel
# lane (see the ``serial`` marker in pyproject.toml and the dedicated
# step in .github/workflows/docker-tests.yml).
#
# Also marked ``nonroot``: every test below self-skips under root (see
# the module docstring above). Declaring it explicitly here -- rather
# than leaning on the fact that CI's 'serial' step happens to invoke
# pytest as an unprivileged user -- makes the non-root requirement a
# property of the test, not an accident of CI wiring that a new lane
# running '-m serial' as root would silently violate.
pytestmark = [pytest.mark.serial, pytest.mark.nonroot]
@pytest.fixture
def manager(tmp_path, monkeypatch):
"""A DatabaseManager writing to an isolated data directory.
Same self-contained pattern as the round-1..5 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 _write_canary(engine, note: str) -> None:
with engine.begin() as conn:
conn.execute(
text(
"CREATE TABLE gap_z_canary (id INTEGER PRIMARY KEY, note TEXT)"
)
)
conn.execute(
text("INSERT INTO gap_z_canary (id, note) VALUES (1, :note)"),
{"note": note},
)
@pytest.mark.parametrize("umask", REALISTIC_UMASKS)
def test_realistic_umasks_keep_artifacts_owner_only(manager, umask):
"""Z1: under 0o022 / 0o077 / 0o002 the data dir is 0o700 (explicit
chmod), .db and .salt are exactly 0o600, WAL/SHM sidecars are
owner-only while open, and the full create/write/open/close cycle
works."""
if not manager.has_encryption:
pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")
if os.geteuid() == 0:
pytest.skip("permission bits are not enforced for root")
original = os.umask(umask)
try:
username = f"umask_{umask:o}_{uuid.uuid4().hex[:8]}"
password = "UmaskProbePw1!" # noqa: S105
engine = manager.create_user_database(username, password)
_write_canary(engine, f"gap-Z-{umask:o}")
db_path = manager._get_user_db_path(username)
salt_path = db_path.with_name(db_path.name.replace(".db", ".db.salt"))
assert stat.S_IMODE(os.stat(manager.data_dir).st_mode) == 0o700
assert stat.S_IMODE(os.stat(db_path).st_mode) == 0o600, (
f"db mode under umask {umask:o}"
)
assert stat.S_IMODE(os.stat(salt_path).st_mode) == 0o600, (
f"salt mode under umask {umask:o}"
)
for suffix in ("-wal", "-shm"):
sidecar = db_path.with_name(db_path.name + suffix)
if sidecar.exists():
mode = stat.S_IMODE(os.stat(sidecar).st_mode)
assert mode & 0o077 == 0, (
f"{suffix} is group/world accessible ({oct(mode)}) "
f"under umask {umask:o}"
)
# Open/close cycle clean under this umask too.
manager.close_user_database(username)
reopened = manager.open_user_database(username, password)
assert reopened is not None
with reopened.connect() as conn:
assert (
conn.execute(
text("SELECT note FROM gap_z_canary WHERE id = 1")
).scalar()
== f"gap-Z-{umask:o}"
)
finally:
os.umask(original)
def test_extreme_umask_0777_fails_loudly_with_zero_orphans(manager):
"""Z2 (ACTUAL, probed): umask 0o777 masks the salt's os.open 0o600
down to 0o000; the salt READ inside creation then raises
PermissionError, and the create-failure cleanup removes EVERY
artifact (the unusable salt included) -- a clean failure, no partial
state, and the username stays reusable once a sane umask is back.
FINDING: deployers must enforce umask <= 0o077-ish; the app fails
closed (correctly) rather than silently degrading."""
if not manager.has_encryption:
pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")
if os.geteuid() == 0:
pytest.skip("permission bits are not enforced for root")
username = f"umask777_{uuid.uuid4().hex[:8]}"
original = os.umask(0o777)
try:
with pytest.raises(PermissionError):
manager.create_user_database(username, "Umask777Pw1!") # noqa: S105
finally:
os.umask(original)
# Zero orphans: no .db, no .salt, no sidecars, nothing.
artifacts = sorted(p.name for p in manager.data_dir.iterdir())
assert artifacts == [], (
f"the umask-0o777 failure left partial artifacts: {artifacts}"
)
assert username not in manager.connections
assert username not in manager._password_verifiers
# The username is reusable under the restored umask.
engine = manager.create_user_database(username, "Umask777Pw1!") # noqa: S105
assert engine is not None
with engine.connect() as conn:
assert conn.execute(text("SELECT 1")).scalar() == 1
def test_fixture_default_umask_artifacts_owner_only(manager):
"""Z3: under whatever umask the runner itself has, every artifact is
at least owner-only (no group/world bits) and the directory is 0o700."""
if not manager.has_encryption:
pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")
if os.geteuid() == 0:
pytest.skip("permission bits are not enforced for root")
username = f"defumask_{uuid.uuid4().hex[:8]}"
engine = manager.create_user_database(username, "DefaultUmaskPw1!") # noqa: S105
_write_canary(engine, "gap-Z3")
manager.close_user_database(username)
assert stat.S_IMODE(os.stat(manager.data_dir).st_mode) == 0o700
for path in manager.data_dir.iterdir():
mode = stat.S_IMODE(os.stat(path).st_mode)
assert mode & 0o077 == 0, (
f"{path.name} is group/world accessible ({oct(mode)}) under "
"the runner's default umask"
)