Skip to content

Commit 02fe0c9

Browse files
kaylendogrichvdh
authored andcommitted
feat: Add RoomKeyWithheldEntry to wrap to-device and bundle payloads.
1 parent 681b221 commit 02fe0c9

File tree

9 files changed

+60
-30
lines changed

9 files changed

+60
-30
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,7 @@ impl OlmMachine {
10101010
.withheld_session_info
10111011
.entry(c.room_id.to_owned())
10121012
.or_default()
1013-
.insert(c.session_id.to_owned(), event.to_owned());
1013+
.insert(c.session_id.to_owned(), event.to_owned().into());
10141014
}
10151015
}
10161016

crates/matrix-sdk-crypto/src/store/integration_tests.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ macro_rules! cryptostore_integration_tests {
5353
store::{
5454
types::{
5555
BackupDecryptionKey, Changes, DehydratedDeviceKey, DeviceChanges,
56-
IdentityChanges, PendingChanges, StoredRoomKeyBundleData, RoomSettings,
56+
IdentityChanges, PendingChanges, StoredRoomKeyBundleData, RoomKeyWithheldEntry,
57+
RoomSettings
5758
},
5859
CryptoStore, GossipRequest,
5960
},
@@ -1138,7 +1139,7 @@ macro_rules! cryptostore_integration_tests {
11381139
async fn test_withheld_info_storage() {
11391140
let (account, store) = get_loaded_store("withheld_info_storage").await;
11401141

1141-
let mut info_list: BTreeMap<_, BTreeMap<_, _>> = BTreeMap::new();
1142+
let mut info_list: BTreeMap<_, BTreeMap<_, RoomKeyWithheldEntry>> = BTreeMap::new();
11421143

11431144
let user_id = account.user_id().to_owned();
11441145
let room_id = room_id!("!DwLygpkclUAfQNnfva:example.com");
@@ -1163,7 +1164,7 @@ macro_rules! cryptostore_integration_tests {
11631164
info_list
11641165
.entry(room_id.to_owned())
11651166
.or_default()
1166-
.insert(session_id_1.to_owned(), event);
1167+
.insert(session_id_1.to_owned(), event.into());
11671168

11681169
let content = RoomKeyWithheldContent::MegolmV1AesSha2(
11691170
MegolmV1AesSha2WithheldContent::BlackListed(
@@ -1183,7 +1184,7 @@ macro_rules! cryptostore_integration_tests {
11831184
info_list
11841185
.entry(room_id.to_owned())
11851186
.or_default()
1186-
.insert(session_id_2.to_owned(), event);
1187+
.insert(session_id_2.to_owned(), event.into());
11871188

11881189
let changes = Changes { withheld_session_info: info_list, ..Default::default() };
11891190
store.save_changes(changes).await.unwrap();

crates/matrix-sdk-crypto/src/store/memorystore.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use crate::{
4545
OutboundGroupSession, PickledAccount, PickledInboundGroupSession, PickledSession,
4646
PrivateCrossSigningIdentity, SenderDataType, StaticAccountData,
4747
},
48-
types::events::room_key_withheld::RoomKeyWithheldEvent,
48+
store::types::RoomKeyWithheldEntry,
4949
};
5050

5151
fn encode_key_info(info: &SecretInfo) -> String {
@@ -97,7 +97,7 @@ pub struct MemoryStore {
9797
identities: StdRwLock<HashMap<OwnedUserId, String>>,
9898
outgoing_key_requests: StdRwLock<HashMap<OwnedTransactionId, GossipRequest>>,
9999
key_requests_by_info: StdRwLock<HashMap<String, OwnedTransactionId>>,
100-
direct_withheld_info: StdRwLock<HashMap<OwnedRoomId, HashMap<String, RoomKeyWithheldEvent>>>,
100+
direct_withheld_info: StdRwLock<HashMap<OwnedRoomId, HashMap<String, RoomKeyWithheldEntry>>>,
101101
custom_values: StdRwLock<HashMap<String, Vec<u8>>>,
102102
leases: StdRwLock<HashMap<String, (String, Instant)>>,
103103
secret_inbox: StdRwLock<HashMap<String, Vec<GossippedSecret>>>,
@@ -420,7 +420,7 @@ impl CryptoStore for MemoryStore {
420420
&self,
421421
room_id: &RoomId,
422422
session_id: &str,
423-
) -> Result<Option<RoomKeyWithheldEvent>> {
423+
) -> Result<Option<RoomKeyWithheldEntry>> {
424424
Ok(self
425425
.direct_withheld_info
426426
.read()
@@ -1272,11 +1272,10 @@ mod integration_tests {
12721272
store::{
12731273
types::{
12741274
BackupKeys, Changes, DehydratedDeviceKey, PendingChanges, RoomKeyCounts,
1275-
RoomSettings, StoredRoomKeyBundleData, TrackedUser,
1275+
RoomKeyWithheldEntry, RoomSettings, StoredRoomKeyBundleData, TrackedUser,
12761276
},
12771277
CryptoStore,
12781278
},
1279-
types::events::room_key_withheld::RoomKeyWithheldEvent,
12801279
Account, DeviceData, GossipRequest, GossippedSecret, SecretInfo, Session, UserIdentityData,
12811280
};
12821281

@@ -1372,7 +1371,7 @@ mod integration_tests {
13721371
&self,
13731372
room_id: &RoomId,
13741373
session_id: &str,
1375-
) -> Result<Option<RoomKeyWithheldEvent>, Self::Error> {
1374+
) -> Result<Option<RoomKeyWithheldEntry>, Self::Error> {
13761375
self.0.get_withheld_info(room_id, session_id).await
13771376
}
13781377

crates/matrix-sdk-crypto/src/store/traits.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use crate::{
3535
InboundGroupSession, OlmMessageHash, OutboundGroupSession, PrivateCrossSigningIdentity,
3636
SenderDataType, Session,
3737
},
38-
types::events::room_key_withheld::RoomKeyWithheldEvent,
38+
store::types::RoomKeyWithheldEntry,
3939
Account, DeviceData, GossipRequest, GossippedSecret, SecretInfo, UserIdentityData,
4040
};
4141

@@ -116,7 +116,7 @@ pub trait CryptoStore: AsyncTraitDeps {
116116
&self,
117117
room_id: &RoomId,
118118
session_id: &str,
119-
) -> Result<Option<RoomKeyWithheldEvent>, Self::Error>;
119+
) -> Result<Option<RoomKeyWithheldEntry>, Self::Error>;
120120

121121
/// Get all the inbound group sessions we have stored.
122122
async fn get_inbound_group_sessions(&self) -> Result<Vec<InboundGroupSession>, Self::Error>;
@@ -588,7 +588,7 @@ impl<T: CryptoStore> CryptoStore for EraseCryptoStoreError<T> {
588588
&self,
589589
room_id: &RoomId,
590590
session_id: &str,
591-
) -> Result<Option<RoomKeyWithheldEvent>, Self::Error> {
591+
) -> Result<Option<RoomKeyWithheldEntry>, Self::Error> {
592592
self.0.get_withheld_info(room_id, session_id).await.map_err(Into::into)
593593
}
594594

crates/matrix-sdk-crypto/src/store/types.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ use crate::{
3434
SenderData,
3535
},
3636
types::{
37-
events::{room_key_bundle::RoomKeyBundleContent, room_key_withheld::RoomKeyWithheldEvent},
37+
events::{
38+
room_key_bundle::RoomKeyBundleContent,
39+
room_key_withheld::{RoomKeyWithheldContent, RoomKeyWithheldEvent},
40+
},
3841
EventEncryptionAlgorithm,
3942
},
4043
Account, Device, DeviceData, GossippedSecret, Session, UserIdentity, UserIdentityData,
@@ -75,7 +78,7 @@ pub struct Changes {
7578
pub identities: IdentityChanges,
7679
pub devices: DeviceChanges,
7780
/// Stores when a `m.room_key.withheld` is received
78-
pub withheld_session_info: BTreeMap<OwnedRoomId, BTreeMap<String, RoomKeyWithheldEvent>>,
81+
pub withheld_session_info: BTreeMap<OwnedRoomId, BTreeMap<String, RoomKeyWithheldEntry>>,
7982
pub room_settings: HashMap<OwnedRoomId, RoomSettings>,
8083
pub secrets: Vec<GossippedSecret>,
8184
pub next_batch_token: Option<String>,
@@ -484,9 +487,31 @@ pub struct RoomKeyWithheldInfo {
484487
/// The ID of the session that the key is for.
485488
pub session_id: String,
486489

487-
/// The `m.room_key.withheld` event that notified us that the key is being
488-
/// withheld.
489-
pub withheld_event: RoomKeyWithheldEvent,
490+
/// The withheld entry from a `m.room_key.withheld` event or [MSC4268] room
491+
/// key bundle.
492+
///
493+
/// [MSC4268]: https://github.com/matrix-org/matrix-spec-proposals/pull/4268
494+
pub withheld_event: RoomKeyWithheldEntry,
495+
}
496+
497+
/// Represents an entry for a withheld room key event, which can be either a
498+
/// to-device event or a bundle entry.
499+
#[derive(Clone, Debug, Serialize, Deserialize)]
500+
pub struct RoomKeyWithheldEntry {
501+
/// The user ID responsible for this entry, either from a
502+
/// `m.room_key.withheld` to-device event or an [MSC4268] room key bundle.
503+
///
504+
/// [MSC4268]: https://github.com/matrix-org/matrix-spec-proposals/pull/4268
505+
pub sender: OwnedUserId,
506+
/// The content of the entry, which provides details about the reason the
507+
/// key was withheld.
508+
pub content: RoomKeyWithheldContent,
509+
}
510+
511+
impl From<RoomKeyWithheldEvent> for RoomKeyWithheldEntry {
512+
fn from(value: RoomKeyWithheldEvent) -> Self {
513+
Self { sender: value.sender, content: value.content }
514+
}
490515
}
491516

492517
/// Information about a received historic room key bundle.

crates/matrix-sdk-indexeddb/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ All notable changes to this project will be documented in this file.
66

77
## [Unreleased] - ReleaseDate
88

9+
### Features
10+
11+
- [**breaking**] `IndexeddbCryptoStore::get_withheld_info` now returns `Result<Option<RoomKeyWithheldEntry>, ...>`
12+
([#5737](https://github.com/matrix-org/matrix-rust-sdk/pull/5737))
13+
914
## [0.14.0] - 2025-09-04
1015

1116
No notable changes in this release.

crates/matrix-sdk-indexeddb/src/crypto_store/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,11 @@ use matrix_sdk_crypto::{
3030
},
3131
store::{
3232
types::{
33-
BackupKeys, Changes, DehydratedDeviceKey, PendingChanges, RoomKeyCounts, RoomSettings,
34-
StoredRoomKeyBundleData,
33+
BackupKeys, Changes, DehydratedDeviceKey, PendingChanges, RoomKeyCounts,
34+
RoomKeyWithheldEntry, RoomSettings, StoredRoomKeyBundleData,
3535
},
3636
CryptoStore, CryptoStoreError,
3737
},
38-
types::events::room_key_withheld::RoomKeyWithheldEvent,
3938
vodozemac::base64_encode,
4039
Account, DeviceData, GossipRequest, GossippedSecret, SecretInfo, TrackedUser, UserIdentityData,
4140
};
@@ -1386,7 +1385,7 @@ impl_crypto_store! {
13861385
&self,
13871386
room_id: &RoomId,
13881387
session_id: &str,
1389-
) -> Result<Option<RoomKeyWithheldEvent>> {
1388+
) -> Result<Option<RoomKeyWithheldEntry>> {
13901389
let key = self.serializer.encode_key(keys::DIRECT_WITHHELD_INFO, (session_id, room_id));
13911390
if let Some(pickle) = self
13921391
.inner

crates/matrix-sdk-sqlite/CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ All notable changes to this project will be documented in this file.
77
## [Unreleased] - ReleaseDate
88

99
### Features
10+
11+
- [**breaking**] `SqliteCryptoStore::get_withheld_info` now returns `Result<Option<RoomKeyWithheldEntry>>`.
12+
([#5737](https://github.com/matrix-org/matrix-rust-sdk/pull/5737))
13+
1014
- Implement a new constructor that allows to open `SqliteCryptoStore` with a cryptographic key
1115
([#5472](https://github.com/matrix-org/matrix-rust-sdk/pull/5472))
1216

@@ -85,5 +89,3 @@ No notable changes in this release.
8589
### Refactor
8690

8791
- Move `event_cache_store/` to `event_cache/store/` in `matrix-sdk-base`.
88-
89-

crates/matrix-sdk-sqlite/src/crypto_store.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,11 @@ use matrix_sdk_crypto::{
2828
},
2929
store::{
3030
types::{
31-
BackupKeys, Changes, DehydratedDeviceKey, PendingChanges, RoomKeyCounts, RoomSettings,
32-
StoredRoomKeyBundleData,
31+
BackupKeys, Changes, DehydratedDeviceKey, PendingChanges, RoomKeyCounts,
32+
RoomKeyWithheldEntry, RoomSettings, StoredRoomKeyBundleData,
3333
},
3434
CryptoStore,
3535
},
36-
types::events::room_key_withheld::RoomKeyWithheldEvent,
3736
Account, DeviceData, GossipRequest, GossippedSecret, SecretInfo, TrackedUser, UserIdentityData,
3837
};
3938
use matrix_sdk_store_encryption::StoreCipher;
@@ -1379,7 +1378,7 @@ impl CryptoStore for SqliteCryptoStore {
13791378
&self,
13801379
room_id: &RoomId,
13811380
session_id: &str,
1382-
) -> Result<Option<RoomKeyWithheldEvent>> {
1381+
) -> Result<Option<RoomKeyWithheldEntry>> {
13831382
let room_id = self.encode_key("direct_withheld_info", room_id);
13841383
let session_id = self.encode_key("direct_withheld_info", session_id);
13851384

@@ -1388,7 +1387,7 @@ impl CryptoStore for SqliteCryptoStore {
13881387
.get_direct_withheld_info(session_id, room_id)
13891388
.await?
13901389
.map(|value| {
1391-
let info = self.deserialize_json::<RoomKeyWithheldEvent>(&value)?;
1390+
let info = self.deserialize_json::<RoomKeyWithheldEntry>(&value)?;
13921391
Ok(info)
13931392
})
13941393
.transpose()

0 commit comments

Comments
 (0)