Skip to content

[PM-40488] fix: clear clipboard setting does not clear the clipboard on Android 13+ - #7169

Merged
david-livefront merged 4 commits into
bitwarden:mainfrom
mvanhorn:fix/7069-clear-clipboard-not-clearing
Sep 21, 2026
Merged

david-livefront merged 4 commits into
bitwarden:mainfrom
mvanhorn:fix/7069-clear-clipboard-not-clearing

Conversation

@mvanhorn

@mvanhorn mvanhorn commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor

🎟️ Tracking

Fixes #7069

📔 Objective

The "clear clipboard" setting does not clear the clipboard on Android 13+. Setting a short interval (e.g. 10 seconds), copying a password, and waiting leaves the secret on the clipboard. Multiple users have confirmed this across recent devices (Samsung Galaxy S25+ on Android 16, Fairphone FP5 on Android 15, Samsung SM-A366B on Android 16) and app versions 2026.2.1 through 2026.6.x.

The clear is scheduled by BitwardenClipboardManagerImpl.setText() as a delayed OneTimeWorkRequest and executed by ClearClipboardWorker.doWork(), which relies solely on clipboardManager.clearPrimaryClip(). On newer Android versions clearPrimaryClip() can be a no-op, so the secret is never evicted.

This change makes ClearClipboardWorker.doWork() clear robustly: it first overwrites the primary clip with an empty, sensitive ClipData (marking ClipDescription.EXTRA_IS_SENSITIVE on API 33+, and the legacy android.content.extra.IS_SENSITIVE key below that, matching the pattern already used in BitwardenClipboardManagerImpl.setText), then calls clearPrimaryClip() (guarded to API 28+) to remove the placeholder entry on devices that honor it. Background clipboard restrictions on Android 10+ gate reads, not writes, so the background overwrite/clear remains permitted. The fix stays entirely within the existing worker, with no scheduling or DI changes.

A unit test (ClearClipboardWorkerTest) covers the Android 13+ path to prevent regression.

📸 Screenshots

Not applicable - this is a background-worker/behavioral fix with no UI surface.


AI was used for assistance.

@mvanhorn
mvanhorn requested review from a team and david-livefront as code owners July 16, 2026 05:26
@bitwarden-bot

Copy link
Copy Markdown
Collaborator

Thank you for your contribution! We've added this to our internal tracking system for review.
ID: PM-40488
Link: https://bitwarden.atlassian.net/browse/PM-40488

Details on our contribution process can be found here: https://contributing.bitwarden.com/contributing/pull-requests/community-pr-process.

@bitwarden-bot bitwarden-bot changed the title fix: clear clipboard setting does not clear the clipboard on Android 13+ [PM-40488] fix: clear clipboard setting does not clear the clipboard on Android 13+ Jul 16, 2026
Remove the isBuildVersionAtLeast(P) gate and the OmitFromCoverage
annotation now that the worker has test coverage.
@mvanhorn

Copy link
Copy Markdown
Contributor Author

Both removed in 5e2465c: the version check is gone and OmitFromCoverage dropped now that the tests cover the worker.

clipboardManager = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
worker = ClearClipboardWorker(
appContext = context,
workerParams = mockk(relaxed = true),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be done inline above?

private val context = RuntimeEnvironment.getApplication()
private val clipboardManager = context.getSystemService<ClipboardManager>()
private val worker: ClearClipboardWorker = ClearClipboardWorker(
    appContext = context,
    workerParams = mockk(relaxed = true),
)

or do these tests require a new instance each time?

Per review, fold the @before setup into property initializers. getSystemService<T>()
returns T?, so the clipboard manager is unwrapped with requireNotNull rather than !!,
which detekt's UnsafeCallOnNullableType rejects.

Signed-off-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
@mvanhorn

Copy link
Copy Markdown
Contributor Author

Pushed a0302ac with the inline setup you suggested.

One deviation from your snippet: context.getSystemService<ClipboardManager>() returns ClipboardManager?, and the tests call it non-null throughout, so as written it does not compile. I unwrapped with requireNotNull(...) rather than !!, since UnsafeCallOnNullableType is active in detekt-config.yml and requireNotNull is already the pattern used elsewhere in the test sources.

On your other two comments, I believe both are already addressed and GitHub has them marked outdated:

  • The isBuildVersionAtLeast(Build.VERSION_CODES.P) guard around clearPrimaryClip() is gone; the call is unconditional now.
  • The code you asked me to drop now that tests cover it is removed.

If either of those is not what you meant, point me at the current line and I will fix it.

Verified locally: :app:testStandardDebugUnitTest --tests "*ClearClipboardWorkerTest*" runs 4 tests, 0 failures, 0 errors; ./gradlew detekt passes.

appContext.getSystemService(CLIPBOARD_SERVICE) as ClipboardManager

override fun doWork(): Result {
clipboardManager.setPrimaryClip(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make sure I'm on the up-and-up... The idea is to set "nothing" to the clipboard, overwriting what sensitive information was there, and then invoke .clearPrimaryClip (removing the new empty content) as per Android platforms on which the method is not a no-op.

Is this correct?

@mvanhorn

mvanhorn commented Sep 3, 2026

Copy link
Copy Markdown
Contributor Author

Yes, that's the intent. setPrimaryClip replaces whatever sensitive content was on the clipboard with an empty clip flagged IS_SENSITIVE, so the data is gone from the clipboard regardless of what happens next. clearPrimaryClip then removes that empty entry so the clipboard isn't left holding a blank item, on platforms where it actually does something. The overwrite is the part that protects the data; the clear is cleanup.

@fnc314

fnc314 commented Sep 3, 2026 via email

Copy link
Copy Markdown

@fnc314

fnc314 commented Sep 4, 2026 via email

Copy link
Copy Markdown

@david-livefront

Copy link
Copy Markdown
Collaborator

Since emails make it difficult to follow exactly which function is deprecated, can you share that explicitly? I cannot get back to my machine until the end of the weekend. All the best, Franco N. Colaizzi
…
On Fri, Sep 4, 2026 at 11:06 AM David Perez @.> wrote: @.* commented on this pull request. ------------------------------ In app/src/main/kotlin/com/x8bit/bitwarden/data/platform/manager/clipboard/ClearClipboardWorker.kt <#7169 (comment)>: > @@ -20,6 +23,19 @@ class ClearClipboardWorker( appContext.getSystemService(CLIPBOARD_SERVICE) as ClipboardManager override fun doWork(): Result { + clipboardManager.setPrimaryClip( + ClipData + .newPlainText("", "") + .apply { + description.extras = persistableBundleOf( Since this function is deprecated, can we do something like this: description.extras = PersistableBundle().apply { putBoolean( if (isBuildVersionAtLeast(version = Build.VERSION_CODES.TIRAMISU)) { ClipDescription.EXTRA_IS_SENSITIVE } else { "android.content.extra.IS_SENSITIVE" }, true, ) } — Reply to this email directly, view it on GitHub <#7169?email_source=notifications&email_token=ABN6AIBVSEL7TEGFFIUMGIL5NLK7DA5CNFSNUABKM5UWIORPF5TWS5BNNB2WEL2QOVWGYUTFOF2WK43UKJSXM2LFO4XTKMJRGQ3DMNBSGAY2M4TFMFZW63VHMNXW23LFNZ2KKZLWMVXHJLDGN5XXIZLSL5RWY2LDNM#pullrequestreview-5114664201>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABN6AIBEK6X53IH2OLQ3SQT5NLK7DAVCNFSNUABEKJSXA33TNF2G64TZHM2TONBVGIYTIMZ3JFZXG5LFHM2DQOJYHE4DONZRGOQXMAQ . You are receiving this because you commented.Message ID: @.***>

persistableBundleOf is deprecated

Screenshot 2026-09-04 at 11 12 00 AM

@fnc314

fnc314 commented Sep 4, 2026 via email

Copy link
Copy Markdown

persistableBundleOf is deprecated; build the extras bundle directly and
set the version-gated IS_SENSITIVE key with putBoolean.
@mvanhorn

Copy link
Copy Markdown
Contributor Author

Done, thanks - that was the right call. Swapped persistableBundleOf for building the PersistableBundle directly and setting the version-gated key with putBoolean, as you suggested.

To answer the question that got lost in the email quoting: the deprecated one was androidx.core.os.persistableBundleOf. It is gone now, along with its import.

Your three earlier comments look settled on this head as well: there is no standalone version check left beyond the inline key selection, the leftover you flagged is gone now that the tests cover it, and the test already hoists context / clipboardManager / worker inline at the class level the way you sketched, since none of the tests need a fresh instance.

@codecov

codecov Bot commented Sep 21, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.95%. Comparing base (5c13e26) to head (ae91f85).
⚠️ Report is 152 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7169      +/-   ##
==========================================
+ Coverage   85.69%   85.95%   +0.25%     
==========================================
  Files         941      947       +6     
  Lines       67045    67263     +218     
  Branches     9660     9355     -305     
==========================================
+ Hits        57454    57814     +360     
+ Misses       6137     5966     -171     
- Partials     3454     3483      +29     
Flag Coverage Δ
app-data 18.32% <100.00%> (+1.02%) ⬆️
app-ui-auth-tools 18.58% <0.00%> (-0.23%) ⬇️
app-ui-platform 16.58% <0.00%> (+0.01%) ⬆️
app-ui-vault 27.59% <0.00%> (-0.51%) ⬇️
authenticator 5.99% <0.00%> (-0.08%) ⬇️
lib-core-network-bridge 4.07% <0.00%> (-0.92%) ⬇️
lib-data-ui 1.18% <0.00%> (+0.04%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@david-livefront
david-livefront added this pull request to the merge queue Sep 21, 2026
Merged via the queue into bitwarden:main with commit d70e133 Sep 21, 2026
21 of 23 checks passed
@mvanhorn

Copy link
Copy Markdown
Contributor Author

Thanks for merging, @david-livefront. Good to have PM-40488 closed out - leaving a copied password on the clipboard past the interval was the part that worried me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[PM-39184] Clear clipboard setting does not clear clipboard

4 participants