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.

8.0 KB
Raw Blame
"""Creation-floor semantics of the effective SQLCipher KDF (round-9, C4).

Attack class: at-rest work-factor downgrade via environment. The KDF
iteration count is derived from the environment at open time
(``database/sqlcipher_utils.py:437-448``) and is NOT stored with the
database, so a hostile or fat-fingered ``LDR_DB_CONFIG_KDF_ITERATIONS``
(or its deprecated ``LDR_DB_KDF_ITERATIONS`` alias) is the only lever an
operator-side misconfiguration has against key-derivation strength.

The clamp is a three-layer pipeline, and this file pins the END-TO-END
semantics of ``get_sqlcipher_settings()["kdf_iterations"]``:

1. Registry (``settings/env_definitions/db_config.py:70-77``): integer
   values outside ``[1000, 1000000]`` raise a ``ValueError`` subclass
   (``EnvironmentValueRangeError``) that ``SettingsRegistry.get`` catches,
   substituting the caller default (256000). Non-integers like ``abc``
   never reach that raise -- ``IntegerSetting._convert_value``
   (``env_settings.py:167-175``) catches the ``int()`` conversion
   failure itself and returns the setting's default directly.
2. Floor window (``sqlcipher_utils.py:441-448``): values outside
   ``[floor, 1000000]`` are REPLACED with the 256000 default -- the code
   resets to default, it does NOT clamp up to the floor.
3. Floor selection (``sqlcipher_utils.py:370-392``): 100000 in
   production; 1 only under ``PYTEST_CURRENT_TEST`` presence or a
   truthy ``LDR_TEST_MODE`` (explicitly parsed -- ``0``/``false`` do not
   relax).

Recon correction (round-9 triage guessed "env=1 -> the 100000 floor"):
empirically, ``LDR_DB_CONFIG_KDF_ITERATIONS=1`` without test markers
yields **256000**, not 100000 -- layer 1 rejects 1 (below the registry
minimum of 1000) before the floor window ever sees it, and sub-floor
in-registry values (e.g. 50000) hit layer 2's reset-to-default. Either
way the security invariant below holds, which is what these tests pin:

    without test-mode markers, the effective iteration count is NEVER
    below 100000, and no environment value can make settings resolution
    crash.

Load-bearing test mechanics: ``PYTEST_CURRENT_TEST`` must be cleared in
the TEST BODY, not in a setup-phase fixture -- pytest re-arms it at the
start of every phase (setup -> call), so a fixture-level ``delenv`` is
silently undone before the assertions run (same pattern as
``tests/database/test_kdf_iterations.py``).

The env-var matrix of ``_get_min_kdf_iterations`` itself is already
pinned in ``tests/database/test_kdf_iterations.py`` (not duplicated
here); this file covers the composed clamp plus the test-mode carve-out
at the settings level, so a regression that accidentally drops the
production floor or lets a weak value through any layer fails loudly.
"""

import pytest

_ENV_KEYS = (
    "PYTEST_CURRENT_TEST",
    "LDR_TEST_MODE",
    "LDR_DB_CONFIG_KDF_ITERATIONS",
    "LDR_DB_KDF_ITERATIONS",
)


def _effective_kdf() -> int:
    from local_deep_research.database.sqlcipher_utils import (
        get_sqlcipher_settings,
    )

    return get_sqlcipher_settings()["kdf_iterations"]


def _production_posture(monkeypatch):
    """Clear every test marker and KDF knob -- in the body, see module
    docstring. Returns the monkeypatch for further ``setenv`` calls."""
    for key in _ENV_KEYS:
        monkeypatch.delenv(key, raising=False)
    return monkeypatch


