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

Defensive coding to ensure encryption when room was once e2e #5136

Merged
merged 3 commits into from Feb 21, 2022

Conversation

BillCarsonFr
Copy link
Member

@BillCarsonFr BillCarsonFr commented Feb 2, 2022

Some defensive coding on the crypto side to ensure that a room that was once known locally has encrypted (with a valid algorithm) will still stays encrypted.
This is very defensive and resist to state resets variations

In the CryptoRoom entity we add a new boolean to remember if the room was once encrypted with a valid algorithm.
We still store the alg got from the state, in order to properly give feedback of misconfigured room

Copy link
Contributor

@ganfra ganfra left a comment

Choose a reason for hiding this comment

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

One remark otherwise LGTM

* But the crypto layer has additional guaranty to ensure that encryption would never been reverted
* It's defensive coding out of precaution (if ever state is reset)
*/
fun shouldEncryptInRoom(roomId: String?): Boolean
Copy link
Contributor

Choose a reason for hiding this comment

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

If we have roomId as nullable here we should do the same for isRoomEncrypted I guess

Copy link
Member Author

Choose a reason for hiding this comment

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

roomId in event are optional (but almost always there), all codes ends up having optional, and function call wrapped in a let with an elvis.
It's a bit annoying, I let the caller pass an optional and return false if null in the code. I find the ending code more readable

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I know...the fact is I think we shouldn't have nullable roomId in Event :/

@github-actions
Copy link

github-actions bot commented Feb 2, 2022

Matrix SDK

Integration Tests Results:

  • [org.matrix.android.sdk.session]
    passed=
  • [org.matrix.android.sdk.account]
    passed=
  • [org.matrix.android.sdk.internal]
    passed=
  • [org.matrix.android.sdk.ordering]
    passed=
  • [org.matrix.android.sdk.PermalinkParserTest]
    passed=

@github-actions
Copy link

github-actions bot commented Feb 2, 2022

Unit Test Results

  84 files  ±0    84 suites  ±0   53s ⏱️ -3s
157 tests ±0  157 ✔️ ±0  0 💤 ±0  0 ±0 
504 runs  ±0  504 ✔️ ±0  0 💤 ±0  0 ±0 

Results for commit 48fffc3. ± Comparison against base commit ec2021d.

♻️ This comment has been updated with latest results.

Copy link
Member

@bmarty bmarty left a comment

Choose a reason for hiding this comment

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

Some thoughts

* But the crypto layer has additional guaranty to ensure that encryption would never been reverted
* It's defensive coding out of precaution (if ever state is reset)
*/
fun shouldEncryptInRoom(roomId: String?): Boolean
Copy link
Member

Choose a reason for hiding this comment

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

Do we really need to expose this to the application?
Using the CryptoStore may be enough internally

Also we can have isRoomEncrypted which return false and shouldEncryptInRoom which return true. How the UI should react in this case? Updating the UI will be maybe for another PR so this is the rational to expose that API?

Copy link
Member Author

Choose a reason for hiding this comment

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

The source of trust is still the presence or not of a state event of type m.room.encryption, so the UI will always reflect that. For example UI will detect a missconfigured encryption.
The shouldEncrypt is more some internal protection, and should not be reflected on UI.

Also I don't expose it anymore

Copy link
Contributor

Choose a reason for hiding this comment

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

It's still in the interface, can you remove it?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@@ -303,7 +303,7 @@ internal class DefaultSendService @AssistedInject constructor(
private fun internalSendMedia(allLocalEchoes: List<Event>, attachment: ContentAttachmentData, compressBeforeSending: Boolean): Cancelable {
val cancelableBag = CancelableBag()

allLocalEchoes.groupBy { cryptoSessionInfoProvider.isRoomEncrypted(it.roomId!!) }
allLocalEchoes.groupBy { cryptoStore.roomWasOnceEncrypted(it.roomId!!) }
Copy link
Member

Choose a reason for hiding this comment

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

We still have !! here, this is not ideal :/ but this is not Event coming from the wild so I think it's fine...

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes it's a private function, so acceptable :/

@bmarty bmarty added this to Inbox in Element Android PRs lifecycle via automation Feb 14, 2022
@bmarty bmarty moved this from Inbox to Crypto team actions in Element Android PRs lifecycle Feb 14, 2022
@BillCarsonFr BillCarsonFr force-pushed the feature/bca/crypto_ensure_e2e_defensive branch from 6424ec6 to 48fffc3 Compare February 18, 2022 09:08
Copy link
Member

@bmarty bmarty left a comment

Choose a reason for hiding this comment

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

Thanks for the update!

import org.matrix.android.sdk.internal.crypto.store.db.model.CryptoRoomEntityFields
import org.matrix.android.sdk.internal.util.database.RealmMigrator

// Version 14L Update the way we remember key sharing
Copy link
Member

Choose a reason for hiding this comment

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

This comment need to be updated (or removed). I can delete it on develop.

Copy link
Member

Choose a reason for hiding this comment

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

Element Android PRs lifecycle automation moved this from Crypto team actions to Reviewer approved Feb 21, 2022
@bmarty bmarty merged commit cf252b0 into develop Feb 21, 2022
Element Android PRs lifecycle automation moved this from Reviewer approved to Done Feb 21, 2022
@bmarty bmarty deleted the feature/bca/crypto_ensure_e2e_defensive branch February 21, 2022 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants