Skip to content

Commit ee82861

Browse files
authored
Merge pull request #5807 from matrix-org/rav/history_sharing/not_shared_code
crypto: use a new withheld code when history is marked as "not shareable"
2 parents 9fff07d + ef3c671 commit ee82861

File tree

8 files changed

+238
-20
lines changed

8 files changed

+238
-20
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,13 @@ pub enum WithheldCode {
12001200
#[ruma_enum(rename = "m.no_olm")]
12011201
NoOlm,
12021202

1203+
/// Normally used when sharing history, per [MSC4268]: indicates
1204+
/// that the session was not marked as "shared_history".
1205+
///
1206+
/// [MSC4268]: https://github.com/matrix-org/matrix-spec-proposals/pull/4268
1207+
#[ruma_enum(rename = "io.element.msc4268.history_not_shared", alias = "m.history_not_shared")]
1208+
HistoryNotShared,
1209+
12031210
#[doc(hidden)]
12041211
_Custom(PrivOwnedStr),
12051212
}
@@ -1212,6 +1219,7 @@ impl fmt::Display for WithheldCode {
12121219
WithheldCode::Unauthorised => "You are not authorised to read the message.",
12131220
WithheldCode::Unavailable => "The requested key was not found.",
12141221
WithheldCode::NoOlm => "Unable to establish a secure channel.",
1222+
WithheldCode::HistoryNotShared => "The sender disabled sharing encrypted history.",
12151223
_ => self.as_str(),
12161224
};
12171225

crates/matrix-sdk-crypto/CHANGELOG.md

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

99
### Features
1010

