Code review

This commit is contained in:
Valere 2022-04-28 09:09:38 +02:00
parent 23f32aae84
commit 0f06368027
6 changed files with 105 additions and 103 deletions

View file

@ -47,6 +47,15 @@ data class ForwardInfo(
val chainIndex: Long?
) : AuditInfo
object UnknownInfo : AuditInfo {
override val roomId: String = ""
override val sessionId: String = ""
override val senderKey: String = ""
override val alg: String = ""
override val userId: String = ""
override val deviceId: String = ""
}
@JsonClass(generateAdapter = true)
data class WithheldInfo(
override val roomId: String,

View file

@ -96,9 +96,9 @@ internal class IncomingKeyRequestManager @Inject constructor(
val requestId = this.requestId ?: return null
if (body.algorithm != MXCRYPTO_ALGORITHM_MEGOLM) return null
val action = when (this.action) {
"request" -> MegolmRequestAction.Request
"request" -> MegolmRequestAction.Request
"request_cancellation" -> MegolmRequestAction.Cancel
else -> null
else -> null
} ?: return null
return ValidMegolmRequestBody(
requestId = requestId,
@ -112,6 +112,11 @@ internal class IncomingKeyRequestManager @Inject constructor(
}
fun addNewIncomingRequest(senderId: String, request: RoomKeyShareRequest) {
if (!cryptoStore.isKeyGossipingEnabled()) {
Timber.tag(loggerTag.value)
.i("Ignore incoming key request as per crypto config in room ${request.body?.roomId}")
return
}
outgoingRequestScope.launch {
// It is important to handle requests in order
sequencer.post {
@ -422,7 +427,8 @@ internal class IncomingKeyRequestManager @Inject constructor(
MXCRYPTO_ALGORITHM_MEGOLM,
requestingDevice.userId,
requestingDevice.deviceId,
chainIndex)
chainIndex
)
true
} catch (failure: Throwable) {
Timber.tag(loggerTag.value)

View file

@ -953,13 +953,13 @@ internal class RealmCryptoStore @Inject constructor(
override fun enableKeyGossiping(enable: Boolean) {
doRealmTransaction(realmConfiguration) {
it.where<CryptoMetadataEntity>().findFirst()?.globalEnableKeyRequestingAndSharing = enable
it.where<CryptoMetadataEntity>().findFirst()?.globalEnableKeyGossiping = enable
}
}
override fun isKeyGossipingEnabled(): Boolean {
return doWithRealm(realmConfiguration) {
it.where<CryptoMetadataEntity>().findFirst()?.globalEnableKeyRequestingAndSharing
it.where<CryptoMetadataEntity>().findFirst()?.globalEnableKeyGossiping
} ?: true
}

View file

@ -61,10 +61,10 @@ internal class MigrateCryptoTo016(realm: DynamicRealm) : RealmMigrator(realm, 16
.addIndex(AuditTrailEntityFields.TYPE)
realm.schema.get("CryptoMetadataEntity")
?.addField(CryptoMetadataEntityFields.GLOBAL_ENABLE_KEY_REQUESTING_AND_SHARING, Boolean::class.java)
?.addField(CryptoMetadataEntityFields.GLOBAL_ENABLE_KEY_GOSSIPING, Boolean::class.java)
?.transform {
// set the default value to true
it.setBoolean(CryptoMetadataEntityFields.GLOBAL_ENABLE_KEY_REQUESTING_AND_SHARING, true)
it.setBoolean(CryptoMetadataEntityFields.GLOBAL_ENABLE_KEY_GOSSIPING, true)
}
}
}

View file

@ -17,11 +17,11 @@
package org.matrix.android.sdk.internal.crypto.store.db.model
import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.api.session.crypto.model.AuditInfo
import org.matrix.android.sdk.api.session.crypto.model.AuditTrail
import org.matrix.android.sdk.api.session.crypto.model.ForwardInfo
import org.matrix.android.sdk.api.session.crypto.model.IncomingKeyRequestInfo
import org.matrix.android.sdk.api.session.crypto.model.TrailType
import org.matrix.android.sdk.api.session.crypto.model.UnknownInfo
import org.matrix.android.sdk.api.session.crypto.model.WithheldInfo
import org.matrix.android.sdk.internal.di.MoshiProvider
@ -30,7 +30,7 @@ internal object AuditTrailMapper {
fun map(entity: AuditTrailEntity): AuditTrail? {
val contentJson = entity.contentJson ?: return null
return when (entity.type) {
TrailType.OutgoingKeyForward.name -> {
TrailType.OutgoingKeyForward.name -> {
val info = tryOrNull {
MoshiProvider.providesMoshi().adapter(ForwardInfo::class.java).fromJson(contentJson)
} ?: return null
@ -50,7 +50,7 @@ internal object AuditTrailMapper {
info = info
)
}
TrailType.IncomingKeyRequest.name -> {
TrailType.IncomingKeyRequest.name -> {
val info = tryOrNull {
MoshiProvider.providesMoshi().adapter(IncomingKeyRequestInfo::class.java).fromJson(contentJson)
} ?: return null
@ -60,7 +60,7 @@ internal object AuditTrailMapper {
info = info
)
}
TrailType.IncomingKeyForward.name -> {
TrailType.IncomingKeyForward.name -> {
val info = tryOrNull {
MoshiProvider.providesMoshi().adapter(ForwardInfo::class.java).fromJson(contentJson)
} ?: return null
@ -70,24 +70,11 @@ internal object AuditTrailMapper {
info = info
)
}
else -> {
else -> {
AuditTrail(
ageLocalTs = entity.ageLocalTs ?: 0,
type = TrailType.Unknown,
info = object : AuditInfo {
override val roomId: String
get() = ""
override val sessionId: String
get() = ""
override val senderKey: String
get() = ""
override val alg: String
get() = ""
override val userId: String
get() = ""
override val deviceId: String
get() = ""
}
info = UnknownInfo
)
}
}

View file

@ -34,7 +34,7 @@ internal open class CryptoMetadataEntity(
// Settings for blacklisting unverified devices.
var globalBlacklistUnverifiedDevices: Boolean = false,
// setting to enable or disable key gossiping
var globalEnableKeyRequestingAndSharing: Boolean = true,
var globalEnableKeyGossiping: Boolean = true,
// The keys backup version currently used. Null means no backup.
var backupVersion: String? = null,