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

Use the DecryptedOlmV1Event when encrypting to-device events #4690

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

poljar
Copy link
Contributor

@poljar poljar commented Feb 19, 2025

This requires a bit of a hack, but I think the stronger typing and having the fields defined in a single place make this worth it.

@poljar poljar requested review from a team as code owners February 19, 2025 13:20
@poljar poljar requested review from Hywan and richvdh and removed request for a team February 19, 2025 13:20
Copy link

codecov bot commented Feb 19, 2025

Codecov Report

Attention: Patch coverage is 87.80488% with 5 lines in your changes missing coverage. Please review.

Project coverage is 85.84%. Comparing base (9de6d28) to head (ffbf31c).
Report is 47 commits behind head on main.

Files with missing lines Patch % Lines
crates/matrix-sdk-crypto/src/olm/session.rs 80.00% 3 Missing ⚠️
...rates/matrix-sdk-crypto/src/types/events/olm_v1.rs 83.33% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4690      +/-   ##
==========================================
+ Coverage   85.82%   85.84%   +0.01%     
==========================================
  Files         292      292              
  Lines       33761    33779      +18     
==========================================
+ Hits        28974    28996      +22     
+ Misses       4787     4783       -4     

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

@poljar poljar force-pushed the poljar/stronger-typing-olm-encryption branch from bfe7f6a to 4195afa Compare February 19, 2025 13:45
…-device events

This ensures that new fields that are added to the
`m.olm.v1.curve25519-aes-sha` content need to be added in a single
place.
@poljar poljar force-pushed the poljar/stronger-typing-olm-encryption branch from 4195afa to ffbf31c Compare February 19, 2025 14:34
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.

Looks good to me, thanks!

Comment on lines +164 to +166
// So we just leave EVENT_TYPE out. This works because the serialization is
// using `event_type()` and this type is contained to this function.
const EVENT_TYPE: &'static str = "";
Copy link
Member

Choose a reason for hiding this comment

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

So EVENT_TYPE is useless and never used in this context, is that alright? If yes, maybe it should be said explicitly in the doc?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's used in all the other types that implement EventType. Only this one implementation is a special hack.

Copy link
Member

Choose a reason for hiding this comment

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

I assume Ivan means we should add this to the documentation of this implementation, to which, +1.

Suggested change
// So we just leave EVENT_TYPE out. This works because the serialization is
// using `event_type()` and this type is contained to this function.
const EVENT_TYPE: &'static str = "";
// We have to provide `EVENT_TYPE` to conform to the `EventType` trait, but
// don't actually use it, so we just leave it empty.
//
// This works because the serialization uses `event_type()` and this type is contained
// to this function.
const EVENT_TYPE: &'static str = "";

Copy link
Member

Choose a reason for hiding this comment

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

Yes, this. Thanks for clarifying my words.

Copy link
Member

@richvdh richvdh left a comment

Choose a reason for hiding this comment

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

LGTM in general, a few nits

Comment on lines 43 to 44
/// **Note**: This should never be implemented manually, this takes the
/// event type from the constant.
Copy link
Member

Choose a reason for hiding this comment

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

This is no longer true? Or at least, the word "never" should be softened to "not usually" or something.

You could fix the comma splice while you're there.

Comment on lines -428 to 437
let (event_type, content) = {
let content = {
let export = if let Some(index) = message_index {
session.export_at_index(index).await
} else {
session.export().await
};
let content: ForwardedRoomKeyContent = export.try_into()?;

(content.event_type(), content)
content
};
Copy link
Member

@richvdh richvdh Feb 20, 2025

Choose a reason for hiding this comment

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

Suggested change
let (event_type, content) = {
let content = {
let export = if let Some(index) = message_index {
session.export_at_index(index).await
} else {
session.export().await
};
let content: ForwardedRoomKeyContent = export.try_into()?;
(content.event_type(), content)
content
};
let content: ForwardedRoomKeyContent = {
let export = if let Some(index) = message_index {
session.export_at_index(index).await
} else {
session.export().await
};
export.try_into()?
};

let event_type = content.event_type().to_owned();
let (used_session, content) = device.encrypt(&event_type, content).await?;

let event_type = content.event_type().to_owned();
Copy link
Member

Choose a reason for hiding this comment

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

this line looks redundant?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

At first glance one would think so. But the first event_type is for content while the second event_type is for content after the device.encrypt() step.

Perhaps renaming the second content to encrypted_content might make this clearer.

Copy link
Member

Choose a reason for hiding this comment

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

oh right, yes. Some different names might help.

@@ -162,7 +162,10 @@ impl AnyDecryptedOlmEvent {
}

/// An `m.olm.v1.curve25519-aes-sha2` decrypted to-device event.
#[derive(Clone, Debug, Deserialize, Serialize)]
///
/// **Note**: This event will reserialize events lossy, unknown fields will be
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// **Note**: This event will reserialize events lossy, unknown fields will be
/// **Note**: This event will reserialize events lossily; unknown fields will be

@@ -182,6 +185,52 @@ where
pub content: C,
}

impl<C: EventType + Debug + Sized + Serialize> Serialize for DecryptedOlmV1Event<C> {
Copy link
Member

Choose a reason for hiding this comment

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

A comment as to why this is needed/what it does differently from derive(Serialize) might be helpful.

Comment on lines +160 to +162
// This is a bit of a hack, usually we just define the `EVENT_TYPE` and use the
// default implementation of `event_type()`, we can't do this here
// because the event type isn't static.
Copy link
Member

Choose a reason for hiding this comment

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

multi-way comma splice!

Suggested change
// This is a bit of a hack, usually we just define the `EVENT_TYPE` and use the
// default implementation of `event_type()`, we can't do this here
// because the event type isn't static.
// This is a bit of a hack: usually we just define the `EVENT_TYPE` and use the
// default implementation of `event_type()`. We can't do this here
// because the event type isn't static.

Comment on lines +164 to +166
// So we just leave EVENT_TYPE out. This works because the serialization is
// using `event_type()` and this type is contained to this function.
const EVENT_TYPE: &'static str = "";
Copy link
Member

Choose a reason for hiding this comment

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

I assume Ivan means we should add this to the documentation of this implementation, to which, +1.

Suggested change
// So we just leave EVENT_TYPE out. This works because the serialization is
// using `event_type()` and this type is contained to this function.
const EVENT_TYPE: &'static str = "";
// We have to provide `EVENT_TYPE` to conform to the `EventType` trait, but
// don't actually use it, so we just leave it empty.
//
// This works because the serialization uses `event_type()` and this type is contained
// to this function.
const EVENT_TYPE: &'static str = "";

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.

3 participants