Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(server): clipboard works over plain http (execCommand fallback), fails loudly

enes134cd1fb6db0
diff --git a/src/renderer/bridge/stubs.test.ts b/src/renderer/bridge/stubs.test.ts
index da73dd20a..91c525a38 100644
--- a/src/renderer/bridge/stubs.test.ts
+++ b/src/renderer/bridge/stubs.test.ts
@@ -1,4 +1,4 @@
-import { describe, it, expect, vi } from 'vitest'
+import { describe, it, expect, vi, afterEach } from 'vitest'
import { buildStubApi, unsupported } from './stubs'
import { E_UNSUPPORTED } from '../../shared/rpc'
@@ -60,3 +60,56 @@ describe('bridge stubs', () => {
}).not.toThrow()
})
})
+
+/** A minimal `document` for the hidden-textarea copy fallback: vitest runs in the node
+ * environment, so there is no DOM to lean on. `execCommand` is the knob each test turns. */
+function fakeDocument(execCommand: () => boolean) {
+ const textarea = {
+ value: '',
+ style: {} as Record<string, string>,
+ setAttribute: vi.fn(),
+ select: vi.fn()
+ }
+ const doc = {
+ createElement: vi.fn(() => textarea),
+ body: { appendChild: vi.fn(), removeChild: vi.fn() },
+ execCommand: vi.fn(execCommand)
+ }
+ return { doc, textarea }
+}
+
+describe('bridge clipboard', () => {
+ afterEach(() => vi.unstubAllGlobals())
+
+ it('uses the Clipboard API when it is available', () => {
+ const writeText = vi.fn().mockResolvedValue(undefined)
+ vi.stubGlobal('navigator', { clipboard: { writeText } })
+ buildStubApi().clipboard.writeText('hi')
+ expect(writeText).toHaveBeenCalledWith('hi')
+ })
+
+ it('falls back to execCommand when there is no Clipboard API (plain http)', () => {
+ const { doc, textarea } = fakeDocument(() => true)
+ vi.stubGlobal('navigator', {})
+ vi.stubGlobal('document', doc)
+ buildStubApi().clipboard.writeText('hi')
+ expect(doc.execCommand).toHaveBeenCalledWith('copy')
+ expect(textarea.value).toBe('hi')
+ // the scratch textarea must not be left behind in the DOM
+ expect(doc.body.removeChild).toHaveBeenCalledWith(textarea)
+ })
+
+ it('surfaces a toast when even execCommand cannot copy (never silent)', () => {
+ const { doc } = fakeDocument(() => false)
+ const dispatchEvent = vi.fn()
+ vi.stubGlobal('navigator', {})
+ vi.stubGlobal('document', doc)
+ vi.stubGlobal('window', { dispatchEvent })
+ buildStubApi().clipboard.writeText('hi')
+ expect(dispatchEvent).toHaveBeenCalledTimes(1)
+ const ev = dispatchEvent.mock.calls[0][0] as CustomEvent<{ kind: string; message: string }>
+ expect(ev.type).toBe('nodeterm:toast')
+ expect(ev.detail.kind).toBe('error')
+ expect(ev.detail.message).toMatch(/http/i)
+ })
+})
diff --git a/src/renderer/bridge/stubs.ts b/src/renderer/bridge/stubs.ts
index c8b2086d8..49af552f2 100644
--- a/src/renderer/bridge/stubs.ts
+++ b/src/renderer/bridge/stubs.ts
@@ -37,6 +37,38 @@ const noop = (): void => {}
/** Promise<void> member: a resolved no-op. */
const pnoop = (): Promise<void> => Promise.resolve()
+/** Copy without the Clipboard API (non-secure context): a hidden textarea + execCommand('copy').
+ * Only works inside a user gesture — which is exactly where copy is triggered from (keydown).
+ * Returns false when even that fails, so the caller can surface it rather than swallow it. */
+function copyViaExecCommand(text: string): boolean {
+ try {
+ const ta = document.createElement('textarea')
+ ta.value = text
+ ta.setAttribute('readonly', '')
+ ta.style.position = 'fixed'
+ ta.style.opacity = '0'
+ document.body.appendChild(ta)
+ ta.select()
+ const ok = document.execCommand('copy')
+ document.body.removeChild(ta)
+ if (!ok) throw new Error('execCommand returned false')
+ return true
+ } catch {
+ // Surfacing beats silence: over plain http there is no way to copy, and the user needs to know
+ // why nothing landed in their clipboard. Canvas listens for this event and shows a banner.
+ window.dispatchEvent(
+ new CustomEvent('nodeterm:toast', {
+ detail: {
+ kind: 'error',
+ message:
+ 'Copy failed — the browser blocks clipboard access over plain http. Use https or localhost.'
+ }
+ })
+ )
+ return false
+ }
+}
+
export function buildStubApi(): Omit<
NodeTerminalApi,
| 'pty'
@@ -73,11 +105,16 @@ export function buildStubApi(): Omit<
write: U('sshFs.write')
},
clipboard: {
- // The one browser-native member: use the Clipboard API when present, swallow failures.
+ // Clipboard API → execCommand → visible error. `navigator.clipboard` only exists in a SECURE
+ // context (https or localhost); over plain http on a LAN it is undefined, and the old
+ // optional-chained call copied nothing and told nobody. execCommand('copy') is deprecated but
+ // is the only thing that works there.
writeText: (text: string): void => {
- if (typeof navigator !== 'undefined') {
- void navigator.clipboard?.writeText(text).catch(() => {})
+ if (typeof navigator !== 'undefined' && navigator.clipboard) {
+ void navigator.clipboard.writeText(text).catch(() => copyViaExecCommand(text))
+ return
}
+ copyViaExecCommand(text)
}
},
shell: {
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index 99614afde..830451240 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -274,6 +274,10 @@ export function Canvas() {
// (the user must pick a side). One-shot v2→v3 migration note (dismissible strip).
const [conflict, setConflict] = useState<Project | null>(null)
const [migrationNote, setMigrationNote] = useState<string | null>(null)
+ // Copy-to-clipboard failure (browser build only): the bridge clipboard stub dispatches
+ // `nodeterm:toast` when neither the Clipboard API nor execCommand can copy — typically a
+ // non-secure context (plain http over a LAN). It must be seen, not swallowed.
+ const [copyError, setCopyError] = useState<string | null>(null)
const [zoomPct, setZoomPct] = useState(100)
const [menu, setMenu] = useState<{ x: number; y: number; items: MenuItem[] } | null>(null)
const [remotePicker, setRemotePicker] = useState<{ x: number; y: number } | null>(null)
@@ -348,6 +352,15 @@ export function Canvas() {
userDataDirRef.current = d
})
}, [])
+ // Clipboard failures reach us as a window event (the bridge stub has no React handle).
+ useEffect(() => {
+ const onToast = (e: Event): void => {
+ const detail = (e as CustomEvent<{ kind: string; message: string }>).detail
+ if (detail?.kind === 'error') setCopyError(detail.message)
+ }
+ window.addEventListener('nodeterm:toast', onToast)
+ return () => window.removeEventListener('nodeterm:toast', onToast)
+ }, [])
// Terminal node id awaiting confirmation to move into its group's worktree.
const [moveTarget, setMoveTarget] = useState<string | null>(null)
// Group awaiting confirmation to remove its worktree (drives the ask-first safety dialog).
@@ -3781,6 +3794,21 @@ export function Canvas() {
</button>
</div>
)}
+ {copyError && (
+ <div className="announce-banner announce-banner--info">
+ <span className="announce-banner__dot" />
+ <div className="announce-banner__content">
+ <span className="announce-banner__body">{copyError}</span>
+ </div>
+ <button
+ className="announce-banner__close"
+ title="Dismiss"
+ onClick={() => setCopyError(null)}
+ >
+ ✕
+ </button>
+ </div>
+ )}
{conflict && (
<ConflictBar
onReload={() => {