Rotate our session when there is a room history visibility change since the last outboundSession

This commit is contained in:
ariskotsomitopoulos 2022-04-29 13:44:26 +03:00 committed by Valere
parent d6358dcb16
commit 497f7cf044
4 changed files with 22 additions and 2 deletions

View File

@ -172,6 +172,8 @@ internal class MXMegolmEncryption(
if (session == null ||
// Need to make a brand new session?
session.needsRotation(sessionRotationPeriodMsgs, sessionRotationPeriodMs) ||
// Is there a room history visibility change since the last outboundSession
cryptoStore.needsRotationDueToVisibilityChange(roomId) ||
// Determine if we have shared with anyone we shouldn't have
session.sharedWithTooManyDevices(devicesInRoom)) {
Timber.tag(loggerTag.value).d("roomId:$roomId Starting new megolm session because we need to rotate.")

View File

@ -341,7 +341,14 @@ internal interface IMXCryptoStore {
fun storeCurrentOutboundGroupSessionForRoom(roomId: String, outboundGroupSession: OlmOutboundGroupSession?)
/**
* Remove an inbound group session.
* Returns true if there is a room history visibility change since the latest outbound
* session. Specifically when the room's history visibility setting changes to
* world_readable or shared from invited or joined, or changes to invited or joined from world_readable or shared
*/
fun needsRotationDueToVisibilityChange(roomId: String): Boolean
/**
* Remove an inbound group session
*
* @param sessionId the session identifier.
* @param senderKey the base64-encoded curve25519 key of the sender.

View File

@ -826,6 +826,8 @@ internal class RealmCryptoStore @Inject constructor(
if (outboundGroupSession != null) {
val info = realm.createObject(OutboundGroupSessionInfoEntity::class.java).apply {
creationTime = clock.epochMillis()
// Store the room history visibility on the outbound session creation
shouldShareHistory = entity.shouldShareHistory
putOutboundGroupSession(outboundGroupSession)
}
entity.outboundSessionInfo = info
@ -834,6 +836,14 @@ internal class RealmCryptoStore @Inject constructor(
}
}
override fun needsRotationDueToVisibilityChange(roomId: String): Boolean {
return doWithRealm(realmConfiguration) { realm ->
CryptoRoomEntity.getById(realm, roomId)?.let { entity ->
entity.shouldShareHistory != entity.outboundSessionInfo?.shouldShareHistory
}
} ?: false
}
/**
* Note: the result will be only use to export all the keys and not to use the OlmInboundGroupSessionWrapper2,
* so there is no need to use or update `inboundGroupSessionToRelease` for native memory management.

View File

@ -24,7 +24,8 @@ import timber.log.Timber
internal open class OutboundGroupSessionInfoEntity(
var serializedOutboundSessionData: String? = null,
var creationTime: Long? = null
var creationTime: Long? = null,
var shouldShareHistory: Boolean = false
) : RealmObject() {
fun getOutboundGroupSession(): OlmOutboundGroupSession? {