Skip to content

Commit 5712c19

Browse files
Rotate our session when there is a room history visibility change since the last outboundSession
1 parent efd082c commit 5712c19

File tree

5 files changed

+25
-1
lines changed

5 files changed

+25
-1
lines changed

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/algorithms/megolm/MXMegolmEncryption.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ internal class MXMegolmEncryption(
167167
if (session == null ||
168168
// Need to make a brand new session?
169169
session.needsRotation(sessionRotationPeriodMsgs, sessionRotationPeriodMs) ||
170+
// Is there a room history visibility change since the last outboundSession
171+
cryptoStore.needsRotationDueToVisibilityChange(roomId) ||
170172
// Determine if we have shared with anyone we shouldn't have
171173
session.sharedWithTooManyDevices(devicesInRoom)) {
172174
Timber.tag(loggerTag.value).d("roomId:$roomId Starting new megolm session because we need to rotate.")

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/IMXCryptoStore.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,13 @@ internal interface IMXCryptoStore {
344344
*/
345345
fun storeCurrentOutboundGroupSessionForRoom(roomId: String, outboundGroupSession: OlmOutboundGroupSession?)
346346

347+
/**
348+
* Returns true if there is a room history visibility change since the latest outbound
349+
* session. Specifically when the room's history visibility setting changes to
350+
* world_readable or shared from invited or joined, or changes to invited or joined from world_readable or shared
351+
*/
352+
fun needsRotationDueToVisibilityChange(roomId: String): Boolean
353+
347354
/**
348355
* Remove an inbound group session
349356
*

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,8 @@ internal class RealmCryptoStore @Inject constructor(
821821
if (outboundGroupSession != null) {
822822
val info = realm.createObject(OutboundGroupSessionInfoEntity::class.java).apply {
823823
creationTime = System.currentTimeMillis()
824+
// Store the room history visibility on the outbound session creation
825+
shouldShareHistory = entity.shouldShareHistory
824826
putOutboundGroupSession(outboundGroupSession)
825827
}
826828
entity.outboundSessionInfo = info
@@ -829,6 +831,14 @@ internal class RealmCryptoStore @Inject constructor(
829831
}
830832
}
831833

834+
override fun needsRotationDueToVisibilityChange(roomId: String): Boolean {
835+
return doWithRealm(realmConfiguration) { realm ->
836+
CryptoRoomEntity.getById(realm, roomId)?.let { entity ->
837+
entity.shouldShareHistory != entity.outboundSessionInfo?.shouldShareHistory
838+
}
839+
} ?: false
840+
}
841+
832842
/**
833843
* Note: the result will be only use to export all the keys and not to use the OlmInboundGroupSessionWrapper2,
834844
* so there is no need to use or update `inboundGroupSessionToRelease` for native memory management

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/migration/MigrateCryptoTo016.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package org.matrix.android.sdk.internal.crypto.store.db.migration
1919
import io.realm.DynamicRealm
2020
import org.matrix.android.sdk.internal.crypto.store.db.model.CryptoRoomEntityFields
2121
import org.matrix.android.sdk.internal.crypto.store.db.model.OlmInboundGroupSessionEntityFields
22+
import org.matrix.android.sdk.internal.crypto.store.db.model.OutboundGroupSessionInfoEntityFields
2223
import org.matrix.android.sdk.internal.util.database.RealmMigrator
2324

2425
// Version 16L enhance OlmInboundGroupSessionEntity to support shared history for MSC3061
@@ -31,5 +32,8 @@ internal class MigrateCryptoTo016(realm: DynamicRealm) : RealmMigrator(realm, 16
3132

3233
realm.schema.get("CryptoRoomEntity")
3334
?.addField(CryptoRoomEntityFields.SHOULD_SHARE_HISTORY, Boolean::class.java)
35+
36+
realm.schema.get("OutboundGroupSessionInfoEntity")
37+
?.addField(OutboundGroupSessionInfoEntityFields.SHOULD_SHARE_HISTORY, Boolean::class.java)
3438
}
3539
}

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/OutboundGroupSessionInfoEntity.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ import timber.log.Timber
2424

2525
internal open class OutboundGroupSessionInfoEntity(
2626
var serializedOutboundSessionData: String? = null,
27-
var creationTime: Long? = null
27+
var creationTime: Long? = null,
28+
var shouldShareHistory: Boolean = false
2829
) : RealmObject() {
2930

3031
fun getOutboundGroupSession(): OlmOutboundGroupSession? {

0 commit comments

Comments
 (0)