Unbind a bound TeamSpeak row without reading its JSON again

KeyStore::sign_out unbinds a device-only secret by get() and put() of the
same blob with the binding cleared. put() ran check_teamspeak on the data
first, so a bound row whose JSON had been damaged outside this crate made the
put fail on every call: the account record was never reached, and with
DeviceKeyChoice::Forget the order of the sign-out kept the named key from
being deleted by the retry loop the module doc promised was unnecessary.

put() now first tries a statement that rewrites only the four binding columns
of the row of the same (kind, uid) whose data equals the blob's, and returns
if it changed a row. Anything else, a new row or other data, goes through the
check and the upsert as before: damage is not replaced by more damage, a
stranger's key still cannot take over a uid, and another kind's put of a held
uid is still refused. Nothing but the binding of the row is written, so the
name, origin, default flag and creation time stay as they were.

Tests: sign_out (Auto, and Forget with and without bound_existing_key)
completes on a damaged bound row, deletes the record, unbinds the row and
leaves its data as it was; the put cases above; each fails on the previous
commit. The scratch directories of the test file are unique per call now, as
two tests that pass the same name run side by side.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Sonnet 5.5 committed 2026-10-20 06:21:10 +03:00
1 parent f3345bccf2
commit 71edc7cc2f
2 files changed
+170 -7

No files matched your search

+36 -6
View File
@@ -18,8 +18,11 @@
//! counter in `data`, as [`Store::identities`] always did, and the level of a
//! blob a caller puts is ignored. A TeamSpeak row whose JSON cannot be read
//! (it could only have been damaged outside this crate) lists and reads with
//! level 0 instead of failing the whole list, so that signing out is never
//! blocked by it.
//! level 0 instead of failing the whole list, and a [`KeyStore::put`] that only
//! changes the binding of a row (the row of that `(kind, uid)` already holds
//! exactly that `data`) writes the four binding columns and does not look at
//! `data` again. That is how [`KeyStore::sign_out`] unbinds a device-only
//! secret, so a damaged row never blocks it.
//!
//! A TeamSpeak blob that is refused says why in words that carry none of its
//! input: its `data` is the identity's private key, and an error reaches log
@@ -46,6 +49,12 @@ const LIST: &str = "SELECT kind, uid, CASE WHEN kind = 'teamspeak' THEN data END
account, integration, integration_rev, synced
FROM identities ORDER BY kind, uid";
/// Replace the binding of the row of this `(kind, uid)` if its `data` is
/// exactly the blob's: nothing else of the row is written.
const REBIND: &str = "UPDATE identities
SET account = ?4, integration = ?5, integration_rev = ?6, synced = ?7
WHERE kind = ?1 AND uid = ?2 AND data = ?3";
/// Insert the blob, or replace the row of the same uid if it is of the same
/// kind. A row of another kind is left alone: the statement then changes
/// nothing, which `put` reports.
@@ -68,7 +77,7 @@ const PUT: &str = "INSERT INTO identities
/// access to the TeamSpeak methods of the same connection.
///
/// [`KeyStore::sign_out`] and the rest of the trait's provided methods work as
/// `voelin-identity` defines them; each call here is one statement.
/// `voelin-identity` defines them, on top of the five methods here.
pub struct SqliteKeyStore {
store: Mutex<Store>,
}
@@ -98,7 +107,8 @@ impl SqliteKeyStore {
fn lock(&self) -> MutexGuard<'_, Store> {
// A panic in another thread cannot leave the table half-updated: every
// method here is one statement.
// write here is one statement, and `put` tries a second one only when
// the first changed nothing.
self.store.lock().unwrap_or_else(PoisonError::into_inner)
}
}
@@ -121,11 +131,31 @@ impl KeyStore for SqliteKeyStore {
}
fn put(&self, blob: &IdentityBlob) -> KsResult<()> {
let columns = blob.binding_columns()?;
let store = self.lock();
// A row that already holds this data changes its binding and nothing
// else, and is not checked again: it may have been damaged outside this
// crate, and a sign-out still has to unbind it.
let rebound = store
.db
.prepare_cached(REBIND)
.map_err(backend)?
.execute(params![
blob.kind,
blob.uid,
blob.json,
columns.account,
columns.integration,
columns.rev,
columns.synced,
])
.map_err(backend)?;
if rebound > 0 {
return Ok(());
}
if blob.kind == KIND_TEAMSPEAK {
check_teamspeak(blob)?;
}
let columns = blob.binding_columns()?;
let store = self.lock();
let changed = store
.db
.prepare_cached(PUT)