Skip to main content

core_crypto/mls/conversation/mutable/
wipe.rs

1use core_crypto_keystore::{entities::PersistedMlsGroup, traits::EntityDeleteBorrowed};
2
3use super::Result;
4use crate::{KeystoreError, OpenMlsError, RecursiveError, mls::conversation::ConversationMut};
5
6impl ConversationMut {
7    /// Destroys a group locally
8    ///
9    /// # Errors
10    /// KeyStore errors, such as IO
11    pub async fn wipe(&mut self) -> Result<()> {
12        // to the degree that it's easy, fallibly get things before doing any mutation
13        let mut conversation_cache = self
14            .tx_context
15            .mls_groups()
16            .await
17            .map_err(RecursiveError::context("getting mls conversation cache"))?;
18
19        self.mutate_group(async |transaction, group, _| {
20            // collect all the relevant proposal refs without holding onto the group;
21            // we'll need to mutate the group in shortly
22            let proposals = group
23                .pending_proposals()
24                .map(|proposal| proposal.proposal_reference().to_owned())
25                .collect::<Vec<_>>();
26            for proposal in proposals {
27                // Update proposals rekey the own leaf node. Hence the associated encryption keypair has to be cleared
28                group
29                    .remove_pending_proposal(transaction, &proposal)
30                    .await
31                    .map_err(OpenMlsError::wrap("removing pending proposal"))?;
32            }
33
34            Ok(())
35        })
36        .await?;
37
38        let id = self.id();
39        let context = self
40            .tx_context
41            .inner()
42            .map_err(RecursiveError::context("getting inner context"))?;
43        let tx = context.transaction();
44        PersistedMlsGroup::delete_borrowed(tx, id.keystore()).map_err(KeystoreError::wrap("deleting mls group"))?;
45        let _ = conversation_cache.remove(id);
46
47        // Release the cache guard before clearing the buffers: that path reaches back into the
48        // transaction context, and holding this guard across it would deadlock.
49        drop(conversation_cache);
50
51        Ok(())
52    }
53}
54
55#[cfg(test)]
56mod tests {
57    use crate::{DeferralReason, test_utils::*};
58
59    /// Wiping a conversation abandons the messages it had buffered.
60    #[apply(all_cred_cipher)]
61    async fn wipe_abandons_buffered_messages(case: TestContext) {
62        Box::pin(async move {
63            let [mut alice, bob] = case.sessions().await;
64            let conversation = case.create_conversation([&alice, &bob]).await;
65
66            // Bob advances the epoch without Alice hearing about it, then speaks in the new epoch.
67            let conversation = conversation
68                .acting_as(&bob)
69                .await
70                .update()
71                .await
72                .process_member_changes()
73                .await
74                .finish();
75            let app_msg = conversation
76                .guard_of(&bob)
77                .await
78                .encrypt_message(b"Hello Alice !")
79                .await
80                .unwrap();
81
82            // Alice cannot decrypt a message from an epoch she has not reached yet, so she buffers it
83            // until the commit which advances her own epoch arrives.
84            let decrypt = conversation.guard_of(&alice).await.decrypt_message(app_msg).await;
85            assert!(
86                matches!(
87                    decrypt.unwrap().into_single_deferred(),
88                    DeferralReason::FutureEpoch { .. }
89                ),
90                "the message must have been deferred"
91            );
92            assert_eq!(
93                alice.transaction.count_entities().await.pending_messages,
94                1,
95                "the message Alice could not decrypt must have been buffered"
96            );
97
98            // That commit never arrives; Alice wipes the conversation instead.
99            conversation.guard_of(&alice).await.wipe().await.unwrap();
100
101            drop(conversation);
102            alice.commit_transaction().await;
103
104            let counts = alice.transaction.count_entities().await;
105            assert_eq!(counts.group, 0, "the wipe must have removed the conversation");
106            assert_eq!(
107                counts.pending_messages, 0,
108                "the buffered message belongs to a conversation which no longer exists, so wiping that \
109                 conversation must have taken the message with it"
110            );
111        })
112        .await
113    }
114
115    /// Wiping a conversation abandons the commit it had buffered.
116    #[apply(all_cred_cipher)]
117    async fn wipe_abandons_buffered_commits(case: TestContext) {
118        Box::pin(async move {
119            let [mut alice, bob, charlie] = case.sessions().await;
120            let conversation = case.create_conversation([&alice, &bob, &charlie]).await;
121
122            // Bob proposes removing Charlie, but nobody else is told about the proposal.
123            let conversation = conversation
124                .acting_as(&bob)
125                .await
126                .remove_proposal(&charlie)
127                .await
128                .finish();
129
130            // Bob then commits it. The commit refers to the proposal by reference, so Alice — who never
131            // received that proposal — cannot apply the commit, and buffers it to retry once she does.
132            let commit_guard = conversation.acting_as(&bob).await.commit_pending_proposals().await;
133            let (commit_guard, result) = commit_guard.notify_member_fallible(&alice).await;
134            assert!(
135                matches!(result.unwrap().into_single_deferred(), DeferralReason::MissingProposal),
136                "the commit must have been deferred until its proposal arrives"
137            );
138            let conversation = commit_guard.finish();
139
140            assert_eq!(
141                alice.transaction.count_entities().await.buffered_commits,
142                1,
143                "the commit Alice could not apply must have been buffered"
144            );
145
146            // The proposal never arrives; Alice wipes the conversation instead.
147            conversation.guard_of(&alice).await.wipe().await.unwrap();
148
149            drop(conversation);
150            alice.commit_transaction().await;
151
152            let counts = alice.transaction.count_entities().await;
153            assert_eq!(counts.group, 0, "the wipe must have removed the conversation");
154            assert_eq!(
155                counts.buffered_commits, 0,
156                "the buffered commit belongs to a conversation which no longer exists, so wiping that \
157                 conversation must have taken the commit with it"
158            );
159        })
160        .await
161    }
162}