Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: Implement OlmMachine::{set_,}room_settings #3042

Merged
merged 6 commits into from
Jan 25, 2024
Merged

Conversation

richvdh
Copy link
Member

@richvdh richvdh commented Jan 19, 2024

We need to make sure we guard against servers downgrading the encryption settings, or breaking them with unsupported algorithms. It's not entirely clear what the expectation is here, but legacy crypto in matrix-js-sdk blocks any changes to the settings at all, so we'll follow suit for now.

This PR also extends the supported RoomSettings. Suggest reviewing each commit separately.

  • Public API changes documented in changelogs (optional)

Signed-off-by:

@richvdh richvdh requested a review from a team as a code owner January 19, 2024 18:41
@richvdh richvdh requested review from andybalaam and removed request for a team January 19, 2024 18:41
/// If the settings are valid, they will be persisted to the crypto store.
/// These settings are not used directly by this library, but the saved
/// settings can be retrieved via [`get_room_settings`].
pub async fn set_room_settings(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not completely sure if this is the best place for this API: OlmMachine already has a million methods and adding more isn't my favourite thing. But the only other likely-looking place seems to be the Store itself, and given this is application-level logic, that doesn't feel right.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with your reasoning. Let's keep it here for the moment. We can refactor later.

Copy link

codecov bot commented Jan 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (d85c7b2) 83.67% compared to head (0b01a2a) 83.72%.
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3042      +/-   ##
==========================================
+ Coverage   83.67%   83.72%   +0.04%     
==========================================
  Files         222      222              
  Lines       23288    23310      +22     
==========================================
+ Hits        19487    19516      +29     
+ Misses       3801     3794       -7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@richvdh richvdh force-pushed the rav/room_settings branch 3 times, most recently from 7ed63ae to 532897a Compare January 19, 2024 19:07
We need to make sure we guard against servers downgrading the encryption
settings, or breaking them with unsupported algorithms. It's not *entirely*
clear what the expectation is here, but legacy crypto in matrix-js-sdk blocks
any changes to the settings at all, so we'll follow suit for now.
Copy link
Member

@Hywan Hywan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR is really good. Commits are well written, the patch contains a lot of docs (I can't highlight enough how much I like that), the tests are good… Just a few tiny nitty gritty things , but overall it looks great. Thanks!

@@ -855,12 +856,15 @@ macro_rules! cryptostore_integration_tests {
let settings_1 = RoomSettings {
algorithm: EventEncryptionAlgorithm::MegolmV1AesSha2,
only_allow_trusted_devices: true,
session_rotation_period: Some(Duration::from_millis(10000)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably a bit more Rust idiomatic to write this as follows:

- Duration::from_millis(10000)),
+ Duration::from_millis(10_000)),

I also wonder if we could simply use : from_secs(10)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

/// encryption algorithm or whether to encrypt only for trusted devices.
///
/// These settings can be modified via [`OlmMachine::set_room_settings`].
pub async fn get_room_settings(&self, room_id: &RoomId) -> StoreResult<Option<RoomSettings>> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The get_ prefix isn't really Rust idiomatic. I know it's present in the store API, let's not propagate this error :-]. Can you rename get_room_settings to room_settings please?

/// If the settings are valid, they will be persisted to the crypto store.
/// These settings are not used directly by this library, but the saved
/// settings can be retrieved via [`get_room_settings`].
pub async fn set_room_settings(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with your reasoning. Let's keep it here for the moment. We can refactor later.

Comment on lines +2074 to +2076
// `set_room_settings`. By way of an easy way to do so, we start a
// StoreTransaction. There's no need to commit() it: we're just using it as a
// lock guard.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me 👍. Thanks for the comment, that's the kind of comment I like!

Comment on lines 4156 to 4159
machine
.set_room_settings(room_id!("!test:localhost"), &RoomSettings::default())
.await
.unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is not necessary for this test. I understand the idea, but it looks we can safely remove it.


/// The maximum number of messages an encryption session should be used for,
/// before it is rotated.
pub session_rotation_period_msgs: Option<usize>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use long names please, like session_rotation_period_for_messages maybe?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can, but it might be worth noting that this name is based on the name in the matrix protocol (rotation_period_msgs, https://spec.matrix.org/v1.9/client-server-api/#mroomencryption)

I'm not really sure I can think of a better name than session_rotation_period_messages. number_of_messages_to_rotate_session_after feels very cumbersome.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh OK. Then session_rotation_period_messages is fine for me. Thanks!

Copy link
Member Author

@richvdh richvdh Jan 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can, but it might be worth noting that this name is based on the name in the matrix protocol (rotation_period_msgs, https://spec.matrix.org/v1.9/client-server-api/#mroomencryption)

There is also a rotation_period_msgs in EncryptionSettings:

... though why it uses a u64 I don't know.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh OK. Then session_rotation_period_messages is fine for me. Thanks!

Ok!

@richvdh richvdh changed the title crypto: Implement OlmMachine::{get,set}_room_settings crypto: Implement OlmMachine::{set_,}room_settings Jan 22, 2024
@richvdh richvdh requested a review from Hywan January 23, 2024 11:05
@richvdh
Copy link
Member Author

richvdh commented Jan 23, 2024

@Hywan I think I've addressed all your comments. Could you take another look?

Copy link
Member

@Hywan Hywan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@Hywan Hywan enabled auto-merge January 25, 2024 15:19
@Hywan Hywan merged commit 4d2c3e3 into main Jan 25, 2024
34 checks passed
@Hywan Hywan deleted the rav/room_settings branch January 25, 2024 15:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants