Repository navigation
[PM-43768] fix: preserve biometric unlock after transient Keystore authorization failure - #7399
Conversation
|
Thank you for your contribution! We've added this to our internal tracking system for review. Details on our contribution process can be found here: https://contributing.bitwarden.com/contributing/pull-requests/community-pr-process. |
aj-rosado
left a comment
There was a problem hiding this comment.
Thank you for your contribution!
I could not reproduce this issue but it is a valuable addition to try to mitigate this issue.
Other than some code cleanup would also ask you if you could add a retry count before resetting the biometrics. I am afraid an user might enter a loop where the key is actually broken and this error code is returned for some reason and we should actually clear the key.
So my suggestion is at first failure we do not clear the biometrics, letting the user retry, but if the biometrics fails twice, then we clear it.
If you want, I can also do that changes myself
| * Authentication failed. This includes an incorrect password as well as other failed | ||
| * authentication attempts, such as a biometric cipher that is missing its Keystore | ||
| * authorization token. Callers should display a dismissible error and preserve biometric | ||
| * eligibility so a user-initiated retry can request a fresh cipher. |
There was a problem hiding this comment.
AuthenticationError should keep its scope of being Incorrect password provided..
We should add a separate error for this new error scenario.
| /** | ||
| * Maps a biometric [Cipher.doFinal] security failure to a vault unlock result. | ||
| * | ||
| * A Keystore authorization miss is retryable and must not clear biometric configuration. | ||
| * All other security failures remain decoding errors. | ||
| */ | ||
| private fun vaultUnlockResultForBiometricSecurityException( | ||
| error: GeneralSecurityException, | ||
| ): VaultUnlockResult = if (isKeystoreUserAuthenticationRequired(error)) { | ||
| VaultUnlockResult.AuthenticationError(error = error) | ||
| } else { | ||
| VaultUnlockResult.BiometricDecodingError(error = error) | ||
| } |
There was a problem hiding this comment.
Can we move this into the VaultUnlockResultExtension?
| /** | ||
| * Returns true when [throwable] or one of its causes is an Android Keystore exception that | ||
| * reports [KeyStoreException.ERROR_USER_AUTHENTICATION_REQUIRED]. | ||
| * | ||
| * The public numeric-error API is only queried on API 33+. | ||
| */ | ||
| private fun isKeystoreUserAuthenticationRequired(throwable: Throwable): Boolean { | ||
| if (!isBuildVersionAtLeast(Build.VERSION_CODES.TIRAMISU)) { | ||
| return false | ||
| } | ||
| return throwable.hasUserAuthenticationRequiredKeystoreCause() | ||
| } | ||
|
|
||
| @RequiresApi(Build.VERSION_CODES.TIRAMISU) | ||
| private fun Throwable.hasUserAuthenticationRequiredKeystoreCause(): Boolean { | ||
| val seen = mutableSetOf<Throwable>() | ||
| var current: Throwable? = this | ||
| while (current != null && seen.add(current)) { | ||
| if ( | ||
| current is KeyStoreException && | ||
| current.numericErrorCode == KeyStoreException.ERROR_USER_AUTHENTICATION_REQUIRED | ||
| ) { | ||
| return true | ||
| } | ||
| current = current.cause | ||
| } | ||
| return false | ||
| } |
There was a problem hiding this comment.
Can you move both those methods into ThrowableExtensions?
|
@aj-rosado thanks for the review, and happy to make the changes myself.
|
| * [KeyStoreException.ERROR_USER_AUTHENTICATION_REQUIRED]. | ||
| */ | ||
| @RequiresApi(Build.VERSION_CODES.TIRAMISU) | ||
| fun Throwable.hasUserAuthenticationRequiredKeystoreCause(): Boolean { |
There was a problem hiding this comment.
Can we make this private
| * Biometric cipher is missing its Keystore authorization. | ||
| */ | ||
| data class BiometricKeystoreAuthorizationError( | ||
| override val error: Throwable?, |
There was a problem hiding this comment.
Can we make this nonnull
| ) | ||
| }, | ||
| ) { | ||
| private var keystoreAuthorizationFailureCount = 0 |
There was a problem hiding this comment.
We shouldn't have state outside of the VaultUnlockState
β¦clearing biometrics Keep AuthenticationError scoped to an incorrect password and add BiometricKeystoreAuthorizationError for the Keystore case. Move the mapping into VaultUnlockResultExtensions and the Keystore cause helpers into ThrowableExtensions. VaultUnlockViewModel keeps biometrics on the first Keystore authorization failure and clears them on the second. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019wSaUMZrtm6XSzwEcxavkv
- Make hasUserAuthenticationRequiredKeystoreCause private and cover the cause-chain cycle through isKeystoreUserAuthenticationRequired. - Make BiometricKeystoreAuthorizationError.error non-null. - Move the Keystore authorization failure count from a ViewModel field into VaultUnlockState and update the tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WXAxFHj4vMTyqdptm468wG
ec6dc9c to
0bfaf11
Compare
|
Thanks @david-livefront, all three addressed in 0bfaf11: |
Codecov Reportβ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #7399 +/- ##
==========================================
- Coverage 85.91% 85.81% -0.10%
==========================================
Files 911 1073 +162
Lines 66236 68938 +2702
Branches 9365 9483 +118
==========================================
+ Hits 56905 59158 +2253
- Misses 5860 6265 +405
- Partials 3471 3515 +44
Flags with carried forward coverage won't be shown. Click here to find out more. β View full report in Codecov by Harness. π New features to boost your workflow:
|
| verify(exactly = 0) { authRepository.clearBiometrics(userId = any()) } | ||
| } | ||
|
|
||
| @Suppress("MaxLineLength") |
There was a problem hiding this comment.
Can we drop this suppression
There was a problem hiding this comment.
Dropped it in 9f601f3. That line is 92 characters, under the 100 limit, so the suppression wasn't needed. detekt passes.
aj-rosado
left a comment
There was a problem hiding this comment.
Thank you for updating this PR. It looks good to me!
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@david-livefront thanks for merging PM-43768. Biometric unlock now survives a transient Keystore authorization failure instead of being turned off. |
ποΈ Tracking
In
VaultRepositoryImpl.kt, classify the exception cause chain at both existingdoFinalcatch sites (stored-IV decryption and legacy no-IV encryption migration), returning the existingVaultUnlockResult.AuthenticationError(error = e)only when an Android Keystore cause reportsERROR_USER_AUTHENTICATION_REQUIRED, while preserving the original outer exception and logging. Guard Android Keystore type/property access with the existingisBuildVersionAtLeast(Build.VERSION_CODES.TIRAMISU)utility because the app supports API 29 and the public numeric-error API begins at API 33; retain existing decoding-error behavior for older platforms and all other failures, without matching message strings or the internal numeric code -26. A private classifier in the repository may share this logic between the two production catch sites; return before vault unlocking, PIN derivation, or biometric key/IV writes when authorization fails.On Xperia 1 VII / Android 16, fingerprint authentication can report success while the pending Keystore cipher lacks its authentication token. The supplied Flight Recorder excerpt shows
IllegalBlockSizeExceptionwrappingandroid.security.KeyStoreExceptionwithERROR_USER_AUTHENTICATION_REQUIRED, despite successful earlier uses of the same stored key.unlockVaultWithBiometricscurrently converts everyGeneralSecurityExceptionfromCipher.doFinalintoBiometricDecodingError, and the unlock screen responds by clearing biometric configuration. The bounded application fix is to preserve configuration after this specific authorization failure, keep the vault locked, and allow a new authenticated attempt in ordinary and autofill unlock flows.Fixes #7339
π Objective
Not applicable to this change.
πΈ Screenshots
No user-visible surface changes in this PR, so there is nothing to show.