Update encrypted raw json with decrypted event content for event matching condition

This commit is contained in:
Florian Renaud 2023-03-01 10:42:57 +01:00
parent b20d74aa1d
commit ccf971afc4
2 changed files with 60 additions and 4 deletions

View file

@ -39,8 +39,20 @@ class EventMatchCondition(
override fun technicalDescription() = "'$key' matches '$pattern'" override fun technicalDescription() = "'$key' matches '$pattern'"
fun isSatisfied(event: Event): Boolean { fun isSatisfied(event: Event): Boolean {
val rawJson: Map<*, *> = event.mxDecryptionResult?.payload val rawJson: Map<*, *> = (MoshiProvider.providesMoshi().adapter(Event::class.java).toJsonValue(event) as? Map<*, *>)
?: MoshiProvider.providesMoshi().adapter(Event::class.java).toJsonValue(event) as? Map<*, *> ?.let { rawJson ->
val decryptedRawJson = event.mxDecryptionResult?.payload
if (decryptedRawJson != null) {
rawJson
.toMutableMap()
.apply {
set("type", decryptedRawJson["type"])
set("content", decryptedRawJson["content"])
}
} else {
rawJson
}
}
?: return false ?: return false
val value = extractField(rawJson, key) ?: return false val value = extractField(rawJson, key) ?: return false

View file

@ -23,7 +23,11 @@ import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue import org.junit.Assert.assertTrue
import org.junit.Test import org.junit.Test
import org.matrix.android.sdk.MatrixTest import org.matrix.android.sdk.MatrixTest
import org.matrix.android.sdk.api.crypto.MXCRYPTO_ALGORITHM_MEGOLM
import org.matrix.android.sdk.api.session.crypto.model.OlmDecryptionResult
import org.matrix.android.sdk.api.session.events.model.Event import org.matrix.android.sdk.api.session.events.model.Event
import org.matrix.android.sdk.api.session.events.model.EventType
import org.matrix.android.sdk.api.session.events.model.content.EncryptedEventContent
import org.matrix.android.sdk.api.session.events.model.toContent import org.matrix.android.sdk.api.session.events.model.toContent
import org.matrix.android.sdk.api.session.room.Room import org.matrix.android.sdk.api.session.room.Room
import org.matrix.android.sdk.api.session.room.members.MembershipService import org.matrix.android.sdk.api.session.room.members.MembershipService
@ -38,12 +42,25 @@ class PushRulesConditionTest : MatrixTest {
* Test EventMatchCondition * Test EventMatchCondition
* ========================================================================================== */ * ========================================================================================== */
private fun createFakeEncryptedEvent() = Event(
type = EventType.ENCRYPTED,
eventId = "mx0",
roomId = "!fakeRoom",
content = EncryptedEventContent(
algorithm = MXCRYPTO_ALGORITHM_MEGOLM,
ciphertext = "AwgBEpACQEKOkd4Gp0+gSXG4M+btcrnPgsF23xs/lUmS2I4YjmqF...",
sessionId = "TO2G4u2HlnhtbIJk",
senderKey = "5e3EIqg3JfooZnLQ2qHIcBarbassQ4qXblai0",
deviceId = "FAKEE"
).toContent()
)
private fun createSimpleTextEvent(text: String): Event { private fun createSimpleTextEvent(text: String): Event {
return Event( return Event(
type = "m.room.message", type = EventType.MESSAGE,
eventId = "mx0", eventId = "mx0",
content = MessageTextContent("m.text", text).toContent(), content = MessageTextContent("m.text", text).toContent(),
originServerTs = 0 originServerTs = 0,
) )
} }
@ -155,6 +172,33 @@ class PushRulesConditionTest : MatrixTest {
} }
} }
@Test
fun test_decrypted_eventmatch_type_condition() {
val condition = EventMatchCondition("type", "m.room.message")
val simpleDecryptedTextEvent = createFakeEncryptedEvent().apply {
mxDecryptionResult = OlmDecryptionResult(
payload = mapOf(
"type" to EventType.MESSAGE,
"content" to MessageTextContent("m.text", "Yo wtf?").toContent(),
),
senderKey = "the_real_sender_key",
)
}
val decryptedDummyEvent = createFakeEncryptedEvent().apply {
mxDecryptionResult = OlmDecryptionResult(
payload = mapOf(
"type" to EventType.DUMMY,
)
)
}
val encryptedEvent = createFakeEncryptedEvent()
assert(condition.isSatisfied(simpleDecryptedTextEvent))
assertFalse(condition.isSatisfied(decryptedDummyEvent))
assertFalse(condition.isSatisfied(encryptedEvent))
}
/* ========================================================================================== /* ==========================================================================================
* Test RoomMemberCountCondition * Test RoomMemberCountCondition
* ========================================================================================== */ * ========================================================================================== */