fix(desktop): allow clipboard writes in the preview browser#3889
fix(desktop): allow clipboard writes in the preview browser#3889carlosricojr wants to merge 1 commit into
Conversation
The embedded preview browser only registered a permission *request* handler,
and its allow-list used `clipboard-write` — which is not a valid Electron
permission name. Async clipboard writes (`navigator.clipboard.writeText()`) are
gated by the permission *check* handler under the `clipboard-sanitized-write`
permission, which was never granted, so built-in "Copy" buttons in preview
content — e.g. the Next.js / Vercel error overlay's copy button — failed with:
Failed to execute 'writeText' on 'Clipboard': Write permission denied
Grant `clipboard-sanitized-write` (the correct permission name) through both a
new `setPermissionCheckHandler` and the existing request handler, sharing a
single allow-list so the two can't drift.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
ApprovabilityVerdict: Approved Straightforward bug fix that corrects Electron permission API usage to enable clipboard writes in the preview browser. The change uses the correct permission name ( You can customize Macroscope's approvability policy. Learn more. |
Problem
Built-in copy-to-clipboard buttons inside the preview browser don't work. The clearest example is the Next.js / Vercel error overlay's copy button, which surfaces:
i.e. the DOMException
Failed to execute 'writeText' on 'Clipboard': Write permission denied. Any preview page callingnavigator.clipboard.writeText()(or.write()) hits the same wall.Root cause
apps/desktop/src/preview/BrowserSession.tsconfigured only asetPermissionRequestHandler, and its allow-list granted"clipboard-write":Two bugs:
clipboard-writeis not a valid Electron permission name. The async Clipboard write API maps toclipboard-sanitized-write(Electronsessiondocs), so the intended grant never matched.setPermissionRequestHandlerto get complete permission handling. Most web APIs do a permission check and then make a permission request if the check is denied." Asyncclipboard.writeText()performs a synchronousclipboard-sanitized-writecheck; with nosetPermissionCheckHandler, Electron's default denies it — hence "Write permission denied".Fix
setPermissionCheckHandleralongside the existing request handler.clipboard-sanitized-write.ALLOWED_PREVIEW_PERMISSIONSset between both handlers so they can't drift.The granted set is unchanged in intent (
clipboard-read, clipboard write,notifications,geolocation) — this only corrects the write permission and wires up the check path. No new capabilities are exposed.Test
Extends
BrowserSession.test.ts(addssetPermissionCheckHandlerto the electron mock) with a case asserting both handlers grantclipboard-sanitized-write(plus the other allowed permissions) and deny the staleclipboard-writename and unrelated permissions likemidi.Verification
tsgo --noEmit(apps/desktop): 0 errorsvitest run src/preview/BrowserSession.test.ts: 6/6 passedvp linton the changed files: clean🤖 Generated with Claude Code
Note
Low Risk
Narrow change to preview Electron session permissions with no new capabilities beyond the prior intent; covered by unit tests.
Overview
Fixes copy-to-clipboard in the desktop preview (e.g. Next.js error overlay) by correcting Electron permission handling in
BrowserSession.Preview sessions now register
setPermissionCheckHandlerin addition to the request handler, because asyncnavigator.clipboard.writeText()is gated on a synchronousclipboard-sanitized-writecheck. The allow-list is centralized inALLOWED_PREVIEW_PERMISSIONS, replacing the invalidclipboard-writename withclipboard-sanitized-writewhile keeping the same intended grants (clipboard-read, notifications, geolocation).Tests mock
setPermissionCheckHandlerand assert both handlers allow the real write permission and deny stale or unrelated names likemidi.Reviewed by Cursor Bugbot for commit 6245874. Bugbot is set up for automated code reviews on this repo. Configure here.
Note
Fix clipboard writes in the preview browser by registering both permission handlers
ALLOWED_PREVIEW_PERMISSIONSconstant inBrowserSession.tscontainingclipboard-read,clipboard-sanitized-write,notifications, andgeolocation.clipboard-writepermission withclipboard-sanitized-write, which is the correct Electron permission for sanitized clipboard access.setPermissionCheckHandleralongside the existingsetPermissionRequestHandlerso permissions are enforced through both the synchronous check and async request paths.Macroscope summarized 6245874.