Fix clipboard routing for tmux and SSH sessions (#45457)

## Why

A persistent tmux session can gain remote clients after Codex starts, so successful native copying must not skip terminal forwarding. Terminal sends also lack delivery acknowledgement and cannot replace native copying reliably.

## What changed

- Attempt native copying first, then independently forward through tmux or OSC 52 in tmux and SSH sessions. Preserve existing native clipboard leases when a later copy returns no new lease.
- Target the most recently active client in the current pane's tmux session, checking that client's clipboard capability before sending. Retain OSC 52 fallback when tmux forwarding fails.
- Reject empty selections without touching clipboards and apply the 100,000-byte terminal payload limit to tmux copies.
- Resolve tmux and PowerShell through trusted system locations, adding Nix system profiles and the WSL PowerShell directory to helper discovery.

## Testing

Add regression coverage for native-before-terminal ordering, fallback routing, clipboard lease retention, empty and oversized payloads, tmux client selection, capability checks, and combined backend errors.

GitOrigin-RevId: df4b66fa4368a3cf19b4d07fde6c76ea96a8fef5
This commit is contained in:
Felipe Coury
2026-09-14 16:15:34 +00:00
committed by copyberry
parent 1a02867bd1
commit b876f88981
11 changed files with 474 additions and 294 deletions
+6 -2
View File
@@ -306,7 +306,9 @@ impl ChatWidget {
match self.transcript.last_agent_markdown.clone() {
Some(markdown) if !markdown.is_empty() => match copy_fn(&markdown) {
Ok(lease) => {
self.clipboard_lease = lease;
if let Some(lease) = lease {
self.clipboard_lease = Some(lease);
}
self.add_to_history(history_cell::new_info_event(
"Copied last message to clipboard".into(),
/*hint*/ None,
@@ -435,7 +437,9 @@ impl ChatWidget {
) {
match copy_fn(text) {
Ok(lease) => {
self.clipboard_lease = lease;
if let Some(lease) = lease {
self.clipboard_lease = Some(lease);
}
self.add_info_message(format!("Copied {label} to clipboard"), /*hint*/ None);
}
Err(error) => self.add_error_message(format!("Copy failed: {error}")),
@@ -2655,7 +2655,7 @@ async fn copy_shortcut_can_be_remapped() {
}
#[tokio::test]
async fn slash_copy_stores_clipboard_lease_and_preserves_it_on_failure() {
async fn slash_copy_preserves_native_lease_after_terminal_copy_or_failure() {
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
chat.transcript.last_agent_markdown = Some("copy me".to_string());
@@ -2673,6 +2673,11 @@ async fn slash_copy_stores_clipboard_lease_and_preserves_it_on_failure() {
"expected success message, got {rendered:?}"
);
chat.copy_last_agent_markdown_with(|_| Ok(None));
assert!(chat.clipboard_lease.is_some());
let rendered = lines_to_single_string(&drain_insert_history(&mut rx)[0]);
assert!(rendered.contains("Copied last message to clipboard"));
chat.copy_last_agent_markdown_with(|markdown| {
assert_eq!(markdown, "copy me");
Err("blocked".into())
@@ -2695,6 +2700,11 @@ async fn slash_copy_stores_clipboard_lease_and_preserves_it_on_failure() {
let rendered = lines_to_single_string(&drain_insert_history(&mut rx)[0]);
assert!(rendered.contains("Copied python code to clipboard"));
chat.copy_selection_with("terminal copy", "code", |_| Ok(None));
assert!(chat.clipboard_lease.is_some());
let rendered = lines_to_single_string(&drain_insert_history(&mut rx)[0]);
assert!(rendered.contains("Copied code to clipboard"));
chat.copy_selection_with("print('blocked')\n", "python code", |content| {
assert_eq!(content, "print('blocked')\n");
Err("blocked selection".to_string())
@@ -10,7 +10,9 @@ impl ChatWidget {
crate::clipboard_copy::CopyFormat::PlainText,
) {
Ok(lease) => {
self.clipboard_lease = lease;
if let Some(lease) = lease {
self.clipboard_lease = Some(lease);
}
self.add_info_message(
"Copied conversation to clipboard".to_string(),
/*hint*/ None,
+89 -287
View File
@@ -1,27 +1,29 @@
//! Clipboard copy backend for the TUI's `/copy` command and `Ctrl+O` hotkey.
//!
//! This module decides *how* to get text onto the user's clipboard based on the
//! current environment. The selection order is:
//! Local copying uses the native clipboard, with WSL PowerShell as a fallback.
//! In tmux, also forward to the attached terminal so clients attached after Codex
//! started receive the copy. Over SSH without tmux, send OSC 52 directly.
//!
//! 1. **SSH session** (`SSH_TTY` / `SSH_CONNECTION` set): use tmux clipboard
//! integration when available, otherwise OSC 52, because the native clipboard
//! belongs to the remote machine.
//! 2. **Local session**: try `arboard` (native clipboard) first. On WSL, fall back
//! to the Windows clipboard through PowerShell if `arboard` fails. Finally, fall
//! back to terminal-mediated copy if no native/WSL clipboard path succeeds.
//! Terminal writes have no delivery acknowledgement: a successful send must not
//! suppress native copying. Both the host and attached client's clipboards may
//! change. Outside tmux and SSH, use OSC 52 only if native copying fails.
//!
//! On Linux, X11 and some Wayland compositors require the process that wrote the
//! clipboard to keep its handle open. `ClipboardLease` wraps the `arboard::Clipboard`
//! so callers can store it for the lifetime of the TUI. On other platforms the lease
//! is always `None`.
//!
//! Empty selections fail without modifying a clipboard.
//! Markdown copies also offer HTML on the native clipboard. Terminal and WSL
//! fallbacks retain the original text. Image paste lives in `clipboard_paste`.
mod tmux;
use base64::Engine;
use std::io::Write;
use tmux::copy as tmux_clipboard_copy;
/// Maximum raw bytes we will base64-encode into an OSC 52 sequence.
/// Maximum raw bytes sent through tmux or directly encoded into OSC 52.
/// Large payloads are rejected before encoding to avoid overwhelming the terminal.
const OSC52_MAX_RAW_BYTES: usize = 100_000;
#[cfg(target_os = "macos")]
@@ -37,11 +39,13 @@ pub(crate) enum CopyFormat {
/// Copy text to the system clipboard.
///
/// Over SSH, uses terminal-mediated copy so the text reaches the *local*
/// terminal emulator's clipboard rather than a remote X11/Wayland clipboard
/// that the user cannot access. On a local session, tries `arboard` (native
/// clipboard) first and falls back to WSL PowerShell, then terminal-mediated
/// copy, if needed.
/// Try native copying, then independently attempt terminal forwarding in tmux or
/// SSH. A terminal send is best effort and does not confirm clipboard delivery.
/// Terminal forwarding may replace native HTML with plain text.
///
/// A returned lease replaces the previous native lease. `None` means no new
/// lease is needed; callers must retain any existing lease so terminal copies
/// do not release ownership of an earlier native clipboard value.
///
/// OSC 52 is supported by kitty, WezTerm, iTerm2, Ghostty, and others.
pub(crate) fn copy_to_clipboard(
@@ -111,107 +115,58 @@ fn copy_to_clipboard_with(
arboard_copy_fn: impl Fn(&str, Option<&str>) -> Result<Option<ClipboardLease>, String>,
wsl_copy_fn: impl Fn(&str) -> Result<(), String>,
) -> Result<Option<ClipboardLease>, String> {
if environment.ssh_session {
// Over SSH the native clipboard writes to the remote machine which is
// useless. Terminal-mediated copy reaches the local terminal emulator.
return terminal_clipboard_copy_with(
text,
environment.tmux_session,
&tmux_copy_fn,
&osc52_copy_fn,
)
.map(|()| None)
.map_err(|terminal_err| {
tracing::warn!("terminal clipboard copy failed over SSH: {terminal_err}");
if environment.tmux_session {
format!("terminal clipboard copy failed over SSH: {terminal_err}")
} else {
format!("OSC 52 clipboard copy failed over SSH: {terminal_err}")
}
});
if text.is_empty() {
return Err("nothing to copy: the selected content is empty".to_string());
}
let terminal_copy = || {
if environment.tmux_session {
let raw_bytes = text.len();
let result = if raw_bytes > OSC52_MAX_RAW_BYTES {
Err(format!(
"tmux clipboard payload too large ({raw_bytes} bytes; max {OSC52_MAX_RAW_BYTES})"
))
} else {
tmux_copy_fn(text)
};
result.or_else(|tmux_error| {
osc52_copy_fn(text).map_err(|osc_error| {
format!("tmux clipboard: {tmux_error}; OSC 52 fallback: {osc_error}")
})
})
} else {
osc52_copy_fn(text)
}
};
let html = match format {
CopyFormat::PlainText => None,
CopyFormat::Markdown => Some(crate::clipboard_html::render_markdown(text)),
};
match arboard_copy_fn(text, html.as_deref()) {
Ok(lease) => Ok(lease),
Err(native_err) => {
if environment.wsl_session {
tracing::warn!(
"native clipboard copy failed: {native_err}, falling back to WSL PowerShell"
);
match wsl_copy_fn(text) {
Ok(()) => return Ok(None),
Err(wsl_err) => {
tracing::warn!(
"WSL PowerShell clipboard copy failed: {wsl_err}, falling back to terminal clipboard"
);
return terminal_clipboard_copy_with(
text,
environment.tmux_session,
&tmux_copy_fn,
&osc52_copy_fn,
)
.map(|()| None)
.map_err(|terminal_err| {
if environment.tmux_session {
format!(
"native clipboard: {native_err}; WSL fallback: {wsl_err}; terminal fallback: {terminal_err}"
)
} else {
format!(
"native clipboard: {native_err}; WSL fallback: {wsl_err}; OSC 52 fallback: {terminal_err}"
)
}
});
}
}
}
tracing::warn!(
"native clipboard copy failed: {native_err}, falling back to terminal clipboard"
);
terminal_clipboard_copy_with(
text,
environment.tmux_session,
&tmux_copy_fn,
&osc52_copy_fn,
)
.map(|()| None)
.map_err(|terminal_err| {
if environment.tmux_session {
format!("native clipboard: {native_err}; terminal fallback: {terminal_err}")
} else {
format!("native clipboard: {native_err}; OSC 52 fallback: {terminal_err}")
}
let native_result = arboard_copy_fn(text, html.as_deref()).or_else(|native_error| {
if environment.wsl_session {
wsl_copy_fn(text).map(|()| None).map_err(|wsl_error| {
format!("native clipboard: {native_error}; WSL fallback: {wsl_error}")
})
} else {
Err(format!("native clipboard: {native_error}"))
}
});
// Copy natively first: an X11 SelectionClear from a terminal write can otherwise
// race with arboard reusing its ownership window and clear the new native data.
// Persistent tmux sessions may gain remote clients after Codex starts, so still
// forward even when no SSH variables were inherited or native copying succeeded.
let terminal_result = (environment.tmux_session || environment.ssh_session).then(terminal_copy);
match native_result {
Ok(lease) => Ok(lease),
Err(native_error) => terminal_result
.unwrap_or_else(terminal_copy)
.map(|()| None)
.map_err(|terminal_error| {
format!("{native_error}; terminal clipboard: {terminal_error}")
}),
}
}
/// Copy through the active terminal, preferring tmux's native clipboard path.
fn terminal_clipboard_copy_with(
text: &str,
tmux_session: bool,
tmux_copy_fn: &impl Fn(&str) -> Result<(), String>,
osc52_copy_fn: &impl Fn(&str) -> Result<(), String>,
) -> Result<(), String> {
if tmux_session {
match tmux_copy_fn(text) {
Ok(()) => return Ok(()),
Err(tmux_err) => {
tracing::warn!("tmux clipboard copy failed: {tmux_err}, falling back to OSC 52");
return osc52_copy_fn(text).map_err(|osc_err| {
format!("tmux clipboard: {tmux_err}; OSC 52 fallback: {osc_err}")
});
}
}
}
osc52_copy_fn(text)
}
/// Detect whether the current process is running inside an SSH session.
fn is_ssh_session() -> bool {
std::env::var_os("SSH_TTY").is_some() || std::env::var_os("SSH_CONNECTION").is_some()
@@ -274,7 +229,12 @@ fn arboard_copy(_text: &str, _html: Option<&str>) -> Result<Option<ClipboardLeas
/// Copy text into the Windows clipboard from a WSL process.
#[cfg(target_os = "linux")]
fn wsl_clipboard_copy(text: &str) -> Result<(), String> {
let mut child = std::process::Command::new("powershell.exe")
let executable = codex_utils_path::system_executable("powershell.exe")
.ok_or_else(|| "PowerShell is unavailable in the system PATH".to_string())?;
let path = codex_utils_path::system_path()
.map_err(|error| format!("failed to resolve system PATH: {error}"))?;
let mut child = std::process::Command::new(executable)
.env("PATH", path)
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::piped())
@@ -322,93 +282,6 @@ fn wsl_clipboard_copy(_text: &str) -> Result<(), String> {
Err("WSL clipboard fallback unavailable on this platform".to_string())
}
/// Copy text through tmux's native clipboard integration.
///
/// `load-buffer -w -` lets tmux read the text from stdin, keep a matching tmux
/// paste buffer, and forward the contents to the outer terminal clipboard when
/// possible without relying on DCS passthrough.
fn tmux_clipboard_copy(text: &str) -> Result<(), String> {
tmux_clipboard_copy_ready(
|| tmux_command_output(["show-options", "-gv", "set-clipboard"]),
|| tmux_command_output(["info"]),
)?;
let mut child = std::process::Command::new("tmux")
.args(["load-buffer", "-w", "-"])
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::piped())
.spawn()
.map_err(|e| format!("failed to spawn tmux: {e}"))?;
let Some(mut stdin) = child.stdin.take() else {
let _ = child.kill();
let _ = child.wait();
return Err("failed to open tmux stdin".to_string());
};
if let Err(err) = stdin.write_all(text.as_bytes()) {
let _ = child.kill();
let _ = child.wait();
return Err(format!("failed to write to tmux: {err}"));
}
drop(stdin);
let output = child
.wait_with_output()
.map_err(|e| format!("failed to wait for tmux: {e}"))?;
if output.status.success() {
Ok(())
} else {
let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
if stderr.is_empty() {
let status = output.status;
Err(format!("tmux exited with status {status}"))
} else {
Err(format!("tmux failed: {stderr}"))
}
}
}
/// Verify that tmux is configured to forward clipboard writes to the outer terminal.
fn tmux_clipboard_copy_ready(
set_clipboard_fn: impl FnOnce() -> Result<String, String>,
tmux_info_fn: impl FnOnce() -> Result<String, String>,
) -> Result<(), String> {
let set_clipboard = set_clipboard_fn()?;
if set_clipboard.trim() == "off" {
return Err("tmux clipboard forwarding is disabled".to_string());
}
let tmux_info = tmux_info_fn()?;
if tmux_info.lines().any(|line| line.contains("Ms: [missing]")) {
return Err("tmux clipboard forwarding is unavailable: missing Ms capability".to_string());
}
Ok(())
}
fn tmux_command_output<const N: usize>(args: [&str; N]) -> Result<String, String> {
let output = std::process::Command::new("tmux")
.args(args)
.output()
.map_err(|e| format!("failed to spawn tmux: {e}"))?;
if output.status.success() {
String::from_utf8(output.stdout).map_err(|e| format!("tmux output was not UTF-8: {e}"))
} else {
let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
if stderr.is_empty() {
let status = output.status;
Err(format!("tmux exited with status {status}"))
} else {
Err(format!("tmux failed: {stderr}"))
}
}
}
/// RAII guard that redirects stderr (fd 2) to `/dev/null` on creation and
/// restores the original fd on drop.
#[cfg(target_os = "macos")]
@@ -522,7 +395,6 @@ mod tests {
use super::OSC52_MAX_RAW_BYTES;
use super::copy_to_clipboard_with;
use super::osc52_sequence;
use super::tmux_clipboard_copy_ready;
use super::write_osc52_to_writer;
fn remote_environment() -> CopyEnvironment {
@@ -555,13 +427,6 @@ mod tests {
}
}
fn local_tmux_environment() -> CopyEnvironment {
CopyEnvironment {
tmux_session: true,
..local_environment()
}
}
#[test]
fn osc52_encoding_roundtrips() {
use base64::Engine;
@@ -605,7 +470,7 @@ mod tests {
}
#[test]
fn ssh_uses_osc52_and_skips_native_on_success() {
fn ssh_attempts_both_native_and_osc52() {
let tmux_calls = Cell::new(0_u8);
let osc_calls = Cell::new(0_u8);
let native_calls = Cell::new(0_u8);
@@ -636,12 +501,12 @@ mod tests {
assert!(matches!(result, Ok(None)));
assert_eq!(tmux_calls.get(), 0);
assert_eq!(osc_calls.get(), 1);
assert_eq!(native_calls.get(), 0);
assert_eq!(native_calls.get(), 1);
assert_eq!(wsl_calls.get(), 0);
}
#[test]
fn ssh_returns_osc52_error_and_skips_native() {
fn ssh_reports_failure_when_all_backends_fail() {
let tmux_calls = Cell::new(0_u8);
let osc_calls = Cell::new(0_u8);
let native_calls = Cell::new(0_u8);
@@ -660,26 +525,29 @@ mod tests {
},
|_, _| {
native_calls.set(native_calls.get() + 1);
Ok(None)
Err("native unavailable".into())
},
|_| {
wsl_calls.set(wsl_calls.get() + 1);
Ok(())
Err("powershell unavailable".into())
},
);
let Err(error) = result else {
panic!("expected OSC 52 error");
};
assert_eq!(error, "OSC 52 clipboard copy failed over SSH: blocked");
assert_eq!(
error,
"native clipboard: native unavailable; WSL fallback: powershell unavailable; terminal clipboard: blocked"
);
assert_eq!(tmux_calls.get(), 0);
assert_eq!(osc_calls.get(), 1);
assert_eq!(native_calls.get(), 0);
assert_eq!(wsl_calls.get(), 0);
assert_eq!(native_calls.get(), 1);
assert_eq!(wsl_calls.get(), 1);
}
#[test]
fn ssh_inside_tmux_prefers_tmux_clipboard() {
fn ssh_inside_tmux_attempts_both_native_and_tmux() {
let tmux_calls = Cell::new(0_u8);
let osc_calls = Cell::new(0_u8);
let native_calls = Cell::new(0_u8);
@@ -709,7 +577,7 @@ mod tests {
assert!(matches!(result, Ok(None)));
assert_eq!(tmux_calls.get(), 1);
assert_eq!(osc_calls.get(), 0);
assert_eq!(native_calls.get(), 0);
assert_eq!(native_calls.get(), 1);
assert_eq!(wsl_calls.get(), 0);
}
@@ -744,7 +612,7 @@ mod tests {
assert!(matches!(result, Ok(None)));
assert_eq!(tmux_calls.get(), 1);
assert_eq!(osc_calls.get(), 1);
assert_eq!(native_calls.get(), 0);
assert_eq!(native_calls.get(), 1);
assert_eq!(wsl_calls.get(), 0);
}
@@ -756,8 +624,8 @@ mod tests {
remote_tmux_environment(),
|_| Err("tmux unavailable".into()),
|_| Err("osc blocked".into()),
|_, _| Ok(None),
|_| Ok(()),
|_, _| Err("native unavailable".into()),
|_| Err("powershell unavailable".into()),
);
let Err(error) = result else {
@@ -765,44 +633,9 @@ mod tests {
};
assert_eq!(
error,
"terminal clipboard copy failed over SSH: tmux clipboard: tmux unavailable; OSC 52 fallback: osc blocked"
);
}
#[test]
fn tmux_clipboard_copy_ready_accepts_forwarding_configuration() {
let result = tmux_clipboard_copy_ready(
|| Ok("external\n".to_string()),
|| Ok("193: Ms: (string) \\033]52;%p1%s;%p2%s\\a\n".to_string()),
);
assert_eq!(result, Ok(()));
}
#[test]
fn tmux_clipboard_copy_ready_rejects_disabled_forwarding() {
let result = tmux_clipboard_copy_ready(
|| Ok("off\n".to_string()),
|| panic!("tmux info should not be queried when forwarding is disabled"),
);
assert_eq!(
result,
Err("tmux clipboard forwarding is disabled".to_string())
);
}
#[test]
fn tmux_clipboard_copy_ready_rejects_missing_ms_capability() {
let result = tmux_clipboard_copy_ready(
|| Ok("external\n".to_string()),
|| Ok("193: Ms: [missing]\n".to_string()),
);
assert_eq!(
result,
Err("tmux clipboard forwarding is unavailable: missing Ms capability".to_string())
"native clipboard: native unavailable; WSL fallback: powershell unavailable; terminal clipboard: tmux clipboard: tmux unavailable; OSC 52 fallback: osc blocked"
);
insta::assert_snapshot!("all_copy_backends_fail", error);
}
#[test]
@@ -891,41 +724,6 @@ mod tests {
assert_eq!(wsl_calls.get(), 0);
}
#[test]
fn local_tmux_fallback_prefers_tmux_when_native_fails() {
let tmux_calls = Cell::new(0_u8);
let osc_calls = Cell::new(0_u8);
let native_calls = Cell::new(0_u8);
let wsl_calls = Cell::new(0_u8);
let result = copy_to_clipboard_with(
"hello",
CopyFormat::PlainText,
local_tmux_environment(),
|_| {
tmux_calls.set(tmux_calls.get() + 1);
Ok(())
},
|_| {
osc_calls.set(osc_calls.get() + 1);
Ok(())
},
|_, _| {
native_calls.set(native_calls.get() + 1);
Err("native unavailable".into())
},
|_| {
wsl_calls.set(wsl_calls.get() + 1);
Ok(())
},
);
assert!(matches!(result, Ok(None)));
assert_eq!(tmux_calls.get(), 1);
assert_eq!(osc_calls.get(), 0);
assert_eq!(native_calls.get(), 1);
assert_eq!(wsl_calls.get(), 0);
}
#[test]
fn local_wsl_native_failure_uses_powershell_and_skips_osc52_on_success() {
let osc_calls = Cell::new(0_u8);
@@ -1015,7 +813,7 @@ mod tests {
};
assert_eq!(
error,
"native clipboard: native unavailable; OSC 52 fallback: osc blocked"
"native clipboard: native unavailable; terminal clipboard: osc blocked"
);
assert_eq!(osc_calls.get(), 1);
assert_eq!(native_calls.get(), 1);
@@ -1051,10 +849,14 @@ mod tests {
};
assert_eq!(
error,
"native clipboard: native unavailable; WSL fallback: powershell unavailable; OSC 52 fallback: osc blocked"
"native clipboard: native unavailable; WSL fallback: powershell unavailable; terminal clipboard: osc blocked"
);
assert_eq!(osc_calls.get(), 1);
assert_eq!(native_calls.get(), 1);
assert_eq!(wsl_calls.get(), 1);
}
}
#[cfg(test)]
#[path = "clipboard_copy/routing_tests.rs"]
mod routing_tests;
@@ -0,0 +1,125 @@
//! Clipboard routing, native ownership, and payload-boundary regressions.
use super::ClipboardLease;
use super::CopyEnvironment;
use super::CopyFormat;
use super::OSC52_MAX_RAW_BYTES;
use super::copy_to_clipboard_with;
use pretty_assertions::assert_eq;
use std::cell::RefCell;
#[test]
fn local_tmux_preserves_native_html_when_terminal_silently_rejects_copy() {
let calls = RefCell::new(Vec::new());
let result = copy_to_clipboard_with(
"**hello**",
CopyFormat::Markdown,
CopyEnvironment {
ssh_session: false,
tmux_session: true,
wsl_session: false,
},
|_| {
// Like default xterm: the command succeeds without delivery.
calls.borrow_mut().push("tmux");
Ok(())
},
|_| panic!("tmux send succeeded"),
|text, html| {
assert_eq!(
(text, html),
("**hello**", Some("<p><strong>hello</strong></p>\n"))
);
calls.borrow_mut().push("native");
Ok(Some(ClipboardLease::test()))
},
|_| panic!("native copy succeeded"),
);
assert!(matches!(result, Ok(Some(_))));
assert_eq!(calls.into_inner(), vec!["native", "tmux"]);
}
#[test]
fn copy_uses_osc52_when_tmux_fails_even_if_native_copy_succeeds() {
let calls = RefCell::new(Vec::new());
let result = copy_to_clipboard_with(
"hello",
CopyFormat::PlainText,
CopyEnvironment {
ssh_session: false,
tmux_session: true,
wsl_session: false,
},
|_| {
calls.borrow_mut().push("tmux");
Err("tmux unavailable".into())
},
|_| {
calls.borrow_mut().push("osc52");
Ok(())
},
|text, html| {
assert_eq!((text, html), ("hello", None));
calls.borrow_mut().push("native");
Ok(Some(ClipboardLease::test()))
},
|_| panic!("native copy succeeded"),
);
assert!(matches!(result, Ok(Some(_))));
assert_eq!(calls.into_inner(), vec!["native", "tmux", "osc52"]);
}
fn local_tmux_environment() -> CopyEnvironment {
CopyEnvironment {
tmux_session: true,
ssh_session: false,
wsl_session: false,
}
}
#[test]
fn oversized_tmux_copy_preserves_native_copy() {
for (size, expected_calls) in [
(OSC52_MAX_RAW_BYTES, vec!["native", "tmux"]),
(OSC52_MAX_RAW_BYTES + 1, vec!["native", "osc52"]),
] {
let text = "x".repeat(size);
let calls = RefCell::new(Vec::new());
let result = copy_to_clipboard_with(
&text,
CopyFormat::PlainText,
local_tmux_environment(),
|actual| {
assert_eq!(actual, text);
calls.borrow_mut().push("tmux");
Ok(())
},
|_| {
calls.borrow_mut().push("osc52");
Err("payload too large".into())
},
|actual, html| {
assert_eq!((actual, html), (text.as_str(), None));
calls.borrow_mut().push("native");
Ok(Some(ClipboardLease::test()))
},
|_| panic!("PowerShell fallback should not be needed"),
);
assert!(result.is_ok());
assert_eq!(calls.into_inner(), expected_calls);
}
}
#[test]
fn empty_copy_reports_failure_without_touching_clipboards() {
let result = copy_to_clipboard_with(
"",
CopyFormat::PlainText,
local_tmux_environment(),
|_| panic!("empty input must not reach tmux"),
|_| panic!("empty input must not reach OSC 52"),
|_, _| panic!("empty input must not reach the native clipboard"),
|_| panic!("empty input must not reach PowerShell"),
);
insta::assert_snapshot!(result.err().expect("empty selection should fail"));
}
@@ -0,0 +1,6 @@
---
source: tui/src/clipboard_copy.rs
assertion_line: 570
expression: "result.err().expect(\"empty selection should fail\")"
---
nothing to copy: the selected content is empty
@@ -0,0 +1,6 @@
---
source: tui/src/clipboard_copy/tmux_tests.rs
assertion_line: 58
expression: "result.expect_err(\"no receiving client\")"
---
tmux clipboard forwarding is unavailable: no attached client
+149
View File
@@ -0,0 +1,149 @@
//! Clipboard forwarding to one attached client in the current pane's tmux session.
//!
//! Capability inspection and delivery use the same client and trusted system
//! executable. The parent routing layer enforces the terminal payload limit.
use std::ffi::OsStr;
use std::io::Write;
use std::path::Path;
/// Copy text through tmux's native clipboard integration.
///
/// `load-buffer -w -` lets tmux read the text from stdin, keep a matching tmux
/// paste buffer, and forward the contents to the outer terminal clipboard when
/// possible without relying on DCS passthrough.
pub(super) fn copy(text: &str) -> Result<(), String> {
let executable = codex_utils_path::system_executable("tmux")
.ok_or_else(|| "tmux is unavailable in the system PATH".to_string())?;
let path = codex_utils_path::system_path()
.map_err(|error| format!("failed to resolve system PATH: {error}"))?;
let pane = std::env::var("TMUX_PANE")
.map_err(|_| "tmux clipboard forwarding is unavailable: no current pane".to_string())?;
let client = clipboard_target(
|| command_output(&executable, &path, ["show-options", "-gv", "set-clipboard"]),
|| {
let session = command_output(
&executable,
&path,
["display-message", "-p", "-t", &pane, "#{session_id}"],
)?;
command_output(
&executable,
&path,
[
"list-clients",
"-t",
session.trim(),
"-F",
"#{client_activity} #{client_name}",
],
)
},
|client| command_output(&executable, &path, ["show-messages", "-T", "-t", client]),
)?;
let mut child = std::process::Command::new(&executable)
.env("PATH", &path)
.args(["load-buffer", "-w", "-t", &client, "-"])
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::piped())
.spawn()
.map_err(|e| format!("failed to spawn tmux: {e}"))?;
let Some(mut stdin) = child.stdin.take() else {
let _ = child.kill();
let _ = child.wait();
return Err("failed to open tmux stdin".to_string());
};
if let Err(err) = stdin.write_all(text.as_bytes()) {
let _ = child.kill();
let _ = child.wait();
return Err(format!("failed to write to tmux: {err}"));
}
drop(stdin);
let output = child
.wait_with_output()
.map_err(|e| format!("failed to wait for tmux: {e}"))?;
if output.status.success() {
Ok(())
} else {
let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
if stderr.is_empty() {
let status = output.status;
Err(format!("tmux exited with status {status}"))
} else {
Err(format!("tmux failed: {stderr}"))
}
}
}
/// Resolve the most recently active client in this session and check its capability.
fn clipboard_target(
set_clipboard_fn: impl FnOnce() -> Result<String, String>,
list_clients_fn: impl FnOnce() -> Result<String, String>,
tmux_info_fn: impl FnOnce(&str) -> Result<String, String>,
) -> Result<String, String> {
let set_clipboard = set_clipboard_fn()?;
if set_clipboard.trim() == "off" {
return Err("tmux clipboard forwarding is disabled".to_string());
}
// list-clients is scoped to the current pane's session. Unlike tmux's
// implicit best-client lookup, an empty list cannot select another session.
let clients = list_clients_fn()?;
let client = clients
.lines()
.filter_map(|line| {
let (activity, client) = line.split_once(' ')?;
Some((activity.parse::<u64>().ok()?, client))
})
.filter(|(_, client)| !client.is_empty())
.max_by_key(|(activity, _)| *activity)
.map(|(_, client)| client.to_string())
.ok_or_else(|| {
"tmux clipboard forwarding is unavailable: no attached client".to_string()
})?;
let tmux_info = tmux_info_fn(&client)?;
if !tmux_info.lines().any(|line| {
line.split_once("Ms: (string) ")
.is_some_and(|(_, sequence)| !sequence.trim().is_empty())
}) {
return Err("tmux clipboard forwarding is unavailable: missing Ms capability".to_string());
}
Ok(client)
}
fn command_output<const N: usize>(
executable: &Path,
path: &OsStr,
args: [&str; N],
) -> Result<String, String> {
let output = std::process::Command::new(executable)
.env("PATH", path)
.args(args)
.stdin(std::process::Stdio::null())
.output()
.map_err(|e| format!("failed to spawn tmux: {e}"))?;
if output.status.success() {
String::from_utf8(output.stdout).map_err(|e| format!("tmux output was not UTF-8: {e}"))
} else {
let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
if stderr.is_empty() {
let status = output.status;
Err(format!("tmux exited with status {status}"))
} else {
Err(format!("tmux failed: {stderr}"))
}
}
}
#[cfg(test)]
#[path = "tmux_tests.rs"]
mod tests;
@@ -0,0 +1,61 @@
//! Verifies tmux client selection and clipboard capability checks.
use super::clipboard_target;
use pretty_assertions::assert_eq;
#[test]
fn tmux_clipboard_copy_ready_accepts_forwarding_configuration() {
let result = clipboard_target(
|| Ok("external\n".to_string()),
|| Ok("42 /dev/pts/7\n1 /dev/pts/8\n".to_string()),
|client| {
assert_eq!(client, "/dev/pts/7");
Ok("193: Ms: (string) \\033]52;%p1%s;%p2%s\\a\n".to_string())
},
);
assert_eq!(result, Ok("/dev/pts/7".to_string()));
}
#[test]
fn tmux_clipboard_copy_ready_rejects_disabled_forwarding() {
let result = clipboard_target(
|| Ok("off\n".to_string()),
|| panic!("client should not be queried when forwarding is disabled"),
|_| panic!("tmux info should not be queried when forwarding is disabled"),
);
assert_eq!(
result,
Err("tmux clipboard forwarding is disabled".to_string())
);
}
#[test]
fn tmux_clipboard_target_requires_nonempty_ms_capability() {
for info in ["", "193: Ms: [missing]\n", "193: Ms: (string) \n"] {
let result = clipboard_target(
|| Ok("external\n".to_string()),
|| Ok("42 /dev/pts/7\n".to_string()),
|client| {
assert_eq!(client, "/dev/pts/7");
Ok(info.to_string())
},
);
assert_eq!(
result,
Err("tmux clipboard forwarding is unavailable: missing Ms capability".to_string())
);
}
}
#[test]
fn tmux_clipboard_target_rejects_no_attached_client() {
let result = clipboard_target(
|| Ok("external\n".to_string()),
|| Ok("\n".to_string()),
|_| panic!("capabilities require an attached client"),
);
insta::assert_snapshot!(result.expect_err("no receiving client"));
}
@@ -0,0 +1,5 @@
---
source: codex-rs/tui/src/clipboard_copy.rs
expression: error
---
native clipboard: native unavailable; WSL fallback: powershell unavailable; terminal clipboard: tmux clipboard: tmux unavailable; OSC 52 fallback: osc blocked
@@ -1,7 +1,8 @@
//! Installed helper discovery for automatic work before workspace trust.
//!
//! Only OS and conventional package-manager locations are trusted, never ambient
//! PATH or the working directory. Custom installations fall back to no helper.
//! PATH or the working directory. Nix system profiles must resolve into the
//! trusted store. Custom installations fall back to no helper.
//! This assumes the installed software directories themselves are trusted; it
//! does not defend against an attacker who can modify system installations.
@@ -77,6 +78,8 @@ fn system_directories() -> Vec<PathBuf> {
"/opt/homebrew/bin",
"/usr/local/bin",
"/opt/local/bin",
"/run/current-system/sw/bin",
"/nix/var/nix/profiles/default/bin",
"/Library/Developer/CommandLineTools/usr/bin",
"/Applications/Xcode.app/Contents/Developer/usr/bin",
"/usr/bin",
@@ -87,8 +90,15 @@ fn system_directories() -> Vec<PathBuf> {
.into_iter()
.map(PathBuf::from);
#[cfg(target_os = "linux")]
let directories =
directories.chain(crate::is_wsl().then(|| PathBuf::from("/mnt/c/Windows/System32")));
let directories = directories.chain(
crate::is_wsl()
.then_some([
PathBuf::from("/mnt/c/Windows/System32"),
PathBuf::from("/mnt/c/Windows/System32/WindowsPowerShell/v1.0"),
])
.into_iter()
.flatten(),
);
#[cfg(windows)]
let directories = installation_roots().into_iter().flat_map(|root| {
vec![