Skip to content

Commit c8536e9

Browse files
andybalaamrichvdhbnjbvr
authored
fix(crypto): Redecrypt non-UTD messages to remove no-longer-relevant warning shields (#4644)
Fixes element-hq/element-meta#2697 Fixes element-hq/crypto-internal#398 I'm sorry it's a big change. I've tried to break it into decent commits, and I did a couple of preparatory PRs to make it less painful, but it's still a bit to get your head around. The basic idea is that when a session is updated and we call `retry_event_decryption`, we don't only look at UTDs any more - now we also look at decrypted events, and re-request their `EncryptionInfo`, in case it has improved. --------- Signed-off-by: Andy Balaam <[email protected]> Co-authored-by: Richard van der Hoff <[email protected]> Co-authored-by: Benjamin Bouvier <[email protected]>
1 parent 1caa606 commit c8536e9

File tree

13 files changed

+730
-39
lines changed

13 files changed

+730
-39
lines changed

crates/matrix-sdk-base/src/event_cache/store/integration_tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub fn make_test_event(room_id: &RoomId, content: &str) -> TimelineEvent {
4848
sender_claimed_keys: Default::default(),
4949
},
5050
verification_state: VerificationState::Verified,
51+
session_id: Some("mysessionid9".to_owned()),
5152
};
5253

5354
let event = EventFactory::new()

crates/matrix-sdk-common/src/deserialized_responses.rs

+21
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,9 @@ pub struct EncryptionInfo {
302302
/// Callers that persist this should mark the state as dirty when a device
303303
/// change is received down the sync.
304304
pub verification_state: VerificationState,
305+
/// The Megolm session ID that was used to encrypt this event, or None if
306+
/// this info was stored before we collected this data.
307+
pub session_id: Option<String>,
305308
}
306309

307310
/// Represents a matrix room event that has been returned from `/sync`,
@@ -540,6 +543,19 @@ impl TimelineEventKind {
540543
TimelineEventKind::PlainText { event } => event,
541544
}
542545
}
546+
547+
/// The Megolm session ID that was used to send this event, if it was
548+
/// encrypted.
549+
pub fn session_id(&self) -> Option<&str> {
550+
match self {
551+
TimelineEventKind::Decrypted(decrypted_room_event) => {
552+
decrypted_room_event.encryption_info.session_id.as_ref()
553+
}
554+
TimelineEventKind::UnableToDecrypt { utd_info, .. } => utd_info.session_id.as_ref(),
555+
TimelineEventKind::PlainText { .. } => None,
556+
}
557+
.map(String::as_str)
558+
}
543559
}
544560

545561
#[cfg(not(tarpaulin_include))]
@@ -1042,6 +1058,7 @@ mod tests {
10421058
sender_claimed_keys: Default::default(),
10431059
},
10441060
verification_state: VerificationState::Verified,
1061+
session_id: Some("xyz".to_owned()),
10451062
},
10461063
unsigned_encryption_info: Some(BTreeMap::from([(
10471064
UnsignedEventLocation::RelationsReplace,
@@ -1080,6 +1097,7 @@ mod tests {
10801097
}
10811098
},
10821099
"verification_state": "Verified",
1100+
"session_id": "xyz",
10831101
},
10841102
"unsigned_encryption_info": {
10851103
"RelationsReplace": {"UnableToDecrypt": {
@@ -1128,6 +1146,7 @@ mod tests {
11281146
event.encryption_info().unwrap().algorithm_info,
11291147
AlgorithmInfo::MegolmV1AesSha2 { .. }
11301148
);
1149+
assert_eq!(event.encryption_info().unwrap().session_id, None);
11311150

11321151
// Test that the previous format, with an undecryptable unsigned event, can also
11331152
// be deserialized.
@@ -1364,6 +1383,7 @@ mod tests {
13641383
sender_claimed_keys: Default::default(),
13651384
},
13661385
verification_state: VerificationState::Verified,
1386+
session_id: Some("mysessionid76".to_owned()),
13671387
};
13681388

13691389
with_settings!({ sort_maps => true, prepend_module_to_snapshot => false }, {
@@ -1393,6 +1413,7 @@ mod tests {
13931413
]),
13941414
},
13951415
verification_state: VerificationState::Verified,
1416+
session_id: Some("mysessionid112".to_owned()),
13961417
},
13971418
unsigned_encryption_info: Some(BTreeMap::from([(
13981419
UnsignedEventLocation::RelationsThreadLatestEvent,

crates/matrix-sdk-common/src/snapshots/snapshot_test_encryption_info.snap

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ expression: info
1212
"sender_claimed_keys": {}
1313
}
1414
},
15-
"verification_state": "Verified"
15+
"verification_state": "Verified",
16+
"session_id": "mysessionid76"
1617
}

crates/matrix-sdk-common/src/snapshots/snapshot_test_sync_timeline_event.snap

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ expression: "serde_json::to_value(&room_event).unwrap()"
1717
},
1818
"sender": "@sender:example.com",
1919
"sender_device": "ABCDEFGHIJ",
20+
"session_id": "mysessionid112",
2021
"verification_state": "Verified"
2122
},
2223
"event": {

crates/matrix-sdk-crypto/src/machine/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1671,6 +1671,7 @@ impl OlmMachine {
16711671
.collect(),
16721672
},
16731673
verification_state,
1674+
session_id: Some(session.session_id().to_owned()),
16741675
})
16751676
}
16761677

0 commit comments

Comments
 (0)