Schedule the sensitive-clipboard clear natively
What changed, and why it matters
This commit changes a Monero wallet's Android app so that sensitive text copied to the clipboard is cleared by the operating system itself after a timeout, instead of relying on a timer in the Dart/Flutter side of the app. The old Dart approach had a problem: when the app was in the background, Android 10 and newer blocked it from reading the clipboard, so the clear operation could fail or be delayed. The new native Android code writes the sensitive data, then schedules a native handler to clear it later. This is a defensive security improvement, not an introduced vulnerability.
No action required; this is a security-hardening patch. Reviewers may want to verify that clearAfterSeconds is validated on the Dart side and that the native handler is removed when the activity is destroyed to avoid leaking the Runnable.
Security signals we found
Sensitive clipboard data is explicitly flagged with android.content.extra.IS_SENSITIVE
Clipboard auto-clear timeout is moved from Dart to native Android to avoid background-clipboard access restrictions
Pending clear is cancelled and rescheduled on each new copy to avoid race conditions
Clear operation is wrapped in a try/catch to suppress failures
Evidence from the diff
The patch modifies MainActivity.kt. The copySensitive method now accepts a clearAfterSeconds parameter and uses a Handler on the main Looper to call clipboard.clearPrimaryClip() (API 28+) or setPrimaryClip with an empty clip on older Android versions. It also cancels any previous pending clear. The change addresses a real operational issue where a Flutter/Dart timer could not reliably read/clear the clipboard while the app was backgrounded on Android 10+ because of clipboard access restrictions for background apps. The commit message and inline comment explicitly frame this as fixing the background-clear behavior for sensitive clipboard data.
Changed components
android/app/src/main/kotlin/com/example/monero_light_wallet/MainActivity.ktcopySensitive methodFlutter MethodChannel handler for copySensitiveInspect captured patch +27 / −2
### android/app/src/main/kotlin/com/example/monero_light_wallet/MainActivity.kt
@@ -4,6 +4,8 @@ import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.os.Build
+import android.os.Handler
+import android.os.Looper
import android.os.PersistableBundle
import io.flutter.embedding.android.FlutterFragmentActivity
import io.flutter.embedding.engine.FlutterEngine
@@ -24,7 +26,10 @@ class MainActivity : FlutterFragmentActivity() {
.setMethodCallHandler { call, result ->
when (call.method) {
"copySensitive" -> {
- copySensitive(call.argument<String>("text") ?: "")
+ copySensitive(
+ call.argument<String>("text") ?: "",
+ call.argument<Int>("clearAfterSeconds") ?: 60,
+ )
result.success(null)
}
// Android 13 shows its own clipboard confirmation, so the
@@ -46,15 +51,35 @@ class MainActivity : FlutterFragmentActivity() {
}
}
+ private val clipboardHandler = Handler(Looper.getMainLooper())
+ private var pendingClipClear: Runnable? = null
+
// Copies [text] flagged as sensitive so keyboards/clipboard UIs don't show a
// preview and it isn't synced. The extras key is honored on Android 13+
// (ClipDescription.EXTRA_IS_SENSITIVE) and by some earlier OEM keyboards.
- private fun copySensitive(text: String) {
+ private fun copySensitive(text: String, clearAfterSeconds: Int) {
val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText("", text)
clip.description.extras = PersistableBundle().apply {
putBoolean("android.content.extra.IS_SENSITIVE", true)
}
clipboard.setPrimaryClip(clip)
+
+ // Clear natively after the timeout: the Dart timer reads the clipboard
+ // first, which Android 10+ blocks for a backgrounded app. Writing isn't.
+ pendingClipClear?.let { clipboardHandler.removeCallbacks(it) }
+ if (clearAfterSeconds <= 0) return
+ val clear = Runnable {
+ pendingClipClear = null
+ try {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
+ clipboard.clearPrimaryClip()
+ } else {
+ clipboard.setPrimaryClip(ClipData.newPlainText("", ""))
+ }
+ } catch (_: Exception) {}
+ }
+ pendingClipClear = clear
+ clipboardHandler.postDelayed(clear, clearAfterSeconds * 1000L)
}
}Why this scored 40/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.