Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat(site): add OSC 52 clipboard support to web terminal - #26437

Merged
aqandrew merged 2 commits into
mainfrom
aqandrew/osc52-clipboard-support
Jul 6, 2026
Merged

feat(site): add OSC 52 clipboard support to web terminal#26437
aqandrew merged 2 commits into
mainfrom
aqandrew/osc52-clipboard-support

Conversation

@aqandrew

@aqandrew aqandrew commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

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 #16577

Generated by Coder Agents on behalf of @aqandrew.

relevant context from agent chat, summarized by me

ref DEVEX-465

Installing @xterm/addon-clipboard would have been another way to implement this feature. This would follow established addon-loading patterns in WorkspaceTerminal:

const fitAddon = new FitAddon();
fitAddonRef.current = fitAddon;
nextTerminal.loadAddon(fitAddon);
nextTerminal.loadAddon(new Unicode11Addon());
nextTerminal.unicode.activeVersion = "11";
nextTerminal.loadAddon(
new WebLinksAddon((_, uri) => {
handleOpenLink(uri);
}),
);

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 🙏🏽

Register an OSC 52 handler on the xterm.js parser so that programs like
tmux can copy text to the browser's system clipboard via escape sequences.
The handler decodes the base64 payload and writes it using the existing
copyToClipboard utility, which provides an HTTP fallback for insecure
contexts.

Closes #16577

@aqandrew aqandrew left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Clean, well-scoped feature. The OSC 52 parsing is correct, the UTF-8 decode path via Uint8Array.from(atob(...)) + TextDecoder handles multi-byte characters properly, and the test exercises the real xterm parser end-to-end. Good reuse of the existing copyToClipboard utility.

2 P3s and 1 nit across 3 inline comments.

Comment thread site/src/modules/terminal/WorkspaceTerminal.tsx
Comment thread site/src/pages/TerminalPage/TerminalPage.test.tsx Outdated
Comment thread site/src/pages/TerminalPage/TerminalPage.test.tsx Outdated
Fix comment accuracy: 'let the default handler run' to 'ignore' since
xterm.js has no built-in OSC 52 handler. Restore navigator.clipboard
after test using save/restore pattern in a scoped describe block. Add
edge-case tests for missing separator, clipboard read query, and
invalid base64.
@aqandrew
aqandrew marked this pull request as ready for review June 25, 2026 00:47
@linear-code

linear-code Bot commented Jun 25, 2026

Copy link
Copy Markdown

DEVEX-465

@coderagents

coderagents Bot commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Documentation Check

Updates Needed

  • docs/user-guides/workspace-access/web-terminal.md - The "Key Features" list and "Copy/Paste Behavior" subsection should mention OSC 52 clipboard support. Programs like tmux can now copy text to the browser clipboard via escape sequences, which is a meaningful capability for terminal users. A short bullet or paragraph (e.g., "OSC 52 clipboard: Programs like tmux can copy text to your browser clipboard using the OSC 52 escape sequence. Clipboard read queries are ignored for security.") would be appropriate.

Automated review via Coder Agents

@ThomasK33 ThomasK33 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it looks good, but I don’t have enough context or knowledge to assess the change confidently.

If you’re confident about it and just need a quick stamp of approval, let me know.

@github-actions github-actions Bot added the stale This issue is like stale bread. label Jul 3, 2026
@aqandrew

aqandrew commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

I wasn't 100% comfortable merging this until somebody could verify it working manually, but we're good now! 🙂 I just successfully did this with @jeremyruppel:

repro steps

  1. in your Coder deployment, open the web terminal by navigating to the URL /:username/:workspace/terminal
  2. make sure the tmux command is installed in your workspace
    • it's included by default in the Write Coder on Coder template
  3. create a ~/.tmux.conf configuration file if it doesn't already exist
  4. give tmux permissions to write to the clipboard, by adding this line to ~/.tmux.conf:
    set -g set-clipboard on
    
  5. reload your tmux configuration (run this outside of tmux):
    tmux source-file ~/.tmux.conf
    
  6. launch tmux
  7. run this command (from OSC52 Support for coder web terminals #16577) inside tmux:
    printf "\033]52;c;$(echo -n 'Coder is Cool' | base64)\a"
    

Coder is Cool gets copied to your clipboard

@aqandrew
aqandrew merged commit 95fde35 into main Jul 6, 2026
47 checks passed
@aqandrew
aqandrew deleted the aqandrew/osc52-clipboard-support branch July 6, 2026 19:27
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 6, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

stale This issue is like stale bread.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OSC52 Support for coder web terminals

3 participants