Skip to content

fix(terminal): make copy work in the HTTP web client - #10534

Merged
OrcaWin merged 4 commits into
stablyai:mainfrom
badaverse:fix/web-client-copy-insecure-context
Jul 28, 2026
Merged

OrcaWin merged 4 commits into
stablyai:mainfrom
badaverse:fix/web-client-copy-insecure-context

Conversation

@badaverse

@badaverse badaverse commented Jul 25, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #10532 · Related: #9900

User-visible change

Copying from a terminal in the paired web client did nothing when the client was connected over plain HTTP (e.g. a LAN address). navigator.clipboard only exists in secure contexts, and the web preload's writeClipboardText resolved successfully without writing anything, so every copy surface failed silently on the client PC:

  • Ctrl/Cmd+Shift+C (terminal.copySelection)
  • Context-menu Copy and right-click-to-copy
  • Copy-on-select (terminalClipboardOnSelect)
  • OSC 52 writes from TUIs
  • Copy Pane ID / Copy Terminal ID

This PR adds the copy-side counterpart of #9900 (which fixed paste in the HTTP web client): when navigator.clipboard.writeText is unavailable, fall back to document.execCommand('copy') on a temporary textarea — the only clipboard write available in insecure contexts. When a secure-context write is permission-gated (rejects), retry through the same fallback while the user gesture's transient activation is still live. When no write path exists at all, reject instead of silently succeeding, so callers can react.

Scope

  • src/renderer/src/web/web-clipboard-copy-fallback.ts (new): execCommand copy helper — hidden readonly textarea, focus restore, cleanup on throw.
  • src/renderer/src/web/web-preload-api.ts: writeClipboardText tries the async clipboard API first, then the fallback; rejects when neither works.
  • Local Electron clipboard behavior, the image-paste RPC path, and all terminal UI are untouched.

Known limitation (out of scope)

Context-menu Paste on an insecure-context web client cannot be fixed: browsers only expose clipboard reads there through a native paste ClipboardEvent, which a menu click cannot synthesize. Keyboard Ctrl/Cmd+V works via the #9900 fallback.

