Skip to content

[PM-43768] fix: preserve biometric unlock after transient Keystore authorization failure - #7399

Merged
david-livefront merged 4 commits into
bitwarden:mainfrom
mvanhorn:fix/7339-preserve-biometrics-keystore-authorization
Oct 9, 2026
Merged

david-livefront merged 4 commits into
bitwarden:mainfrom
mvanhorn:fix/7339-preserve-biometrics-keystore-authorization

Conversation

@mvanhorn

Copy link
Copy Markdown
Contributor

🎟️ Tracking

In VaultRepositoryImpl.kt, classify the exception cause chain at both existing doFinal catch sites (stored-IV decryption and legacy no-IV encryption migration), returning the existing VaultUnlockResult.AuthenticationError(error = e) only when an Android Keystore cause reports ERROR_USER_AUTHENTICATION_REQUIRED, while preserving the original outer exception and logging. Guard Android Keystore type/property access with the existing isBuildVersionAtLeast(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 IllegalBlockSizeException wrapping android.security.KeyStoreException with ERROR_USER_AUTHENTICATION_REQUIRED, despite successful earlier uses of the same stored key. unlockVaultWithBiometrics currently converts every GeneralSecurityException from Cipher.doFinal into BiometricDecodingError, 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.

@mvanhorn
mvanhorn requested review from a team and david-livefront as code owners September 16, 2026 14:35
@bitwarden-bot

Copy link
Copy Markdown
Collaborator

Thank you for your contribution! We've added this to our internal tracking system for review.
ID: PM-43768
Link: https://bitwarden.atlassian.net/browse/PM-43768

Details on our contribution process can be found here: https://contributing.bitwarden.com/contributing/pull-requests/community-pr-process.

@bitwarden-bot bitwarden-bot changed the title fix: preserve biometric unlock after transient Keystore authorization failure [PM-43768] fix: preserve biometric unlock after transient Keystore authorization failure Sep 16, 2026

@aj-rosado aj-rosado left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment on lines +14 to +17
* 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AuthenticationError should keep its scope of being Incorrect password provided..
We should add a separate error for this new error scenario.

Comment on lines +592 to +604
/**
* 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)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this into the VaultUnlockResultExtension?

Comment on lines +606 to +633
/**
* 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
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move both those methods into ThrowableExtensions?

@mvanhorn

mvanhorn commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

@aj-rosado thanks for the review, and happy to make the changes myself.

  • AuthenticationError is back to meaning an incorrect password. The Keystore case now returns a new BiometricKeystoreAuthorizationError.
  • The exception to result mapping moved into VaultUnlockResultExtensions, and both Keystore helpers moved into ThrowableExtensions with their own tests.
  • Added the retry count in VaultUnlockViewModel. The first Keystore authorization failure shows an error and keeps biometrics so the user can try again. A second one clears biometrics and shows the decoding failure dialog. The count resets on a successful unlock or an account switch.

* [KeyStoreException.ERROR_USER_AUTHENTICATION_REQUIRED].
*/
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
fun Throwable.hasUserAuthenticationRequiredKeystoreCause(): Boolean {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this private

* Biometric cipher is missing its Keystore authorization.
*/
data class BiometricKeystoreAuthorizationError(
override val error: Throwable?,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this nonnull

)
},
) {
private var keystoreAuthorizationFailureCount = 0

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't have state outside of the VaultUnlockState

mvanhorn and others added 3 commits October 6, 2026 22:50
…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
@mvanhorn
mvanhorn force-pushed the fix/7339-preserve-biometrics-keystore-authorization branch from ec6dc9c to 0bfaf11 Compare October 7, 2026 18:48
@mvanhorn

mvanhorn commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @david-livefront, all three addressed in 0bfaf11: hasUserAuthenticationRequiredKeystoreCause is private now, BiometricKeystoreAuthorizationError.error is non-null, and the Keystore retry count moved into VaultUnlockState (reset on success and on account switch) so the ViewModel holds no state outside it. Also rebased on main to clear the conflict. detekt and the VaultUnlock, ThrowableExtensions and VaultRepository unit tests pass locally.

@aj-rosado aj-rosado added the ai-review Request a Claude code review label Oct 8, 2026
@codecov

codecov Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.45283% with 4 lines in your changes missing coverage. Please review.
βœ… Project coverage is 85.81%. Comparing base (1f03415) to head (9f601f3).
⚠️ Report is 11 commits behind head on main.

Files with missing lines Patch % Lines
...rm/repository/AuthenticatorBridgeRepositoryImpl.kt 0.00% 0 Missing and 1 partial ⚠️
...itwarden/data/platform/util/ThrowableExtensions.kt 91.66% 1 Missing ⚠️
...i/auth/feature/vaultunlock/VaultUnlockViewModel.kt 96.87% 0 Missing and 1 partial ⚠️
...ortitems/verifypassword/VerifyPasswordViewModel.kt 0.00% 0 Missing and 1 partial ⚠️
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     
Flag Coverage Ξ”
app-data 18.65% <33.96%> (-0.01%) ⬇️
app-ui-auth-tools 18.93% <62.26%> (+0.30%) ⬆️
app-ui-platform 16.92% <0.00%> (+0.44%) ⬆️
app-ui-vault 27.48% <0.00%> (+0.59%) ⬆️
authenticator 5.98% <0.00%> (+0.02%) ⬆️
lib-core-network-bridge 4.09% <0.00%> (-0.01%) ⬇️
lib-data-ui 1.23% <0.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

β˜” View full report in Codecov by Harness.
πŸ“’ Have feedback on the report? Share it here.

πŸš€ New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • πŸ“¦ JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@aj-rosado aj-rosado added t:bug Change Type - Bug and removed ai-review Request a Claude code review t:bug Change Type - Bug labels Oct 8, 2026
verify(exactly = 0) { authRepository.clearBiometrics(userId = any()) }
}

@Suppress("MaxLineLength")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we drop this suppression

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropped it in 9f601f3. That line is 92 characters, under the 100 limit, so the suppression wasn't needed. detekt passes.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for that!

aj-rosado
aj-rosado previously approved these changes Oct 9, 2026

@aj-rosado aj-rosado left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for updating this PR. It looks good to me!

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@david-livefront
david-livefront added this pull request to the merge queue Oct 9, 2026
Merged via the queue into bitwarden:main with commit 2cb8b8f Oct 9, 2026
30 of 32 checks passed
@mvanhorn

Copy link
Copy Markdown
Contributor Author

@david-livefront thanks for merging PM-43768. Biometric unlock now survives a transient Keystore authorization failure instead of being turned off.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[PM-42871] [Xperia 1 VII / Android 16] Successful fingerprint authentication fails Keystore authorization and disables biometric unlock

4 participants