Skip to content

Commit

Permalink
Merge pull request element-hq#7854 from vector-im/fix/mna/info-sessio…
Browse files Browse the repository at this point in the history
…n-without-crypto-support

[Session manager] Missing info when a session does not support encryption (PSG-1074)
  • Loading branch information
mnaturel authored Jan 5, 2023
2 parents c6a0a03 + 5373771 commit f1bd9b2
Show file tree
Hide file tree
Showing 15 changed files with 167 additions and 75 deletions.
1 change: 1 addition & 0 deletions changelog.d/7853.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[Session manager] Missing info when a session does not support encryption
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class DevicesViewModel @AssistedInject constructor(
.map { deviceInfo ->
val cryptoDeviceInfo = cryptoList.firstOrNull { it.deviceId == deviceInfo.deviceId }
val trustLevelForShield = getEncryptionTrustLevelForDeviceUseCase.execute(currentSessionCrossSigningInfo, cryptoDeviceInfo)
val isInactive = checkIfSessionIsInactiveUseCase.execute(deviceInfo.lastSeenTs ?: 0)
val isInactive = checkIfSessionIsInactiveUseCase.execute(deviceInfo.lastSeenTs)
DeviceFullInfo(deviceInfo, cryptoDeviceInfo, trustLevelForShield, isInactive)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package im.vector.app.features.settings.devices.v2

import android.content.SharedPreferences
import com.airbnb.mvrx.MavericksViewModelFactory
import com.airbnb.mvrx.Success
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
Expand All @@ -32,10 +31,10 @@ import im.vector.app.features.settings.devices.v2.signout.SignoutSessionsReAuthN
import im.vector.app.features.settings.devices.v2.signout.SignoutSessionsUseCase
import im.vector.app.features.settings.devices.v2.verification.CheckIfCurrentSessionCanBeVerifiedUseCase
import im.vector.app.features.settings.devices.v2.verification.GetCurrentSessionCrossSigningInfoUseCase
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.extensions.orFalse
import org.matrix.android.sdk.api.session.uia.DefaultBaseAuth
import timber.log.Timber

Expand Down Expand Up @@ -103,27 +102,27 @@ class DevicesViewModel @AssistedInject constructor(
}

private fun observeDevices() {
getDeviceFullInfoListUseCase.execute(
val allSessionsFlow = getDeviceFullInfoListUseCase.execute(
filterType = DeviceManagerFilterType.ALL_SESSIONS,
excludeCurrentDevice = false
excludeCurrentDevice = false,
)
.execute { async ->
if (async is Success) {
val deviceFullInfoList = async.invoke()
val unverifiedSessionsCount = deviceFullInfoList.count { !it.cryptoDeviceInfo?.trustLevel?.isCrossSigningVerified().orFalse() }
val inactiveSessionsCount = deviceFullInfoList.count { it.isInactive }

copy(
devices = async,
unverifiedSessionsCount = unverifiedSessionsCount,
inactiveSessionsCount = inactiveSessionsCount,
)
} else {
copy(
devices = async
)
}
}
val unverifiedSessionsFlow = getDeviceFullInfoListUseCase.execute(
filterType = DeviceManagerFilterType.UNVERIFIED,
excludeCurrentDevice = true,
)
val inactiveSessionsFlow = getDeviceFullInfoListUseCase.execute(
filterType = DeviceManagerFilterType.INACTIVE,
excludeCurrentDevice = true,
)

combine(allSessionsFlow, unverifiedSessionsFlow, inactiveSessionsFlow) { allSessions, unverifiedSessions, inactiveSessions ->
DeviceFullInfoList(
allSessions = allSessions,
unverifiedSessionsCount = unverifiedSessions.size,
inactiveSessionsCount = inactiveSessions.size,
)
}
.execute { async -> copy(devices = async) }
}

private fun refreshDevicesOnCryptoDevicesChange() {
Expand Down Expand Up @@ -185,6 +184,7 @@ class DevicesViewModel @AssistedInject constructor(
private fun getDeviceIdsOfOtherSessions(state: DevicesViewState): List<String> {
val currentDeviceId = state.currentSessionCrossSigningInfo.deviceId
return state.devices()
?.allSessions
?.mapNotNull { fullInfo -> fullInfo.deviceInfo.deviceId.takeUnless { it == currentDeviceId } }
.orEmpty()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ import im.vector.app.features.settings.devices.v2.verification.CurrentSessionCro

data class DevicesViewState(
val currentSessionCrossSigningInfo: CurrentSessionCrossSigningInfo = CurrentSessionCrossSigningInfo(),
val devices: Async<List<DeviceFullInfo>> = Uninitialized,
val unverifiedSessionsCount: Int = 0,
val inactiveSessionsCount: Int = 0,
val devices: Async<DeviceFullInfoList> = Uninitialized,
val isLoading: Boolean = false,
val isShowingIpAddress: Boolean = false,
) : MavericksState

data class DeviceFullInfoList(
val allSessions: List<DeviceFullInfo>,
val unverifiedSessionsCount: Int,
val inactiveSessionsCount: Int,
)
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class GetDeviceFullInfoListUseCase @Inject constructor(
.map { deviceInfo ->
val cryptoDeviceInfo = cryptoList.firstOrNull { it.deviceId == deviceInfo.deviceId }
val roomEncryptionTrustLevel = getEncryptionTrustLevelForDeviceUseCase.execute(currentSessionCrossSigningInfo, cryptoDeviceInfo)
val isInactive = checkIfSessionIsInactiveUseCase.execute(deviceInfo.lastSeenTs ?: 0)
val isInactive = checkIfSessionIsInactiveUseCase.execute(deviceInfo.lastSeenTs)
val isCurrentDevice = currentSessionCrossSigningInfo.deviceId == cryptoDeviceInfo?.deviceId
val deviceExtendedInfo = parseDeviceUserAgentUseCase.execute(deviceInfo.getBestLastSeenUserAgent())
val matrixClientInfo = deviceInfo.deviceId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ import im.vector.app.features.settings.devices.v2.signout.BuildConfirmSignoutDia
import im.vector.app.features.workers.signout.SignOutUiWorker
import org.matrix.android.sdk.api.auth.data.LoginFlowTypes
import org.matrix.android.sdk.api.extensions.orFalse
import org.matrix.android.sdk.api.session.crypto.model.RoomEncryptionTrustLevel
import javax.inject.Inject

/**
Expand Down Expand Up @@ -282,13 +281,15 @@ class VectorSettingsDevicesFragment :

override fun invalidate() = withState(viewModel) { state ->
if (state.devices is Success) {
val devices = state.devices()
val deviceFullInfoList = state.devices()
val devices = deviceFullInfoList?.allSessions
val currentDeviceId = state.currentSessionCrossSigningInfo.deviceId
val currentDeviceInfo = devices?.firstOrNull { it.deviceInfo.deviceId == currentDeviceId }
val isCurrentSessionVerified = currentDeviceInfo?.roomEncryptionTrustLevel == RoomEncryptionTrustLevel.Trusted
val otherDevices = devices?.filter { it.deviceInfo.deviceId != currentDeviceId }
val inactiveSessionsCount = deviceFullInfoList?.inactiveSessionsCount ?: 0
val unverifiedSessionsCount = deviceFullInfoList?.unverifiedSessionsCount ?: 0

renderSecurityRecommendations(state.inactiveSessionsCount, state.unverifiedSessionsCount, isCurrentSessionVerified)
renderSecurityRecommendations(inactiveSessionsCount, unverifiedSessionsCount)
renderCurrentSessionView(currentDeviceInfo, hasOtherDevices = otherDevices?.isNotEmpty().orFalse())
renderOtherSessionsView(otherDevices, state.isShowingIpAddress)
} else {
Expand All @@ -303,9 +304,8 @@ class VectorSettingsDevicesFragment :
private fun renderSecurityRecommendations(
inactiveSessionsCount: Int,
unverifiedSessionsCount: Int,
isCurrentSessionVerified: Boolean,
) {
val isUnverifiedSectionVisible = unverifiedSessionsCount > 0 && isCurrentSessionVerified
val isUnverifiedSectionVisible = unverifiedSessionsCount > 0
val isInactiveSectionVisible = inactiveSessionsCount > 0
if (isUnverifiedSectionVisible.not() && isInactiveSectionVisible.not()) {
hideSecurityRecommendations()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ class FilterDevicesUseCase @Inject constructor() {
// when current session is not verified, other session status cannot be trusted
DeviceManagerFilterType.VERIFIED -> isCurrentSessionVerified && it.cryptoDeviceInfo?.trustLevel?.isCrossSigningVerified().orFalse()
// when current session is not verified, other session status cannot be trusted
DeviceManagerFilterType.UNVERIFIED -> isCurrentSessionVerified && !it.cryptoDeviceInfo?.trustLevel?.isCrossSigningVerified().orFalse()
DeviceManagerFilterType.UNVERIFIED ->
(isCurrentSessionVerified && !it.cryptoDeviceInfo?.trustLevel?.isCrossSigningVerified().orFalse()) ||
it.cryptoDeviceInfo == null
DeviceManagerFilterType.INACTIVE -> it.isInactive
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ class CheckIfSessionIsInactiveUseCase @Inject constructor(
private val clock: Clock,
) {

fun execute(lastSeenTs: Long): Boolean {
// In case of the server doesn't send the last seen date.
if (lastSeenTs == 0L) return true

val diffMilliseconds = clock.epochMillis() - lastSeenTs
return diffMilliseconds >= TimeUnit.DAYS.toMillis(SESSION_IS_MARKED_AS_INACTIVE_AFTER_DAYS.toLong())
fun execute(lastSeenTsMillis: Long?): Boolean {
return if (lastSeenTsMillis == null || lastSeenTsMillis <= 0) {
// in these situations we cannot say anything about the inactivity of the session
false
} else {
val diffMilliseconds = clock.epochMillis() - lastSeenTsMillis
diffMilliseconds >= TimeUnit.DAYS.toMillis(SESSION_IS_MARKED_AS_INACTIVE_AFTER_DAYS.toLong())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@ class OtherSessionsController @Inject constructor(
}
val drawableColor = host.colorProvider.getColorFromAttribute(R.attr.vctr_content_secondary)
val descriptionDrawable = if (device.isInactive) host.drawableProvider.getDrawable(R.drawable.ic_inactive_sessions, drawableColor) else null
val sessionName = device.deviceInfo.displayName ?: device.deviceInfo.deviceId

otherSessionItem {
id(device.deviceInfo.deviceId)
deviceType(device.deviceExtendedInfo.deviceType)
roomEncryptionTrustLevel(device.roomEncryptionTrustLevel)
sessionName(device.deviceInfo.displayName)
sessionName(sessionName)
sessionDescription(description)
sessionDescriptionDrawable(descriptionDrawable)
sessionDescriptionColor(descriptionColor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ class SessionInfoView @JvmOverloads constructor(
stringProvider: StringProvider,
) {
renderDeviceInfo(
sessionInfoViewState.deviceFullInfo.deviceInfo.displayName.orEmpty(),
sessionInfoViewState.deviceFullInfo.deviceExtendedInfo.deviceType,
stringProvider,
sessionName = sessionInfoViewState.deviceFullInfo.deviceInfo.displayName
?: sessionInfoViewState.deviceFullInfo.deviceInfo.deviceId.orEmpty(),
deviceType = sessionInfoViewState.deviceFullInfo.deviceExtendedInfo.deviceType,
stringProvider = stringProvider,
)
renderVerificationStatus(
sessionInfoViewState.deviceFullInfo.roomEncryptionTrustLevel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ class GetDeviceFullInfoUseCase @Inject constructor(
) { currentSessionCrossSigningInfo, deviceInfo, cryptoDeviceInfo ->
val info = deviceInfo.getOrNull()
val cryptoInfo = cryptoDeviceInfo.getOrNull()
val fullInfo = if (info != null && cryptoInfo != null) {
val fullInfo = if (info != null) {
val roomEncryptionTrustLevel = getEncryptionTrustLevelForDeviceUseCase.execute(currentSessionCrossSigningInfo, cryptoInfo)
val isInactive = checkIfSessionIsInactiveUseCase.execute(info.lastSeenTs ?: 0)
val isCurrentDevice = currentSessionCrossSigningInfo.deviceId == cryptoInfo.deviceId
val isInactive = checkIfSessionIsInactiveUseCase.execute(info.lastSeenTs)
val isCurrentDevice = currentSessionCrossSigningInfo.deviceId == info.deviceId
val deviceUserAgent = parseDeviceUserAgentUseCase.execute(info.getBestLastSeenUserAgent())
val matrixClientInfo = info.deviceId
?.takeIf { it.isNotEmpty() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.airbnb.mvrx.Success
import com.airbnb.mvrx.test.MavericksTestRule
import im.vector.app.core.session.clientinfo.MatrixClientInfoContent
import im.vector.app.features.settings.devices.v2.details.extended.DeviceExtendedInfo
import im.vector.app.features.settings.devices.v2.filter.DeviceManagerFilterType
import im.vector.app.features.settings.devices.v2.list.DeviceType
import im.vector.app.features.settings.devices.v2.verification.CheckIfCurrentSessionCanBeVerifiedUseCase
import im.vector.app.features.settings.devices.v2.verification.CurrentSessionCrossSigningInfo
Expand Down Expand Up @@ -176,10 +177,7 @@ class DevicesViewModelTest {
val viewModelTest = createViewModel().test()

// Then
viewModelTest.assertLatestState {
it.devices is Success && it.devices.invoke() == deviceFullInfoList &&
it.inactiveSessionsCount == 1 && it.unverifiedSessionsCount == 1
}
viewModelTest.assertLatestState { it.devices is Success && it.devices.invoke() == deviceFullInfoList }
viewModelTest.finish()
}

Expand Down Expand Up @@ -403,7 +401,7 @@ class DevicesViewModelTest {
/**
* Generate mocked deviceFullInfo list with 1 unverified and inactive + 1 verified and active.
*/
private fun givenDeviceFullInfoList(deviceId1: String, deviceId2: String): List<DeviceFullInfo> {
private fun givenDeviceFullInfoList(deviceId1: String, deviceId2: String): DeviceFullInfoList {
val verifiedCryptoDeviceInfo = mockk<CryptoDeviceInfo>()
every { verifiedCryptoDeviceInfo.trustLevel } returns DeviceTrustLevel(crossSigningVerified = true, locallyVerified = true)
val unverifiedCryptoDeviceInfo = mockk<CryptoDeviceInfo>()
Expand Down Expand Up @@ -432,10 +430,15 @@ class DevicesViewModelTest {
deviceExtendedInfo = DeviceExtendedInfo(DeviceType.MOBILE),
matrixClientInfo = MatrixClientInfoContent(),
)
val deviceFullInfoList = listOf(deviceFullInfo1, deviceFullInfo2)
val deviceFullInfoListFlow = flowOf(deviceFullInfoList)
every { getDeviceFullInfoListUseCase.execute(any(), any()) } returns deviceFullInfoListFlow
return deviceFullInfoList
val devices = listOf(deviceFullInfo1, deviceFullInfo2)
every { getDeviceFullInfoListUseCase.execute(DeviceManagerFilterType.ALL_SESSIONS, any()) } returns flowOf(devices)
every { getDeviceFullInfoListUseCase.execute(DeviceManagerFilterType.UNVERIFIED, any()) } returns flowOf(listOf(deviceFullInfo2))
every { getDeviceFullInfoListUseCase.execute(DeviceManagerFilterType.INACTIVE, any()) } returns flowOf(listOf(deviceFullInfo1))
return DeviceFullInfoList(
allSessions = devices,
unverifiedSessionsCount = 1,
inactiveSessionsCount = 1,
)
}

private fun givenInitialViewState(deviceId1: String, deviceId2: String): DevicesViewState {
Expand All @@ -444,8 +447,6 @@ class DevicesViewModelTest {
return DevicesViewState(
currentSessionCrossSigningInfo = currentSessionCrossSigningInfo,
devices = Success(deviceFullInfoList),
unverifiedSessionsCount = 1,
inactiveSessionsCount = 1,
isLoading = false,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import im.vector.app.features.settings.devices.v2.details.extended.DeviceExtende
import im.vector.app.features.settings.devices.v2.list.DeviceType
import im.vector.app.features.settings.devices.v2.verification.CurrentSessionCrossSigningInfo
import org.amshove.kluent.shouldBeEqualTo
import org.amshove.kluent.shouldContain
import org.amshove.kluent.shouldContainAll
import org.junit.Test
import org.matrix.android.sdk.api.session.crypto.crosssigning.DeviceTrustLevel
Expand Down Expand Up @@ -82,11 +83,22 @@ private val inactiveUnverifiedDevice = DeviceFullInfo(
matrixClientInfo = MatrixClientInfoContent(),
)

private val deviceWithoutEncryptionSupport = DeviceFullInfo(
deviceInfo = DeviceInfo(deviceId = "DEVICE_WITHOUT_ENCRYPTION_SUPPORT"),
cryptoDeviceInfo = null,
roomEncryptionTrustLevel = null,
isInactive = false,
isCurrentDevice = false,
deviceExtendedInfo = DeviceExtendedInfo(DeviceType.UNKNOWN),
matrixClientInfo = MatrixClientInfoContent(),
)

private val devices = listOf(
activeVerifiedDevice,
inactiveVerifiedDevice,
activeUnverifiedDevice,
inactiveUnverifiedDevice,
deviceWithoutEncryptionSupport,
)

class FilterDevicesUseCaseTest {
Expand Down Expand Up @@ -123,16 +135,17 @@ class FilterDevicesUseCaseTest {
val currentSessionCrossSigningInfo = givenCurrentSessionVerified(true)
val filteredDeviceList = filterDevicesUseCase.execute(currentSessionCrossSigningInfo, devices, DeviceManagerFilterType.UNVERIFIED, emptyList())

filteredDeviceList.size shouldBeEqualTo 2
filteredDeviceList shouldContainAll listOf(activeUnverifiedDevice, inactiveUnverifiedDevice)
filteredDeviceList.size shouldBeEqualTo 3
filteredDeviceList shouldContainAll listOf(activeUnverifiedDevice, inactiveUnverifiedDevice, deviceWithoutEncryptionSupport)
}

@Test
fun `given a device list and current session is unverified when filter type is UNVERIFIED then returns empty list of devices`() {
val currentSessionCrossSigningInfo = givenCurrentSessionVerified(false)
val filteredDeviceList = filterDevicesUseCase.execute(currentSessionCrossSigningInfo, devices, DeviceManagerFilterType.UNVERIFIED, emptyList())

filteredDeviceList.size shouldBeEqualTo 0
filteredDeviceList.size shouldBeEqualTo 1
filteredDeviceList shouldContain deviceWithoutEncryptionSupport
}

@Test
Expand Down
Loading

0 comments on commit f1bd9b2

Please sign in to comment.