class TestAttemptedDowngradeWithoutTestMarkers:
    """A weak configured KDF must not survive without test-mode markers."""

    def test_env_of_one_is_rejected_not_floored(self, monkeypatch):
        """``LDR_DB_CONFIG_KDF_ITERATIONS=1`` without test markers yields
        the 256000 default -- not 1, and not the 100000 floor (the
        registry's 1000 minimum rejects the value before the floor
        window applies)."""
        env = _production_posture(monkeypatch)
        env.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "1")
        assert _effective_kdf() == 256000

    def test_sub_floor_in_registry_range_resets_to_default(self, monkeypatch):
        """50000 parses cleanly and passes the registry, but sits below
        the production floor: the clamp resets it to 256000 rather than
        clamping UP to 100000."""
        env = _production_posture(monkeypatch)
        env.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "50000")
        assert _effective_kdf() == 256000

    def test_floor_boundary_is_exact(self, monkeypatch):
        """99999 (one below the floor) is reset to 256000; 100000 (the
        floor itself) passes through unchanged -- pin the boundary so an
        off-by-one edit to the floor constant trips this test."""
        env = _production_posture(monkeypatch)
        env.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "99999")
        assert _effective_kdf() == 256000
        env.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "100000")
        assert _effective_kdf() == 100000


class TestAbsurdValuesNeverCrashNeverWeaken:
    """Absurd or malformed values resolve to the 256000 default without
    raising, and never to a below-floor count."""

    @pytest.mark.parametrize(
        "raw",
        ["0", "-5", "1000000000000", "abc", "", "1.5"],
        ids=[
            "zero",
            "negative",
            "ten-to-the-twelve",
            "non-numeric",
            "empty",
            "float-literal",
        ],
    )
    def test_absurd_value_resolves_to_default(self, raw, monkeypatch):
        env = _production_posture(monkeypatch)
        env.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", raw)
        assert _effective_kdf() == 256000


class TestTestModeCarveOut:
    """The documented test-mode relaxation, pinned so that production
    regressions which drop the floor (or widen the carve-out beyond
    these markers) are caught."""

    def test_ldr_test_mode_accepts_weak_kdf(self, monkeypatch):
        """With ``LDR_TEST_MODE=1`` (and pytest's own marker cleared),
        the floor drops to 1 and an in-registry 1000 is accepted -- the
        sanctioned fast-KDF path used across the test suite."""
        env = _production_posture(monkeypatch)
        env.setenv("LDR_TEST_MODE", "1")
        env.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "1000")
        assert _effective_kdf() == 1000

    @pytest.mark.parametrize("falsey", ["0", "false", "False", "no"])
    def test_explicit_false_test_mode_does_not_relax(self, falsey, monkeypatch):
        """``LDR_TEST_MODE`` is boolean-parsed: an explicit falsey value
        leaves the production floor in force, so a weak 1000 resets to
        256000."""
        env = _production_posture(monkeypatch)
        env.setenv("LDR_TEST_MODE", falsey)
        env.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "1000")
        assert _effective_kdf() == 256000

    def test_deprecated_alias_cannot_bypass_the_clamp(self, monkeypatch):
        """The deprecated ``LDR_DB_KDF_ITERATIONS`` alias feeds the same
        pipeline: under test mode it is honoured (1000 accepted), so
        operators get no bypass the canonical name lacks."""
        env = _production_posture(monkeypatch)
        env.setenv("LDR_TEST_MODE", "1")
        env.setenv("LDR_DB_KDF_ITERATIONS", "1000")
        assert _effective_kdf() == 1000

    def test_deprecated_alias_is_clamped_without_test_mode(self, monkeypatch):
        """Without test-mode markers, the deprecated alias goes through the
        same production clamp as the canonical name: a sub-floor value
        (50000) resets to the 256000 default rather than being honoured
        raw. A mutant that reads the alias straight off the environment
        and skips the clamp entirely would return 50000 here."""
        env = _production_posture(monkeypatch)
        env.setenv("LDR_DB_KDF_ITERATIONS", "50000")
        assert _effective_kdf() == 256000

    def test_test_mode_still_rejects_below_registry_minimum(self, monkeypatch):
        """Even in test mode the registry minimum (1000) holds: 1 never
        reaches the floor, so the weakest effective configured KDF via
        env is 1000, not 1."""
        env = _production_posture(monkeypatch)
        env.setenv("LDR_TEST_MODE", "1")
        env.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "1")
        assert _effective_kdf() == 256000