-
Notifications
You must be signed in to change notification settings - Fork 734
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(crypto): Add support for verification violation warnings #8933
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Show a notice when a previously verified user is not anymore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
262 changes: 262 additions & 0 deletions
262
...droidTest/java/org/matrix/android/sdk/internal/crypto/ComputeShieldForGroupUseCaseTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,262 @@ | ||
/* | ||
* Copyright 2024 The Matrix.org Foundation C.I.C. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.matrix.android.sdk.internal.crypto | ||
|
||
import io.mockk.coEvery | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import kotlinx.coroutines.test.runTest | ||
import org.amshove.kluent.shouldBeEqualTo | ||
import org.junit.Test | ||
import org.matrix.android.sdk.api.session.crypto.crosssigning.DeviceTrustLevel | ||
import org.matrix.android.sdk.api.session.crypto.crosssigning.MXCrossSigningInfo | ||
import org.matrix.android.sdk.api.session.crypto.model.CryptoDeviceInfo | ||
import org.matrix.android.sdk.api.session.crypto.model.RoomEncryptionTrustLevel | ||
|
||
class ComputeShieldForGroupUseCaseTest { | ||
|
||
@Test | ||
fun shouldReturnDefaultShieldWhenNoOneIsVerified() = runTest { | ||
val mockMachine = mockk<OlmMachine> { | ||
coEvery { | ||
getIdentity("@me:localhost") | ||
} returns mockk<UserIdentities>(relaxed = true) | ||
|
||
coEvery { | ||
getIdentity("@alice:localhost") | ||
} returns fakeIdentity(isVerified = false, hasVerificationViolation = false) | ||
|
||
coEvery { | ||
getUserDevices("@alice:localhost") | ||
} returns listOf(fakeDevice("@alice:localhost", "A0", false)) | ||
|
||
coEvery { | ||
getIdentity("@bob:localhost") | ||
} returns fakeIdentity(isVerified = false, hasVerificationViolation = false) | ||
|
||
coEvery { | ||
getUserDevices("@bob:localhost") | ||
} returns listOf(fakeDevice("@bob:localhost", "B0", false)) | ||
|
||
coEvery { | ||
getIdentity("@charly:localhost") | ||
} returns fakeIdentity(isVerified = false, hasVerificationViolation = false) | ||
|
||
coEvery { | ||
getUserDevices("@charly:localhost") | ||
} returns listOf(fakeDevice("@charly:localhost", "C0", false)) | ||
} | ||
|
||
val computeShieldOp = ComputeShieldForGroupUseCase("@me:localhost") | ||
|
||
val shield = computeShieldOp.invoke(mockMachine, listOf("@alice:localhost", "@bob:localhost", "@charly:localhost")) | ||
|
||
shield shouldBeEqualTo RoomEncryptionTrustLevel.Default | ||
} | ||
|
||
@Test | ||
fun shouldReturnDefaultShieldWhenVerifiedUsersHaveSecureDevices() = runTest { | ||
val mockMachine = mockk<OlmMachine> { | ||
coEvery { | ||
getIdentity("@me:localhost") | ||
} returns mockk<UserIdentities>(relaxed = true) | ||
|
||
// Alice is verified | ||
coEvery { | ||
getIdentity("@alice:localhost") | ||
} returns fakeIdentity(isVerified = true, hasVerificationViolation = false) | ||
|
||
coEvery { | ||
getUserDevices("@alice:localhost") | ||
} returns listOf( | ||
fakeDevice("@alice:localhost", "A0", true), | ||
fakeDevice("@alice:localhost", "A1", true) | ||
) | ||
|
||
coEvery { | ||
getIdentity("@bob:localhost") | ||
} returns fakeIdentity(isVerified = false, hasVerificationViolation = false) | ||
|
||
coEvery { | ||
getUserDevices("@bob:localhost") | ||
} returns listOf(fakeDevice("@bob:localhost", "B0", false)) | ||
|
||
coEvery { | ||
getIdentity("@charly:localhost") | ||
} returns fakeIdentity(isVerified = false, hasVerificationViolation = false) | ||
|
||
coEvery { | ||
getUserDevices("@charly:localhost") | ||
} returns listOf(fakeDevice("@charly:localhost", "C0", false)) | ||
} | ||
|
||
val computeShieldOp = ComputeShieldForGroupUseCase("@me:localhost") | ||
|
||
val shield = computeShieldOp.invoke(mockMachine, listOf("@alice:localhost", "@bob:localhost", "@charly:localhost")) | ||
|
||
shield shouldBeEqualTo RoomEncryptionTrustLevel.Default | ||
} | ||
|
||
@Test | ||
fun shouldReturnWarningShieldWhenPreviouslyVerifiedUsersHaveInSecureDevices() = runTest { | ||
val mockMachine = mockk<OlmMachine> { | ||
coEvery { | ||
getIdentity("@me:localhost") | ||
} returns mockk<UserIdentities>(relaxed = true) | ||
|
||
// Alice is verified | ||
coEvery { | ||
getIdentity("@alice:localhost") | ||
} returns fakeIdentity(isVerified = false, hasVerificationViolation = true) | ||
|
||
coEvery { | ||
getUserDevices("@alice:localhost") | ||
} returns listOf( | ||
fakeDevice("@alice:localhost", "A0", false), | ||
fakeDevice("@alice:localhost", "A1", false) | ||
) | ||
|
||
coEvery { | ||
getIdentity("@bob:localhost") | ||
} returns fakeIdentity(isVerified = false, hasVerificationViolation = false) | ||
|
||
coEvery { | ||
getUserDevices("@bob:localhost") | ||
} returns listOf(fakeDevice("@bob:localhost", "B0", false)) | ||
|
||
coEvery { | ||
getIdentity("@charly:localhost") | ||
} returns fakeIdentity(isVerified = false, hasVerificationViolation = false) | ||
|
||
coEvery { | ||
getUserDevices("@charly:localhost") | ||
} returns listOf(fakeDevice("@charly:localhost", "C0", false)) | ||
} | ||
|
||
val computeShieldOp = ComputeShieldForGroupUseCase("@me:localhost") | ||
|
||
val shield = computeShieldOp.invoke(mockMachine, listOf("@alice:localhost", "@bob:localhost", "@charly:localhost")) | ||
|
||
shield shouldBeEqualTo RoomEncryptionTrustLevel.Warning | ||
} | ||
|
||
@Test | ||
fun shouldReturnRedShieldWhenVerifiedUserHaveInsecureDevices() = runTest { | ||
val mockMachine = mockk<OlmMachine> { | ||
coEvery { | ||
getIdentity("@me:localhost") | ||
} returns mockk<UserIdentities>(relaxed = true) | ||
|
||
// Alice is verified | ||
coEvery { | ||
getIdentity("@alice:localhost") | ||
} returns fakeIdentity(isVerified = true, hasVerificationViolation = false) | ||
|
||
// And has an insecure device | ||
coEvery { | ||
getUserDevices("@alice:localhost") | ||
} returns listOf( | ||
fakeDevice("@alice:localhost", "A0", true), | ||
fakeDevice("@alice:localhost", "A1", false) | ||
) | ||
|
||
coEvery { | ||
getIdentity("@bob:localhost") | ||
} returns fakeIdentity(isVerified = false, hasVerificationViolation = false) | ||
|
||
coEvery { | ||
getUserDevices("@bob:localhost") | ||
} returns listOf(fakeDevice("@bob:localhost", "B0", false)) | ||
|
||
coEvery { | ||
getIdentity("@charly:localhost") | ||
} returns fakeIdentity(isVerified = false, hasVerificationViolation = false) | ||
|
||
coEvery { | ||
getUserDevices("@charly:localhost") | ||
} returns listOf(fakeDevice("@charly:localhost", "C0", false)) | ||
} | ||
|
||
val computeShieldOp = ComputeShieldForGroupUseCase("@me:localhost") | ||
|
||
val shield = computeShieldOp.invoke(mockMachine, listOf("@alice:localhost", "@bob:localhost", "@charly:localhost")) | ||
|
||
shield shouldBeEqualTo RoomEncryptionTrustLevel.Warning | ||
} | ||
|
||
@Test | ||
fun shouldReturnGreenShieldWhenAllUsersAreVerifiedAndHaveSecuredDevices() = runTest { | ||
val mockMachine = mockk<OlmMachine> { | ||
coEvery { | ||
getIdentity("@me:localhost") | ||
} returns mockk<UserIdentities>(relaxed = true) | ||
|
||
// Alice is verified | ||
coEvery { | ||
getIdentity("@alice:localhost") | ||
} returns fakeIdentity(isVerified = true, hasVerificationViolation = false) | ||
|
||
coEvery { | ||
getUserDevices("@alice:localhost") | ||
} returns listOf( | ||
fakeDevice("@alice:localhost", "A0", true), | ||
fakeDevice("@alice:localhost", "A1", false) | ||
) | ||
|
||
coEvery { | ||
getIdentity("@bob:localhost") | ||
} returns fakeIdentity(isVerified = true, hasVerificationViolation = false) | ||
|
||
coEvery { | ||
getUserDevices("@bob:localhost") | ||
} returns listOf(fakeDevice("@bob:localhost", "B0", true)) | ||
|
||
coEvery { | ||
getIdentity("@charly:localhost") | ||
} returns fakeIdentity(isVerified = true, hasVerificationViolation = false) | ||
|
||
coEvery { | ||
getUserDevices("@charly:localhost") | ||
} returns listOf(fakeDevice("@charly:localhost", "C0", true)) | ||
} | ||
|
||
val computeShieldOp = ComputeShieldForGroupUseCase("@me:localhost") | ||
|
||
val shield = computeShieldOp.invoke(mockMachine, listOf("@alice:localhost", "@bob:localhost", "@charly:localhost")) | ||
|
||
shield shouldBeEqualTo RoomEncryptionTrustLevel.Warning | ||
} | ||
|
||
companion object { | ||
internal fun fakeDevice(userId: String, deviceId: String, isSecure: Boolean) = mockk<Device>(relaxed = true) { | ||
every { toCryptoDeviceInfo() } returns CryptoDeviceInfo( | ||
deviceId = deviceId, | ||
userId = userId, | ||
trustLevel = DeviceTrustLevel( | ||
crossSigningVerified = isSecure, locallyVerified = null | ||
) | ||
) | ||
} | ||
|
||
internal fun fakeIdentity(isVerified: Boolean, hasVerificationViolation: Boolean) = mockk<UserIdentities>(relaxed = true) { | ||
coEvery { toMxCrossSigningInfo() } returns mockk<MXCrossSigningInfo> { | ||
every { wasTrustedOnce } returns hasVerificationViolation | ||
every { isTrusted() } returns isVerified | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. Hopefully this code is only used in test with
otherUserId
different than the current user. Else I guess we would have detected the problem faster.