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

@ -1,76 +1,85 @@
/* /*
* Copyright 2022 The Matrix.org Foundation C.I.C. * Copyright 2022 The Matrix.org Foundation C.I.C.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.matrix.android.sdk.api.session.crypto.model package org.matrix.android.sdk.api.session.crypto.model
import com.squareup.moshi.JsonClass import com.squareup.moshi.JsonClass
import org.matrix.android.sdk.api.session.events.model.content.WithHeldCode import org.matrix.android.sdk.api.session.events.model.content.WithHeldCode
enum class TrailType { enum class TrailType {
OutgoingKeyForward, OutgoingKeyForward,
IncomingKeyForward, IncomingKeyForward,
OutgoingKeyWithheld, OutgoingKeyWithheld,
IncomingKeyRequest, IncomingKeyRequest,
Unknown Unknown
} }
interface AuditInfo { interface AuditInfo {
val roomId: String val roomId: String
val sessionId: String val sessionId: String
val senderKey: String val senderKey: String
val alg: String val alg: String
val userId: String val userId: String
val deviceId: String val deviceId: String
} }
@JsonClass(generateAdapter = true) @JsonClass(generateAdapter = true)
data class ForwardInfo( data class ForwardInfo(
override val roomId: String, override val roomId: String,
override val sessionId: String, override val sessionId: String,
override val senderKey: String, override val senderKey: String,
override val alg: String, override val alg: String,
override val userId: String, override val userId: String,
override val deviceId: String, override val deviceId: String,
val chainIndex: Long? val chainIndex: Long?
) : AuditInfo ) : AuditInfo
@JsonClass(generateAdapter = true) object UnknownInfo : AuditInfo {
data class WithheldInfo( override val roomId: String = ""
override val roomId: String, override val sessionId: String = ""
override val sessionId: String, override val senderKey: String = ""
override val senderKey: String, override val alg: String = ""
override val alg: String, override val userId: String = ""
val code: WithHeldCode, override val deviceId: String = ""
override val userId: String, }
override val deviceId: String
) : AuditInfo @JsonClass(generateAdapter = true)
data class WithheldInfo(
@JsonClass(generateAdapter = true) override val roomId: String,
data class IncomingKeyRequestInfo( override val sessionId: String,
override val roomId: String, override val senderKey: String,
override val sessionId: String, override val alg: String,
override val senderKey: String, val code: WithHeldCode,
override val alg: String, override val userId: String,
override val userId: String, override val deviceId: String
override val deviceId: String, ) : AuditInfo
val requestId: String
) : AuditInfo @JsonClass(generateAdapter = true)
data class IncomingKeyRequestInfo(
data class AuditTrail( override val roomId: String,
val ageLocalTs: Long, override val sessionId: String,
val type: TrailType, override val senderKey: String,
val info: AuditInfo override val alg: String,
) override val userId: String,
override val deviceId: String,
val requestId: String
) : AuditInfo
data class AuditTrail(
val ageLocalTs: Long,
val type: TrailType,
val info: AuditInfo
)

View file

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

View file

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

View file

@ -61,10 +61,10 @@ internal class MigrateCryptoTo016(realm: DynamicRealm) : RealmMigrator(realm, 16
.addIndex(AuditTrailEntityFields.TYPE) .addIndex(AuditTrailEntityFields.TYPE)
realm.schema.get("CryptoMetadataEntity") 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 { ?.transform {
// set the default value to true // 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 package org.matrix.android.sdk.internal.crypto.store.db.model
import org.matrix.android.sdk.api.extensions.tryOrNull 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.AuditTrail
import org.matrix.android.sdk.api.session.crypto.model.ForwardInfo 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.IncomingKeyRequestInfo
import org.matrix.android.sdk.api.session.crypto.model.TrailType 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.api.session.crypto.model.WithheldInfo
import org.matrix.android.sdk.internal.di.MoshiProvider import org.matrix.android.sdk.internal.di.MoshiProvider
@ -30,7 +30,7 @@ internal object AuditTrailMapper {
fun map(entity: AuditTrailEntity): AuditTrail? { fun map(entity: AuditTrailEntity): AuditTrail? {
val contentJson = entity.contentJson ?: return null val contentJson = entity.contentJson ?: return null
return when (entity.type) { return when (entity.type) {
TrailType.OutgoingKeyForward.name -> { TrailType.OutgoingKeyForward.name -> {
val info = tryOrNull { val info = tryOrNull {
MoshiProvider.providesMoshi().adapter(ForwardInfo::class.java).fromJson(contentJson) MoshiProvider.providesMoshi().adapter(ForwardInfo::class.java).fromJson(contentJson)
} ?: return null } ?: return null
@ -50,7 +50,7 @@ internal object AuditTrailMapper {
info = info info = info
) )
} }
TrailType.IncomingKeyRequest.name -> { TrailType.IncomingKeyRequest.name -> {
val info = tryOrNull { val info = tryOrNull {
MoshiProvider.providesMoshi().adapter(IncomingKeyRequestInfo::class.java).fromJson(contentJson) MoshiProvider.providesMoshi().adapter(IncomingKeyRequestInfo::class.java).fromJson(contentJson)
} ?: return null } ?: return null
@ -60,7 +60,7 @@ internal object AuditTrailMapper {
info = info info = info
) )
} }
TrailType.IncomingKeyForward.name -> { TrailType.IncomingKeyForward.name -> {
val info = tryOrNull { val info = tryOrNull {
MoshiProvider.providesMoshi().adapter(ForwardInfo::class.java).fromJson(contentJson) MoshiProvider.providesMoshi().adapter(ForwardInfo::class.java).fromJson(contentJson)
} ?: return null } ?: return null
@ -70,24 +70,11 @@ internal object AuditTrailMapper {
info = info info = info
) )
} }
else -> { else -> {
AuditTrail( AuditTrail(
ageLocalTs = entity.ageLocalTs ?: 0, ageLocalTs = entity.ageLocalTs ?: 0,
type = TrailType.Unknown, type = TrailType.Unknown,
info = object : AuditInfo { info = UnknownInfo
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() = ""
}
) )
} }
} }

View file

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