mirror of
https://github.com/coder/coder.git
synced 2026-11-04 06:44:46 +08:00
Registers an OSC 52 handler on the xterm.js terminal parser so that programs like tmux can copy text to the browser's system clipboard via escape sequences (e.g. `printf "\\033]52;c;$(echo -n 'Coder is Cool' | base64)\\a"`). The handler decodes the base64 payload and writes it using the existing `copyToClipboard` utility, which provides an HTTP fallback for insecure contexts. Clipboard read queries (`?`) are ignored since responding would require writing back to the PTY. No new dependencies are needed; xterm.js 5.5.0 already exposes `terminal.parser.registerOscHandler`. Closes https://github.com/coder/coder/issues/16577 Generated by Coder Agents on behalf of @aqandrew. ## relevant context from agent chat, summarized by me ref DEVEX-465 Installing [@xterm/addon-clipboard](https://npmx.dev/package/@xterm/addon-clipboard) would have been another way to implement this feature. This would follow established addon-loading patterns in `WorkspaceTerminal`: https://github.com/coder/coder/blob/612b6d4e95ac4eb1c4227dfd5979a04c2de6e600/site/src/modules/terminal/WorkspaceTerminal.tsx#L232-L241 However, registering a custom handler instead has a few advantages re: security and flexibility: - security - **ignoring OSC 52 with clipboard read queries** (`payload === "?"`) prevents processes in the workspace from reading the user's clipboard without consent - flexibility - **ignoring OSC 52 with invalid base64** -- If the addon catches an error while decoding base64, an empty string will be copied to the clipboard, i.e., the clipboard contents get cleared, which would be surprising/frustrating to users. The custom handler doesn't write anything to the clipboard in this case. - **ignoring OSC 52 with missing separator** -- The addon returns true in this case (marks the OSC 52 as handled); the custom handler returns false (doesn't mark the OSC 52 as handled) The approach/tests seem sound to me. I would just ask that someone among reviewers manually test this with tmux, since I'm not a tmux user 🙏🏽