fix: enforce max clipboard size - #21782
Conversation
david-allison
left a comment
There was a problem hiding this comment.
LGTM, my thoughts almost certainly want to be split out (or ignored) unless they're really easy.
There was a problem hiding this comment.
I'm wondering whether we should have a value class to enforce the invariant/trimming, rather than doing it silently here
There was a problem hiding this comment.
Good idea. I've taken a shot at this in the form of TruncatedString, could you take a look and let me know if this was what you were thinking?
| .getApplicationContext<Context>() | ||
| .getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager | ||
| context = ApplicationProvider.getApplicationContext() | ||
| clipboardManager = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager |
There was a problem hiding this comment.
nit: there's getSystemService<T>
(Maybe a requireSystemService<T> would improve it further)
| @@ -155,7 +155,7 @@ class SyncMediaWorker( | |||
| internal fun getCopyToClipboardIntent(text: String): PendingIntent { | |||
There was a problem hiding this comment.
Also here for the value class
There is already a Kotlin string function for this function with identical behaviour.
This helper function is no longer used anywhere. Introduced at `092f4f9f47`. Usage removed at `aabd99dc4e`.
Clipboard size is limited by the Binder IPC buffer size. If too much is copied, the app may throw an error (TransactionTooLarge). Previously, we protected against copying too much only at callsites (SyncMediaWorker) or not at all. This commit enforces a 200KB maximum copy size (100K chars) at the clipboard copying function so that callers no longer need to worry about it. This is purposefully much lower than 1MB because the 1MB limit is shared between all ongoing transactions for the entire device. Adds unit tests for copyToClipboard. Assisted-by: Claude Opus 5
d0d6885 to
df07b89
Compare
|
Changes:
Ready for review! |
Suggested by David. Use an inline value class so that callers of copyToClipboard are made aware that the string they pass may be truncated if it is too long. Modifies unit tests accordingly. Assisted-by: Claude Opus 5
A new helper method for getting parcelable extras, modeled after Bundle.getParcelableCompat in BundleUtils. Assisted-by: Claude Opus 5
Hanging docstring from 95a1a87. Moved to NoteEditorMultimediaController already, so this should be safe to delete. It is currently annotating a completely different method. Discovered as part of a refactor involving getParcelableExtraCompat.
Following the getIntent pattern for this codebase. Assisted-by: Claude Opus 5
df07b89 to
49ae922
Compare
|
Changes:
|
criticalAY
left a comment
There was a problem hiding this comment.
LGTM! once the concerns from David are handled, feel free to add to queue
@david-allison fyi
| text: TruncatedString, | ||
| ): Intent = | ||
| Intent(context, CopyToClipboardReceiver::class.java).apply { | ||
| putExtra(EXTRA_SYNC_ERROR_LOG, text) |
There was a problem hiding this comment.
non-blocking: Could the extra stay a String
There was a problem hiding this comment.
Interesting! I assumed value classes would be handled as a 0-cost abstraction.
I'll check when I'm next at my PC
There was a problem hiding this comment.
Ah, as in have the getIntent method take a TruncatedString, turn it into a raw string, send it, then have the onReceive method pack it back into a TruncatedString?
I think I've already handled all of David's feedback. Though, I am now realizing that for some reason this is a BroadcastReceiver, not an AnkiBroadcastReceiver. I'll probably stack on a new commit to turn it into an AnkiBroadcastReceiver, too.
| private const val EXTRA_SYNC_ERROR_LOG = "syncErrorLog" | ||
|
|
||
| /** | ||
| * Method for getting an intent for this service. |
There was a problem hiding this comment.
nit:
Method for getting an intent for this receiver....
Note
Assisted-by: Claude Opus 5
Purpose / Description
Clipboard size is limited by the Binder IPC buffer size. If too much is copied, the app may throw an error (TransactionTooLarge). Previously, we protected against copying too much only at callsites (SyncMediaWorker) or not at all.
Fixes
Approach
This commit enforces a 200KB maximum copy size (100K chars) at the clipboard copying function so that callers no longer need to worry about it. This is purposefully much lower than 1MB because the 1MB limit is shared between all ongoing transactions for the entire device.
Adds unit tests for copyToClipboard. Deletes a useless string util function that was only used at a clipboard calling site. Removed an unused clipboard util function.
How Has This Been Tested?
Learning
Checklist