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.

test(security): assert final-password engines are keyed, not identical, in the rekey stress test (#6873)

LearningCircuit7bcacfa97446
diff --git a/tests/security/test_db_connection_change_password_race.py b/tests/security/test_db_connection_change_password_race.py
index 9072279951..7c7b50762b 100644
--- a/tests/security/test_db_connection_change_password_race.py
+++ b/tests/security/test_db_connection_change_password_race.py
@@ -305,9 +305,15 @@ def test_stress_change_password_chain_vs_open_hammers(manager):
Invariants after stabilization:
* every engine obtained with the ORIGINAL password during the run is
dead (its key was rekeyed away and can never return);
- * every engine obtained with the FINAL password is exactly the one
- surviving current-key engine -- it can only have been handed out
- AFTER the final rekey landed, never a mis-keyed duplicate;
+ * every engine obtained with the FINAL password is keyed with the
+ final password: disposed first (to force a fresh connection through
+ its creator, where SQLCipher key verification runs), it still opens
+ and reads the canary. More than one such engine can legitimately
+ exist -- a final-password engine cold-opened between the last
+ rekey's commit and change_password's trailing close_user_database is
+ evicted by that close (it evicts by username), and a later open
+ caches a new one -- so engine identity is NOT the invariant, the
+ key is;
* no intermediate or original password can open at the end;
* the final password opens and reads the canary written at creation;
* the engine/verifier PAIRING invariant, sampled under the connections
@@ -410,7 +416,7 @@ def test_stress_change_password_chain_vs_open_hammers(manager):
if not grabbed:
warnings.warn(
"engine-verdict loop not exercised: neither hammer grabbed an "
- f"engine across {hammer_attempts} attempts, so no stale/surviving "
+ f"engine across {hammer_attempts} attempts, so no stale/mis-keyed "
"engine verdict was checked. The end-state assertions still ran.",
stacklevel=1,
)
@@ -424,29 +430,48 @@ def test_stress_change_password_chain_vs_open_hammers(manager):
# Stabilized end-state, part 2: the final password opens and reads the
# data written before any rekey happened. The engine it gets back is
- # the one surviving current-key engine (or, if no hammer grabbed one
- # after the last rekey, the freshly cold-opened one we just created).
+ # whichever final-password engine is currently cached (or, if no
+ # hammer grabbed one after the last rekey, the freshly cold-opened one
+ # we just created) -- cache identity, not "the surviving engine": the
+ # verdict loop below allows more than one final-password engine to be
+ # legitimate.
reopened = manager.open_user_database(username, final_password)
assert reopened is not None
surviving = manager.connections.get(username)
assert surviving is reopened, "the final-password engine must be cached"
+ # Dispose first so the read goes through the engine's creator, where the
+ # key is applied: a pooled connection that was rekeyed in place would
+ # otherwise read the canary even if the engine's creator derives an
+ # earlier password.
+ reopened.dispose()
with reopened.connect() as conn:
note = conn.execute(
text("SELECT note FROM gap_b_canary WHERE id = 1")
).scalar()
assert note == "gap-B-stress", "rekey chain must preserve data"
- # Engine verdicts, computed AFTER stabilization so the surviving
- # current-key engine is known:
+ # Engine verdicts, computed AFTER stabilization so the file's key is
+ # settled on the final password:
# * 'original': every engine handed out for the ORIGINAL password
# must be dead -- its key was rekeyed away and can never return.
- # * 'final': a final-password engine is legitimate ONLY once the
- # final rekey has landed, and there is exactly one such engine:
- # the surviving cached engine. Anything else -- a second engine
- # object, or one handed out before the final rekey completed --
- # would mean a mis-keyed engine escaped. (A final-password open
- # cannot succeed before the final rekey: the file is keyed with
- # an earlier password and SQLCipher verification fails.)
+ # * 'final': every engine handed out for the FINAL password must be
+ # keyed with the final password. Each is disposed first, forcing a
+ # fresh connection through its creator -- where SQLCipher key
+ # verification actually runs -- and only then must open and read
+ # the canary. Without the dispose, a stale pooled connection could
+ # pass both probes vacuously: SELECT 1 never touches an encrypted
+ # page, and an engine rekeyed in place keeps a live connection
+ # that already carries the new key even though the engine's own
+ # creator still derives an earlier password. Several distinct
+ # final-password engines can legitimately exist: one cold-opened
+ # after the last rekey commits but before change_password's
+ # trailing close_user_database is evicted by that close (it
+ # evicts by username), and a later open caches a new one. Its
+ # creator still derives the final key, so it is not mis-keyed.
+ # Once disposed, an engine keyed with any earlier password fails
+ # SQLCipher verification against the rekeyed file and trips this.
+ # (A final-password open cannot succeed before the final rekey: the
+ # file is keyed with an earlier password and verification fails.)
for index, (label, engine) in enumerate(grabbed):
if label == "original":
usable, detail = _engine_usable(engine)
@@ -456,8 +481,25 @@ def test_stress_change_password_chain_vs_open_hammers(manager):
"engine escaped the eviction"
)
else:
- assert engine is surviving, (
- f"engine #{index} (hammer={label}) is not the surviving "
- f"current-key engine -- a mis-keyed engine was handed out "
- "for the final password"
+ # Force a new connection through the engine's creator closure
+ # -- where SQLCipher key verification runs -- before probing
+ # it. Without this, an engine rekeyed in place (its live
+ # pooled connection carries the new key even though its
+ # creator still derives an earlier one) would pass both
+ # probes on the stale connection.
+ engine.dispose()
+ usable, detail = _engine_usable(engine)
+ assert usable, (
+ f"engine #{index} (hammer={label}) cannot connect with its "
+ f"key after the rekey chain completed ({detail}) -- a "
+ "mis-keyed engine was handed out for the final password"
+ )
+ with engine.connect() as conn:
+ note = conn.execute(
+ text("SELECT note FROM gap_b_canary WHERE id = 1")
+ ).scalar()
+ assert note == "gap-B-stress", (
+ f"engine #{index} (hammer={label}) does not read the canary "
+ f"({note!r}) -- a mis-keyed engine was handed out for the "
+ "final password"
)