store: persist per-peer ratchet session state in the SQLCipher store
All checks were successful
android-ci / android (pull_request) Successful in 9m3s
ci / test (pull_request) Successful in 4m11s
ci / sdk (pull_request) Successful in 59s

New additive ratchet_sessions table (friend_id PK -> friends(id) ON DELETE
CASCADE, state BLOB, updated_at), plus load/save/clear_ratchet on Store. The
blob is the opaque versioned RatchetState::to_bytes output; the store never
parses or re-serializes it. wipe_messages deliberately keeps sessions: wiping
history is not a session reset.
This commit is contained in:
agent 2026-09-16 18:30:32 -04:00
commit 34c257dab5
No known key found for this signature in database
2 changed files with 262 additions and 0 deletions

View file

@ -440,3 +440,103 @@ fn missing_env_empty_secret_is_rejected() {
let err = onionwire::resolve_store_passphrase(|_| Ok(String::new())).unwrap_err();
assert!(err.to_string().contains("empty"), "got {err}");
}
/// A stand-in for `RatchetState::to_bytes()`: version byte then secrets. The
/// store must treat it as opaque, so the exact bytes are the contract.
fn ratchet_blob(tag: u8) -> Vec<u8> {
let mut blob = Vec::with_capacity(64);
blob.push(1);
blob.extend_from_slice(&[tag; 63]);
blob
}
#[test]
fn ratchet_save_load_round_trips_exact_bytes() {
let dir = tempfile::tempdir().expect("tempdir");
let store = Store::open_at_with_passphrase(dir.path(), "ratchet-pass").expect("open");
store
.upsert_friend(&pk(1), "a.onion", None)
.expect("friend");
let blob = ratchet_blob(0x11);
store.save_ratchet(&pk(1), &blob).expect("save");
assert_eq!(
store.load_ratchet(&pk(1)).expect("load"),
Some(blob.clone())
);
drop(store);
assert!(
!store_contains(dir.path(), &blob),
"ratchet secrets must not sit in the db file in the clear"
);
}
#[test]
fn ratchet_load_for_friend_without_a_row_is_none() {
let dir = tempfile::tempdir().expect("tempdir");
let store = Store::open_at_with_passphrase(dir.path(), "ratchet-pass").expect("open");
store
.upsert_friend(&pk(1), "a.onion", None)
.expect("friend");
assert_eq!(store.load_ratchet(&pk(1)).expect("known friend"), None);
assert_eq!(
store.load_ratchet(&pk(2)).expect("unknown friend"),
None,
"an unknown peer is not an error, just no session"
);
}
#[test]
fn ratchet_sessions_are_per_friend() {
let dir = tempfile::tempdir().expect("tempdir");
let store = Store::open_at_with_passphrase(dir.path(), "ratchet-pass").expect("open");
store
.upsert_friend(&pk(1), "a.onion", None)
.expect("friend a");
store
.upsert_friend(&pk(2), "b.onion", None)
.expect("friend b");
let a = ratchet_blob(0xaa);
let b = ratchet_blob(0xbb);
store.save_ratchet(&pk(1), &a).expect("save a");
store.save_ratchet(&pk(2), &b).expect("save b");
assert_eq!(store.load_ratchet(&pk(1)).unwrap(), Some(a.clone()));
assert_eq!(store.load_ratchet(&pk(2)).unwrap(), Some(b.clone()));
store.clear_ratchet(&pk(1)).expect("clear a");
assert_eq!(store.load_ratchet(&pk(1)).unwrap(), None);
assert_eq!(store.load_ratchet(&pk(2)).unwrap(), Some(b));
}
#[test]
fn ratchet_clear_on_missing_row_is_a_no_op() {
let dir = tempfile::tempdir().expect("tempdir");
let store = Store::open_at_with_passphrase(dir.path(), "ratchet-pass").expect("open");
store
.upsert_friend(&pk(1), "a.onion", None)
.expect("friend");
store.clear_ratchet(&pk(1)).expect("no row to clear");
store
.clear_ratchet(&pk(9))
.expect("unknown friend is not an error");
let blob = ratchet_blob(0x22);
store.save_ratchet(&pk(1), &blob).expect("save");
store.clear_ratchet(&pk(1)).expect("clear");
assert_eq!(store.load_ratchet(&pk(1)).unwrap(), None);
}
#[test]
fn ratchet_state_survives_reopening_the_store() {
let dir = tempfile::tempdir().expect("tempdir");
let blob = ratchet_blob(0x33);
{
let store = Store::open_at_with_passphrase(dir.path(), "same-pass").expect("open");
store
.upsert_friend(&pk(1), "a.onion", None)
.expect("friend");
store.append_message(&pk(1), "in", b"hello").expect("msg");
store.save_ratchet(&pk(1), &blob).expect("save");
}
let store = Store::open_at_with_passphrase(dir.path(), "same-pass").expect("reopen");
assert_eq!(store.load_ratchet(&pk(1)).expect("load"), Some(blob));
assert_eq!(store.list_messages(&pk(1)).unwrap()[0].plaintext, b"hello");
}