fish_clipboard_copy: bypass tmux, write OSC 52 to the underlying terminal

For security reasons, some terminals require explicit permission from the
user to interpret OSC 52. One of them is [tmux] but that one usually runs
inside another terminal. This means we can usually write directly to the
underlying terminal, bypassing tmux and the need for user configuration.

This only works if the underlying terminal is writable to the fish user,
which may not be the case if we switched user. For this reason, keep writing
to stdout as well, which should work fine if tmux is configured correctly.

[tmux]: https://github.com/tmux/tmux/wiki/Clipboard
This commit is contained in:
Johannes Altmanninger 2022-10-23 16:47:39 +02:00
parent 4de2891507
commit 90b2c95bbc

View file

@ -33,5 +33,16 @@ function fish_clipboard_copy
end end
set -l encoded (printf %s $cmdline | base64 | string join '') set -l encoded (printf %s $cmdline | base64 | string join '')
printf '\e]52;c;%s\a' "$encoded" printf '\e]52;c;%s\a' "$encoded"
# tmux requires user configuration to interpret OSC 52 on stdout.
# Luckily we can still make this work for the common case by bypassing
# tmux and writing to its underlying terminal.
if set -q TMUX
set -l tmux_tty (tmux display-message -p '#{client_tty}')
or return 1
# The terminal might not be writable if we switched user.
if test -w $tmux_tty
printf '\e]52;c;%s\a' "$encoded" >$tmux_tty
end
end
end end
end end