Skip to content

feat(terminal): opt-in OSC 52 clipboard writes from TUIs - #952

Merged
nwparker merged 1 commit into
mainfrom
nwparker/osc52-clipboard
Apr 22, 2026
Merged

nwparker merged 1 commit into
mainfrom
nwparker/osc52-clipboard

Conversation

@nwparker

Copy link
Copy Markdown
Contributor

Summary

xterm.js doesn't ship an OSC 52 handler, so tmux, Neovim, fzf, and ripgrep running inside Orca — locally or over SSH — silently fail when they try to copy to the system clipboard. This is one of the most-requested workflows for users who SSH into remote hosts and expect "yank in vim, paste in my browser" to just work.

Add an opt-in handler gated behind a new setting terminalAllowOsc52Clipboard (default off because OSC 52 is a classic data-exfil / clipboard-overwrite vector — piping an attacker-controlled log into the terminal could silently replace what the user has copied).

Design

Mirrors the DEC 2031 wiring from #896:

  • Parsing is a pure helper (osc52-clipboard.ts :: parseOsc52) with a full unit-test suite — no xterm dependency, no IPC, so it's trivially testable.
  • The xterm handler is registered per-pane in onPaneCreated, disposed in onPaneClosed alongside the existing mode-2031 and selection disposables.
  • The setting is read from settingsRef.current at fire time so toggling it takes effect without recreating panes.
  • Clipboard queries (OSC 52 ; c ; ?) are intentionally dropped — answering them would leak clipboard contents to any process writing to the PTY. Same for malformed payloads, unknown selection letters, and payloads over 128 KB.

Files

  • src/shared/types.ts, src/shared/constants.ts — add setting.
  • src/renderer/src/components/terminal-pane/osc52-clipboard.ts — pure parser.
  • src/renderer/src/components/terminal-pane/osc52-clipboard.test.ts — 11 tests covering the happy path, multi-byte UTF-8, combined selectors, queries, whitespace tolerance, and all five invalid-payload cases.
  • src/renderer/src/components/terminal-pane/use-terminal-pane-lifecycle.ts — register + dispose.
  • src/renderer/src/components/settings/TerminalPane.tsx — settings UI toggle with keywords for search.
  • Two test fixtures updated with the new field so the full GlobalSettings objects still typecheck.

Testing

  • pnpm lint
  • pnpm typecheck
  • New: osc52-clipboard.test.ts — 11/11 passing
  • All 113 terminal-pane tests pass

xterm.js ships with no OSC 52 handler, so tmux, Neovim, fzf, and ripgrep
running inside Orca — locally or over SSH — silently fail to copy to the
system clipboard. Add an opt-in handler gated behind a new setting
`terminalAllowOsc52Clipboard` (default off because untrusted output
piped into the terminal can silently overwrite the clipboard).

Implementation mirrors the DEC 2031 wiring from PR #896: extract parsing
into a pure helper (parseOsc52) with a full unit-test suite, register
the handler in onPaneCreated and dispose it symmetrically in
onPaneClosed, and read the gate from settingsRef at fire time so the
toggle takes effect without recreating panes.

Clipboard *queries* (Pd = "?") are intentionally dropped — answering
them would leak the user's clipboard to any process writing to the
PTY — along with malformed payloads, oversized payloads (>128KB),
and payloads using unknown selection letters.
@nwparker
nwparker merged commit 2819cdc into main Apr 22, 2026
2 checks passed
nwparker added a commit that referenced this pull request Apr 22, 2026
…956)

Wire the two escape sequences that TUIs and modern shells use to tell
the terminal about state that xterm.js doesn't handle natively:

- OSC 7  — shell emits its current working directory on every `cd`.
- OSC 133 — FinalTerm / iTerm semantic prompt marks (A/B/C/D with
  optional exit code on D).

Parsing lives in a new pure `shell-integration.ts` helper with 21 unit
tests covering URI decoding, Windows UNC / drive-letter paths, all four
OSC 133 letters, the `key=value` hint suffix some emitters append, and
the ring-buffer cap on recorded marks.

Wiring follows the DEC 2031 / OSC 52 pattern from #896 and #952:

- Handlers registered per-pane in onPaneCreated, disposed symmetrically
  in onPaneClosed.
- Two new refs (`paneCwdRef`, `paneSemanticMarksRef`) live on
  TerminalPane and are passed through UseTerminalPaneLifecycleDeps.
- Semantic marks record the absolute buffer row (`baseY + cursorY`) so
  future block-navigation consumers can scroll to them.
- A 10 000-entry ring buffer caps per-pane memory.

Shipped consumer: the terminal context menu grows a platform-aware
"Reveal in Finder / Show in Explorer / Open Folder" item that opens
the shell's current directory in the native file manager. The item
is hidden when no OSC 7 has been received, consistent with how other
terminal emulators surface shell-integration-dependent features.

No consumer yet for OSC 133 — recording is cheap and the consumer UX
(block-nav keybinds, exit-code gutters) deserves its own design pass.
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
xterm.js ships with no OSC 52 handler, so tmux, Neovim, fzf, and ripgrep
running inside Orca — locally or over SSH — silently fail to copy to the
system clipboard. Add an opt-in handler gated behind a new setting
`terminalAllowOsc52Clipboard` (default off because untrusted output
piped into the terminal can silently overwrite the clipboard).

Implementation mirrors the DEC 2031 wiring from PR stablyai#896: extract parsing
into a pure helper (parseOsc52) with a full unit-test suite, register
the handler in onPaneCreated and dispose it symmetrically in
onPaneClosed, and read the gate from settingsRef at fire time so the
toggle takes effect without recreating panes.

Clipboard *queries* (Pd = "?") are intentionally dropped — answering
them would leak the user's clipboard to any process writing to the
PTY — along with malformed payloads, oversized payloads (>128KB),
and payloads using unknown selection letters.
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…tablyai#956)

Wire the two escape sequences that TUIs and modern shells use to tell
the terminal about state that xterm.js doesn't handle natively:

- OSC 7  — shell emits its current working directory on every `cd`.
- OSC 133 — FinalTerm / iTerm semantic prompt marks (A/B/C/D with
  optional exit code on D).

Parsing lives in a new pure `shell-integration.ts` helper with 21 unit
tests covering URI decoding, Windows UNC / drive-letter paths, all four
OSC 133 letters, the `key=value` hint suffix some emitters append, and
the ring-buffer cap on recorded marks.

Wiring follows the DEC 2031 / OSC 52 pattern from stablyai#896 and stablyai#952:

- Handlers registered per-pane in onPaneCreated, disposed symmetrically
  in onPaneClosed.
- Two new refs (`paneCwdRef`, `paneSemanticMarksRef`) live on
  TerminalPane and are passed through UseTerminalPaneLifecycleDeps.
- Semantic marks record the absolute buffer row (`baseY + cursorY`) so
  future block-navigation consumers can scroll to them.
- A 10 000-entry ring buffer caps per-pane memory.

Shipped consumer: the terminal context menu grows a platform-aware
"Reveal in Finder / Show in Explorer / Open Folder" item that opens
the shell's current directory in the native file manager. The item
is hidden when no OSC 7 has been received, consistent with how other
terminal emulators surface shell-integration-dependent features.

No consumer yet for OSC 133 — recording is cheap and the consumer UX
(block-nav keybinds, exit-code gutters) deserves its own design pass.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant