Skip to content

feat(crypto): Add EncryptionInfo to Decrypted to-device variant #5074

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

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

Conversation

BillCarsonFr
Copy link
Member

The ProcessedToDeviceEvent::Decrypted variant now also have an EncryptionInfo field.
Format changed from Decrypted(Raw<AnyToDeviceEvent>) to Decrypted { raw: Raw<AnyToDeviceEvent>, encryption_info: EncryptionInfo) }

It is needed for future work on widget in order to support encrypted to device properly viaadd_event_handler

This is a follow-up on

Signed-off-by:

Copy link

codecov bot commented May 22, 2025

Codecov Report

Attention: Patch coverage is 95.23810% with 2 lines in your changes missing coverage. Please review.

Project coverage is 85.82%. Comparing base (6d5ad4e) to head (0c4a895).

Files with missing lines Patch % Lines
...es/matrix-sdk-common/src/deserialized_responses.rs 90.00% 1 Missing ⚠️
crates/matrix-sdk-crypto/src/types/mod.rs 91.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5074      +/-   ##
==========================================
+ Coverage   85.80%   85.82%   +0.01%     
==========================================
  Files         332      332              
  Lines       36149    36171      +22     
==========================================
+ Hits        31017    31042      +25     
+ Misses       5132     5129       -3     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@@ -127,6 +130,7 @@ pub(crate) struct DecryptionResult {
pub event: Box<AnyDecryptedOlmEvent>,
pub raw_event: Raw<AnyToDeviceEvent>,
pub sender_key: Curve25519PublicKey,
Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe we can now remove this sender_key field? given that it is now in the encryption info.
It is a bit less convenient because it is now in the AlgorithmInfo that could be olm or megolm.
Maybe I could had a function sender_key() that would use a as_variant!(self, Self::OlmV1Curve25519AesSha2) ?

/// The info if the event was encrypted using m.olm.v1.curve25519-aes-sha2
OlmV1Curve25519AesSha2 {
// The sender device key, base64 encoded
curve25519_public_key_base64: String,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why the stringly typing? Curve25519Publickey is a Copy type so it's cheaper to copy it around, provides stronger typing and has a to_base64() method if you do need a string.

I feel like we've been here already once.

Copy link
Member Author

Choose a reason for hiding this comment

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

It tried to keep it consistent with the other variant.
I suppose I have then to use serde(deserialize_with = "deserialize_curve_key", .. "serialize_curve_key"?
I'll do it too for MegolmV1AesSha2 to stay consistant

Comment on lines +1473 to +1477
{
assert_eq!(session_id, expected_session_id);
} else {
panic!("Expected MegolmV1AesSha2");
}
Copy link
Contributor

Choose a reason for hiding this comment

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

There's assert_let!() and assert_matches!() for this kind of code snippet.

#[serde(deserialize_with = "deserialize_decrypted_variant")]
Decrypted {
/// The raw decrypted event
raw: Raw<AnyToDeviceEvent>,
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't need to be accessible?

Suggested change
raw: Raw<AnyToDeviceEvent>,
pub raw: Raw<AnyToDeviceEvent>,

/// The raw decrypted event
raw: Raw<AnyToDeviceEvent>,
/// The olm encryption info
encryption_info: EncryptionInfo,
Copy link
Contributor

Choose a reason for hiding this comment

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

Likewise:

Suggested change
encryption_info: EncryptionInfo,
pub encryption_info: EncryptionInfo,

Comment on lines +676 to +682
if json.get("raw").is_some() && json.get("encryption_info").is_some() {
// If the "raw" field is present, it's a NewFormat
#[derive(Deserialize)]
struct NewFormat {
raw: Raw<AnyToDeviceEvent>,
encryption_info: EncryptionInfo,
}
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens if someone puts a raw and encryption_info field into the JSON of the event?

It seems to me that the sender of the event, perhaps in collusion with the homeserver, might insert an encryption_info on their own.

Come to think of, why does this need to support Serialize and Deserialize? Because the widgets API will transfer this verbatim to the widget using JSON?

In any case, I think it's safer to not have a migration here since we don't seem to need it.

Copy link
Member Author

@BillCarsonFr BillCarsonFr May 30, 2025

Choose a reason for hiding this comment

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

Come to think of, why does this need to support Serialize and Deserialize?

I'll check but I think it mostly to export them to bindings?
But I agree that those should not be persisted, but we previously said that if we put something as serializable we should provide migration. I wish we had another trait for things that can be persisted

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