Skip to content

Commit a1cf8b6

Browse files
committed
Drop start requests from lower precedence parties if sas has already been started.
According to the guide for implementing verification started from a verification request, both parties should (or at least allowed to) send a start request when the verification is ready. However, only the start request from the party with lexicographically smaller user id (or device id, for device verification, and thus equal user id) is supposed to be accepted, and the other one ignored.
1 parent 798464b commit a1cf8b6

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

crates/matrix-sdk-crypto/src/verification/requests.rs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use super::{
4444
event_enums::{
4545
CancelContent, DoneContent, OutgoingContent, ReadyContent, RequestContent, StartContent,
4646
},
47-
CancelInfo, Cancelled, FlowId, VerificationStore,
47+
CancelInfo, Cancelled, FlowId, Verification, VerificationStore,
4848
};
4949
#[cfg(feature = "qrcode")]
5050
use super::{
@@ -1126,11 +1126,10 @@ impl RequestState<Ready> {
11261126
};
11271127

11281128
let identity = self.store.get_user_identity(sender).await?;
1129-
let own_identity = self
1130-
.store
1131-
.get_user_identity(self.store.account.user_id())
1132-
.await?
1133-
.and_then(|i| i.into_own());
1129+
let own_user_id = self.store.account.user_id();
1130+
let own_device_id = self.store.account.device_id();
1131+
let own_identity =
1132+
self.store.get_user_identity(own_user_id).await?.and_then(|i| i.into_own());
11341133

11351134
match content.method() {
11361135
StartMethod::SasV1(_) => {
@@ -1142,13 +1141,27 @@ impl RequestState<Ready> {
11421141
we_started,
11431142
request_handle,
11441143
) {
1145-
// TODO check if there is already a SAS verification, i.e. we
1146-
// already started one before the other side tried to do the
1147-
// same; ignore it if we did and we're the lexicographically
1148-
// smaller user ID, otherwise auto-accept the newly started one.
11491144
Ok(s) => {
1150-
info!("Started a new SAS verification.");
1151-
self.verification_cache.insert_sas(s);
1145+
let start_new = if let Some(Verification::SasV1(_sas)) =
1146+
self.verification_cache.get(sender, self.flow_id.as_str())
1147+
{
1148+
// If there is already a SAS verification, i.e. we already started one
1149+
// before the other side tried to do the same; ignore it if we did and
1150+
// we're the lexicographically smaller user ID (or device ID if equal).
1151+
use std::cmp::Ordering;
1152+
match (sender.cmp(own_user_id), device.device_id().cmp(own_device_id)) {
1153+
(Ordering::Greater, _) | (Ordering::Equal, Ordering::Greater) => {
1154+
false
1155+
}
1156+
_ => true,
1157+
}
1158+
} else {
1159+
true
1160+
};
1161+
if start_new {
1162+
info!("Started a new SAS verification.");
1163+
self.verification_cache.insert_sas(s);
1164+
}
11521165
}
11531166
Err(c) => {
11541167
warn!(

0 commit comments

Comments
 (0)