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
docs/shell-integration.md Outdated Show resolved Hide resolved
@Kreijstal
Copy link

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

how come this works on mintty tho?

@39555
Copy link
Author

39555 commented Dec 5, 2024

@Kreijstal Just discovered your issue at mintty/mintty#1264 Thanks! Does mintty use conpty? Because neither Allacritty nor Rio can paste from the clipboard on Windows. We need to dig inside the https://github.com/microsoft/terminal/tree/aa5459df4a33c731b25be9f47fe481d8c73db14d/src/winconpty

@Kreijstal
Copy link

Kreijstal commented Dec 5, 2024

@Kreijstal Just discovered your issue at mintty/mintty#1264 Thanks! Does mintty use conpty? Because neither Allacritty nor Rio can paste from the clipboard on Windows. We need to dig inside the https://github.com/microsoft/terminal/tree/aa5459df4a33c731b25be9f47fe481d8c73db14d/src/winconpty

I guess windows terminal vendors their own conpty? crazy

@39555
Copy link
Author

39555 commented Dec 5, 2024

I researched this a little bit. winconpty starts the OpenConsole.exe process. OpenConsole.exe is built from the src/host folder https://github.com/microsoft/terminal/blob/aa5459df4a33c731b25be9f47fe481d8c73db14d/doc/ORGANIZATION.md?plain=1#L39C1-L39C58 than some magic happens inside that scary state machine https://github.com/microsoft/terminal/blob/aa5459df4a33c731b25be9f47fe481d8c73db14d/src/terminal/parser/stateMachine.cpp and seems like the sequence gets lost here but im not sure https://github.com/microsoft/terminal/blob/0961a77a5aec003e9f79d388f8e7bffb9a550cdb/src/terminal/parser/OutputStateMachineEngine.cpp#L803

@adoal
Copy link

adoal commented Dec 6, 2024

Maybe there can be a third option ask for reading clipboard besides true and false, just like kitty's clipboard_control.

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)
8 participants