From a4f42ad42712a345542e5a730052a81fef6c8b60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kevin=20Espi=C3=B1eira?= Date: Fri, 24 Jul 2026 09:26:30 +0200 Subject: [PATCH] fix(terminal): make Ctrl+V / Cmd+V paste work in the HTTP web client (#9900) * fix(terminal): make Ctrl+V paste work in the HTTP web client navigator.clipboard only exists in secure contexts, so the web client served over plain HTTP (e.g. a LAN address) read an empty clipboard and Ctrl+V silently did nothing: the keydown handler preventDefault-ed the chord and suppressed the native paste event whose clipboardData is the only clipboard access available there. When the async clipboard reader is unavailable in the web client, let the chord's native paste event fire and feed its clipboardData text through the existing paste pipeline (size limits, bracketed-paste handling and error toasts included). Co-Authored-By: Claude Fable 5 Claude-Session: https://claude.ai/code/session_01RTjQWLg4wy1CgmcZKmGAuN * fix: address review finding (bug-bash takeover) Co-authored-by: Orca --------- Co-authored-by: Claude Co-authored-by: Neil <4138956+nwparker@users.noreply.github.com> Co-authored-by: Orca --- .../components/terminal-pane/TerminalPane.tsx | 29 ++++++- .../terminal-clipboard-event-paste.test.ts | 84 +++++++++++++++++++ .../terminal-clipboard-event-paste.ts | 46 ++++++++++ 3 files changed, 157 insertions(+), 2 deletions(-) create mode 100644 src/renderer/src/components/terminal-pane/terminal-clipboard-event-paste.test.ts create mode 100644 src/renderer/src/components/terminal-pane/terminal-clipboard-event-paste.ts diff --git a/src/renderer/src/components/terminal-pane/TerminalPane.tsx b/src/renderer/src/components/terminal-pane/TerminalPane.tsx index 95038053f..50ea86eac 100644 --- a/src/renderer/src/components/terminal-pane/TerminalPane.tsx +++ b/src/renderer/src/components/terminal-pane/TerminalPane.tsx @@ -158,6 +158,15 @@ import { } from '@/components/terminal-quick-commands/TerminalQuickCommandDialog' import { keybindingMatchesAction } from '../../../../shared/keybindings' import { pasteTerminalClipboard } from './terminal-clipboard-paste' +import { + firesNativePasteEvent, + getClipboardEventText, + isClipboardEventPasteRequired +} from './terminal-clipboard-event-paste' +import { + assertClipboardTextWithinLimitWithYield, + type ReadClipboardTextOptions +} from '../../../../shared/clipboard-text' import { scheduleImagePasteWebglAtlasRecovery } from './terminal-webgl-atlas-recovery' import { restoreTerminalFitToDesktop, restoreTerminalFitsToDesktop } from './terminal-fit-restore' import { useVisibleTerminalTabClaim } from './use-visible-terminal-tab-claim' @@ -2006,7 +2015,9 @@ export default function TerminalPane({ const pasteFromClipboard = ( pane: ManagedPane, - source: Extract + source: Extract, + readClipboardText: (options?: ReadClipboardTextOptions) => Promise = window.api.ui + .readClipboardText ): void => { const connectionId = getConnectionId(worktreeId) ?? null const runtimeEnvironmentId = getRuntimeEnvironmentIdForWorktree( @@ -2015,7 +2026,7 @@ export default function TerminalPane({ ) const activeElementAtDispatch = document.activeElement void pasteTerminalClipboard({ - readClipboardText: window.api.ui.readClipboardText, + readClipboardText, saveClipboardImageAsTempFile: window.api.ui.saveClipboardImageAsTempFile, connectionId, runtimeEnvironmentId, @@ -2069,6 +2080,13 @@ export default function TerminalPane({ } return } + if (isClipboardEventPasteRequired() && firesNativePasteEvent(e, isMac)) { + // Why: without navigator.clipboard the chord's native paste event is the + // only clipboard access — let its default fire and handle it in onPaste. + // A remapped chord (e.g. Ctrl+Y) fires no paste event, so keep consuming it + // below instead of letting xterm encode it to the PTY as a raw control char. + return + } e.preventDefault() e.stopPropagation() const manager = managerRef.current @@ -2119,6 +2137,13 @@ export default function TerminalPane({ if (!pane) { return } + if (isClipboardEventPasteRequired()) { + const eventText = getClipboardEventText(e) + pasteFromClipboard(pane, 'paste-event', (options) => + assertClipboardTextWithinLimitWithYield(eventText, options) + ) + return + } pasteFromClipboard(pane, 'paste-event') } diff --git a/src/renderer/src/components/terminal-pane/terminal-clipboard-event-paste.test.ts b/src/renderer/src/components/terminal-pane/terminal-clipboard-event-paste.test.ts new file mode 100644 index 000000000..2a9c252c9 --- /dev/null +++ b/src/renderer/src/components/terminal-pane/terminal-clipboard-event-paste.test.ts @@ -0,0 +1,84 @@ +import { describe, expect, it } from 'vitest' +import { + firesNativePasteEvent, + getClipboardEventText, + shouldUseClipboardEventPaste +} from './terminal-clipboard-event-paste' + +function makeKeyEvent( + overrides: Partial> +): Pick { + return { key: '', metaKey: false, ctrlKey: false, altKey: false, shiftKey: false, ...overrides } +} + +function makeClipboardEvent(text: string | null): ClipboardEvent { + return { + clipboardData: + text === null + ? null + : { + getData: (type: string) => (type === 'text/plain' ? text : '') + } + } as unknown as ClipboardEvent +} + +describe('shouldUseClipboardEventPaste', () => { + it('requires the fallback for web clients without navigator.clipboard.readText', () => { + expect( + shouldUseClipboardEventPaste({ isWebClient: true, clipboardReadTextAvailable: false }) + ).toBe(true) + }) + + it('keeps async clipboard reads for secure-context web clients', () => { + expect( + shouldUseClipboardEventPaste({ isWebClient: true, clipboardReadTextAvailable: true }) + ).toBe(false) + }) + + it('never applies to the Electron renderer, which reads the clipboard over IPC', () => { + expect( + shouldUseClipboardEventPaste({ isWebClient: false, clipboardReadTextAvailable: false }) + ).toBe(false) + expect( + shouldUseClipboardEventPaste({ isWebClient: false, clipboardReadTextAvailable: true }) + ).toBe(false) + }) +}) + +describe('getClipboardEventText', () => { + it('reads text/plain from the event clipboardData', () => { + expect(getClipboardEventText(makeClipboardEvent('echo hi'))).toBe('echo hi') + }) + + it('returns empty text when clipboardData is missing', () => { + expect(getClipboardEventText(makeClipboardEvent(null))).toBe('') + }) +}) + +describe('firesNativePasteEvent', () => { + it('recognizes the default native paste chords on macOS', () => { + expect(firesNativePasteEvent(makeKeyEvent({ key: 'v', metaKey: true }), true)).toBe(true) + }) + + it('recognizes the default native paste chords on Windows/Linux', () => { + expect(firesNativePasteEvent(makeKeyEvent({ key: 'v', ctrlKey: true }), false)).toBe(true) + // Ctrl+Shift+V is the default terminal paste and still dispatches a native event. + expect( + firesNativePasteEvent(makeKeyEvent({ key: 'v', ctrlKey: true, shiftKey: true }), false) + ).toBe(true) + expect( + firesNativePasteEvent(makeKeyEvent({ key: 'Insert', shiftKey: true }), false) + ).toBe(true) + }) + + it('does not treat a remapped non-clipboard chord as a native paste event', () => { + // A custom terminal.paste binding like Ctrl+Y fires no native paste event and + // must stay consumed so it is not encoded to the PTY as a control char. + expect(firesNativePasteEvent(makeKeyEvent({ key: 'y', ctrlKey: true }), false)).toBe(false) + expect(firesNativePasteEvent(makeKeyEvent({ key: 'd', ctrlKey: true }), false)).toBe(false) + // Cmd+V does not fire a native paste event on Windows/Linux. + expect(firesNativePasteEvent(makeKeyEvent({ key: 'v', metaKey: true }), false)).toBe(false) + // Ctrl+V does not fire a native paste event on macOS (readline quote-insert). + expect(firesNativePasteEvent(makeKeyEvent({ key: 'v', ctrlKey: true }), true)).toBe(false) + }) +}) diff --git a/src/renderer/src/components/terminal-pane/terminal-clipboard-event-paste.ts b/src/renderer/src/components/terminal-pane/terminal-clipboard-event-paste.ts new file mode 100644 index 000000000..15bf439fd --- /dev/null +++ b/src/renderer/src/components/terminal-pane/terminal-clipboard-event-paste.ts @@ -0,0 +1,46 @@ +import { isWebClientLocation } from '@/lib/web-client-location' + +// Why: navigator.clipboard only exists in secure contexts. The web client served +// over plain HTTP (e.g. a LAN address) can reach the clipboard only through the +// chord's native ClipboardEvent, so Ctrl/Cmd+V must not be preventDefault-ed there. +export function shouldUseClipboardEventPaste(args: { + isWebClient: boolean + clipboardReadTextAvailable: boolean +}): boolean { + return args.isWebClient && !args.clipboardReadTextAvailable +} + +export function isClipboardEventPasteRequired(): boolean { + return shouldUseClipboardEventPaste({ + isWebClient: isWebClientLocation(), + clipboardReadTextAvailable: typeof navigator.clipboard?.readText === 'function' + }) +} + +export function getClipboardEventText(event: ClipboardEvent): string { + return event.clipboardData?.getData('text/plain') ?? '' +} + +type PasteChordEvent = Pick + +// Why: only these chords make the browser dispatch a native `paste` ClipboardEvent, +// the sole clipboard path on insecure web. A remapped terminal.paste chord (e.g. Ctrl+Y) +// fires no paste event, so it must still be consumed rather than encoded to the PTY as +// raw control chars. +export function firesNativePasteEvent(event: PasteChordEvent, isMac: boolean): boolean { + const key = event.key.toLowerCase() + if (isMac) { + return key === 'v' && event.metaKey && !event.ctrlKey && !event.altKey && !event.shiftKey + } + // Ctrl+V and Ctrl+Shift+V both dispatch a native paste event on Windows/Linux. + if (key === 'v' && event.ctrlKey && !event.metaKey && !event.altKey) { + return true + } + return ( + event.key === 'Insert' && + event.shiftKey && + !event.ctrlKey && + !event.metaKey && + !event.altKey + ) +}