Skip to content

Commit 8646cc4

Browse files
committed
do not add tracing ids to verification events
1 parent 139eb17 commit 8646cc4

File tree

3 files changed

+109
-3
lines changed

3 files changed

+109
-3
lines changed

matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/events/model/EventType.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.matrix.android.sdk.api.session.events.model
1818

19+
import org.matrix.android.sdk.api.session.room.model.message.MessageType.MSGTYPE_VERIFICATION_REQUEST
20+
1921
/**
2022
* Constants defining known event types from Matrix specifications.
2123
*/
@@ -126,6 +128,7 @@ object EventType {
126128

127129
fun isVerificationEvent(type: String): Boolean {
128130
return when (type) {
131+
MSGTYPE_VERIFICATION_REQUEST,
129132
KEY_VERIFICATION_START,
130133
KEY_VERIFICATION_ACCEPT,
131134
KEY_VERIFICATION_KEY,

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/tasks/SendToDeviceTask.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package org.matrix.android.sdk.internal.crypto.tasks
1818

1919
import org.matrix.android.sdk.api.session.crypto.model.MXUsersDevicesMap
2020
import org.matrix.android.sdk.api.session.events.model.Event
21+
import org.matrix.android.sdk.api.session.events.model.EventType
2122
import org.matrix.android.sdk.api.session.events.model.toContent
2223
import org.matrix.android.sdk.internal.crypto.api.CryptoApi
2324
import org.matrix.android.sdk.internal.crypto.model.rest.SendToDeviceBody
@@ -39,7 +40,9 @@ internal interface SendToDeviceTask : Task<SendToDeviceTask.Params, Unit> {
3940
// the content to send. Map from user_id to device_id to content dictionary.
4041
val contentMap: MXUsersDevicesMap<Any>,
4142
// the transactionId. If not provided, a transactionId will be created by the task
42-
val transactionId: String? = null
43+
val transactionId: String? = null,
44+
// add tracing id, notice that to device events that do signature on content might be broken by it
45+
val addTracingIds: Boolean = !EventType.isVerificationEvent(eventType),
4346
)
4447
}
4548

@@ -55,7 +58,12 @@ internal class DefaultSendToDeviceTask @Inject constructor(
5558
val txnId = params.transactionId ?: createUniqueTxnId()
5659

5760
// add id tracing to debug
58-
val decorated = decorateWithToDeviceTracingIds(params)
61+
val decorated = if (params.addTracingIds) {
62+
decorateWithToDeviceTracingIds(params)
63+
} else {
64+
params.contentMap.map to emptyList()
65+
}
66+
5967
val sendToDeviceBody = SendToDeviceBody(
6068
messages = decorated.first
6169
)

matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/crypto/DefaultSendToDeviceTaskTest.kt

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import org.matrix.android.sdk.api.session.crypto.model.DeviceInfo
2525
import org.matrix.android.sdk.api.session.crypto.model.DevicesListResponse
2626
import org.matrix.android.sdk.api.session.crypto.model.MXUsersDevicesMap
2727
import org.matrix.android.sdk.api.session.events.model.EventType
28+
import org.matrix.android.sdk.api.session.room.model.message.MessageType
2829
import org.matrix.android.sdk.internal.crypto.api.CryptoApi
2930
import org.matrix.android.sdk.internal.crypto.model.rest.DeleteDeviceParams
3031
import org.matrix.android.sdk.internal.crypto.model.rest.DeleteDevicesParams
@@ -60,8 +61,28 @@ class DefaultSendToDeviceTaskTest {
6061
)
6162
)
6263

64+
private val fakeStartVerificationContent = mapOf(
65+
"method" to "m.sas.v1",
66+
"from_device" to "MNQHVEISFQ",
67+
"key_agreement_protocols" to listOf(
68+
"curve25519-hkdf-sha256",
69+
"curve25519"
70+
),
71+
"hashes" to listOf("sha256"),
72+
"message_authentication_codes" to listOf(
73+
"org.matrix.msc3783.hkdf-hmac-sha256",
74+
"hkdf-hmac-sha256",
75+
"hmac-sha256"
76+
),
77+
"short_authentication_string" to listOf(
78+
"decimal",
79+
"emoji"
80+
),
81+
"transaction_id" to "4wNOpkHGwGZPXjkZToooCDWfb8hsf7vW"
82+
)
83+
6384
@Test
64-
fun `tracing id should be added to all to_device contents`() {
85+
fun `tracing id should be added to to_device contents`() {
6586
val fakeCryptoAPi = FakeCryptoApi()
6687

6788
val sendToDeviceTask = DefaultSendToDeviceTask(
@@ -107,6 +128,80 @@ class DefaultSendToDeviceTaskTest {
107128
println("modified content ${fakeCryptoAPi.body}")
108129
}
109130

131+
@Test
132+
fun `tracing id should not be added to verification start to_device contents`() {
133+
val fakeCryptoAPi = FakeCryptoApi()
134+
135+
val sendToDeviceTask = DefaultSendToDeviceTask(
136+
cryptoApi = fakeCryptoAPi,
137+
globalErrorReceiver = mockk(relaxed = true)
138+
)
139+
val contentMap = MXUsersDevicesMap<Any>()
140+
contentMap.setObject("@alice:example.com", "MNQHVEISFQ", fakeStartVerificationContent)
141+
142+
val params = SendToDeviceTask.Params(
143+
eventType = EventType.KEY_VERIFICATION_START,
144+
contentMap = contentMap
145+
)
146+
147+
runBlocking {
148+
sendToDeviceTask.execute(params)
149+
}
150+
151+
val modifiedContent = fakeCryptoAPi.body!!.messages!!["@alice:example.com"]!!["MNQHVEISFQ"] as Map<*, *>
152+
Assert.assertNull("Tracing id should not have been added", modifiedContent["org.matrix.msgid"])
153+
154+
// try to force
155+
runBlocking {
156+
sendToDeviceTask.execute(
157+
SendToDeviceTask.Params(
158+
eventType = EventType.KEY_VERIFICATION_START,
159+
contentMap = contentMap,
160+
addTracingIds = true
161+
)
162+
)
163+
}
164+
165+
val modifiedContentForced = fakeCryptoAPi.body!!.messages!!["@alice:example.com"]!!["MNQHVEISFQ"] as Map<*, *>
166+
Assert.assertNotNull("Tracing id should have been added", modifiedContentForced["org.matrix.msgid"])
167+
}
168+
169+
@Test
170+
fun `tracing id should not be added to all verification to_device contents`() {
171+
val fakeCryptoAPi = FakeCryptoApi()
172+
173+
val sendToDeviceTask = DefaultSendToDeviceTask(
174+
cryptoApi = fakeCryptoAPi,
175+
globalErrorReceiver = mockk(relaxed = true)
176+
)
177+
val contentMap = MXUsersDevicesMap<Any>()
178+
contentMap.setObject("@alice:example.com", "MNQHVEISFQ", emptyMap<String, Any>())
179+
180+
val verificationEvents = listOf(
181+
MessageType.MSGTYPE_VERIFICATION_REQUEST,
182+
EventType.KEY_VERIFICATION_START,
183+
EventType.KEY_VERIFICATION_ACCEPT,
184+
EventType.KEY_VERIFICATION_KEY,
185+
EventType.KEY_VERIFICATION_MAC,
186+
EventType.KEY_VERIFICATION_CANCEL,
187+
EventType.KEY_VERIFICATION_DONE,
188+
EventType.KEY_VERIFICATION_READY
189+
)
190+
191+
for (type in verificationEvents) {
192+
val params = SendToDeviceTask.Params(
193+
eventType = type,
194+
contentMap = contentMap
195+
)
196+
runBlocking {
197+
sendToDeviceTask.execute(params)
198+
}
199+
200+
val modifiedContent = fakeCryptoAPi.body!!.messages!!["@alice:example.com"]!!["MNQHVEISFQ"] as Map<*, *>
201+
Assert.assertNull("Tracing id should not have been added", modifiedContent["org.matrix.msgid"])
202+
}
203+
}
204+
110205
internal class FakeCryptoApi : CryptoApi {
111206
override suspend fun getDevices(): DevicesListResponse {
112207
throw java.lang.AssertionError("Should not be called")

0 commit comments

Comments
 (0)