Tests

  • New web-clipboard-copy-fallback.test.ts (5 tests): copies through the temporary textarea, cleans up and restores focus, reports failure when execCommand returns false / throws / is absent.
  • web-preload-api.test.ts (+3 tests): insecure-context fallback engages, no-write-path rejects with a clear error, permission-gated writeText rejection falls back and still succeeds.
  • Focused suites pass: web preload (86), terminal clipboard paste/event-paste, OSC 52, main-process clipboard IPC (65). pnpm lint, pnpm typecheck pass.
  • pnpm test full suite passes (35,747 passed; the only 2 failures were in src/relay/agent-exec-handler.test.ts and are environment-dependent — they compare spawn env against the dev shell's injected GIT_CONFIG_* vars and pass with a clean environment; unrelated to this change).
  • pnpm build passes.

Manual validation

Validated on a real paired session: host app (patched web bundle) + browser client over plain http://<LAN IP> (window.isSecureContext === false, navigator.clipboard === undefined). Terminal copy now lands on the client PC's clipboard, and Ctrl/Cmd+V paste (#9900 path) still works. Happy to attach a screen recording in a comment if useful.

Platform / remote notes

  • Renderer web-client code only; no Node/Electron API use, no platform conditionals needed. execCommand('copy') is deprecated but supported by Chrome, Edge, Firefox, and Safari, and is the established fallback for insecure contexts.
  • Text clipboard intentionally stays on the client machine (host↔client text never crosses the runtime RPC boundary — unchanged). SSH/remote image-paste routing is untouched.
  • OSC 52 writes that arrive outside a user gesture on an insecure-context client still cannot copy (browser restriction); they now reject instead of pretending success, and existing callers already swallow that.

Screenshots

No visual change — no new UI; the fallback textarea is invisible and removed synchronously. See "Manual validation" above for behavior verification.

Code review summary

Reviewed by Claude (Fable 5) during implementation:

  • Cross-platform: Change is confined to the browser web-client preload (src/renderer/src/web/). No Node/Electron APIs, no path handling, no keyboard-chord changes, so macOS/Windows/Linux desktop builds are unaffected; client browsers (Chrome, Edge, Firefox, Safari) all support the execCommand('copy') fallback. The Electron preload (src/preload/index.ts) and main-process clipboard IPC are untouched.
  • SSH / remote / local: Text clipboard remains client-local by design — no text ever crosses the runtime RPC boundary (CLIPBOARD_METHODS stays image-only). The SSH/remote image temp-file path and connectionId routing are unchanged. Local Electron copy behavior is unchanged.
  • Agents / integrations / git providers: Not touched; the change is below every agent- or provider-specific layer.
  • Performance: The fallback only runs when the async clipboard API is missing or rejects. Cost per copy is one hidden textarea append/remove and one execCommand call. Copy-on-select on an insecure-context client triggers this per selection change (xterm can fire several during a drag); bounded, synchronous DOM work with focus restored each time — acceptable, and only on the already-degraded HTTP path.
  • UI quality: No visual change. The helper textarea is position: fixed, opacity: 0, readonly (no mobile keyboard), removed in finally, and the previously focused element is re-focused so terminal input keeps working after copy (same concern as [Bug]: Terminal loses focus after context menu Copy/Paste #592).
  • Security: No new data paths or IPC surface. The existing clipboard text size assertion still runs before any write. Failure mode changes from silent fake-success to an explicit rejection, which existing callers already catch; OSC 52 writes without a user gesture still cannot copy (browser restriction) and now fail honestly.
  • Tests: 8 new unit tests cover the fallback engaging, cleanup on throw/failure, focus restore, explicit rejection when no path exists, and the permission-gated retry. Focused clipboard suites (151 tests), pnpm lint, and pnpm typecheck pass.

Residual risk: browser clipboard behavior is vendor-dependent; paired-client manual validation over plain HTTP is required in addition to unit tests (matches the residual-risk note in docs/remote-web-paste-activity-fixes.md).


X: @badaverse_dev

🤖 Generated with Claude Code

ELI5

Copy from terminals in the HTTP web client silently failed because the secure clipboard API isn't available on plain HTTP. Copy now falls back so selection and shortcuts work on LAN web clients.

@coderabbitai

coderabbitai Bot commented Jul 25, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 03767d35-ac99-4fa8-bd01-56a9c3a9f15b

📥 Commits

Reviewing files that changed from the base of the PR and between ef8806226a02d1cc8b512a51824f26059b5c292e and c8f6561bc7e0d545e4d1fcb9baee1a9574c893f2.

📒 Files selected for processing (2)
  • src/renderer/src/web/web-clipboard-copy-fallback.test.ts
  • src/renderer/src/web/web-clipboard-copy-fallback.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/renderer/src/web/web-clipboard-copy-fallback.ts

📝 Walkthrough

Walkthrough

Adds a textarea-based execCommand('copy') fallback with availability checks, exception handling, cleanup, focus and selection restoration. Updates web clipboard writes to retry through this fallback when the primary API is unavailable or rejects, and to throw when no mechanism succeeds. Adds tests covering fallback success, failure, permission rejection, cleanup, focus, and selection behavior.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: clipboard copy support for HTTP web clients.
Description check ✅ Passed The description includes the required summary, screenshots, testing, review, security, and notes content, even if its headings differ from the template.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 53fbc930-e3af-4069-b7db-2b7633f94c36

📥 Commits

Reviewing files that changed from the base of the PR and between d8e0f11 and ef8806226a02d1cc8b512a51824f26059b5c292e.

📒 Files selected for processing (4)
  • src/renderer/src/web/web-clipboard-copy-fallback.test.ts
  • src/renderer/src/web/web-clipboard-copy-fallback.ts
  • src/renderer/src/web/web-preload-api.test.ts
  • src/renderer/src/web/web-preload-api.ts

@nwparker nwparker added the bug Something isn't working label Jul 27, 2026
badaverse and others added 4 commits July 27, 2026 18:02
navigator.clipboard only exists in secure contexts, so the web client
served over plain HTTP (e.g. a LAN address) silently dropped every copy:
writeClipboardText resolved successfully without writing anything. This
broke Ctrl/Cmd+Shift+C, context-menu Copy, right-click copy,
copy-on-select, OSC 52, and Copy Pane/Terminal ID on the client PC.

Fall back to document.execCommand('copy') on a temporary textarea — the
only clipboard write available in insecure contexts — mirroring the
paste-side ClipboardEvent fallback (stablyai#9900). Also retry via execCommand
when a secure-context clipboard write is permission-gated, and reject
instead of silently succeeding when no write path exists.

Co-Authored-By: Claude Fable 5 <[email protected]>
textarea.select() clobbers the document selection (chat/diff text in the
web client UI; xterm's canvas-internal selection is unaffected). Clone the
selection ranges before copying and restore them in finally.

Co-Authored-By: Claude Fable 5 <[email protected]>
… hidden textarea

Review fixes on top of the PR's insecure-context copy fallback.

The hidden-textarea approach worked but had three measurable defects, all
reproduced over a real plain-HTTP LAN origin (isSecureContext=false,
navigator.clipboard=undefined) in Chromium 147, Firefox 148, and WebKit 26.4:

1. It clobbered the page's DOM selection whenever focus was in an editable
   element. The restore ran before previousFocus.focus(), and refocusing an
   input collapses the document selection, undoing the restore. Reproduced in
   all three engines with focus in a textarea and in a contenteditable.
2. It was O(text). A 16 MB copy (CLIPBOARD_TEXT_WRITE_MAX_BYTES) blocked the
   renderer for 1423ms in Chromium, 2111ms in Firefox, 3511ms in WebKit. The
   copy event path does the same write in 13-64ms.
3. It appended a textarea holding the copied text to document.body, where a
   MutationObserver installed by any page script observed the value.

Serving the text from a capture-phase copy ClipboardEvent needs no DOM node,
no selection change, and no focus change. The textarea path is kept as a
fallback because WebKit declines execCommand('copy') when nothing is selected,
and its cleanup order is corrected.

Also guards the copy surfaces against the API's new rejection. writeClipboardText
used to resolve unconditionally, so several call sites never attached a handler;
an insecure-context copy with no live user gesture (transient activation is ~5s
on Chromium/Firefox, ~1s on WebKit) now produced 5 unhandled rejections per the
repro, recorded as renderer crash breadcrumbs. Copy Pane ID additionally showed
a success toast for a copy that never happened.
@OrcaWin
OrcaWin force-pushed the fix/web-client-copy-insecure-context branch from c8f6561 to fa8e1d2 Compare July 28, 2026 01:24
@OrcaWin

OrcaWin commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator

Reviewed with an independent reproduction over a genuine plain-HTTP LAN origin (not localhost, which counts as a secure context and would have masked the bug). Confirmed: every copy surface resolved successfully while writing nothing, and your tests are load-bearing — 3 fail on revert.

One implementation change, pushed to this branch. The hidden-textarea approach carried three defects I measured in Chromium, Firefox and WebKit:

  • The copied text was observable in the page DOM by any script via MutationObserver, for as long as the element was attached.
  • It blocked the renderer 1.4–3.5s at the 16MB write ceiling, on the copy-on-select path — which fires on every selection.
  • The selection-restore added in your second commit was undone by the refocus.

The replacement serves the copy from a capture-phase copy event instead, so clipboard text never enters the DOM and a failed event copy rejects rather than silently falling back to the unsafe path. That also lines it up with the shape #9900 used for paste.

One claim didn't survive: the PR says "existing callers already swallow that." I measured 5 unhandled rejections per run across all three browsers from the real call shapes — onCopy, onCopyPaneId, and right-click-to-copy had no handler. Copy Pane ID also toasted success for a copy that never happened. Those now have rejection handlers; Vitest reports 0 unhandled rejections.

All five surfaces you named still work: shortcut, context-menu copy, copy-on-select, OSC 52, Copy Pane ID.

Validation: 152 web tests, 77 terminal copy tests, typecheck 0, oxlint clean.

@OrcaWin
OrcaWin merged commit 872a9c3 into stablyai:main Jul 28, 2026
25 checks passed
@AmethystLiang

Copy link
Copy Markdown
Contributor

looking into it

enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
Reviewed with an independent reproduction over a real plain-HTTP origin. Replaced the hidden-textarea fallback with a capture-phase copy event so clipboard text never enters the DOM, and added rejection handlers to the surfaces that had none.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Terminal copy silently fails in the paired web client over plain HTTP (no navigator.clipboard in insecure contexts)

4 participants