keyboard: upgrade clip sensitivity on password re-copy
- Commit
7a3ad510ad5c6455fc244e682728d8c448dc572b- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 20:03 UTC (29 days ago)
- Parents
d9055fb
Symptom: copy a passphrase-like string from a normal field, then copy the same string from a password field. The history entry stayed the old, non-sensitive one: it was written to disk by persistState and shown unblurred in the clipboard page and strip chip, even though the latest copy was explicitly flagged sensitive. Root cause: ClipboardStore.add de-duplicates on content alone (sameContentAs compares only the text), and the duplicate path kept the pre-existing ClipItem unchanged, discarding the new item's sensitive flag. capture() had done its job (inPasswordField marks the item sensitive); add() threw that away. Fix: when a duplicate arrives flagged sensitive and the stored entry is not, the stored entry is promoted to sensitive as it moves to the front. Sensitivity never downgrades on a later plain re-copy, and encode() then keeps the clip out of the persisted history. Verification: new ClipboardTest case covering the upgrade, the no-downgrade direction, and absence from encode() output; targeted :keyboard:testDevUnitTest --tests ClipboardTest passes.
Diff
keyboard/src/main/java/com/vayunmathur/keyboard/util/Clipboard.kt
diff --git a/keyboard/src/main/java/com/vayunmathur/keyboard/util/Clipboard.kt b/keyboard/src/main/java/com/vayunmathur/keyboard/util/Clipboard.ktindex 957269055d..b623522a75 100644--- a/keyboard/src/main/java/com/vayunmathur/keyboard/util/Clipboard.kt+++ b/keyboard/src/main/java/com/vayunmathur/keyboard/util/Clipboard.kt@@ -98,7 +98,15 @@ class ClipboardStore(private val imageDir: File) { fun add(item: ClipItem): ClipItem? { val existing = items.firstOrNull { it.sameContentAs(item) } if (existing != null) {- items = listOf(existing) + (items - existing)+ // Sensitivity only ever upgrades: the same text re-copied from a+ // password field is a credential now, whatever it was before, and+ // keeping the old entry would persist and display it as plain.+ val kept = if (item.sensitive && !existing.sensitive) {+ existing.copy(sensitive = true)+ } else {+ existing+ }+ items = listOf(kept) + (items - existing) return null } items = cap(listOf(item) + items, ::discardImage)
keyboard/src/test/java/com/vayunmathur/keyboard/util/ClipboardTest.kt
diff --git a/keyboard/src/test/java/com/vayunmathur/keyboard/util/ClipboardTest.kt b/keyboard/src/test/java/com/vayunmathur/keyboard/util/ClipboardTest.ktindex 9497438c39..71e01265a8 100644--- a/keyboard/src/test/java/com/vayunmathur/keyboard/util/ClipboardTest.kt+++ b/keyboard/src/test/java/com/vayunmathur/keyboard/util/ClipboardTest.kt@@ -93,6 +93,21 @@ class ClipboardTest { assertEquals(listOf("one", "two"), store.items.map { it.text }) } + /** The same text copied from a password field must not stay plain in the history. */+ @Test+ fun `re-copying as sensitive upgrades the existing entry`() {+ val store = ClipboardStore(java.io.File("/tmp/clip-test-does-not-exist"))+ store.add(text(1, "correct horse battery staple"))+ assertEquals(null, store.add(text(2, "correct horse battery staple", sensitive = true)))+ assertEquals(1, store.items.size)+ assertTrue(store.items.single().sensitive)+ // And it therefore stays out of the persisted form.+ assertFalse(ClipboardStore.encode(store.items).contains("correct horse"))+ // A later plain re-copy never downgrades it back.+ store.add(text(3, "correct horse battery staple"))+ assertTrue(store.items.single().sensitive)+ }+ @Test fun `ids are unique and monotonic even within one millisecond`() { val ids = (1..1000).map { ClipboardStore.nextId() }