Modern Apps git

← Modern-Apps.git

passwords: fix crashes on bad TOTP, passkey and import data

Commit
cf856cfa90e65510be350b5c3a969f7a16eaab69
Author
Vayun Mathur <[email protected]>
When
2026-11-19 02:32 UTC (17 days ago)
Parents
9fdbb12
Crash paths from imported/synced/calling-app data:

- PasswordScreen detail did TOTP.generate unguarded during
  composition (list rows already use tryOrDefault). A stored
  secret with no Base32 characters (typed freely in the edit
  screen or imported via CSV) decodes to an empty key and
  SecretKeySpec throws IllegalArgumentException. Use
  tryOrDefault(----) like MenuScreen.
- PasskeyAuthActivity.proceedWithFlow caught only
  IllegalStateException: malformed calling-app requestJson
  (JSONException), bad Base64url user handle / stored
  credential id (IllegalArgumentException) and corrupt
  imported private keys (GeneralSecurityException from
  KeyFactory.generatePrivate in signAssertion) all crashed
  the activity. Each now cancels the flow (RESULT_CANCELED).
- PasskeyEntries built JSONObject(option.requestJson)
  unguarded; a malformed BeginGetPublicKeyCredentialOption
  crashed credential enumeration. Skip malformed options.
- PasswordsViewModel.importCsv caught only
  IllegalStateException; a provider deleting/revoking the
  picked document made openInputStream/readText throw
  IOException/SecurityException out of viewModelScope. Both
  now report Import failed.
- PasswordAutofillService.onSaveRequest ran the vault write
  in unguarded runBlocking (onFillRequest guards its read):
  a locked/unavailable vault threw IllegalStateException out
  of the autofill callback. Wrap and answer
  callback.onFailure instead. The reach-the-vault dispatch
  itself is unchanged (AutofillServiceDispatchTest pins it).

Verification: ./gradlew :passwords:testDevUnitTest
(BUILD SUCCESSFUL, 432 tests), ./check photos office
passwords (exit 0). Rust KDBX crates untouched.

Diff

