Keep the stored bytes when a sign-out unbinds a row whose data is not text

KeyStore::get reads a data column that is a BLOB, or TEXT of bytes that are no
UTF-8, as text with the odd bytes replaced. The sign-out's unbind step puts that
text back with the binding removed; REBIND (data = ?3) matches nothing for such
a row, so put fell through to the full write: the lossy string replaced the
original bytes (invalid UTF-8 became U+FFFD, a BLOB became TEXT), and for a
TeamSpeak row the replaced text is no identity, so check_teamspeak refused it
and the sign-out could never finish for that row.

- put() now tries a second statement when REBIND changed nothing:
  rebind_stored_bytes reads the stored data raw and, if it reads (lossy_text) as
  exactly the blob's JSON, updates only the four binding columns by (kind, uid)
  (REBIND_BYTES) and returns without check_teamspeak, so the original bytes are
  kept and the TeamSpeak case unsticks. Any other JSON is a new identity for the
  uid, checked and stored as before.
- The doc sentences at the top of keystore.rs (what a binding-only put and a
  non-text data column mean) and docs/architecture.md are narrowed to what holds:
  the bytes are kept for the text get returned, any other text replaces them.

Tests: the gate's probe (a bound voelin row with data x'7b2276223aff317d', two
future-kind rows, one a BLOB and one TEXT of no UTF-8, and two TeamSpeak BLOB
rows, one of a good identity and one of garbage): sign_out(Auto) and
sign_out(Forget) are Ok and every data value is byte-identical afterwards; a put
of what get read changes the binding and keeps the bytes; a put of other data is
checked and replaces them. The three tests fail on the previous code.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Sonnet 5.5 committed 2026-10-20 14:35:46 +03:00
1 parent 0ac4095143
commit 670ed9f29b
3 files changed
+236 -15

No files matched your search

