Skip to content

Commit

Permalink
fix(crypto): Store session ID in EncryptionInfo, and use it to redecr…
Browse files Browse the repository at this point in the history
…ypt items even if they are not UTDs.
  • Loading branch information
andybalaam committed Feb 7, 2025
1 parent a1dbda3 commit 9a19fcf
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub fn make_test_event(room_id: &RoomId, content: &str) -> TimelineEvent {
sender_claimed_keys: Default::default(),
},
verification_state: VerificationState::Verified,
session_id: "mysessionid9".to_owned(),
};

let event = EventFactory::new()
Expand Down
15 changes: 13 additions & 2 deletions crates/matrix-sdk-common/src/deserialized_responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,9 @@ pub struct EncryptionInfo {
/// Callers that persist this should mark the state as dirty when a device
/// change is received down the sync.
pub verification_state: VerificationState,
/// The Megolm session ID that was used to encrypt this event.
#[serde(default)]
pub session_id: String,
}

/// Represents a matrix room event that has been returned from `/sync`,
Expand Down Expand Up @@ -541,10 +544,13 @@ impl TimelineEventKind {
}
}

/// The Megolm session ID that was used to send this event, if it is a UTD.
/// The Megolm session ID that was used to send this event, if it was
/// encrypted.
pub fn session_id(&self) -> Option<String> {
match self {
TimelineEventKind::Decrypted(_decrypted_room_event) => None,
TimelineEventKind::Decrypted(decrypted_room_event) => {
Some(decrypted_room_event.encryption_info.session_id.clone())
}
TimelineEventKind::UnableToDecrypt { utd_info, .. } => utd_info.session_id.clone(),
TimelineEventKind::PlainText { .. } => None,
}
Expand Down Expand Up @@ -1051,6 +1057,7 @@ mod tests {
sender_claimed_keys: Default::default(),
},
verification_state: VerificationState::Verified,
session_id: "xyz".to_owned(),
},
unsigned_encryption_info: Some(BTreeMap::from([(
UnsignedEventLocation::RelationsReplace,
Expand Down Expand Up @@ -1089,6 +1096,7 @@ mod tests {
}
},
"verification_state": "Verified",
"session_id": "xyz",
},
"unsigned_encryption_info": {
"RelationsReplace": {"UnableToDecrypt": {
Expand Down Expand Up @@ -1137,6 +1145,7 @@ mod tests {
event.encryption_info().unwrap().algorithm_info,
AlgorithmInfo::MegolmV1AesSha2 { .. }
);
assert_eq!(event.encryption_info().unwrap().session_id, "");

// Test that the previous format, with an undecryptable unsigned event, can also
// be deserialized.
Expand Down Expand Up @@ -1363,6 +1372,7 @@ mod tests {
sender_claimed_keys: Default::default(),
},
verification_state: VerificationState::Verified,
session_id: "mysessionid76".to_owned(),
};

with_settings!({sort_maps =>true}, {
Expand Down Expand Up @@ -1392,6 +1402,7 @@ mod tests {
]),
},
verification_state: VerificationState::Verified,
session_id: "mysessionid112".to_owned(),
},
unsigned_encryption_info: Some(BTreeMap::from([(
UnsignedEventLocation::RelationsThreadLatestEvent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ expression: info
"sender_claimed_keys": {}
}
},
"verification_state": "Verified"
"verification_state": "Verified",
"session_id": "mysessionid76"
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ expression: "serde_json::to_value(&room_event).unwrap()"
},
"sender": "@sender:example.com",
"sender_device": "ABCDEFGHIJ",
"session_id": "mysessionid112",
"verification_state": "Verified"
},
"event": {
Expand Down
1 change: 1 addition & 0 deletions crates/matrix-sdk-crypto/src/machine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1678,6 +1678,7 @@ impl OlmMachine {
.collect(),
},
verification_state,
session_id: session.session_id().to_owned(),
})
}

Expand Down
2 changes: 1 addition & 1 deletion crates/matrix-sdk-ui/src/timeline/event_item/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub(in crate::timeline) struct RemoteEventTimelineItem {
/// Where we got this event from: A sync response or pagination.
pub origin: RemoteEventOrigin,

/// The megolm session ID used to send this event, if it is UTD.
/// The megolm session ID used to send this event, if it was encrypted.
pub session_id: Option<String>,
}

Expand Down
1 change: 1 addition & 0 deletions crates/matrix-sdk-ui/src/timeline/tests/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ async fn test_edit_updates_encryption_info() {
sender_claimed_keys: BTreeMap::new(),
},
verification_state: VerificationState::Verified,
session_id: "mysessionid6333".to_owned(),
};

let original_event: TimelineEvent = DecryptedRoomEvent {
Expand Down

0 comments on commit 9a19fcf

Please sign in to comment.