908ebebfa6ec
fix: MF-8 PeachClipboard — IS_SENSITIVE flag (API33+) + 30s process-lifetime auto-clear (only if unchanged) + 'clears in 30s' hint on all copy paths (entry detail/edit, vault list, recovery words, export, QS tile); 3 tests (v2.2.2)
- time
- 2026-06-09 18:32 UTC · 5 months ago
- machine
- koh2
- agent
- claude-opus-4-8
- session
- audit fixes clipboard
- parent
- 6f3c80a1efb3
↓ peach-android-908ebebf.tar.gzatlas at this save
files changed8 files
app/src/main/java/com/peachpasswords/qs/PeachTileService.kt+2 −9app/src/main/java/com/peachpasswords/shared/PeachClipboard.kt+72 −0app/src/main/java/com/peachpasswords/ui/vault/EntryDetailViewModel.kt+2 −4app/src/main/java/com/peachpasswords/ui/vault/EntryEditViewModel.kt+3 −6app/src/main/java/com/peachpasswords/ui/vault/ExportScreen.kt+2 −4app/src/main/java/com/peachpasswords/ui/vault/RecoveryWordsScreen.kt+2 −5app/src/main/java/com/peachpasswords/ui/vault/VaultListViewModel.kt+3 −6app/src/test/java/com/peachpasswords/shared/PeachClipboardTest.kt+52 −0app/src/main/java/com/peachpasswords/qs/PeachTileService.kt+2 −9
@@ -1,7 +1,5 @@11 package com.peachpasswords.qs22 3-import android.content.ClipData4-import android.content.ClipboardManager53 import android.content.Context64 import android.content.Intent75 import android.graphics.drawable.Icon@@ -65,13 +63,8 @@6563 }6664 6765 private fun copyToClipboard(label: String, value: String) {68- val clip = ClipData.newPlainText(label, value).apply {69- description.extras = android.os.PersistableBundle().apply {70- // Hide the value in the clipboard preview / history (API 33+).71- putBoolean("android.content.extra.IS_SENSITIVE", true)72- }73- }74- (getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager).setPrimaryClip(clip)66+ // Shared IS_SENSITIVE + 30s auto-clear path (MF-8).67+ com.peachpasswords.shared.PeachClipboard.copy(this, label, value)7568 }7669 7770 private fun updateTile() {app/src/main/java/com/peachpasswords/shared/PeachClipboard.kt+72 −0
@@ -0,0 +1,72 @@1+package com.peachpasswords.shared2+3+import android.content.ClipData4+import android.content.ClipDescription5+import android.content.ClipboardManager6+import android.content.Context7+import android.os.Build8+import android.os.PersistableBundle9+import android.widget.Toast10+import kotlinx.coroutines.CoroutineScope11+import kotlinx.coroutines.Dispatchers12+import kotlinx.coroutines.Job13+import kotlinx.coroutines.SupervisorJob14+import kotlinx.coroutines.delay15+import kotlinx.coroutines.launch16+17+/**18+ * Centralised clipboard writes for sensitive values (MF-8). Every copy:19+ * - flags the clip IS_SENSITIVE on API 33+ so the value is kept out of the20+ * clipboard preview overlay and clipboard history,21+ * - auto-clears [CLEAR_DELAY_MS] later, but only if our value is still on the22+ * clipboard — it never stomps something the user copied afterwards,23+ * - shows a brief "Copied — clears in 30s" hint, matching the browser.24+ *25+ * The wipe runs on a process-lifetime scope, NOT the caller's, so navigating away26+ * from the screen that triggered the copy cannot cancel it.27+ */28+object PeachClipboard {29+ const val CLEAR_DELAY_MS = 30_000L30+31+ private val scope = CoroutineScope(Dispatchers.Main.immediate + SupervisorJob())32+ private var pending: Job? = null33+34+ fun copy(context: Context, label: String, value: String, hint: Boolean = true) {35+ if (value.isEmpty()) return36+ val cm = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager37+ cm.setPrimaryClip(sensitiveClip(label, value))38+ if (hint) Toast.makeText(context, "Copied — clears in 30s", Toast.LENGTH_SHORT).show()39+ pending?.cancel()40+ pending = scheduleClear(41+ scope = scope,42+ expected = value,43+ readCurrent = { cm.primaryClip?.getItemAt(0)?.text?.toString() },44+ clear = { cm.setPrimaryClip(ClipData.newPlainText("", "")) },45+ )46+ }47+48+ private fun sensitiveClip(label: String, value: String): ClipData =49+ ClipData.newPlainText(label, value).apply {50+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {51+ description.extras = PersistableBundle().apply {52+ putBoolean(ClipDescription.EXTRA_IS_SENSITIVE, true)53+ }54+ }55+ }56+57+ /**58+ * Schedule a clipboard wipe after [delayMs] that fires only if [readCurrent]59+ * still returns [expected]. Extracted as a pure seam so the timing can be60+ * verified on a virtual scheduler without Android.61+ */62+ internal fun scheduleClear(63+ scope: CoroutineScope,64+ expected: String,65+ readCurrent: () -> String?,66+ clear: () -> Unit,67+ delayMs: Long = CLEAR_DELAY_MS,68+ ): Job = scope.launch {69+ delay(delayMs)70+ if (readCurrent() == expected) clear()71+ }72+}app/src/main/java/com/peachpasswords/ui/vault/EntryDetailViewModel.kt+2 −4
@@ -1,8 +1,7 @@11 package com.peachpasswords.ui.vault22 3-import android.content.ClipData4-import android.content.ClipboardManager53 import android.content.Context4+import com.peachpasswords.shared.PeachClipboard65 import androidx.lifecycle.SavedStateHandle76 import androidx.lifecycle.ViewModel87 import androidx.lifecycle.viewModelScope@@ -71,8 +70,7 @@7170 7271 fun copy(field: String, value: String?) {7372 val v = value ?: return74- val cm = appContext.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager75- cm.setPrimaryClip(ClipData.newPlainText("peach", v))73+ PeachClipboard.copy(appContext, "peach", v)7674 viewModelScope.launch {7775 _copiedField.value = field7876 kotlinx.coroutines.delay(1200)app/src/main/java/com/peachpasswords/ui/vault/EntryEditViewModel.kt+3 −6
@@ -1,8 +1,7 @@11 package com.peachpasswords.ui.vault22 3-import android.content.ClipData4-import android.content.ClipboardManager53 import android.content.Context4+import com.peachpasswords.shared.PeachClipboard65 import androidx.lifecycle.SavedStateHandle76 import androidx.lifecycle.ViewModel87 import androidx.lifecycle.viewModelScope@@ -141,8 +140,7 @@141140 fun copyGenerated() {142141 val pw = _form.value.password143142 if (pw.isEmpty()) return144- val cm = appContext.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager145- cm.setPrimaryClip(ClipData.newPlainText("peach", pw))143+ PeachClipboard.copy(appContext, "peach", pw)146144 viewModelScope.launch {147145 _form.update { it.copy(genCopied = true) }148146 kotlinx.coroutines.delay(1200)@@ -151,8 +149,7 @@151149 }152150 153151 fun copyHistoryPassword(pw: String) {154- val cm = appContext.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager155- cm.setPrimaryClip(ClipData.newPlainText("peach", pw))152+ PeachClipboard.copy(appContext, "peach", pw)156153 }157154 fun setGenLength(n: Int) = _form.update { it.copy(genLength = n) }158155 fun setGenUpper(b: Boolean) = _form.update { it.copy(genUpper = b) }app/src/main/java/com/peachpasswords/ui/vault/ExportScreen.kt+2 −4
@@ -1,8 +1,7 @@11 package com.peachpasswords.ui.vault22 3-import android.content.ClipData4-import android.content.ClipboardManager53 import android.content.Context4+import com.peachpasswords.shared.PeachClipboard65 import android.net.Uri76 import androidx.activity.compose.rememberLauncherForActivityResult87 import androidx.activity.result.contract.ActivityResultContracts@@ -307,8 +306,7 @@307306 } else {308307 val text = output!!309308 GhostButton("Copy to clipboard", accent) {310- val cm = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager311- cm.setPrimaryClip(ClipData.newPlainText("peach backup", text))309+ PeachClipboard.copy(context, "peach backup", text)312310 status = ExportStrings.COPIED313311 }314312 Spacer(Modifier.size(8.dp))app/src/main/java/com/peachpasswords/ui/vault/RecoveryWordsScreen.kt+2 −5
@@ -1,8 +1,6 @@11 package com.peachpasswords.ui.vault22 3-import android.content.ClipData4-import android.content.ClipboardManager5-import android.content.Context3+import com.peachpasswords.shared.PeachClipboard64 import androidx.activity.compose.rememberLauncherForActivityResult75 import androidx.activity.result.contract.ActivityResultContracts86 import androidx.compose.foundation.background@@ -163,8 +161,7 @@163161 }164162 Divider()165163 GhostButton("copy all words", accent) {166- val cm = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager167- cm.setPrimaryClip(ClipData.newPlainText("peach recovery", joined))164+ PeachClipboard.copy(context, "peach recovery", joined)168165 }169166 Spacer(Modifier.size(8.dp))170167 GhostButton("save as text file", peachColors.T1) { saveLauncher.launch("peach-recovery.txt") }app/src/main/java/com/peachpasswords/ui/vault/VaultListViewModel.kt+3 −6
@@ -1,8 +1,7 @@11 package com.peachpasswords.ui.vault22 3-import android.content.ClipData4-import android.content.ClipboardManager53 import android.content.Context4+import com.peachpasswords.shared.PeachClipboard65 import androidx.lifecycle.ViewModel76 import androidx.lifecycle.viewModelScope87 import com.peachpasswords.vault.TOTPGenerator@@ -131,8 +130,7 @@131130 // ── Context-menu copy helpers ───────────────────────────────────────────132131 private fun copyText(value: String?) {133132 val v = value?.takeIf { it.isNotEmpty() } ?: return134- val cm = appContext.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager135- cm.setPrimaryClip(ClipData.newPlainText("peach", v))133+ PeachClipboard.copy(appContext, "peach", v)136134 }137135 138136 fun copyUsername(entry: VaultEntry) = copyText(entry.username ?: entry.email)@@ -146,8 +144,7 @@146144 /** Copy a login's password, flash the row, and fire the swipe-copy haptic. */147145 fun copyPassword(entry: VaultEntry) {148146 val pw = entry.password ?: return149- val cm = appContext.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager150- cm.setPrimaryClip(ClipData.newPlainText("peach", pw))147+ PeachClipboard.copy(appContext, "peach", pw)151148 viewModelScope.launch {152149 _flashId.value = entry.id153150 kotlinx.coroutines.delay(600)app/src/test/java/com/peachpasswords/shared/PeachClipboardTest.kt+52 −0
@@ -0,0 +1,52 @@1+package com.peachpasswords.shared2+3+import kotlinx.coroutines.CoroutineScope4+import kotlinx.coroutines.test.StandardTestDispatcher5+import kotlinx.coroutines.test.advanceTimeBy6+import kotlinx.coroutines.test.runCurrent7+import kotlinx.coroutines.test.runTest8+import org.junit.Assert.assertEquals9+import org.junit.Assert.assertFalse10+import org.junit.Assert.assertTrue11+import org.junit.Test12+13+/**14+ * MF-8: a sensitive copy auto-clears after 30s — but only if our value is still on15+ * the clipboard. Drives the pure [PeachClipboard.scheduleClear] seam on a virtual16+ * scheduler (the Android ClipboardManager is faked with a plain string box).17+ */18+class PeachClipboardTest {19+20+ @Test fun clears_after_delay_when_value_unchanged() = runTest {21+ var clipboard: String? = "s3cret"22+ PeachClipboard.scheduleClear(23+ scope = CoroutineScope(StandardTestDispatcher(testScheduler)),24+ expected = "s3cret",25+ readCurrent = { clipboard },26+ clear = { clipboard = "" },27+ )28+ advanceTimeBy(PeachClipboard.CLEAR_DELAY_MS - 1_000); runCurrent()29+ assertEquals("not cleared before the window elapses", "s3cret", clipboard)30+ advanceTimeBy(2_000); runCurrent()31+ assertEquals("cleared once the 30s window elapses", "", clipboard)32+ }33+34+ @Test fun does_not_clear_when_user_copied_something_else() = runTest {35+ var clipboard: String? = "s3cret"36+ var cleared = false37+ PeachClipboard.scheduleClear(38+ scope = CoroutineScope(StandardTestDispatcher(testScheduler)),39+ expected = "s3cret",40+ readCurrent = { clipboard },41+ clear = { cleared = true },42+ )43+ clipboard = "user typed this after copying" // user copied something else within 30s44+ advanceTimeBy(PeachClipboard.CLEAR_DELAY_MS + 1_000); runCurrent()45+ assertFalse("must never wipe what the user copied afterwards", cleared)46+ assertEquals("user typed this after copying", clipboard)47+ }48+49+ @Test fun thirty_second_default_matches_browser() {50+ assertTrue(PeachClipboard.CLEAR_DELAY_MS == 30_000L)51+ }52+}