passwords/src/main/java/com/vayunmathur/passwords/platform/PasskeyEntries.kt
diff --git a/passwords/src/main/java/com/vayunmathur/passwords/platform/PasskeyEntries.kt b/passwords/src/main/java/com/vayunmathur/passwords/platform/PasskeyEntries.ktindex f56bdc8320..6ffd776122 100644--- a/passwords/src/main/java/com/vayunmathur/passwords/platform/PasskeyEntries.kt+++ b/passwords/src/main/java/com/vayunmathur/passwords/platform/PasskeyEntries.kt@@ -31,7 +31,8 @@ suspend fun buildGetCredentialResponse(     for (option in options) {         when (option) {             is BeginGetPublicKeyCredentialOption -> {-                val rpId = JSONObject(option.requestJson).optString("rpId", "")+                // requestJson comes from the calling app; skip malformed options.+                val rpId = runCatching { JSONObject(option.requestJson).optString("rpId", "") }.getOrNull() ?: continue                 if (rpId.isBlank()) continue                 for (passkey in passkeyDao.getByRpId(rpId)) {                     val intent = Intent(context, PasskeyAuthActivity::class.java).apply {
passwords/src/main/java/com/vayunmathur/passwords/platform/PasswordAutofillService.kt
diff --git a/passwords/src/main/java/com/vayunmathur/passwords/platform/PasswordAutofillService.kt b/passwords/src/main/java/com/vayunmathur/passwords/platform/PasswordAutofillService.ktindex 817ba29c89..486ea2887a 100644--- a/passwords/src/main/java/com/vayunmathur/passwords/platform/PasswordAutofillService.kt+++ b/passwords/src/main/java/com/vayunmathur/passwords/platform/PasswordAutofillService.kt@@ -114,24 +114,30 @@ class PasswordAutofillService : AutofillService() {         val password = parser.passwordText          if (!username.isNullOrBlank() && !password.isNullOrBlank()) {-            runBlocking {-                val candidates = repository.getAllPasswords()-                val existing = candidates.firstOrNull { it.username == username || it.email == username }-                if (existing != null) {-                    repository.upsertPassword(existing.copy(password = password))-                } else {-                    val looksLikeEmail = username.contains("@")-                    repository.upsertPassword(-                        Password(-                            name = "Saved Login",-                            username = if (looksLikeEmail) "" else username,-                            email = if (looksLikeEmail) username else "",-                            password = password,-                            totpSecret = null,-                            websites = listOfNotNull(parser.webDomain)+            try {+                runBlocking {+                    val candidates = repository.getAllPasswords()+                    val existing = candidates.firstOrNull { it.username == username || it.email == username }+                    if (existing != null) {+                        repository.upsertPassword(existing.copy(password = password))+                    } else {+                        val looksLikeEmail = username.contains("@")+                        repository.upsertPassword(+                            Password(+                                name = "Saved Login",+                                username = if (looksLikeEmail) "" else username,+                                email = if (looksLikeEmail) username else "",+                                password = password,+                                totpSecret = null,+                                websites = listOfNotNull(parser.webDomain)+                            )                         )-                    )-                }+                    }+            }+            } catch (expected: IllegalStateException) {+                Log.error(tag, "Error in onSaveRequest", expected)+                callback.onFailure("Unable to open vault")+                return             }         }         callback.onSuccess()
passwords/src/main/java/com/vayunmathur/passwords/platform/PasswordsViewModel.kt
diff --git a/passwords/src/main/java/com/vayunmathur/passwords/platform/PasswordsViewModel.kt b/passwords/src/main/java/com/vayunmathur/passwords/platform/PasswordsViewModel.ktindex 819eb4e7b3..f8266cccbe 100644--- a/passwords/src/main/java/com/vayunmathur/passwords/platform/PasswordsViewModel.kt+++ b/passwords/src/main/java/com/vayunmathur/passwords/platform/PasswordsViewModel.kt@@ -256,6 +256,11 @@ class PasswordsViewModel(                     "Imported ${result.inserted} rows, skipped ${result.skipped} rows"             } catch (expected: IllegalStateException) {                 _importMessage.value = "Import failed: ${expected.message}"+            } catch (expected: java.io.IOException) {+                // Provider deleted/revoked the picked document mid-import.+                _importMessage.value = "Import failed: ${expected.message}"+            } catch (expected: SecurityException) {+                _importMessage.value = "Import failed: ${expected.message}"             } finally {                 _importing.value = false             }
passwords/src/main/java/com/vayunmathur/passwords/ui/PasskeyAuthActivity.kt
diff --git a/passwords/src/main/java/com/vayunmathur/passwords/ui/PasskeyAuthActivity.kt b/passwords/src/main/java/com/vayunmathur/passwords/ui/PasskeyAuthActivity.ktindex 22a870bbaf..26aad18556 100644--- a/passwords/src/main/java/com/vayunmathur/passwords/ui/PasskeyAuthActivity.kt+++ b/passwords/src/main/java/com/vayunmathur/passwords/ui/PasskeyAuthActivity.kt@@ -71,6 +71,18 @@ class PasskeyAuthActivity : FragmentActivity() {         } catch (expected: IllegalStateException) {             Log.error(TAG, "Error in passkey $flow flow", expected)             setResult(RESULT_CANCELED)+        } catch (expected: org.json.JSONException) {+            // Calling app supplied malformed requestJson; cancel, don't crash.+            Log.error(TAG, "Malformed passkey request JSON in $flow flow", expected)+            setResult(RESULT_CANCELED)+        } catch (expected: IllegalArgumentException) {+            // Bad Base64url user handle / stored credential id from the calling app or import.+            Log.error(TAG, "Malformed passkey data in $flow flow", expected)+            setResult(RESULT_CANCELED)+        } catch (expected: java.security.GeneralSecurityException) {+            // Corrupt imported private key cannot sign; cancel the assertion.+            Log.error(TAG, "Passkey signing failed in $flow flow", expected)+            setResult(RESULT_CANCELED)         }         finish()     }
passwords/src/main/java/com/vayunmathur/passwords/ui/PasswordScreen.kt
diff --git a/passwords/src/main/java/com/vayunmathur/passwords/ui/PasswordScreen.kt b/passwords/src/main/java/com/vayunmathur/passwords/ui/PasswordScreen.ktindex df62fd8d76..8a704d32f6 100644--- a/passwords/src/main/java/com/vayunmathur/passwords/ui/PasswordScreen.kt+++ b/passwords/src/main/java/com/vayunmathur/passwords/ui/PasswordScreen.kt@@ -31,6 +31,7 @@ import androidx.core.net.toUri import com.vayunmathur.library.ui.Card import com.vayunmathur.library.ui.R as UiR import com.vayunmathur.library.util.sharedText+import com.vayunmathur.library.util.tryOrDefault import com.vayunmathur.library.ui.CircularProgressIndicator import com.vayunmathur.library.ui.ExperimentalMaterial3Api import com.vayunmathur.library.ui.ExternalIntents@@ -218,8 +219,9 @@ fun PasswordScreen(                         Text(stringResource(R.string.totp_not_configured))                     } else {                         val timeStep = state.now() / 1000 / 30+                        // Imported/synced secrets may not decode to a key; show placeholder, not a crash.                         val currentCode = remember(secret, timeStep) {-                            TOTP.generate(secret, timeStep * 30)+                            tryOrDefault("----") { TOTP.generate(secret, timeStep * 30) }                         }                         val secondsRemaining = ((30000 - state.now() % 30000) / 1000).toInt()