+76 -13
View File
@@ -20,9 +20,12 @@
//! (it could only have been damaged outside this crate) lists and reads with
//! 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.
//! that `data`, as [`KeyStore::get`] reads it) writes the four binding columns,
//! does not look at `data` again and does not rewrite it. That is how
//! [`KeyStore::sign_out`] unbinds a device-only secret: a row whose `data` was
//! damaged outside this crate (JSON that is no identity, a BLOB, bytes that are
//! no UTF-8) does not keep the sign-out from unbinding it, and its `data` is
//! kept byte for byte.
//!
//! A row that is damaged elsewhere never fails a call that is not about it. A
//! row whose binding columns cannot be read (`synced = 2`, an `account` that is
@@ -35,11 +38,15 @@
//! and with it the deletion of the key the user asked to forget. Unbound is the
//! reading that never deletes on a guess: a sign-out neither deletes such a row
//! as a synced copy nor writes it, it stays as it is until a `put` of it writes
//! four good binding columns. A `data` column that is not text is read as text
//! with the odd bytes replaced, so that a record that cannot be read is deleted
//! by the sign-out, as the trait says, instead of its read failing; a row whose
//! `kind` or `uid` is not text cannot be addressed, and the list leaves it out
//! with a warning.
//! four good binding columns. A `data` column that is not text (a BLOB, or text
//! of bytes that are no UTF-8) is read as text with the odd bytes replaced, so
//! that a record that cannot be read is deleted by the sign-out, as the trait
//! says, instead of its read failing. A [`KeyStore::put`] of the text that
//! [`KeyStore::get`] returned for such a row is taken for the data that is
//! there: it changes the binding and keeps the stored bytes, where any other
//! text replaces them (a TeamSpeak blob still has to be an identity). A row
//! whose `kind` or `uid` is not text cannot be addressed, and the list leaves it
//! out with a warning.
//!
//! The rows are private keys, so a removed row is not left behind in the file:
//! every connection sets `secure_delete`, which makes SQLite overwrite what it
@@ -75,13 +82,13 @@ use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Mutex, MutexGuard, PoisonError};
use rusqlite::params;
use rusqlite::types::ValueRef;
use rusqlite::{OptionalExtension, params};
use tsclientlib::Identity;
use voelin_identity::migrate::SELECT_ACCOUNT_COLUMNS;
use voelin_identity::{
AccountBinding, BlobError, DeviceKeyChoice, IdentityBlob, IdentityInfo, KIND_ACCOUNT,
KIND_TEAMSPEAK, KIND_VOELIN, KeyStore, KeyStoreError, SignOut, Uid,
AccountBinding, BindingColumns, BlobError, DeviceKeyChoice, IdentityBlob, IdentityInfo,
KIND_ACCOUNT, KIND_TEAMSPEAK, KIND_VOELIN, KeyStore, KeyStoreError, SignOut, Uid,
};
use crate::{Error, Result, Store};
@@ -101,6 +108,12 @@ const REBIND: &str = "UPDATE identities
SET account = ?4, integration = ?5, integration_rev = ?6, synced = ?7
WHERE kind = ?1 AND uid = ?2 AND data = ?3";
/// [`REBIND`] for a row whose `data` is not text and reads, as [`lossy_text`]
/// reads it, as the blob's: the same four columns, and the stored bytes stay.
const REBIND_BYTES: &str = "UPDATE identities
SET account = ?3, integration = ?4, integration_rev = ?5, synced = ?6
WHERE kind = ?1 AND uid = ?2";
/// 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.
@@ -194,7 +207,10 @@ impl KeyStore for SqliteKeyStore {
}
// 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.
// crate, and a sign-out still has to unbind it. "Holds this data" is
// meant as `get` reads it: for a `data` that is not text, `get` hands out
// the text with the odd bytes replaced, `REBIND` cannot match that, and
// the second statement takes the blob for the data that is there.
let rebound = store
.db
.prepare_cached(REBIND)
@@ -209,7 +225,7 @@ impl KeyStore for SqliteKeyStore {
columns.synced,
])
.map_err(backend)?;
if rebound > 0 {
if rebound > 0 || rebind_stored_bytes(&store, blob, &columns)? {
return Ok(());
}
if blob.kind == KIND_TEAMSPEAK {
@@ -312,6 +328,53 @@ impl KeyStore for SignOutPass<'_> {
}
}
/// Change only the binding of the row of the blob's `(kind, uid)` if its `data`
/// is not text but reads as exactly the blob's JSON, and say whether it did.
///
/// [`KeyStore::get`] reads a `data` that is a BLOB, or TEXT of bytes that are no
/// UTF-8, as text with the odd bytes replaced ([`lossy_text`]). The blob that a
/// sign-out puts back to unbind such a row carries that text, so `REBIND` finds
/// nothing, and writing the blob would replace the stored bytes with the
/// replaced text (and refuse a TeamSpeak row, whose replaced text is no
/// identity, for ever). The text `get` handed out is taken for the data that is
/// there, and the bytes are kept.
///
/// It is called after `REBIND` changed nothing, which is why the stored value is
/// known not to be exactly the blob's text. Any other JSON does not match, and
/// is a new identity for the uid as ever.
fn rebind_stored_bytes(
store: &Store,
blob: &IdentityBlob,
columns: &BindingColumns,
) -> KsResult<bool> {
let reads_as_blob = store
.db
.prepare_cached("SELECT data FROM identities WHERE kind = ?1 AND uid = ?2")
.map_err(backend)?
.query_row(params![blob.kind, blob.uid], |row| {
Ok(lossy_text(row.get_ref(0)?).is_some_and(|text| text == blob.json))
})
.optional()
.map_err(backend)?;
if reads_as_blob != Some(true) {
return Ok(false);
}
store
.db
.prepare_cached(REBIND_BYTES)
.map_err(backend)?
.execute(params![
blob.kind,
blob.uid,
columns.account,
columns.integration,
columns.rev,
columns.synced,
])
.map_err(backend)?;
Ok(true)
}
/// A TeamSpeak blob is stored only if it is what its kind says: the JSON of a
/// `tsproto::Identity` whose key has the uid the blob names.
///