From 72cc2bd60cb7046bb7250c5d09baf3a54f0fbfe4 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Thu, 12 Sep 2024 17:01:41 +0100 Subject: [PATCH] crypto: Include megolm ratchet index in logging span fields This field is helpful as it tells us the sequence number of the message in the megolm session, which gives us a clue about how long it will have been since the session should have been shared with us. --- crates/matrix-sdk-crypto/CHANGELOG.md | 3 +++ crates/matrix-sdk-crypto/src/machine/mod.rs | 4 +++- .../matrix-sdk-crypto/src/types/events/room/encrypted.rs | 9 +++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/crates/matrix-sdk-crypto/CHANGELOG.md b/crates/matrix-sdk-crypto/CHANGELOG.md index 7ec36d90b..7db9cb8c7 100644 --- a/crates/matrix-sdk-crypto/CHANGELOG.md +++ b/crates/matrix-sdk-crypto/CHANGELOG.md @@ -2,6 +2,9 @@ Changes: +- Improve logging for undecryptable Megolm events. + ([#3989](https://github.com/matrix-org/matrix-rust-sdk/pull/3989)) + - Miscellaneous improvements to logging for verification and `OwnUserIdentity` updates. ([#3949](https://github.com/matrix-org/matrix-rust-sdk/pull/3949)) diff --git a/crates/matrix-sdk-crypto/src/machine/mod.rs b/crates/matrix-sdk-crypto/src/machine/mod.rs index d0e985d05..95a435a25 100644 --- a/crates/matrix-sdk-crypto/src/machine/mod.rs +++ b/crates/matrix-sdk-crypto/src/machine/mod.rs @@ -1747,7 +1747,7 @@ impl OlmMachine { self.decrypt_room_event_inner(event, room_id, true, decryption_settings).await } - #[instrument(name = "decrypt_room_event", skip_all, fields(?room_id, event_id, origin_server_ts, sender, algorithm, session_id, sender_key))] + #[instrument(name = "decrypt_room_event", skip_all, fields(?room_id, event_id, origin_server_ts, sender, algorithm, session_id, message_index, sender_key))] async fn decrypt_room_event_inner( &self, event: &Raw, @@ -1781,6 +1781,8 @@ impl OlmMachine { }; Span::current().record("session_id", content.session_id()); + Span::current().record("message_index", content.message_index()); + let result = self.decrypt_megolm_events(room_id, &event, &content, decryption_settings).await; diff --git a/crates/matrix-sdk-crypto/src/types/events/room/encrypted.rs b/crates/matrix-sdk-crypto/src/types/events/room/encrypted.rs index debb082f3..21f8e5c18 100644 --- a/crates/matrix-sdk-crypto/src/types/events/room/encrypted.rs +++ b/crates/matrix-sdk-crypto/src/types/events/room/encrypted.rs @@ -268,6 +268,15 @@ impl SupportedEventEncryptionSchemes<'_> { SupportedEventEncryptionSchemes::MegolmV2AesSha2(c) => &c.session_id, } } + + /// The index of the Megolm ratchet that was used to encrypt the message. + pub fn message_index(&self) -> u32 { + match self { + SupportedEventEncryptionSchemes::MegolmV1AesSha2(c) => c.ciphertext.message_index(), + #[cfg(feature = "experimental-algorithms")] + SupportedEventEncryptionSchemes::MegolmV2AesSha2(c) => c.ciphertext.message_index(), + } + } } impl<'a> From<&'a MegolmV1AesSha2Content> for SupportedEventEncryptionSchemes<'a> {