fix(desktop): allow clipboard writes in the preview browser#3889

merged2
main
GitHub fork: fix/preview-clipboard-write-permission
openedJul 11, 2026byCcarlosricojr
View on GitHubTwo-way sync enabledPR last imported 9d ago

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:

1execute 'writeText' ... 'Clipboard': Write permission ...

i.e. the DOMException Failed to execute 'writeText' on 'Clipboard': Write permission denied. Any preview page calling navigator.clipboard.writeText() (or .write()) hits the same wall.

Root cause

apps/desktop/src/preview/BrowserSession.ts configured only a setPermissionRequestHandler, and its allow-list granted "clipboard-write":

ts
1const allowed = ["clipboard-read", "clipboard-write", "notifications", "geolocation"];
2callback(allowed.includes(permission));

Two bugs:

  1. clipboard-write is not a valid Electron permission name. The async Clipboard write API maps to clipboard-sanitized-write (Electron session docs), so the intended grant never matched.
  2. The write is gated by the permission check handler, which was never set. Per the docs: "you must also implement setPermissionRequestHandler to get complete permission handling. Most web APIs do a permission check and then make a permission request if the check is denied." Async clipboard.writeText() performs a synchronous clipboard-sanitized-write check; with no setPermissionCheckHandler, Electron's default denies it — hence "Write permission denied".

Fix

  • Add a setPermissionCheckHandler alongside the existing request handler.
  • Use the correct permission name clipboard-sanitized-write.
  • Share a single ALLOWED_PREVIEW_PERMISSIONS set 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 (adds setPermissionCheckHandler to the electron mock) with a case asserting both handlers grant clipboard-sanitized-write (plus the other allowed permissions) and deny the stale clipboard-write name and unrelated permissions like midi.

Verification

  • tsgo --noEmit (apps/desktop): 0 errors
  • vitest run src/preview/BrowserSession.test.ts: 6/6 passed
  • vp lint on the changed files: clean

🤖 Generated with Claude Code


Note

Low Risk Small, localized preview-session permission fix with no new capabilities beyond correcting clipboard write; covered by new unit tests.

Overview Fixes copy-to-clipboard in the desktop preview (e.g. Next.js/Vercel error overlay) by correcting Electron permission handling in BrowserSession.

Preview sessions now register setPermissionCheckHandler in addition to the request handler, because async navigator.clipboard.writeText() is gated on a synchronous permission check. Both handlers share a new ALLOWED_PREVIEW_PERMISSIONS set that swaps the invalid clipboard-write name for clipboard-sanitized-write, while keeping the same intended grants (clipboard-read, notifications, geolocation).

Tests extend the Electron mock and assert both handlers allow the real clipboard write permission and deny stale or unrelated names like midi.

Reviewed by Cursor Bugbot for commit 4520cdbf42bcb643fb2480ee582ac273ee0af9a9. 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

  • Adds ALLOWED_PREVIEW_PERMISSIONS constant in BrowserSession.ts containing clipboard-read, clipboard-sanitized-write, notifications, and geolocation.
  • Replaces the stale clipboard-write permission with clipboard-sanitized-write, which is the correct Electron permission for sanitized clipboard access.
  • Adds a setPermissionCheckHandler alongside the existing setPermissionRequestHandler so permissions are enforced through both the synchronous check and async request paths.

Macroscope summarized 4520cdb.

Added 1 commit

  • fix(desktop): allow clipboard writes in the preview browser6245874
Synced from GitHub

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 9220a93d-cef4-4a23-9af3-ccdf9a139ece

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Synced from GitHub

Approvability

Verdict: 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 (clipboard-sanitized-write) and adds the required check handler. No new permissions are introduced; comprehensive tests cover the behavior.

You can customize Macroscope's approvability policy. Learn more.

Added 1 commit

  • Merge branch 'main' into fix/preview-clipboard-write-permission4520cdb