11+
- Use new withheld code in key bundles for sessions not marked as
12+
`shared_history`.
13+
([#5807](https://github.com/matrix-org/matrix-rust-sdk/pull/5807)
1114
- Improve feedback support for shared history when downloading room key bundles.
1215
([#5737](https://github.com/matrix-org/matrix-rust-sdk/pull/5737))
1316
- Add `RoomKeyWithheldEntry` enum, wrapping either a received to-device `m.room_key.withheld` event or

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,7 @@ impl OlmMachine {
897897
// This function is only ever called by add_room_key via
898898
// handle_decrypted_to_device_event, so sender, sender_key, and algorithm are
899899
// already recorded.
900-
fields(room_id = ? content.room_id, session_id, message_index)
900+
fields(room_id = ? content.room_id, session_id, message_index, shared_history = content.shared_history)
901901
)]
902902
async fn handle_key(
903903
&self,

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,7 +1600,7 @@ impl Store {
16001600
} else {
16011601
bundle.withheld.push(RoomKeyWithheldContent::new(
16021602
session.algorithm().to_owned(),
1603-
WithheldCode::Unauthorised,
1603+
WithheldCode::HistoryNotShared,
16041604
session.room_id().to_owned(),
16051605
session.session_id().to_owned(),
16061606
session.sender_key().to_owned(),
@@ -1717,6 +1717,8 @@ impl Store {
17171717
bundle_info: &StoredRoomKeyBundleData,
17181718
bundle: &RoomKeyBundle,
17191719
) -> Result<(), CryptoStoreError> {
1720+
let mut session_id_to_withheld_code_map = BTreeMap::new();
1721+
17201722
let mut changes = Changes::default();
17211723
for withheld in &bundle.withheld {
17221724
let (room_id, session_id) = match withheld {
@@ -1744,9 +1746,17 @@ impl Store {
17441746
content: withheld.to_owned(),
17451747
},
17461748
);
1749+
session_id_to_withheld_code_map.insert(session_id, withheld.withheld_code());
17471750
}
1751+
17481752
self.save_changes(changes).await?;
17491753

1754+
info!(
1755+
room_id = ?bundle_info.bundle_data.room_id,
1756+
?session_id_to_withheld_code_map,
1757+
"Successfully imported withheld info from room key bundle",
1758+
);
1759+
17501760
Ok(())
17511761
}
17521762
}
@@ -2144,11 +2154,11 @@ mod tests {
21442154
RoomKeyWithheldEntry {
21452155
#[cfg(not(feature = "experimental-algorithms"))]
21462156
content: RoomKeyWithheldContent::MegolmV1AesSha2(
2147-
MegolmV1AesSha2WithheldContent::Unauthorised(_)
2157+
MegolmV1AesSha2WithheldContent::HistoryNotShared(_)
21482158
),
21492159
#[cfg(feature = "experimental-algorithms")]
21502160
content: RoomKeyWithheldContent::MegolmV2AesSha2(
2151-
MegolmV1AesSha2WithheldContent::Unauthorised(_)
2161+
MegolmV1AesSha2WithheldContent::HistoryNotShared(_)
21522162
),
21532163
..
21542164
}

crates/matrix-sdk-crypto/src/store/snapshots/matrix_sdk_crypto__store__tests__build_room_key_bundle.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ expression: bundle
2828
"withheld": [
2929
{
3030
"algorithm": "[algorithm]",
31-
"code": "m.unauthorised",
31+
"code": "io.element.msc4268.history_not_shared",
3232
"from_device": "BOB",
33-
"reason": "You are not authorised to read the message.",
33+
"reason": "The sender disabled sharing encrypted history.",
3434
"room_id": "!room1:localhost",
3535
"sender_key": "[alice curve key]",
3636
"session_id": "lpRzTgD3Nook/Wk62Fm9ECWGnKYZgeCwO1Y+uuPJz/I"

crates/matrix-sdk-crypto/src/types/events/room_key_withheld.rs

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ macro_rules! construct_withheld_content {
6565
WithheldCode::Blacklisted
6666
| WithheldCode::Unverified
6767
| WithheldCode::Unauthorised
68-
| WithheldCode::Unavailable => {
68+
| WithheldCode::Unavailable
69+
| WithheldCode::HistoryNotShared => {
6970
let content = CommonWithheldCodeContent {
7071
$room_id,
7172
$session_id,
@@ -84,7 +85,9 @@ macro_rules! construct_withheld_content {
8485
.into(),
8586
))
8687
}
87-
_ => unreachable!("Can't create an unknown withheld code content"),
88+
WithheldCode::_Custom(_) => {
89+
unreachable!("Can't create an unknown withheld code content")
90+
}
8891
}
8992
};
9093
}
@@ -180,6 +183,9 @@ pub enum MegolmV1AesSha2WithheldContent {
180183
Unauthorised(Box<CommonWithheldCodeContent>),
181184
/// The `m.unavailable` variant of the withheld code content.
182185
Unavailable(Box<CommonWithheldCodeContent>),
186+
/// The `m.history_not_shared` variant of the withheld code content (cf
187+
/// [MSC4268](https://github.com/matrix-org/matrix-spec-proposals/pull/4268)).
188+
HistoryNotShared(Box<CommonWithheldCodeContent>),
183189
/// The `m.no_olm` variant of the withheld code content.
184190
NoOlm(Box<NoOlmWithheldContent>),
185191
}
@@ -231,7 +237,10 @@ impl MegolmV1AesSha2WithheldContent {
231237
MegolmV1AesSha2WithheldContent::BlackListed(content)
232238
| MegolmV1AesSha2WithheldContent::Unverified(content)
233239
| MegolmV1AesSha2WithheldContent::Unauthorised(content)
234-
| MegolmV1AesSha2WithheldContent::Unavailable(content) => Some(&content.session_id),
240+
| MegolmV1AesSha2WithheldContent::Unavailable(content)
241+
| MegolmV1AesSha2WithheldContent::HistoryNotShared(content) => {
242+
Some(&content.session_id)
243+
}
235244
MegolmV1AesSha2WithheldContent::NoOlm(_) => None,
236245
}
237246
}
@@ -242,7 +251,8 @@ impl MegolmV1AesSha2WithheldContent {
242251
MegolmV1AesSha2WithheldContent::BlackListed(content)
243252
| MegolmV1AesSha2WithheldContent::Unverified(content)
244253
| MegolmV1AesSha2WithheldContent::Unauthorised(content)
245-
| MegolmV1AesSha2WithheldContent::Unavailable(content) => Some(&content.room_id),
254+
| MegolmV1AesSha2WithheldContent::Unavailable(content)
255+
| MegolmV1AesSha2WithheldContent::HistoryNotShared(content) => Some(&content.room_id),
246256
MegolmV1AesSha2WithheldContent::NoOlm(_) => None,
247257
}
248258
}
@@ -254,6 +264,7 @@ impl MegolmV1AesSha2WithheldContent {
254264
MegolmV1AesSha2WithheldContent::Unverified(_) => WithheldCode::Unverified,
255265
MegolmV1AesSha2WithheldContent::Unauthorised(_) => WithheldCode::Unauthorised,
256266
MegolmV1AesSha2WithheldContent::Unavailable(_) => WithheldCode::Unavailable,
267+
MegolmV1AesSha2WithheldContent::HistoryNotShared(_) => WithheldCode::HistoryNotShared,
257268
MegolmV1AesSha2WithheldContent::NoOlm(_) => WithheldCode::NoOlm,
258269
}
259270
}
@@ -266,7 +277,10 @@ impl MegolmV1AesSha2WithheldContent {
266277
WithheldCode::Unverified => Self::Unverified(content),
267278
WithheldCode::Unauthorised => Self::Unauthorised(content),
268279
WithheldCode::Unavailable => Self::Unavailable(content),
269-
_ => unreachable!("This constructor requires one of the common withheld codes"),
280+
WithheldCode::HistoryNotShared => Self::HistoryNotShared(content),
281+
WithheldCode::NoOlm | WithheldCode::_Custom(_) => {
282+
unreachable!("This constructor requires one of the common withheld codes")
283+
}
270284
}
271285
}
272286
}
@@ -349,14 +363,15 @@ impl TryFrom<WithheldHelper> for RoomKeyWithheldContent {
349363
WithheldCode::Blacklisted
350364
| WithheldCode::Unverified
351365
| WithheldCode::Unauthorised
352-
| WithheldCode::Unavailable => {
366+
| WithheldCode::Unavailable
367+
| WithheldCode::HistoryNotShared => {
353368
let content: CommonWithheldCodeContent = serde_json::from_value(value.other)?;
354369

355370
Self::MegolmV1AesSha2(MegolmV1AesSha2WithheldContent::from_code_and_content(
356371
value.code, content,
357372
))
358373
}
359-
_ => unknown(value)?,
374+
WithheldCode::_Custom(_) => unknown(value)?,
360375
},
361376
#[cfg(feature = "experimental-algorithms")]
362377
EventEncryptionAlgorithm::MegolmV2AesSha2 => match value.code {
@@ -367,14 +382,15 @@ impl TryFrom<WithheldHelper> for RoomKeyWithheldContent {
367382
WithheldCode::Blacklisted
368383
| WithheldCode::Unverified
369384
| WithheldCode::Unauthorised
370-
| WithheldCode::Unavailable => {
385+
| WithheldCode::Unavailable
386+
| WithheldCode::HistoryNotShared => {
371387
let content: CommonWithheldCodeContent = serde_json::from_value(value.other)?;
372388

373389
Self::MegolmV1AesSha2(MegolmV1AesSha2WithheldContent::from_code_and_content(
374390
value.code, content,
375391
))
376392
}
377-
_ => unknown(value)?,
393+
WithheldCode::_Custom(_) => unknown(value)?,
378394
},
379395
_ => unknown(value)?,
380396
})
@@ -397,7 +413,8 @@ impl Serialize for RoomKeyWithheldContent {
397413
MegolmV1AesSha2WithheldContent::BlackListed(content)
398414
| MegolmV1AesSha2WithheldContent::Unverified(content)
399415
| MegolmV1AesSha2WithheldContent::Unauthorised(content)
400-
| MegolmV1AesSha2WithheldContent::Unavailable(content) => WithheldHelper {
416+
| MegolmV1AesSha2WithheldContent::Unavailable(content)
417+
| MegolmV1AesSha2WithheldContent::HistoryNotShared(content) => WithheldHelper {
401418
algorithm,
402419
code,
403420
reason,
@@ -420,7 +437,8 @@ impl Serialize for RoomKeyWithheldContent {
420437
MegolmV1AesSha2WithheldContent::BlackListed(content)
421438
| MegolmV1AesSha2WithheldContent::Unverified(content)
422439
| MegolmV1AesSha2WithheldContent::Unauthorised(content)
423-
| MegolmV1AesSha2WithheldContent::Unavailable(content) => WithheldHelper {
440+
| MegolmV1AesSha2WithheldContent::Unavailable(content)
441+
| MegolmV1AesSha2WithheldContent::HistoryNotShared(content) => WithheldHelper {
424442
algorithm,
425443
code,
426444
reason,
@@ -534,6 +552,7 @@ pub(super) mod tests {
534552
WithheldCode::Blacklisted,
535553
WithheldCode::Unauthorised,
536554
WithheldCode::Unavailable,
555+
WithheldCode::HistoryNotShared,
537556
];
538557
for code in codes {
539558
let json = json(&code);

crates/matrix-sdk-crypto/src/types/events/utd_cause.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ impl UtdCause {
150150
WithheldCode::Blacklisted
151151
| WithheldCode::Unauthorised
152152
| WithheldCode::Unavailable
153+
| WithheldCode::HistoryNotShared
153154
| WithheldCode::NoOlm
154155
| WithheldCode::_Custom(_) => UtdCause::WithheldBySender,
155156
}

0 commit comments

Comments
 (0)