[client] F22a: snapshot and restore; staged calls that send nothing before the app has saved #15
Reference in New Issue
Block a user
Delete Branch "feature/client-state"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
F22a: the client survives a restart, and sends nothing before the app has saved. Part of F22 (chat in Amicitia). Analysis:
.agents/features/client-state.md.Client::snapshot()/Client::restore(bytes, transport): the whole state as versioned JSON, OpenMLS's storage included. All or nothing: a bad snapshot is refused (ClientError::State).compose,accept,receivedo the local work and return what still has to reach the relay (Outgoing, serialisable for an outbox).deliverandfinishsend it after the app has saved.send,accept_inviteandpollstay as the one-step forms.Event::Joinedsays which invite it came through (via), so the app knows which person joined.receivereturns what it processed when the relay fails part-way (interrupted), instead of losing it.forget_invite(id): stop fetching an unclaimed invite's mailbox.PROTOCOL.md: the relay sees exactly what it saw before.Adversarial self-review — F22a, client state
Base:
mainat47ed9ee. Findings R1–R7 are on their lines.Evidence
cargo test --workspace --locked(Windows)amicitia_test, Android 15, x86_64, NDK r27b overadb: 24 passed, 0 failedcargo build -p tabellarius-clientforaarch64-andarmv7-Android-D warnings), fmt,check-deps.shbase640.22, already used byprotocolreceiveacknowledges inside the callcomposepostsacceptposts the Welcome at oncerestoreskips OpenMLS's storagerestorestartsnext_contactat 0finishacknowledges before deliveringfinishnever drops a used welcome mailboxreceivegives up on a relay failureforget_inviteforgets nothingNot verified
Verdict
R3 fixed on this branch; R1, R2, R5–R7 stated, with their answers; R4 kept as defence in depth. Merging.
@@ -94,0 +113,4 @@/// `Debug` shows neither the post token nor the bytes.#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]#[serde(rename_all = "camelCase", deny_unknown_fields)]pub struct Outgoing {R7 — an
Outgoingcarries a post token. An app that keeps an outbox must keep it in encrypted storage too.Debugprints only the size. The blob itself is MLS ciphertext.@@ -226,0 +297,4 @@/// Stops fetching an invite's welcome mailbox: the invite was cancelled, or is older than the/// relay keeps unclaimed invites (7 days; the app has the clock, the library has none)./// Otherwise an unclaimed invite's mailbox is fetched for ever. Save the snapshot after it.pub fn forget_invite(&mut self, invite: &InviteId) {R8 — the library has no clock. An unclaimed invite's welcome mailbox would be fetched for ever: wasted requests, and one more mailbox linked by timing (PROTOCOL.md section 1).
forget_invitelets the app stop it after the relay's 7 days, or when the user cancels.@@ -226,1 +305,4 @@/// creates our mailbox, then makes the group. The Welcome and our `hello` come back for the/// app to deliver after saving.pub fn accept(&mut self, invite: &Invite) -> Result<(ContactId, Vec<Outgoing>), ClientError> {let kp_bytes = self.transport.claim_invite(&invite.id, &invite.secret)?;R6 — a claimed invite is used up before anything is saved. If the app dies between the claim and saving, the link no longer works; the person asks for a new one. Unavoidable with a one-time claim, and harmless: nothing readable is exposed.
@@ -351,0 +477,4 @@let blobs = match self.transport.fetch(&mailbox.id, &mailbox.fetch) {Ok(blobs) => blobs,Err(e) => {got.interrupted = Some(e);R5 — a partial receive. If the relay fails after some mailboxes were processed, the client's state already includes them. Returning an error here would lose those messages: the app would not save them, and fetching them again gives MLS replays that are dropped. So
receivereturns what it has, withinterruptedset.@@ -359,0 +550,4 @@.filter(|c| c.name.is_some()).filter_map(|c| c.via).collect();self.welcome_boxes.retain(|w| !joined.contains(&w.invite));R3 — found while building, fixed. The used welcome mailbox was first dropped inside
receive. A snapshot saved betweenreceiveandfinishthen forgot it, so after a crash the Welcome andhellostayed on the relay for 30 days. Test a crash between receive and finish… failed on it (3 blobs waiting instead of 1). The drop now happens infinish, after acknowledging.@@ -367,0 +581,4 @@.map_err(|_| ClientError::Mls)?;// The same group twice (a Welcome fetched again after a crash): refused.let group_id = staged.group_context().group_id();if self.contacts.iter().any(|c| c.group.group_id() == group_id) {R4 — an equivalent planted bug. Removing this guard is not caught, because OpenMLS deletes the KeyPackage when it joins from a Welcome, so a second copy of the Welcome already fails. The guard stays as defence in depth, for a last-resort KeyPackage or a change in OpenMLS.
@@ -0,0 +65,4 @@impl<T: Transport> Client<T> {/// The whole state, to keep in the app's encrypted store. Secret: never log it, never send it.pub fn snapshot(&self) -> Result<Vec<u8>, ClientError> {R1 — the snapshot is the identity. It holds every secret: MLS private keys, fetch and post tokens. It must live only in encrypted storage (Amicitia: SQLCipher), never in a log or a backup in the clear. The library writes no file and implements no
Debugfor it. Encrypting it again inside the library would need a second key stored somewhere; not done, stated in the analysis.@@ -0,0 +75,4 @@.iter().map(|(k, v)| (Bytes(k.clone()), Bytes(v.clone()))).collect();storage.sort();R2 — written whole after every change. A few KB per contact. Fine for 1:1 chats with dozens of people; a table per key would scale better. Not measured with many contacts; for later, when measured.
Merged into
mainwith--no-ffas4f78101735(remote hash verified). The self-review is review 376 on this PR (its summary says R1–R7; there are eight, R8 is onforget_invite).Pull request closed