Skip to content

Support xterm VT sequence (OSC 52) for setting the clipboard #2946

@fpqc

Description

@fpqc

Mintty and xterm support options to allow setting the clipboard by VT sequences, a feature used by tmux to sync its internal clipboard with the system clipboard. I also wrote the patch for st to add support for setting Xorg's PRIMARY clipboard from VT as well as supporting OSC 52 clipboard setting in tmux if it is emitted from something like vim or a remote nested tmux instance (see patch).

The current code in st is under

strhandle(void)
{
[...]
case 52

in st.c, where it is implemented in a few lines (and the low-level logic is clearer than in my tmux snippet). Notice that the first argument is ignored (since it is only used to read back the system clipboard, which is insecure for reasons spelled out below).

Implementation:
The documentation for this control sequence in xterm is available on invisible-island (see OSC Ps for Ps = 52). The documentation isn't amazing, but essentially the data is emitted as base64 and is decoded back to UTF-8 by base64dec. I don't know how you would implement this into the Windows clipboard, which iirc still uses USC2/UTF-16, but base64dec does give you back a UTF-8 string, and reencoding that to a string compatible with the Windows clipboard is probably something that I think is just handled nowadays by a standard Windows API.

Potential security implications:
I would leave this setting as optional and on a profile-by-profile basis (gated behind an option 'allowClipboardSharing', in case a nasty script emits uses this to try to write data to the clipboard (which at least in the past had been the source of an exploit in an older version of Windows). Note: I would not add support for the read portion of the control sequence (my tmux patch for example simply ignores this case), since this would allow unauthorized reading of the system clipboard.

Example Use Case:
https://sunaku.github.io/tmux-yank-osc52.html

Note that X11 forwarding is not really a desirable option just for clipboard sharing in Windows, given that the Windows clipboard is not an X clipboard, and X11 forwarding theoretically has the same security implications (in fact worse implications).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-VTVirtual Terminal sequence supportHelp WantedWe encourage anyone to jump in on these.Issue-TaskIt's a feature request, but it doesn't really need a major design.Product-TerminalThe new Windows Terminal.Resolution-Fix-CommittedFix is checked in, but it might be 3-4 weeks until a release.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions