Add validity checks to ensure that changes to biometrics require a master password or pin to continue (#839)

This commit is contained in:
David Perez
2024-06-20 17:08:07 +01:00
committed by Álison Fernandes
parent 2623fc3cbe
commit 9a8aca9fe1
15 changed files with 344 additions and 2 deletions
@@ -65,6 +65,32 @@ class SettingsDiskSourceTest {
)
}
@Test
fun `systemBiometricIntegritySource should pull from SharedPreferences`() {
val biometricIntegritySource = "bwPreferencesStorage:biometricIntegritySource"
val expected = "mockBiometricIntegritySource"
// Verify initial value is null and disk source matches shared preferences.
assertNull(fakeSharedPreferences.getString(biometricIntegritySource, null))
assertNull(settingsDiskSource.systemBiometricIntegritySource)
// Updating the shared preferences should update disk source.
fakeSharedPreferences.edit {
putString(biometricIntegritySource, expected)
}
val actual = settingsDiskSource.systemBiometricIntegritySource
assertEquals(expected, actual)
}
@Test
fun `setting systemBiometricIntegritySource should update SharedPreferences`() {
val biometricIntegritySource = "bwPreferencesStorage:biometricIntegritySource"
val expected = "mockBiometricIntegritySource"
settingsDiskSource.systemBiometricIntegritySource = expected
val actual = fakeSharedPreferences.getString(biometricIntegritySource, null)
assertEquals(expected, actual)
}
@Test
fun `clearData should clear all necessary data for the given user`() {
val userId = "userId"
@@ -108,6 +134,12 @@ class SettingsDiskSourceTest {
userId = userId,
isScreenCaptureAllowed = true,
)
val systemBioIntegrityState = "system_biometrics_integrity_state"
settingsDiskSource.storeAccountBiometricIntegrityValidity(
userId = userId,
systemBioIntegrityState = systemBioIntegrityState,
value = true,
)
settingsDiskSource.clearData(userId = userId)
@@ -121,6 +153,51 @@ class SettingsDiskSourceTest {
assertNull(settingsDiskSource.getApprovePasswordlessLoginsEnabled(userId = userId))
assertNull(settingsDiskSource.getLastSyncTime(userId = userId))
assertNull(settingsDiskSource.getScreenCaptureAllowed(userId = userId))
assertNull(
settingsDiskSource.getAccountBiometricIntegrityValidity(
userId = userId,
systemBioIntegrityState = systemBioIntegrityState,
),
)
}
@Suppress("MaxLineLength")
@Test
fun `getAccountBiometricIntegrityValidity should pull from and update SharedPreferences`() {
val userId = "userId-1234"
val systemBiometricIntegritySource = "systemValidity"
val accountBioIntegrityValid =
"bwPreferencesStorage:accountBiometricIntegrityValid_${userId}_$systemBiometricIntegritySource"
val isValid = true
// Assert that the default value in disk source is null
assertNull(
settingsDiskSource.getAccountBiometricIntegrityValidity(
userId = userId,
systemBioIntegrityState = systemBiometricIntegritySource,
),
)
// Updating the shared preferences should update disk source.
fakeSharedPreferences.edit { putBoolean(accountBioIntegrityValid, isValid) }
assertEquals(
isValid,
settingsDiskSource.getAccountBiometricIntegrityValidity(
userId = userId,
systemBioIntegrityState = systemBiometricIntegritySource,
),
)
// Updating the disk source updates the shared preferences
settingsDiskSource.storeAccountBiometricIntegrityValidity(
userId = userId,
systemBioIntegrityState = systemBiometricIntegritySource,
value = isValid,
)
assertEquals(
fakeSharedPreferences.getBoolean(accountBioIntegrityValid, false),
isValid,
)
}
@Test
@@ -48,6 +48,8 @@ class FakeSettingsDiskSource : SettingsDiskSource {
private var storedIsIconLoadingDisabled: Boolean? = null
private val storedApprovePasswordLoginsEnabled = mutableMapOf<String, Boolean?>()
private val storedScreenCaptureAllowed = mutableMapOf<String, Boolean?>()
private var storedSystemBiometricIntegritySource: String? = null
private val storedAccountBiometricIntegrityValidity = mutableMapOf<String, Boolean?>()
override var appLanguage: AppLanguage? = null
@@ -63,6 +65,12 @@ class FakeSettingsDiskSource : SettingsDiskSource {
emit(appTheme)
}
override var systemBiometricIntegritySource: String?
get() = storedSystemBiometricIntegritySource
set(value) {
storedSystemBiometricIntegritySource = value
}
override var isIconLoadingDisabled: Boolean?
get() = storedIsIconLoadingDisabled
set(value) {
@@ -75,6 +83,19 @@ class FakeSettingsDiskSource : SettingsDiskSource {
emit(isIconLoadingDisabled)
}
override fun getAccountBiometricIntegrityValidity(
userId: String,
systemBioIntegrityState: String,
): Boolean? = storedAccountBiometricIntegrityValidity["${userId}_$systemBioIntegrityState"]
override fun storeAccountBiometricIntegrityValidity(
userId: String,
systemBioIntegrityState: String,
value: Boolean?,
) {
storedAccountBiometricIntegrityValidity["${userId}_$systemBioIntegrityState"] = value
}
override fun clearData(userId: String) {
storedVaultTimeoutActions.remove(userId)
storedVaultTimeoutInMinutes.remove(userId)
@@ -9,6 +9,7 @@ import com.x8bit.bitwarden.data.auth.repository.model.UserFingerprintResult
import com.x8bit.bitwarden.data.platform.base.FakeDispatcherManager
import com.x8bit.bitwarden.data.platform.datasource.disk.util.FakeSettingsDiskSource
import com.x8bit.bitwarden.data.platform.manager.AppForegroundManager
import com.x8bit.bitwarden.data.platform.manager.BiometricsEncryptionManager
import com.x8bit.bitwarden.data.platform.manager.model.AppForegroundState
import com.x8bit.bitwarden.data.platform.repository.model.BiometricsKeyResult
import com.x8bit.bitwarden.data.platform.repository.model.UriMatchType
@@ -50,6 +51,7 @@ class SettingsRepositoryTest {
private val fakeAuthDiskSource = FakeAuthDiskSource()
private val fakeSettingsDiskSource = FakeSettingsDiskSource()
private val vaultSdkSource: VaultSdkSource = mockk()
private val biometricsEncryptionManager: BiometricsEncryptionManager = mockk()
private var isAutofillEnabledAndSupported = false
@@ -59,6 +61,7 @@ class SettingsRepositoryTest {
authDiskSource = fakeAuthDiskSource,
settingsDiskSource = fakeSettingsDiskSource,
vaultSdkSource = vaultSdkSource,
biometricsEncryptionManager = biometricsEncryptionManager,
dispatcherManager = FakeDispatcherManager(),
)
@@ -687,6 +690,7 @@ class SettingsRepositoryTest {
runTest {
fakeAuthDiskSource.userState = MOCK_USER_STATE
val userId = MOCK_USER_STATE.activeUserId
every { biometricsEncryptionManager.setupBiometrics(userId) } just runs
coEvery {
vaultSdkSource.getUserEncryptionKey(userId = userId)
} returns Throwable("Fail").asFailure()
@@ -694,6 +698,9 @@ class SettingsRepositoryTest {
val result = settingsRepository.setupBiometricsKey()
assertEquals(BiometricsKeyResult.Error, result)
verify(exactly = 1) {
biometricsEncryptionManager.setupBiometrics(userId)
}
coVerify(exactly = 1) {
vaultSdkSource.getUserEncryptionKey(userId = userId)
}
@@ -706,6 +713,7 @@ class SettingsRepositoryTest {
fakeAuthDiskSource.userState = MOCK_USER_STATE
val userId = MOCK_USER_STATE.activeUserId
val encryptedKey = "asdf1234"
every { biometricsEncryptionManager.setupBiometrics(userId) } just runs
coEvery {
vaultSdkSource.getUserEncryptionKey(userId = userId)
} returns encryptedKey.asSuccess()
@@ -714,6 +722,9 @@ class SettingsRepositoryTest {
assertEquals(BiometricsKeyResult.Success, result)
fakeAuthDiskSource.assertBiometricsKey(userId = userId, biometricsKey = encryptedKey)
verify(exactly = 1) {
biometricsEncryptionManager.setupBiometrics(userId)
}
coVerify(exactly = 1) {
vaultSdkSource.getUserEncryptionKey(userId = userId)
}
@@ -421,6 +421,7 @@ private val DEFAULT_STATE: VaultUnlockState = VaultUnlockState(
environmentUrl = DEFAULT_ENVIRONMENT_URL,
initials = "AU",
input = "",
isBiometricsValid = true,
isBiometricEnabled = true,
vaultUnlockType = VaultUnlockType.MASTER_PASSWORD,
)
@@ -7,6 +7,7 @@ import com.x8bit.bitwarden.data.auth.repository.AuthRepository
import com.x8bit.bitwarden.data.auth.repository.model.SwitchAccountResult
import com.x8bit.bitwarden.data.auth.repository.model.UserState
import com.x8bit.bitwarden.data.auth.repository.model.VaultUnlockType
import com.x8bit.bitwarden.data.platform.manager.BiometricsEncryptionManager
import com.x8bit.bitwarden.data.platform.repository.EnvironmentRepository
import com.x8bit.bitwarden.data.platform.repository.model.Environment
import com.x8bit.bitwarden.data.platform.repository.util.FakeEnvironmentRepository
@@ -46,6 +47,9 @@ class VaultUnlockViewModelTest : BaseViewModelTest() {
private val vaultRepository: VaultRepository = mockk(relaxed = true) {
every { lockVault(any()) } just runs
}
private val encryptionManager: BiometricsEncryptionManager = mockk {
every { isBiometricIntegrityValid(userId = DEFAULT_USER_STATE.activeUserId) } returns true
}
@Test
fun `initial state should be correct when not set`() {
@@ -678,11 +682,13 @@ class VaultUnlockViewModelTest : BaseViewModelTest() {
state: VaultUnlockState? = DEFAULT_STATE,
environmentRepo: EnvironmentRepository = environmentRepository,
vaultRepo: VaultRepository = vaultRepository,
biometricsEncryptionManager: BiometricsEncryptionManager = encryptionManager,
): VaultUnlockViewModel = VaultUnlockViewModel(
savedStateHandle = SavedStateHandle().apply { set("state", state) },
authRepository = authRepository,
vaultRepo = vaultRepo,
environmentRepo = environmentRepo,
biometricsEncryptionManager = biometricsEncryptionManager,
)
}
@@ -705,6 +711,7 @@ private val DEFAULT_STATE: VaultUnlockState = VaultUnlockState(
dialog = null,
environmentUrl = Environment.Us.label,
input = "",
isBiometricsValid = true,
isBiometricEnabled = false,
vaultUnlockType = VaultUnlockType.MASTER_PASSWORD,
)