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

ElementR: Ensure Encryption order per room #3973

Merged
merged 23 commits into from
Jan 2, 2024

Conversation

BillCarsonFr
Copy link
Member

@BillCarsonFr BillCarsonFr commented Dec 22, 2023

Fixes element-hq/element-web#26684

Encryption order was not ensured with the existing code. So in some case editing an inflight message would encrypt the edit first and schedule it first. That will cause an error because it will send an edit of an unknown event.
It can also invert the message order.

There is a sheduler currently but it doesn't handle encryption, it only queues the event for sending.
The scheduler should probably handle the encryption, but for now I modified RoomEncryptor to ensure order.

Notice that there was already a ordering mechanism but only to ensure order of olmMachine.shareRoomKey but it was not enough.

Added a test to reproduce the problem if the fix is not present.

Checklist

  • Tests written for new code (and old code if feasible)
  • Linter and other CI checks pass
  • Sign-off given on the changes (see CONTRIBUTING.md)

Here's what your changelog entry will look like:

🐛 Bug Fixes

@BillCarsonFr BillCarsonFr added T-Defect A-Element-R Issues affecting the port of Element's crypto layer to Rust labels Dec 22, 2023
@BillCarsonFr BillCarsonFr marked this pull request as ready for review December 22, 2023 09:16
@BillCarsonFr BillCarsonFr requested a review from a team as a code owner December 22, 2023 09:16
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.

as discussed elsewhere: I don't think this does the right thing

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 mostly. A few suggestions, nits, etc.

src/rust-crypto/RoomEncryptor.ts Outdated Show resolved Hide resolved
Comment on lines 127 to 128
// message is finally sent. The actual event encryption request will arrive after wait for the prepareForEncryption
// promise to resolve, and then do again an ensureEncryptionSession that should be no op as we already share the room key.
Copy link
Member

Choose a reason for hiding this comment

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

The actual event encryption request will arrive after wait for the prepareForEncryption promise to resolve

I don't really understand this, or what it is trying to say. "The actual event encryption request" == "a call to encryptEvent"? So you're saying that the application will wait for prepareForEncryption to complete before it calls encryptEvent? I don't think that's right.

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 think it's how it's supposed to work. Legacy is doing the same.
If you starts typing then call encrypt event, we let the prepare finish, then do the encrypt. The share room key part for the secong call should be no op as the room key is already shared

Copy link
Member Author

Choose a reason for hiding this comment

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

Notice that anyhow the olmMachine.shareRoomKey() should be locked per room

Copy link
Member

Choose a reason for hiding this comment

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

if you starts typing then call encrypt event, we let the prepare finish, then do the encrypt.

but that's not what the comment says?

Copy link
Member

Choose a reason for hiding this comment

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

... or rather the comment says that we expect the application to wait, and I don't think we do. That's why we have to wait here.

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 updated the comment:

// If `encryptEvent` is invoked before `prepareForEncryption` has completed, the `encryptEvent` call will wait for
// `prepareForEncryption` to complete before executing.
// The part where `encryptEvent` shares the room key would be no op as it was already performed by prepareForEncryption.
        

src/rust-crypto/RoomEncryptor.ts Outdated Show resolved Hide resolved
src/rust-crypto/RoomEncryptor.ts Outdated Show resolved Hide resolved
src/rust-crypto/RoomEncryptor.ts Outdated Show resolved Hide resolved
src/rust-crypto/RoomEncryptor.ts Show resolved Hide resolved
src/rust-crypto/RoomEncryptor.ts Outdated Show resolved Hide resolved
spec/unit/rust-crypto/RoomEncryptor.spec.ts Outdated Show resolved Hide resolved
spec/unit/rust-crypto/RoomEncryptor.spec.ts Show resolved Hide resolved
src/rust-crypto/RoomEncryptor.ts Outdated Show resolved Hide resolved
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.

A couple of nits, lgtm otherwise

src/rust-crypto/RoomEncryptor.ts Outdated Show resolved Hide resolved
src/rust-crypto/RoomEncryptor.ts Outdated Show resolved Hide resolved
BillCarsonFr and others added 2 commits January 2, 2024 13:13
Co-authored-by: Richard van der Hoff <[email protected]>
Co-authored-by: Richard van der Hoff <[email protected]>
@BillCarsonFr BillCarsonFr added this pull request to the merge queue Jan 2, 2024
Merged via the queue into develop with commit a1ff63a Jan 2, 2024
24 checks passed
@BillCarsonFr BillCarsonFr deleted the valere/element-r/ensure_encryption_order branch January 2, 2024 13:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Element-R Issues affecting the port of Element's crypto layer to Rust T-Defect
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Element-R: Wrong Message send order (2nd inflight message finally sent first)
2 participants