Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement opt-in OSC52 clipboard querying #6239

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

39555
Copy link

@39555 39555 commented Oct 6, 2024

Closes: #2050

Hi Wez! This is my first time contribution to the project. I've implemented the missing osc52 clipboard reading command. The feature is disabled by default and can be enabled via the config option config.enable_osc52_clipboard_reading = true . I've also added the documentation with security notes and some tests related to osc52.

The clipboard content is received asynchronously through a channel, which is passed as the second parameter in the Clipboard::get_contents function. For the TerminalState it is the ThreadedWriter directly, for the mux client it is a tx-rx channel that than sends the pdu responce with the clipboard data back to the server and than to the server's ThreadedWriter.

The waiting of the clipboard data on the mux server side is a bit clunky wezterm-mux-server-impl/src/dispatch.rs
because there is no promise handling mechanism similar to what the client has so I left a mutable promise variable before the message loop and handled the response with if Pdu::QueryClipboardResponce(..) = before passing a pdu to the actual server's message handler. I would like to hear any idea how to improve it.

I am open to any suggestions for naming, fixes and improvements.

@loops
Copy link
Contributor

loops commented Oct 6, 2024

Hi 39555,

This works nicely! Especially nice that you made it work with the mux-server as well. (It might be worth
noting in the docs that the config option also needs to be enabled on the remote machine, for the mux-server).

Great work.

@39555
Copy link
Author

39555 commented Oct 7, 2024

"@loops, what do you mean by the config on the remote server? I thought WezTerm only reads the config from the local machine. Could you write a small guide?

@39555
Copy link
Author

39555 commented Oct 7, 2024

I’m experiencing a strange bug on windows both in the GUI and when using wezterm connect unix or connecting via SSH.... The OSC sequence isn’t being parsed at all. However, everything works perfectly on WSL (via default_domain), macOS, and when using SSH from WSL to macOS or from Windows to macOS via wezterm connect <ssh_domain> or wezterm ssh <addr>."

@39555
Copy link
Author

39555 commented Oct 9, 2024

I've spend some time digging in. Unfortunately. This feature can't work on windows due to ConPTY. They implicitly filter this osc without any user notification.... Both Alacritty and Rio terminals implement this feature and it doesn't work on windows either.

alacritty/alacritty#7962

- add new config option `enable_osc52_clipboard_reading = false`
- add a new trait function:
`Clipboard::get_contents(&self, selection: ClipboardSelection, writer: Box<dyn ClipboardReader>)`

The clipboard content is read asynchronously by providing a sender
channel `Box<dyn ClipboardReader>`

The sender `ClipboardReader` is a trait that requires the channel to be
the `Send` + `Sync` + `Debug` (for the `MuxNotification` enum) along
with the special hackery trait `ClipboardReaderBoxClone` that allows `Box<dyn
ClipboardReader>)` to be `Clone`
@Jendker
Copy link

Jendker commented Oct 9, 2024

Thank you for this! This feature was essential and previously missing in wezterm.

While using your code, I noticed something that might be useful. When I use a terminal without OSC52 query support (like wezterm without your PR) and attempt to paste with OSC 52 in neovim, I see the message:

Waiting for OSC 52 response from the terminal. Press Ctrl-C to interrupt...

This informs the user that OSC 52 paste is unavailable.

However, with your PR in wezterm, if I don’t set config.enable_osc52_clipboard_reading = true, the query fails silently and retrieves the last copied item in neovim. I'm not sure if this is expected behavior, but it could be confusing for users who might overlook the opt-in setting.

EDIT: I cannot reproduce it anymore. Please disregard the comment. I really appreciate your work on this feature — it has vastly improved my experience with wezterm!

@melMass
Copy link

melMass commented Nov 4, 2024

Thanks a lot! Is there a blocker for getting a review/merge?

@39555
Copy link
Author

39555 commented Nov 4, 2024

Thanks a lot! Is there a blocker for getting a review/merge?

Wez currently has no bandwidth to work on Wezterm. Maybe someone else could review it and suggest improvements before Wez is ready

sequences to monitor whatever you have on the clipboard, which may occasionally
contain sensitive data.

Setting clipboard data that contains escape sequences or malicious commands and
Copy link

Choose a reason for hiding this comment

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

One question, since I don't understand. Is this still valid even using ssh (encrypted tunnel)? How can an attacker inject harmful characters without already knowing how to access to the encrypted tunnel?

Copy link
Author

Choose a reason for hiding this comment

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

They probably can't, but OSC 52 is a way to access the local clipboard from a remote location. If it's not your server — for example a telnet multiplayer game or netcat .. — those servers could easily access your personal data. I doubt they could execute anything, though, as the data is base64-encoded.

docs/shell-integration.md Outdated Show resolved Hide resolved
Co-authored-by: Len Trigg <lenbok@gmail.com>
docs/shell-integration.md Outdated Show resolved Hide resolved
Co-authored-by: Len Trigg <lenbok@gmail.com>
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.

Support OSC 52 clipboard querying (opt-in)
6 participants