passwords: commit the website field on Save, not just IME Done (#949)
- Commit
efb59c56cec1d02f07c0d1af29ce367461a2170b- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-30 18:48 UTC (5 days ago)
- Parents
6baf72b
The website field's text lived in local remember state and only reached the draft via KeyboardActions(onDone): typing a website and tapping Save silently dropped it (downstream draft -> Room converter -> KDBX EntryMapper URL/Websites mapping -> detail read-back were all verified sound; username/password/notes/TOTP bind directly, which is why only the website was lost). Save now commits the pending website first via new pure domain/WebsiteInput (Password.withPendingWebsite): scheme-less and full URLs accepted verbatim, whitespace/host-less text rejected — and a rejection aborts the save with a visible inline error (text kept), never a silent drop. New WebsiteInputTest (8 tests incl. Room + KDBX round-trips and foreign URL-only entries) and compose regressions (type website -> tap Save without IME Done; invalid input errors visibly without saving). Verified: :passwords:testDevUnitTest 459 tests, 0 failures; lint errors are pre-existing detector findings in untouched files.
Diff
passwords/AGENTS.md
diff --git a/passwords/AGENTS.md b/passwords/AGENTS.mdindex 4fb46424bf..5030263a90 100644--- a/passwords/AGENTS.md+++ b/passwords/AGENTS.md@@ -44,6 +44,7 @@ _Install: ./install passwords (dev by default)._ - com/vayunmathur/passwords/domain/Cbor.kt - com/vayunmathur/passwords/domain/ImportSource.kt - com/vayunmathur/passwords/domain/PasskeyLink.kt+- com/vayunmathur/passwords/domain/WebsiteInput.kt — pending-website merge/validation for the edit form (Save and IME Done both commit through it) ### platform/ — ViewModel, autofill, passkeys, backup - com/vayunmathur/passwords/platform/PasswordsViewModel.kt - com/vayunmathur/passwords/platform/PasswordsUiContract.kt
passwords/src/main/java/com/vayunmathur/passwords/domain/WebsiteInput.kt
diff --git a/passwords/src/main/java/com/vayunmathur/passwords/domain/WebsiteInput.kt b/passwords/src/main/java/com/vayunmathur/passwords/domain/WebsiteInput.ktnew file mode 100644index 0000000000..4f2c820c76--- /dev/null+++ b/passwords/src/main/java/com/vayunmathur/passwords/domain/WebsiteInput.kt@@ -0,0 +1,38 @@+package com.vayunmathur.passwords.domain++import com.vayunmathur.passwords.data.Password++/** Outcome of merging text typed into the website field into a draft. */+sealed interface PendingWebsite {+ /** Nothing pending, or the pending value was merged into [password]. */+ data class Accepted(val password: Password) : PendingWebsite++ /** The pending value cannot be a website; [input] is the trimmed text to show back. */+ data class Rejected(val input: String) : PendingWebsite+}++/**+ * Merges [rawInput] (the website field's not-yet-committed text) into this draft.+ *+ * The field keeps its text in local UI state until it is committed, so every exit+ * from the editor — IME Done and Save alike — must go through here. Accepting is+ * deliberately broad: a scheme-less host such as `example.com` is a valid website+ * (the detail screen adds `https://` when opening it and [DomainMatch] strips the+ * scheme when matching), and a full URL is stored as typed. Only text that cannot+ * be a host at all — whitespace inside it, or a scheme with no host after it — is+ * [PendingWebsite.Rejected], so the caller can surface it instead of dropping it.+ */+fun Password.withPendingWebsite(rawInput: String): PendingWebsite {+ val candidate = rawInput.trim()+ if (candidate.isEmpty()) return PendingWebsite.Accepted(this)+ if (!isPlausibleWebsite(candidate)) return PendingWebsite.Rejected(candidate)+ if (candidate in websites) return PendingWebsite.Accepted(this)+ return PendingWebsite.Accepted(copy(websites = websites + candidate))+}++/** True for text that can be stored as a website; see [withPendingWebsite]. */+fun isPlausibleWebsite(candidate: String): Boolean {+ if (candidate.any { it.isWhitespace() }) return false+ val host = candidate.substringAfter("://").substringBefore('/').substringBefore('?')+ return host.isNotEmpty()+}
passwords/src/main/java/com/vayunmathur/passwords/ui/PasswordEditScreen.kt
diff --git a/passwords/src/main/java/com/vayunmathur/passwords/ui/PasswordEditScreen.kt b/passwords/src/main/java/com/vayunmathur/passwords/ui/PasswordEditScreen.ktindex 200ea2fcd7..418e6bc7a5 100644--- a/passwords/src/main/java/com/vayunmathur/passwords/ui/PasswordEditScreen.kt+++ b/passwords/src/main/java/com/vayunmathur/passwords/ui/PasswordEditScreen.kt@@ -33,6 +33,8 @@ import com.vayunmathur.library.ui.Text import com.vayunmathur.library.ui.TextButton import com.vayunmathur.library.ui.R as UiR import com.vayunmathur.passwords.R+import com.vayunmathur.passwords.domain.PendingWebsite+import com.vayunmathur.passwords.domain.withPendingWebsite import com.vayunmathur.passwords.platform.PasswordEditUiState import com.vayunmathur.passwords.platform.PasswordsActions @@ -53,17 +55,36 @@ fun PasswordEditScreen( var saveError by remember { mutableStateOf<String?>(null) } val nameRequired = stringResource(R.string.name_required)+ val websiteInvalid = stringResource(R.string.website_invalid) val focusManager = LocalFocusManager.current - fun addWebsiteFromInput() {- val candidate = websiteInput.trim()- if (candidate.isNotEmpty()) {- actions.updateDraft { d ->- if (d.websites.contains(candidate)) d- else d.copy(websites = d.websites + candidate)+ /**+ * Commits the website field's pending text into the draft. Returns false when+ * the text cannot be a website: the error is shown and the text is kept, so it+ * is never silently dropped. Blank text is not an error (no website pending).+ */+ fun addWebsiteFromInput(): Boolean {+ if (websiteInput.isBlank()) {+ websiteInput = ""+ return true+ }+ var rejected = false+ actions.updateDraft { d ->+ when (val result = d.withPendingWebsite(websiteInput)) {+ is PendingWebsite.Accepted -> result.password+ is PendingWebsite.Rejected -> {+ rejected = true+ d+ } } }+ if (rejected) {+ saveError = websiteInvalid+ return false+ }+ saveError = null websiteInput = ""+ return true } DetailScaffold(@@ -76,6 +97,11 @@ fun PasswordEditScreen( saveError = nameRequired return@Button }+ // The website field's text lives outside the draft until committed;+ // commit it here too, or typing a website and tapping Save would+ // silently drop it. An implausible website aborts the save with a+ // visible error instead.+ if (!addWebsiteFromInput()) return@Button // Normalize empty TOTP to null before saving. actions.updateDraft { it.copy(totpSecret = it.totpSecret?.ifBlank { null }) } if (d.id == 0L) {
passwords/src/main/res/values/strings.xml
diff --git a/passwords/src/main/res/values/strings.xml b/passwords/src/main/res/values/strings.xmlindex 9978414f01..b15ee217d0 100644--- a/passwords/src/main/res/values/strings.xml+++ b/passwords/src/main/res/values/strings.xml@@ -127,6 +127,8 @@ <!-- Message when the device has no Bluetooth adapter, so cross-device passkeys cannot run --> <string name="cable_bluetooth_unsupported_message">This device does not support Bluetooth, so cross-device sign-in is unavailable.</string> <string name="name_required">Name cannot be empty</string>+ <!-- Error under the edit form when the pending website text cannot be a website. Sentence -->+ <string name="website_invalid">Enter a website without spaces, e.g. example.com</string> <!-- Section header for the two-way KeePass file sync settings. KDBX = KeePass database file extension, keep as-is --> <string name="sync_section">KDBX Sync</string>
passwords/src/test/java/com/vayunmathur/passwords/domain/WebsiteInputTest.kt
diff --git a/passwords/src/test/java/com/vayunmathur/passwords/domain/WebsiteInputTest.kt b/passwords/src/test/java/com/vayunmathur/passwords/domain/WebsiteInputTest.ktnew file mode 100644index 0000000000..2efe606d48--- /dev/null+++ b/passwords/src/test/java/com/vayunmathur/passwords/domain/WebsiteInputTest.kt@@ -0,0 +1,104 @@+package com.vayunmathur.passwords.domain++import com.vayunmathur.passwords.data.Converters+import com.vayunmathur.passwords.data.Password+import com.vayunmathur.passwords.sync.EntryMapper+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue++/**+ * The pending-website merge is the hand-off that used to lose websites: the edit+ * form keeps the field's text outside the draft until it is committed, so Save+ * must merge it (scheme-less and full URLs alike) and the merged value must then+ * round-trip through the Room converter and the KDBX field map.+ */+class WebsiteInputTest {++ private fun base() = Password(+ id = 5,+ name = "Example",+ username = "alice",+ email = "[email protected]",+ password = "s3cret",+ note = "a note",+ totpSecret = "JBSWY3DPEHPK3PXP",+ syncId = "sync-web",+ updatedAt = 1_700_000_000_000,+ )++ private fun accepted(result: PendingWebsite): Password {+ assertTrue(result is PendingWebsite.Accepted, "expected Accepted, got $result")+ return result.password+ }++ @Test fun schemeLessWebsiteIsMergedAndRoundTrips() {+ val merged = accepted(base().withPendingWebsite("example.com"))+ assertEquals(listOf("example.com"), merged.websites)+ // Neighbouring fields are untouched by the merge.+ assertEquals(base().copy(websites = listOf("example.com")), merged)+ assertRoundTrips(merged)+ }++ @Test fun fullUrlIsMergedVerbatimAndRoundTrips() {+ val merged = accepted(base().withPendingWebsite(" https://example.com/login?next=/home "))+ assertEquals(listOf("https://example.com/login?next=/home"), merged.websites)+ assertRoundTrips(merged)+ }++ @Test fun httpUrlIsMergedAndRoundTrips() {+ val merged = accepted(base().withPendingWebsite("http://example.com"))+ assertEquals(listOf("http://example.com"), merged.websites)+ assertRoundTrips(merged)+ }++ @Test fun pendingWebsiteAppendsAfterExistingWebsites() {+ val start = base().copy(websites = listOf("first.example"))+ val merged = accepted(start.withPendingWebsite("second.example"))+ assertEquals(listOf("first.example", "second.example"), merged.websites)+ assertRoundTrips(merged)+ }++ @Test fun blankPendingWebsiteChangesNothing() {+ val start = base().copy(websites = listOf("first.example"))+ assertEquals(start, accepted(start.withPendingWebsite(" ")))+ assertEquals(start, accepted(start.withPendingWebsite("")))+ }++ @Test fun duplicatePendingWebsiteIsNotAddedTwice() {+ val start = base().copy(websites = listOf("example.com"))+ assertEquals(start, accepted(start.withPendingWebsite("example.com")))+ }++ @Test fun implausibleWebsitesAreRejectedNotDropped() {+ for (bad in listOf("exa mple.com", "https://", "http://", "://", "https:///path")) {+ val result = base().withPendingWebsite(bad)+ assertTrue(result is PendingWebsite.Rejected, "expected Rejected for <$bad>, got $result")+ assertEquals(bad.trim(), result.input)+ }+ // A rejection carries no merged password, so nothing reaches the vault.+ assertEquals(emptyList(), base().websites)+ }++ @Test fun existingEntryWebsitesStillLoadFromVaultFields() {+ // Entries written by other KeePass clients carry only the standard URL field.+ val loaded = EntryMapper.toPassword(+ mapOf("Title" to "Old", "UserName" to "bob", "URL" to "example.com"),+ )+ assertEquals(listOf("example.com"), loaded.websites)+ // And the Room converter restores the stored list verbatim.+ assertEquals(+ listOf("example.com", "https://other.example"),+ Converters.toWebsites(Converters.fromWebsites(listOf("example.com", "https://other.example"))),+ )+ }++ private fun assertRoundTrips(pw: Password) {+ // Vault (Room) round-trip.+ assertEquals(pw.websites, Converters.toWebsites(Converters.fromWebsites(pw.websites)))+ // KDBX field-map round-trip: URL holds the first website, the rest survive too.+ val fields = EntryMapper.toFields(pw)+ assertEquals(pw.websites.first(), fields["URL"])+ assertEquals(pw, EntryMapper.toPassword(fields).copy(id = pw.id))+ }+}
passwords/src/test/java/com/vayunmathur/passwords/ui/PasswordScreensComposeTest.kt
diff --git a/passwords/src/test/java/com/vayunmathur/passwords/ui/PasswordScreensComposeTest.kt b/passwords/src/test/java/com/vayunmathur/passwords/ui/PasswordScreensComposeTest.ktindex ed3c446a1b..32f3325175 100644--- a/passwords/src/test/java/com/vayunmathur/passwords/ui/PasswordScreensComposeTest.kt+++ b/passwords/src/test/java/com/vayunmathur/passwords/ui/PasswordScreensComposeTest.kt@@ -1,6 +1,7 @@ package com.vayunmathur.passwords.ui import androidx.compose.ui.test.assertIsDisplayed+import androidx.compose.ui.test.hasSetTextAction import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onAllNodesWithText import androidx.compose.ui.test.hasScrollAction@@ -152,6 +153,50 @@ class PasswordScreensComposeTest { assertTrue(!saved) } + @Test fun editScreenSaveCommitsPendingWebsiteWithoutImeDone() {+ // Regression (#949): typing a website and tapping Save without pressing+ // IME Done used to drop the website silently, because only IME Done+ // committed the field's local text into the draft.+ var draft = Password(id = 5, name = "GitHub", username = "octocat")+ var savedDraft: Password? = null+ val actions = object : PasswordsActions {+ override fun updateDraft(transform: (Password) -> Password) { draft = transform(draft) }+ override fun saveDraft(onSaved: ((Long) -> Unit)?) { savedDraft = draft }+ }+ rule.setContent {+ PasswordEditScreen(+ state = PasswordEditUiState(saved = draft, draft = draft),+ actions = actions,+ )+ }+ val fields = rule.onAllNodes(hasSetTextAction())+ fields[fields.fetchSemanticsNodes().size - 1].performTextReplacement("example.com")+ rule.onNodeWithText("Save").performClick()+ assertEquals(listOf("example.com"), savedDraft?.websites)+ }++ @Test fun editScreenSaveWithImplausibleWebsiteShowsErrorAndDoesNotSave() {+ var draft = Password(id = 5, name = "GitHub", username = "octocat")+ var saved = false+ val actions = object : PasswordsActions {+ override fun updateDraft(transform: (Password) -> Password) { draft = transform(draft) }+ override fun saveDraft(onSaved: ((Long) -> Unit)?) { saved = true }+ }+ rule.setContent {+ PasswordEditScreen(+ state = PasswordEditUiState(saved = draft, draft = draft),+ actions = actions,+ )+ }+ val fields = rule.onAllNodes(hasSetTextAction())+ fields[fields.fetchSemanticsNodes().size - 1].performTextReplacement("exa mple.com")+ rule.onNodeWithText("Save").performClick()+ // The error renders at the top of the form, above the scrolled-to website field.+ rule.onNodeWithText("Enter a website without spaces, e.g. example.com").assertExists()+ assertTrue(!saved)+ assertEquals(emptyList(), draft.websites)+ }+ @Test fun editScreenFieldEditsFlowThroughUpdateDraft() { var draft = Password(id = 7, name = "Old", username = "", websites = listOf("a.com")) val actions = object : PasswordsActions {