diff --git a/Cargo.lock b/Cargo.lock index 4d23f89e9dd..05a1bb03a6d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14999,6 +14999,7 @@ dependencies = [ "action_log", "agent", "agent-client-protocol", + "agent_servers", "anyhow", "askpass", "async-channel 2.5.0", @@ -15027,6 +15028,7 @@ dependencies = [ "language_extension", "language_model", "languages", + "libc", "log", "lsp", "net", diff --git a/crates/agent_servers/src/acp/transport.rs b/crates/agent_servers/src/acp/transport.rs index d771430b698..e2499bdfd39 100644 --- a/crates/agent_servers/src/acp/transport.rs +++ b/crates/agent_servers/src/acp/transport.rs @@ -6,10 +6,10 @@ use futures::{ }; use gpui::{AsyncApp, Entity}; use project::{Project, agent_server_store::AgentServerCommand}; -use remote::remote_client::Interactive; +use remote::command::RemoteCommand; use std::{io, pin::Pin, process::Stdio}; use task::{Shell, ShellBuilder}; -use util::{ResultExt as _, process::Child}; +use util::process::Child; pub(super) struct StdioProcess { pub child: Child, @@ -32,32 +32,32 @@ pub(super) fn spawn_stdio( .next() .cloned() }); - let (path, arguments, environment) = project - .read_with(cx, |project, cx| { - project.remote_client().and_then(|client| { - let template = client - .read(cx) - .build_command( - Some(command.path.display().to_string()), - &command.args, - &command.env.clone().into_iter().flatten().collect(), - root_dir.as_ref().map(|path| path.display().to_string()), - None, - Interactive::No, - ) - .log_err()?; - Some((template.program, template.args, template.env)) + let remote_command = project.read_with(cx, |project, cx| { + project + .remote_client() + .map(|client| { + client.read(cx).build_stdio_command(RemoteCommand { + program: command.path.display().to_string(), + args: command.args.clone(), + env: command.env.clone().unwrap_or_default(), + working_dir: root_dir.as_ref().map(|path| path.display().to_string()), + }) }) - }) - .unwrap_or_else(|| { - ( - command.path.display().to_string(), - command.args, - command.env.unwrap_or_default(), - ) - }); + .transpose() + })?; + let (path, arguments, environment, command_input) = match remote_command { + Some((template, input)) => (template.program, template.args, template.env, Some(input)), + None => ( + command.path.display().to_string(), + command.args, + command.env.unwrap_or_default(), + None, + ), + }; - let builder = ShellBuilder::new(&Shell::System, cfg!(windows)).non_interactive(); + let builder = ShellBuilder::new(&Shell::System, cfg!(windows)) + .non_interactive() + .literal_args(); let mut child = builder.build_std_command(Some(path.clone()), &arguments); child.envs(environment); if let Some(cwd) = project.read_with(cx, |project, _cx| { @@ -96,13 +96,16 @@ pub(super) fn spawn_stdio( .boxed(); let outgoing = Box::pin(futures::sink::unfold( - (Box::pin(stdin), debug_log.clone()), - async move |(mut writer, debug_log), line: String| { + (Box::pin(stdin), debug_log.clone(), command_input), + async move |(mut writer, debug_log, mut command_input), line: String| { + if let Some(input) = command_input.take() { + writer.write_all(&input).await?; + } debug_log.record_line(AcpDebugMessageDirection::Outgoing, &line); let mut bytes = line.into_bytes(); bytes.push(b'\n'); writer.write_all(&bytes).await?; - Ok::<_, io::Error>((writer, debug_log)) + Ok::<_, io::Error>((writer, debug_log, command_input)) }, )); diff --git a/crates/context_server/src/client.rs b/crates/context_server/src/client.rs index b88bc301db7..1425267d3fd 100644 --- a/crates/context_server/src/client.rs +++ b/crates/context_server/src/client.rs @@ -160,6 +160,17 @@ pub struct ModelContextServerBinary { pub args: Vec, pub env: Option>, pub timeout: Option, + #[serde(skip)] + pub stdin_prefix: Option, +} + +#[derive(Clone, PartialEq, Eq)] +pub struct StdinPrefix(pub Vec); + +impl std::fmt::Debug for StdinPrefix { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "StdinPrefix([REDACTED {} bytes])", self.0.len()) + } } impl Client { diff --git a/crates/context_server/src/context_server.rs b/crates/context_server/src/context_server.rs index 865f779b160..a3bae6a5e0f 100644 --- a/crates/context_server/src/context_server.rs +++ b/crates/context_server/src/context_server.rs @@ -15,7 +15,7 @@ use std::time::Duration; use std::{fmt::Display, path::PathBuf}; use anyhow::Result; -use client::Client; +use client::{Client, StdinPrefix}; use gpui::AsyncApp; use parking_lot::RwLock; pub use settings::ContextServerCommand; @@ -34,7 +34,7 @@ impl Display for ContextServerId { } enum ContextServerTransport { - Stdio(ContextServerCommand, Option), + Stdio(ContextServerCommand, Option, Option), Custom(Arc), } @@ -50,6 +50,7 @@ impl ContextServer { id: ContextServerId, command: ContextServerCommand, working_directory: Option>, + stdin_prefix: Option, ) -> Self { Self { id, @@ -57,6 +58,7 @@ impl ContextServer { configuration: ContextServerTransport::Stdio( command, working_directory.map(|directory| directory.to_path_buf()), + stdin_prefix, ), request_timeout: None, } @@ -123,17 +125,20 @@ impl ContextServer { fn new_client(&self, cx: &AsyncApp) -> Result { Ok(match &self.configuration { - ContextServerTransport::Stdio(command, working_directory) => Client::stdio( - client::ContextServerId(self.id.0.clone()), - client::ModelContextServerBinary { - executable: Path::new(&command.path).to_path_buf(), - args: command.args.clone(), - env: command.env.clone(), - timeout: command.timeout, - }, - working_directory, - cx.clone(), - )?, + ContextServerTransport::Stdio(command, working_directory, stdin_prefix) => { + Client::stdio( + client::ContextServerId(self.id.0.clone()), + client::ModelContextServerBinary { + executable: Path::new(&command.path).to_path_buf(), + args: command.args.clone(), + env: command.env.clone(), + timeout: command.timeout, + stdin_prefix: stdin_prefix.clone(), + }, + working_directory, + cx.clone(), + )? + } ContextServerTransport::Custom(transport) => Client::new( client::ContextServerId(self.id.0.clone()), self.id().0, diff --git a/crates/context_server/src/transport/stdio_transport.rs b/crates/context_server/src/transport/stdio_transport.rs index 5ee5fc30c22..98ad813dd0f 100644 --- a/crates/context_server/src/transport/stdio_transport.rs +++ b/crates/context_server/src/transport/stdio_transport.rs @@ -14,7 +14,7 @@ use util::process::Child; use util::shell::Shell; use util::shell_builder::ShellBuilder; -use crate::client::ModelContextServerBinary; +use crate::client::{ModelContextServerBinary, StdinPrefix}; use crate::transport::Transport; pub struct StdioTransport { @@ -30,7 +30,9 @@ impl StdioTransport { working_directory: &Option, cx: &AsyncApp, ) -> Result { - let builder = ShellBuilder::new(&Shell::System, cfg!(windows)).non_interactive(); + let builder = ShellBuilder::new(&Shell::System, cfg!(windows)) + .non_interactive() + .literal_args(); let mut command = builder.build_std_command(Some(binary.executable.display().to_string()), &binary.args); @@ -55,8 +57,13 @@ impl StdioTransport { let (stdout_sender, stdout_receiver) = async_channel::unbounded::(); let (stderr_sender, stderr_receiver) = async_channel::unbounded::(); - cx.spawn(async move |_| Self::handle_output(stdin, stdout_receiver).log_err().await) - .detach(); + let stdin_prefix = binary.stdin_prefix; + cx.spawn(async move |_| { + Self::handle_output(stdin, stdin_prefix, stdout_receiver) + .log_err() + .await + }) + .detach(); cx.spawn(async move |_| Self::handle_input(stdout, stdin_sender).await) .detach(); @@ -91,12 +98,17 @@ impl StdioTransport { async fn handle_output( stdin: Stdin, + stdin_prefix: Option, outbound_rx: async_channel::Receiver, ) -> Result<()> where Stdin: AsyncWrite + Unpin + Send + 'static, { let mut stdin = BufWriter::new(stdin); + if let Some(StdinPrefix(prefix)) = stdin_prefix { + stdin.write_all(&prefix).await?; + stdin.flush().await?; + } let mut pinned_rx = Box::pin(outbound_rx); while let Some(message) = pinned_rx.next().await { log::trace!("outgoing message: {}", message); diff --git a/crates/project/src/context_server_store.rs b/crates/project/src/context_server_store.rs index 85a3d822ca9..eacdbcfb822 100644 --- a/crates/project/src/context_server_store.rs +++ b/crates/project/src/context_server_store.rs @@ -1,12 +1,13 @@ pub mod extension; pub mod registry; -use std::path::Path; +use std::path::{Path, PathBuf}; use std::sync::Arc; use std::time::Duration; use anyhow::{Context as _, Result}; use collections::{HashMap, HashSet}; +use context_server::client::StdinPrefix; use context_server::oauth::{self, McpOAuthTokenProvider, OAuthDiscovery, OAuthSession}; use context_server::transport::HttpTransport; use context_server::{ContextServer, ContextServerCommand, ContextServerId}; @@ -20,7 +21,7 @@ use http_client::HttpClient; use itertools::Itertools; use rand::Rng as _; use registry::ContextServerDescriptorRegistry; -use remote::{Interactive, RemoteClient}; +use remote::{RemoteClient, command::RemoteCommand}; use rpc::{AnyProtoClient, TypedEnvelope, proto}; use settings::{Settings as _, SettingsLocation, SettingsStore, WorktreeId}; use util::{ResultExt as _, rel_path::RelPath}; @@ -161,6 +162,7 @@ pub enum ContextServerConfiguration { Custom { command: ContextServerCommand, remote: bool, + launcher: Option, }, Extension { command: ContextServerCommand, @@ -175,6 +177,12 @@ pub enum ContextServerConfiguration { }, } +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct RemoteStdioLauncher { + pub command: ContextServerCommand, + pub stdin_prefix: StdinPrefix, +} + impl ContextServerConfiguration { pub fn command(&self) -> Option<&ContextServerCommand> { match self { @@ -215,7 +223,11 @@ impl ContextServerConfiguration { enabled: _, command, remote, - } => Some(ContextServerConfiguration::Custom { command, remote }), + } => Some(ContextServerConfiguration::Custom { + command, + remote, + launcher: None, + }), ContextServerSettings::Extension { enabled: _, settings, @@ -446,6 +458,7 @@ impl ContextServerStore { timeout: None, }, remote: false, + launcher: None, }); self.run_server(server, configuration, cx); } @@ -956,25 +969,35 @@ impl ContextServerStore { }) .await?; - let remote_command = upstream_client.update(cx, |client, _| { - client.build_command( - Some(response.path), - &response.args, - &response.env.into_iter().collect(), - root_dir, - None, - Interactive::Yes, - ) - })?; - let command = ContextServerCommand { - path: remote_command.program.into(), - args: remote_command.args, - env: Some(remote_command.env.into_iter().collect()), + path: PathBuf::from(&response.path), + args: response.args.clone(), + env: Some(response.env.clone().into_iter().collect()), timeout: None, }; - Arc::new(ContextServerConfiguration::Custom { command, remote }) + let (launcher_command, stdin_prefix) = upstream_client.update(cx, |client, _| { + client.build_stdio_command(RemoteCommand { + program: response.path, + args: response.args, + env: response.env.into_iter().collect(), + working_dir: root_dir, + }) + })?; + + Arc::new(ContextServerConfiguration::Custom { + command, + remote, + launcher: Some(RemoteStdioLauncher { + command: ContextServerCommand { + path: launcher_command.program.into(), + args: launcher_command.args, + env: Some(launcher_command.env.into_iter().collect()), + timeout: None, + }, + stdin_prefix: StdinPrefix(stdin_prefix), + }), + }) } else { configuration }; @@ -1044,10 +1067,22 @@ impl ContextServerStore { ))) } _ => { - let mut command = configuration - .command() - .context("Missing command configuration for stdio context server")? - .clone(); + let (mut command, stdin_prefix) = match configuration.as_ref() { + ContextServerConfiguration::Custom { + launcher: Some(launcher), + .. + } => ( + launcher.command.clone(), + Some(launcher.stdin_prefix.clone()), + ), + _ => ( + configuration + .command() + .context("Missing command configuration for stdio context server")? + .clone(), + None, + ), + }; command.timeout = Some( command .timeout @@ -1061,6 +1096,7 @@ impl ContextServerStore { id, command, working_directory, + stdin_prefix, ))) } } diff --git a/crates/project/tests/integration/context_server_store.rs b/crates/project/tests/integration/context_server_store.rs index e7857b9f0c7..3a1c1684f99 100644 --- a/crates/project/tests/integration/context_server_store.rs +++ b/crates/project/tests/integration/context_server_store.rs @@ -1542,6 +1542,7 @@ async fn test_context_server_stdio_timeout(cx: &mut TestAppContext) { timeout: Some(180000), }, remote: false, + launcher: None, }), &mut async_cx, ) diff --git a/crates/remote/src/command.rs b/crates/remote/src/command.rs new file mode 100644 index 00000000000..3696a2d4200 --- /dev/null +++ b/crates/remote/src/command.rs @@ -0,0 +1,226 @@ +use anyhow::{Context as _, Result, ensure}; +use collections::HashMap; +use serde::{Deserialize, Serialize}; +use std::io::Read; +use util::shell::ShellKind; + +#[derive(Serialize, Deserialize)] +pub struct RemoteCommand { + pub program: String, + pub args: Vec, + pub env: HashMap, + pub working_dir: Option, +} + +impl RemoteCommand { + pub fn retain_valid_env(&mut self) { + self.env.retain(|name, _| { + let valid = !name.is_empty() && !name.contains(['=', '\0']); + if !valid { + log::warn!("Skipping environment variable with invalid name"); + } + valid + }); + } + + pub fn encode(&self) -> Result> { + let mut buffer = vec![0; size_of::()]; + serde_json::to_writer(&mut buffer, self)?; + let len = buffer.len() - size_of::(); + ensure!(len <= MAX_COMMAND_SIZE, "Remote command exceeds 16 MiB"); + buffer[..size_of::()].copy_from_slice(&(len as u32).to_le_bytes()); + Ok(buffer) + } + + pub fn read(reader: &mut impl Read) -> Result { + let mut len = [0; size_of::()]; + reader + .read_exact(&mut len) + .context("Failed to read remote command length")?; + let len = u32::from_le_bytes(len) as usize; + ensure!(len <= MAX_COMMAND_SIZE, "Remote command exceeds 16 MiB"); + let mut buffer = vec![0; len]; + reader + .read_exact(&mut buffer) + .context("Failed to read remote command")?; + serde_json::from_slice(&buffer).map_err(|_| anyhow::anyhow!("Invalid remote command")) + } +} + +pub fn stdio_launcher_command(shell_kind: ShellKind, remote_binary_path: &str) -> Result { + let helper = shell_kind + .try_quote(remote_binary_path) + .context("shell quoting")?; + Ok(format!("exec {helper} exec")) +} + +pub fn home_stdio_launcher_command( + shell_kind: ShellKind, + home_dir: Option<&str>, + relative_binary_path: &str, +) -> Result { + match home_dir { + Some(home_dir) => stdio_launcher_command( + shell_kind, + &format!("{}/{relative_binary_path}", home_dir.trim_end_matches('/')), + ), + None => Ok(format!( + "exec {} exec", + home_relative_path(shell_kind, Some(relative_binary_path))? + )), + } +} + +pub fn home_relative_path(shell_kind: ShellKind, relative_path: Option<&str>) -> Result { + let quoted = relative_path + .map(|relative_path| shell_kind.try_quote(relative_path).context("shell quoting")) + .transpose()?; + Ok(match (shell_kind, quoted) { + (ShellKind::Nushell, Some(quoted)) => format!("($env.HOME | path join {quoted})"), + (ShellKind::Nushell, None) => String::from("$env.HOME"), + (_, Some(quoted)) => format!("\"$HOME\"/{quoted}"), + (_, None) => String::from("\"$HOME\""), + }) +} + +const MAX_COMMAND_SIZE: usize = 16 * 1024 * 1024; + +#[cfg(test)] +mod tests { + use super::*; + use std::io::Cursor; + + #[test] + fn test_remote_command_frame() -> Result<()> { + let json = br#"{"program":"agent","args":["","--acp"],"env":{"TOKEN":"'\"\n\r\t\\$()=secret\n"},"working_dir":"~/project with spaces"}"#; + let mut input = (json.len() as u32).to_le_bytes().to_vec(); + input.extend_from_slice(json); + let remaining = b"{\"jsonrpc\":\"2.0\",\"id\":1}\n\0\xff"; + input.extend_from_slice(remaining); + let mut input = Cursor::new(input); + let command = RemoteCommand::read(&mut input)?; + assert_eq!(command.program, "agent"); + assert_eq!(command.args, ["", "--acp"]); + assert_eq!( + command.env, + HashMap::from_iter([( + String::from("TOKEN"), + String::from("'\"\n\r\t\\$()=secret\n") + )]) + ); + assert_eq!( + command.working_dir.as_deref(), + Some("~/project with spaces") + ); + let mut output = Vec::new(); + input.read_to_end(&mut output)?; + assert_eq!(output, remaining); + let encoded = command.encode()?; + assert_eq!(&encoded[4..], json); + assert_eq!(&encoded[..4], &(json.len() as u32).to_le_bytes()); + Ok(()) + } + + #[test] + fn test_remote_command_size_limit() -> Result<()> { + let mut command = RemoteCommand { + program: String::from("agent"), + args: Vec::new(), + env: HashMap::default(), + working_dir: None, + }; + let overhead = command.encode()?.len() - size_of::(); + command + .program + .push_str(&"a".repeat(MAX_COMMAND_SIZE - overhead)); + let encoded = command.encode()?; + assert_eq!(encoded.len(), MAX_COMMAND_SIZE + size_of::()); + assert_eq!( + RemoteCommand::read(&mut &encoded[..])?.program, + command.program + ); + command.program.push('a'); + assert_eq!( + command.encode().unwrap_err().to_string(), + "Remote command exceeds 16 MiB" + ); + let oversized = (MAX_COMMAND_SIZE as u32 + 1).to_le_bytes(); + let error = RemoteCommand::read(&mut &oversized[..]).err().unwrap(); + assert_eq!(error.to_string(), "Remote command exceeds 16 MiB"); + Ok(()) + } + + #[test] + fn test_retain_valid_env() { + let mut command = RemoteCommand { + program: String::from("agent"), + args: Vec::new(), + env: HashMap::from_iter( + [ + ("", "empty"), + ("NAME=VALUE", "equals"), + ("NAME\0NUL", "nul"), + ("LINE\nBREAK", "kept"), + ("TAB\tKEY", "kept"), + ("KEY-WITH-DASH", "kept"), + ("GH_TOKEN", "kept"), + ] + .map(|(name, value)| (name.to_owned(), value.to_owned())), + ), + working_dir: None, + }; + command.retain_valid_env(); + assert_eq!( + command.env, + HashMap::from_iter([ + (String::from("LINE\nBREAK"), String::from("kept")), + (String::from("TAB\tKEY"), String::from("kept")), + (String::from("KEY-WITH-DASH"), String::from("kept")), + (String::from("GH_TOKEN"), String::from("kept")), + ]) + ); + } + + #[test] + fn test_home_relative_path() -> Result<()> { + assert_eq!(home_relative_path(ShellKind::Posix, None)?, "\"$HOME\""); + assert_eq!( + home_relative_path(ShellKind::Posix, Some("a b/c"))?, + "\"$HOME\"/'a b/c'" + ); + assert_eq!( + home_relative_path(ShellKind::Tcsh, Some("bang!"))?, + "\"$HOME\"/'bang'\\!" + ); + assert_eq!(home_relative_path(ShellKind::Nushell, None)?, "$env.HOME"); + assert_eq!( + home_relative_path(ShellKind::Nushell, Some("a b/O'Brien"))?, + "($env.HOME | path join \"a b/O'Brien\")" + ); + assert_eq!( + home_stdio_launcher_command(ShellKind::Nushell, None, ".zed_server/server")?, + "exec ($env.HOME | path join .zed_server/server) exec" + ); + assert_eq!( + home_stdio_launcher_command(ShellKind::Nushell, Some("/home/u/"), "x y")?, + "exec '/home/u/x y' exec" + ); + assert_eq!( + home_stdio_launcher_command(ShellKind::Posix, None, "x y")?, + "exec \"$HOME\"/'x y' exec" + ); + Ok(()) + } + + #[test] + fn test_remote_command_invalid_input() { + for input in [&b""[..], &[1, 0, 0], &[1, 0, 0, 0]] { + assert!(RemoteCommand::read(&mut &input[..]).is_err()); + } + let json = br#"{"program":"agent","args":"secret"}"#; + let mut input = (json.len() as u32).to_le_bytes().to_vec(); + input.extend_from_slice(json); + let error = RemoteCommand::read(&mut &input[..]).err().unwrap(); + assert_eq!(error.to_string(), "Invalid remote command"); + } +} diff --git a/crates/remote/src/remote.rs b/crates/remote/src/remote.rs index de8f9348752..2a8e5160ffe 100644 --- a/crates/remote/src/remote.rs +++ b/crates/remote/src/remote.rs @@ -1,3 +1,4 @@ +pub mod command; pub mod json_log; pub mod protocol; pub mod proxy; @@ -23,5 +24,6 @@ pub use transport::wsl::wsl_path_to_windows_path; #[cfg(any(test, feature = "test-support"))] pub use transport::mock::{ - MockConnection, MockConnectionOptions, MockConnectionRegistry, MockDelegate, + MOCK_STDIO_FRAME_HEX, MockConnection, MockConnectionOptions, MockConnectionRegistry, + MockDelegate, }; diff --git a/crates/remote/src/remote_client.rs b/crates/remote/src/remote_client.rs index c645a31d129..1563a95f43b 100644 --- a/crates/remote/src/remote_client.rs +++ b/crates/remote/src/remote_client.rs @@ -2,6 +2,7 @@ use crate::transport::mock::ConnectGuard; use crate::{ SshConnectionOptions, + command::RemoteCommand, protocol::MessageId, proxy::ProxyLaunchError, transport::{ @@ -981,6 +982,16 @@ impl RemoteClient { connection.build_command(program, args, env, working_dir, port_forward, interactive) } + pub fn build_stdio_command( + &self, + command: RemoteCommand, + ) -> Result<(CommandTemplate, Vec)> { + let Some(connection) = self.remote_connection() else { + return Err(anyhow!("no remote connection")); + }; + connection.build_stdio_command(command) + } + pub fn build_forward_ports_command( &self, forwards: Vec<(u16, String, u16)>, @@ -1652,6 +1663,11 @@ pub trait RemoteConnection: Send + Sync { port_forward: Option<(u16, String, u16)>, interactive: Interactive, ) -> Result; + fn build_stdio_command(&self, _command: RemoteCommand) -> Result<(CommandTemplate, Vec)> { + Err(anyhow!( + "stdio commands are not supported by this remote connection" + )) + } fn build_forward_ports_command( &self, forwards: Vec<(u16, String, u16)>, diff --git a/crates/remote/src/transport.rs b/crates/remote/src/transport.rs index 9a2d872655e..8a370a547db 100644 --- a/crates/remote/src/transport.rs +++ b/crates/remote/src/transport.rs @@ -125,6 +125,26 @@ fn parse_shell(output: &str, fallback_shell: &str) -> String { } } +const HOME_DIR_PROGRAM: &str = "/bin/sh"; +const HOME_DIR_ARGS: [&str; 2] = [ + "-c", + "printf ZED_HOME_BEGIN; echo; pwd; printf ZED_HOME_END", +]; + +fn parse_home_dir(output: &str) -> Option { + let (_, output) = output.split_once("ZED_HOME_BEGIN")?; + let (home_dir, _) = output.rsplit_once("ZED_HOME_END")?; + let line_ending = if home_dir.starts_with("\r\n") { + "\r\n" + } else { + "\n" + }; + let home_dir = home_dir + .strip_prefix(line_ending)? + .strip_suffix(line_ending)?; + (home_dir.starts_with('/') && !home_dir.contains('\u{FFFD}')).then(|| home_dir.to_owned()) +} + fn handle_rpc_messages_over_child_process_stdio( mut remote_proxy_process: Child, incoming_tx: UnboundedSender, @@ -632,4 +652,106 @@ mod tests { assert_eq!(parse_shell("", "sh"), "sh"); assert_eq!(parse_shell("\n", "sh"), "sh"); } + + #[test] + fn test_parse_home_dir() { + assert_eq!( + parse_home_dir("ZED_HOME_BEGIN\n/home/user\nZED_HOME_END"), + Some(String::from("/home/user")) + ); + assert_eq!( + parse_home_dir( + "some shell init output\r\nZED_HOME_BEGIN\r\n/home/user\r\nZED_HOME_END\r\n" + ), + Some(String::from("/home/user")) + ); + assert_eq!( + parse_home_dir("ZED_HOME_BEGIN\n/home/user\r\nZED_HOME_END"), + Some(String::from("/home/user\r")) + ); + assert_eq!( + parse_home_dir("ZED_HOME_BEGIN\r\n/home/user\r\r\nZED_HOME_END"), + Some(String::from("/home/user\r")) + ); + assert_eq!( + parse_home_dir("ZED_HOME_BEGIN\n/home/user \nZED_HOME_END"), + Some(String::from("/home/user ")) + ); + assert_eq!( + parse_home_dir("ZED_HOME_BEGIN\n/home/user\n\nZED_HOME_END"), + Some(String::from("/home/user\n")) + ); + assert_eq!( + parse_home_dir("ZED_HOME_BEGIN\n/home/ZED_HOME_BEGIN/user\nZED_HOME_END"), + Some(String::from("/home/ZED_HOME_BEGIN/user")) + ); + assert_eq!( + parse_home_dir("ZED_HOME_BEGIN\n/home/ZED_HOME_END/user\nZED_HOME_END"), + Some(String::from("/home/ZED_HOME_END/user")) + ); + assert_eq!( + parse_home_dir("ZED_HOME_BEGIN\n/ZED_HOME_ENDZED_HOME_BEGIN\nZED_HOME_END\n"), + Some(String::from("/ZED_HOME_ENDZED_HOME_BEGIN")) + ); + assert_eq!(parse_home_dir(""), None); + assert_eq!(parse_home_dir("/home/user\n"), None); + assert_eq!( + parse_home_dir("ZED_HOME_BEGIN/home/user\nZED_HOME_END"), + None + ); + assert_eq!( + parse_home_dir("ZED_HOME_BEGIN\n/home/userZED_HOME_END"), + None + ); + assert_eq!( + parse_home_dir("ZED_HOME_BEGIN\nrelative/output\nZED_HOME_END"), + None + ); + assert_eq!( + parse_home_dir(&format!( + "ZED_HOME_BEGIN\n{}\nZED_HOME_END", + String::from_utf8_lossy(b"/home/user-\xff") + )), + None + ); + } + + #[cfg(unix)] + #[test] + fn test_home_dir_probe_without_path_lookup() { + let temp_dir = tempfile::tempdir().unwrap(); + for home_name in [ + "ZED_HOME_BEGIN/ZED_HOME_END dir", + "carriage return\r", + "trailing\n", + ] { + let home = temp_dir.path().join(home_name); + std::fs::create_dir_all(&home).unwrap(); + let mut args = vec![ + String::from("-c"), + String::from("cd; exec \"$0\" \"$@\""), + String::from(HOME_DIR_PROGRAM), + ]; + args.extend(HOME_DIR_ARGS.map(str::to_owned)); + let output = smol::block_on( + smol::process::Command::new("/bin/sh") + .args(args) + .env_clear() + .env("HOME", &home) + .env("PATH", "/nonexistent") + .output(), + ) + .unwrap(); + assert!( + output.status.success(), + "{home_name:?}: {}", + String::from_utf8_lossy(&output.stderr) + ); + assert_eq!( + parse_home_dir(&String::from_utf8(output.stdout).unwrap()), + Some(home.display().to_string()), + "{home_name:?}" + ); + } + } } diff --git a/crates/remote/src/transport/docker.rs b/crates/remote/src/transport/docker.rs index 7ffdbd4e3f4..fb7c2218765 100644 --- a/crates/remote/src/transport/docker.rs +++ b/crates/remote/src/transport/docker.rs @@ -29,6 +29,7 @@ use rpc::proto::Envelope; use crate::{ RemoteArch, RemoteClientDelegate, RemoteConnection, RemoteConnectionOptions, RemoteOs, RemotePlatform, + command::RemoteCommand, remote_client::{CommandTemplate, Interactive}, transport::parse_platform, }; @@ -844,6 +845,64 @@ impl RemoteConnection for DockerExecConnection { }) } + fn build_stdio_command( + &self, + mut command: RemoteCommand, + ) -> Result<(CommandTemplate, Vec)> { + let remote_binary_relpath = self + .remote_binary_relpath + .as_ref() + .context("Remote binary path not set")?; + let mut env = self + .connection_options + .remote_env + .iter() + .map(|(name, value)| (name.clone(), value.clone())) + .collect::>(); + env.extend(command.env); + command.env = env; + command.retain_valid_env(); + if let Some(working_dir) = command.working_dir.take() { + let working_dir = RemotePathBuf::new(working_dir, self.path_style()).to_string(); + command.working_dir = Some(match working_dir.strip_prefix("~/") { + Some(remainder) => format!( + "{}/{}", + self.remote_dir_for_server.trim_end_matches('/'), + remainder.trim_start_matches('/') + ), + None => working_dir, + }); + } + let remote_binary_path = format!( + "{}/{}", + self.remote_dir_for_server.trim_end_matches('/'), + remote_binary_relpath.display(PathStyle::Unix), + ); + let mut args = vec![ + String::from("exec"), + String::from("-u"), + self.connection_options.remote_user.clone(), + ]; + if let Some(working_dir) = &command.working_dir { + args.push(String::from("-w")); + args.push(working_dir.clone()); + } + args.extend([ + String::from("-i"), + self.connection_options.container_id.clone(), + remote_binary_path, + String::from("exec"), + ]); + Ok(( + CommandTemplate { + program: self.docker_cli().to_string(), + args, + env: HashMap::default(), + }, + command.encode()?, + )) + } + fn build_forward_ports_command( &self, _forwards: Vec<(u16, String, u16)>, @@ -1052,6 +1111,73 @@ mod tests { ); } + #[test] + fn test_build_stdio_command() -> Result<()> { + let mut connection = connection(&[("API_KEY", "remote-secret"), ("NAME\0NUL", "dropped")]); + let command = |env, working_dir: Option<&str>| RemoteCommand { + program: String::from("agent"), + args: vec![String::from("--token=argument-secret")], + env, + working_dir: working_dir.map(str::to_owned), + }; + assert_eq!( + connection + .build_stdio_command(command(HashMap::default(), None)) + .unwrap_err() + .to_string(), + "Remote binary path not set" + ); + + connection.remote_binary_relpath = Some(Arc::from(RelPath::from_unix_str( + ".local/share/zed/remote server", + )?)); + for (env_override, working_dir, expected_working_dir, use_podman, expected_program) in [ + (None, None, None, false, "docker"), + ( + Some("agent-secret"), + Some("~/project"), + Some("/tmp/zed/project"), + true, + "podman", + ), + ( + None, + Some("/workspace"), + Some("/workspace"), + false, + "docker", + ), + ] { + connection.connection_options.use_podman = use_podman; + let expected_env = HashMap::from_iter([( + String::from("API_KEY"), + String::from(env_override.unwrap_or("remote-secret")), + )]); + let expected_payload = command(expected_env, expected_working_dir).encode()?; + let mut env = env_override + .map(|value| (String::from("API_KEY"), value.to_owned())) + .into_iter() + .collect::>(); + env.insert(String::from("NAME=VALUE"), String::from("dropped")); + let (template, payload) = connection.build_stdio_command(command(env, working_dir))?; + assert_eq!(template.program, expected_program); + let mut expected_args = vec!["exec", "-u", "user"]; + if let Some(expected_working_dir) = expected_working_dir { + expected_args.extend(["-w", expected_working_dir]); + } + expected_args.extend([ + "-i", + "container_id", + "/tmp/zed/.local/share/zed/remote server", + "exec", + ]); + assert_eq!(template.args, expected_args); + assert_eq!(template.env, HashMap::default()); + assert_eq!(payload, expected_payload); + } + Ok(()) + } + fn connection(remote_env: &[(&str, &str)]) -> DockerExecConnection { DockerExecConnection { proxy_process: Mutex::new(None), diff --git a/crates/remote/src/transport/mock.rs b/crates/remote/src/transport/mock.rs index e39d123a1ce..79a4587de6f 100644 --- a/crates/remote/src/transport/mock.rs +++ b/crates/remote/src/transport/mock.rs @@ -29,9 +29,12 @@ //! } //! ``` -use crate::remote_client::{ - ChannelClient, CommandTemplate, Interactive, RemoteClientDelegate, RemoteConnection, - RemoteConnectionOptions, +use crate::{ + command::RemoteCommand, + remote_client::{ + ChannelClient, CommandTemplate, Interactive, RemoteClientDelegate, RemoteConnection, + RemoteConnectionOptions, + }, }; use anyhow::Result; use async_trait::async_trait; @@ -213,6 +216,10 @@ impl RemoteConnection for MockRemoteConnection { }) } + fn build_stdio_command(&self, command: RemoteCommand) -> Result<(CommandTemplate, Vec)> { + stdio_bootstrap_command(command) + } + fn build_forward_ports_command( &self, forwards: Vec<(u16, String, u16)>, @@ -316,6 +323,108 @@ impl RemoteConnection for MockRemoteConnection { } } +pub const MOCK_STDIO_FRAME_HEX: &str = "MOCK_STDIO_FRAME_HEX"; + +fn stdio_bootstrap_command(command: RemoteCommand) -> Result<(CommandTemplate, Vec)> { + let payload = command.encode()?; + let script = format!( + "{MOCK_STDIO_FRAME_HEX}=\"$(command -p dd bs=1 count={} 2>/dev/null | command -p od -An -v -tx1 | command -p tr -d ' \\n')\" || exit 1; [ \"${{#{MOCK_STDIO_FRAME_HEX}}}\" -eq {} ] || {{ printf 'mock stdio bootstrap read %s hex digits, expected {}\\n' \"${{#{MOCK_STDIO_FRAME_HEX}}}\" >&2; exit 1; }}; export {MOCK_STDIO_FRAME_HEX}; exec \"$@\"", + payload.len(), + payload.len() * 2, + payload.len() * 2, + ); + let mut args = vec![ + String::from("-c"), + script, + String::from("sh"), + command.program, + ]; + args.extend(command.args); + Ok(( + CommandTemplate { + program: String::from("/bin/sh"), + args, + env: command.env, + }, + payload, + )) +} + +#[cfg(all(test, unix))] +mod tests { + use super::*; + use futures::{AsyncReadExt as _, AsyncWriteExt as _}; + use smol::process::{Command, Stdio}; + + #[test] + fn test_stdio_bootstrap_without_target_path() -> Result<()> { + smol::block_on(async { + let (template, payload) = stdio_bootstrap_command(RemoteCommand { + program: String::from("/bin/sh"), + args: vec![ + String::from("-c"), + format!("printf '%s\\n' \"${MOCK_STDIO_FRAME_HEX}\"; exec /bin/cat"), + ], + env: HashMap::from_iter([(String::from("PATH"), String::from("/nonexistent"))]), + working_dir: None, + })?; + let expected_hex = payload + .iter() + .map(|byte| format!("{byte:02x}")) + .collect::(); + let message = b"{\"jsonrpc\":\"2.0\",\"id\":1}\n\0\xff"; + + let mut child = spawn(&template)?; + let mut stdin = child.stdin.take().unwrap(); + stdin.write_all(&payload).await?; + stdin.write_all(message).await?; + drop(stdin); + let output = child.output().await?; + assert_eq!(String::from_utf8_lossy(&output.stderr), ""); + assert!(output.status.success()); + assert_eq!( + output.stdout, + [expected_hex.as_bytes(), b"\n", message].concat() + ); + + let mut child = spawn(&template)?; + let mut stdin = child.stdin.take().unwrap(); + stdin.write_all(&payload[..payload.len() / 2]).await?; + drop(stdin); + let mut stdout = Vec::new(); + child + .stdout + .take() + .unwrap() + .read_to_end(&mut stdout) + .await?; + let output = child.output().await?; + assert_eq!(output.status.code(), Some(1)); + assert_eq!(stdout, b""); + assert_eq!( + String::from_utf8_lossy(&output.stderr), + format!( + "mock stdio bootstrap read {} hex digits, expected {}\n", + payload.len() / 2 * 2, + payload.len() * 2 + ) + ); + Ok(()) + }) + } + + fn spawn(template: &CommandTemplate) -> Result { + Ok(Command::new(&template.program) + .args(&template.args) + .env_clear() + .envs(&template.env) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn()?) + } +} + /// Mock delegate for tests that don't need delegate functionality. pub struct MockDelegate; diff --git a/crates/remote/src/transport/ssh.rs b/crates/remote/src/transport/ssh.rs index 86ad4c41afb..cd11807f889 100644 --- a/crates/remote/src/transport/ssh.rs +++ b/crates/remote/src/transport/ssh.rs @@ -1,7 +1,8 @@ use crate::{ RemoteArch, RemoteClientDelegate, RemoteOs, RemotePlatform, + command::{RemoteCommand, home_relative_path, home_stdio_launcher_command}, remote_client::{CommandTemplate, Interactive, RemoteConnection, RemoteConnectionOptions}, - transport::{parse_platform, parse_shell}, + transport::{HOME_DIR_ARGS, HOME_DIR_PROGRAM, parse_home_dir, parse_platform, parse_shell}, }; use anyhow::{Context as _, Result, anyhow}; use async_trait::async_trait; @@ -20,6 +21,7 @@ use semver::Version; pub use settings_content::SshPortForwardOption; use smol::fs; use std::{ + fmt::Write as _, net::IpAddr, path::{Path, PathBuf}, sync::{ @@ -52,6 +54,7 @@ pub(crate) struct SshRemoteConnection { ssh_shell: String, ssh_shell_kind: ShellKind, ssh_default_system_shell: String, + ssh_home_dir: Option, _temp_dir: TempDir, } @@ -378,6 +381,47 @@ impl RemoteConnection for SshRemoteConnection { } } + fn build_stdio_command( + &self, + mut command: RemoteCommand, + ) -> Result<(CommandTemplate, Vec)> { + if self.ssh_platform.os.is_windows() { + let template = self.build_command( + Some(command.program), + &command.args, + &HashMap::default(), + command.working_dir, + None, + Interactive::No, + )?; + return Ok((template, Vec::new())); + } + let remote_binary_path = self + .remote_binary_path + .as_ref() + .context("Remote binary path not set")?; + command.retain_valid_env(); + let mut exec = posix_working_dir_prefix( + command.working_dir.take(), + self.ssh_path_style, + self.ssh_shell_kind, + )?; + exec.push_str(&home_stdio_launcher_command( + self.ssh_shell_kind, + self.ssh_home_dir.as_deref(), + &remote_binary_path.display(PathStyle::Unix), + )?); + let template = ssh_command_template( + self.socket.ssh_command_options(), + None, + Interactive::No, + &self.socket.connection_options.ssh_destination(), + exec, + self.socket.envs.clone(), + ); + Ok((template, command.encode()?)) + } + fn build_forward_ports_command( &self, forwards: Vec<(u16, String, u16)>, @@ -805,6 +849,13 @@ impl SshRemoteConnection { let ssh_os_version = socket.os_version(ssh_platform.os, ssh_shell_kind).await; log::info!("Remote OS version discovered: {:?}", ssh_os_version); + let ssh_home_dir = if is_windows { + None + } else { + socket.home_dir(ssh_shell_kind).await + }; + log::info!("Remote home directory discovered: {:?}", ssh_home_dir); + let (ssh_path_style, ssh_default_system_shell) = match ssh_platform.os { RemoteOs::Windows => (PathStyle::Windows, ssh_shell.clone()), _ => (PathStyle::Unix, String::from("/bin/sh")), @@ -822,6 +873,7 @@ impl SshRemoteConnection { ssh_shell, ssh_shell_kind, ssh_default_system_shell, + ssh_home_dir, }; let (release_channel, version) = @@ -1490,6 +1542,25 @@ impl SshSocket { } } + async fn home_dir(&self, shell: ShellKind) -> Option { + match self + .run_command(shell, HOME_DIR_PROGRAM, &HOME_DIR_ARGS, false) + .await + { + Ok(output) => { + let home_dir = parse_home_dir(&output); + if home_dir.is_none() { + log::warn!("Failed to parse remote home directory from {output:?}"); + } + home_dir + } + Err(error) => { + log::warn!("Failed to determine remote home directory: {error:#}"); + None + } + } + } + async fn platform_windows(&self, shell: ShellKind) -> Result { let output = self .run_command( @@ -1865,51 +1936,7 @@ fn build_command_posix( ssh_destination: &str, interactive: Interactive, ) -> Result { - use std::fmt::Write as _; - - let mut exec = String::new(); - if let Some(working_dir) = working_dir { - let working_dir = RemotePathBuf::new(working_dir, ssh_path_style).to_string(); - - // For paths starting with ~/, we need $HOME to expand, but the remainder - // must be properly quoted to prevent command injection. - // Pattern: cd "$HOME"/'quoted/remainder' - $HOME expands, rest is single-quoted - const TILDE_PREFIX: &str = "~/"; - if working_dir.starts_with(TILDE_PREFIX) { - let remainder = working_dir.trim_start_matches(TILDE_PREFIX); - if remainder.is_empty() { - write!( - exec, - "cd \"$HOME\" {} ", - ssh_shell_kind.sequential_and_commands_separator() - )?; - } else { - let quoted_remainder = ssh_shell_kind - .try_quote(remainder) - .context("shell quoting")?; - write!( - exec, - "cd \"$HOME\"/{quoted_remainder} {} ", - ssh_shell_kind.sequential_and_commands_separator() - )?; - } - } else { - let quoted_dir = ssh_shell_kind - .try_quote(&working_dir) - .context("shell quoting")?; - write!( - exec, - "cd {quoted_dir} {} ", - ssh_shell_kind.sequential_and_commands_separator() - )?; - } - } else { - write!( - exec, - "cd {} ", - ssh_shell_kind.sequential_and_commands_separator() - )?; - }; + let mut exec = posix_working_dir_prefix(working_dir, ssh_path_style, ssh_shell_kind)?; write!(exec, "exec env ")?; for (k, v) in input_env.iter() { @@ -1936,6 +1963,65 @@ fn build_command_posix( write!(exec, "{ssh_shell} -l")?; }; + Ok(ssh_command_template( + ssh_options, + port_forward, + interactive, + ssh_destination, + exec, + ssh_env, + )) +} + +fn posix_working_dir_prefix( + working_dir: Option, + ssh_path_style: PathStyle, + ssh_shell_kind: ShellKind, +) -> Result { + let mut exec = String::new(); + if let Some(working_dir) = working_dir { + let working_dir = RemotePathBuf::new(working_dir, ssh_path_style).to_string(); + + // For paths starting with ~/, we need $HOME to expand, but the remainder + // must be properly quoted to prevent command injection. + // Pattern: cd "$HOME"/'quoted/remainder' - $HOME expands, rest is single-quoted + const TILDE_PREFIX: &str = "~/"; + if working_dir.starts_with(TILDE_PREFIX) { + let remainder = working_dir.trim_start_matches(TILDE_PREFIX); + write!( + exec, + "cd {} {} ", + home_relative_path(ssh_shell_kind, (!remainder.is_empty()).then_some(remainder))?, + ssh_shell_kind.sequential_and_commands_separator() + )?; + } else { + let quoted_dir = ssh_shell_kind + .try_quote(&working_dir) + .context("shell quoting")?; + write!( + exec, + "cd {quoted_dir} {} ", + ssh_shell_kind.sequential_and_commands_separator() + )?; + } + } else { + write!( + exec, + "cd {} ", + ssh_shell_kind.sequential_and_commands_separator() + )?; + }; + Ok(exec) +} + +fn ssh_command_template( + ssh_options: Vec, + port_forward: Option<(u16, String, u16)>, + interactive: Interactive, + ssh_destination: &str, + exec: String, + ssh_env: HashMap, +) -> CommandTemplate { let mut args = Vec::new(); args.extend(ssh_options); @@ -1962,11 +2048,11 @@ fn build_command_posix( args.push(ssh_destination.into()); args.push(exec); - Ok(CommandTemplate { + CommandTemplate { program: "ssh".into(), args, env: ssh_env, - }) + } } fn build_command_windows( @@ -2076,6 +2162,170 @@ fn build_command_windows( mod tests { use super::*; + #[cfg(not(windows))] + #[test] + fn test_build_stdio_command() -> Result<()> { + let mut connection = SshRemoteConnection { + socket: SshSocket { + connection_options: SshConnectionOptions { + host: SshConnectionHost::from("host"), + username: Some(String::from("user")), + port: Some(2222), + ..SshConnectionOptions::default() + }, + socket_path: PathBuf::from("/tmp/zed-test.sock"), + envs: HashMap::from_iter([( + String::from("SSH_ASKPASS_REQUIRE"), + String::from("force"), + )]), + }, + master_process: Mutex::new(None), + killed: AtomicBool::new(false), + remote_binary_path: None, + ssh_platform: RemotePlatform { + os: RemoteOs::Linux, + arch: RemoteArch::X86_64, + }, + ssh_os_version: None, + ssh_path_style: PathStyle::Unix, + ssh_shell: String::from("/bin/bash"), + ssh_shell_kind: ShellKind::Posix, + ssh_default_system_shell: String::from("/bin/sh"), + ssh_home_dir: None, + _temp_dir: TempDir::new()?, + }; + let command = |env: HashMap, working_dir: Option<&str>| RemoteCommand { + program: String::from("agent"), + args: vec![String::from("--token=argument-secret")], + env, + working_dir: working_dir.map(str::to_owned), + }; + let valid_env = + HashMap::from_iter([(String::from("API_KEY"), String::from("agent-secret"))]); + assert_eq!( + connection + .build_stdio_command(command(valid_env.clone(), Some("~/project"))) + .unwrap_err() + .to_string(), + "Remote binary path not set" + ); + + connection.remote_binary_path = Some(Arc::from(RelPath::from_unix_str( + ".local/share/zed/remote server", + )?)); + for (home_dir, working_dir, shell_kind, expected_exec) in [ + ( + None, + Some("~/project"), + ShellKind::Posix, + "cd \"$HOME\"/project && exec \"$HOME\"/'.local/share/zed/remote server' exec", + ), + ( + None, + Some("/srv/project!"), + ShellKind::Tcsh, + "cd '/srv/project'\\! && exec \"$HOME\"/'.local/share/zed/remote server' exec", + ), + ( + Some("/home/user/"), + Some("~/project"), + ShellKind::Posix, + "cd \"$HOME\"/project && exec '/home/user/.local/share/zed/remote server' exec", + ), + ( + Some("/home/bang!user"), + Some("/srv/project!"), + ShellKind::Tcsh, + "cd '/srv/project'\\! && exec '/home/bang'\\!'user/.local/share/zed/remote server' exec", + ), + ( + Some("/home/user/"), + Some("/srv/project 'x'"), + ShellKind::Fish, + "cd \"/srv/project 'x'\" && exec '/home/user/.local/share/zed/remote server' exec", + ), + ( + Some("/home/user/"), + Some("/srv/project"), + ShellKind::Nushell, + "cd /srv/project ; exec '/home/user/.local/share/zed/remote server' exec", + ), + ( + None, + Some("~/project 'x'"), + ShellKind::Nushell, + "cd ($env.HOME | path join \"project 'x'\") ; exec ($env.HOME | path join '.local/share/zed/remote server') exec", + ), + ( + None, + Some("~/"), + ShellKind::Nushell, + "cd $env.HOME ; exec ($env.HOME | path join '.local/share/zed/remote server') exec", + ), + ( + Some("/home/a^b"), + Some("/srv/O'Brien $x"), + ShellKind::Nushell, + "cd \"/srv/O'Brien $x\" ; exec '/home/a^b/.local/share/zed/remote server' exec", + ), + ( + Some("/home/user/"), + None, + ShellKind::Posix, + "cd && exec '/home/user/.local/share/zed/remote server' exec", + ), + ] { + connection.ssh_home_dir = home_dir.map(String::from); + connection.ssh_shell_kind = shell_kind; + let expected_payload = command(valid_env.clone(), None).encode()?; + let mut env = valid_env.clone(); + env.insert(String::from("NAME\0NUL"), String::from("dropped")); + let (template, payload) = connection.build_stdio_command(command(env, working_dir))?; + assert_eq!(template.program, "ssh"); + assert_eq!( + template.args, + [ + "-p", + "2222", + "-o", + "ControlMaster=no", + "-o", + "ControlPath=/tmp/zed-test.sock", + "-o", + "LogLevel=ERROR", + "-T", + "user@host", + expected_exec, + ] + ); + assert_eq!(template.env, connection.socket.envs); + assert_eq!(payload, expected_payload); + } + + connection.ssh_platform.os = RemoteOs::Windows; + connection.ssh_path_style = PathStyle::Windows; + let command = RemoteCommand { + program: String::from("agent"), + args: vec![String::from("--acp")], + env: HashMap::from_iter([(String::from("API_KEY"), String::from("agent-secret"))]), + working_dir: Some(String::from(r"C:\project")), + }; + let expected = connection.build_command( + Some(command.program.clone()), + &command.args, + &HashMap::default(), + command.working_dir.clone(), + None, + Interactive::No, + )?; + let (template, payload) = connection.build_stdio_command(command)?; + assert_eq!(template.program, expected.program); + assert_eq!(template.args, expected.args); + assert_eq!(template.env, expected.env); + assert_eq!(payload, Vec::::new()); + Ok(()) + } + #[test] fn test_build_command() -> Result<()> { let mut input_env = HashMap::default(); diff --git a/crates/remote/src/transport/wsl.rs b/crates/remote/src/transport/wsl.rs index c5010767449..25cce39847c 100644 --- a/crates/remote/src/transport/wsl.rs +++ b/crates/remote/src/transport/wsl.rs @@ -1,7 +1,8 @@ use crate::{ RemoteArch, RemoteClientDelegate, RemoteOs, RemotePlatform, + command::{RemoteCommand, home_stdio_launcher_command}, remote_client::{CommandTemplate, Interactive, RemoteConnection, RemoteConnectionOptions}, - transport::{parse_platform, parse_shell}, + transport::{HOME_DIR_ARGS, HOME_DIR_PROGRAM, parse_home_dir, parse_platform, parse_shell}, }; use anyhow::{Context, Result, anyhow, bail}; use async_trait::async_trait; @@ -57,6 +58,7 @@ pub(crate) struct WslRemoteConnection { shell_kind: ShellKind, default_system_shell: String, has_wsl_interop: bool, + home_dir: Option, connection_options: WslConnectionOptions, } @@ -86,6 +88,7 @@ impl WslRemoteConnection { shell_kind: ShellKind::Posix, default_system_shell: String::from("/bin/sh"), has_wsl_interop: false, + home_dir: None, }; delegate.set_status(Some("Detecting WSL environment"), cx); this.shell = this @@ -110,6 +113,8 @@ impl WslRemoteConnection { log::info!("Remote platform discovered: {:?}", this.platform); this.os_version = this.detect_os_version().await; log::info!("Remote OS version discovered: {:?}", this.os_version); + this.home_dir = this.detect_home_dir().await; + log::info!("Remote home directory discovered: {:?}", this.home_dir); this.remote_binary_path = Some( this.ensure_server_binary(&delegate, release_channel, version, cx) .await @@ -140,6 +145,26 @@ impl WslRemoteConnection { } } + async fn detect_home_dir(&self) -> Option { + let program = self.shell_kind.prepend_command_prefix(HOME_DIR_PROGRAM); + match self + .run_wsl_command_with_output(&program, &HOME_DIR_ARGS) + .await + { + Ok(output) => { + let home_dir = parse_home_dir(&output); + if home_dir.is_none() { + log::warn!("Failed to parse remote home directory from {output:?}"); + } + home_dir + } + Err(error) => { + log::warn!("Failed to determine remote home directory: {error:#}"); + None + } + } + } + async fn detect_shell(&self) -> Result { const DEFAULT_SHELL: &str = "sh"; match self @@ -598,6 +623,47 @@ impl RemoteConnection for WslRemoteConnection { }) } + fn build_stdio_command( + &self, + mut command: RemoteCommand, + ) -> Result<(CommandTemplate, Vec)> { + let remote_binary_path = self + .remote_binary_path + .as_ref() + .context("Remote binary path not set")?; + command.retain_valid_env(); + let startup_dir = command + .working_dir + .take() + .map(|working_dir| RemotePathBuf::new(working_dir, PathStyle::Unix).to_string()) + .unwrap_or_else(|| String::from("~")); + let exec = home_stdio_launcher_command( + self.shell_kind, + self.home_dir.as_deref(), + &remote_binary_path.display(PathStyle::Unix), + )?; + let (shell, shell_args) = + ShellBuilder::new(&Shell::Program(self.shell.clone()), false).build(Some(exec), &[]); + let mut args = vec![ + String::from("--distribution"), + self.connection_options.distro_name.clone(), + ]; + if let Some(user) = &self.connection_options.user { + args.push(String::from("--user")); + args.push(user.clone()); + } + args.extend([String::from("--cd"), startup_dir, String::from("--"), shell]); + args.extend(shell_args); + Ok(( + CommandTemplate { + program: String::from("wsl.exe"), + args, + env: HashMap::default(), + }, + command.encode()?, + )) + } + fn build_forward_ports_command( &self, _: Vec<(u16, String, u16)>, @@ -755,3 +821,174 @@ fn wsl_command_impl( log::debug!("wsl {:?}", command); command } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_build_stdio_command() -> Result<()> { + let mut connection = WslRemoteConnection { + remote_binary_path: None, + platform: RemotePlatform { + os: RemoteOs::Linux, + arch: RemoteArch::X86_64, + }, + os_version: None, + shell: String::from("/bin/fish"), + shell_kind: ShellKind::Fish, + default_system_shell: String::from("/bin/sh"), + has_wsl_interop: false, + home_dir: None, + connection_options: WslConnectionOptions { + distro_name: String::from("Ubuntu"), + user: None, + }, + }; + let command = |env: HashMap, working_dir: Option<&str>| RemoteCommand { + program: String::from("agent"), + args: vec![String::from("--token=argument-secret")], + env, + working_dir: working_dir.map(str::to_owned), + }; + let valid_env = + HashMap::from_iter([(String::from("API_KEY"), String::from("agent-secret"))]); + assert_eq!( + connection + .build_stdio_command(command(valid_env.clone(), Some("~/project"))) + .unwrap_err() + .to_string(), + "Remote binary path not set" + ); + + connection.remote_binary_path = Some(Arc::from(RelPath::from_unix_str( + ".local/share/zed/remote server", + )?)); + for (home_dir, user, working_dir, shell, shell_kind, expected_args) in [ + ( + None, + None, + Some("~/project"), + "/bin/bash", + ShellKind::Posix, + vec![ + "--distribution", + "Ubuntu", + "--cd", + "~/project", + "--", + "/bin/bash", + "-i", + "-c", + "exec \"$HOME\"/'.local/share/zed/remote server' exec", + ], + ), + ( + Some("/home/user"), + None, + Some("/home/user$UNSET/project 'x'"), + "/bin/fish", + ShellKind::Fish, + vec![ + "--distribution", + "Ubuntu", + "--cd", + "/home/user$UNSET/project 'x'", + "--", + "/bin/fish", + "-i", + "-c", + "exec '/home/user/.local/share/zed/remote server' exec", + ], + ), + ( + Some("/home/user"), + Some(String::from("user")), + None, + "/usr/bin/nu", + ShellKind::Nushell, + vec![ + "--distribution", + "Ubuntu", + "--user", + "user", + "--cd", + "~", + "--", + "/usr/bin/nu", + "-i", + "-c", + "exec '/home/user/.local/share/zed/remote server' exec", + ], + ), + ( + Some("/home/a^b"), + None, + None, + "/usr/bin/nu", + ShellKind::Nushell, + vec![ + "--distribution", + "Ubuntu", + "--cd", + "~", + "--", + "/usr/bin/nu", + "-i", + "-c", + "exec '/home/a^b/.local/share/zed/remote server' exec", + ], + ), + ( + None, + None, + Some("~/project"), + "/usr/bin/nu", + ShellKind::Nushell, + vec![ + "--distribution", + "Ubuntu", + "--cd", + "~/project", + "--", + "/usr/bin/nu", + "-i", + "-c", + "exec ($env.HOME | path join '.local/share/zed/remote server') exec", + ], + ), + ( + Some("/home/bang!user"), + None, + None, + "/bin/tcsh", + ShellKind::Tcsh, + vec![ + "--distribution", + "Ubuntu", + "--cd", + "~", + "--", + "/bin/tcsh", + "-i", + "-c", + "exec '/home/bang'\\!'user/.local/share/zed/remote server' exec", + ], + ), + ] { + connection.home_dir = home_dir.map(String::from); + connection.connection_options.user = user; + connection.shell = String::from(shell); + connection.shell_kind = shell_kind; + let expected_payload = command(valid_env.clone(), None).encode()?; + let mut env = valid_env.clone(); + env.insert(String::from("NAME=VALUE"), String::from("dropped")); + let (template, payload) = connection.build_stdio_command(command(env, working_dir))?; + assert_eq!(template.program, "wsl.exe"); + assert_eq!(template.args, expected_args); + assert_eq!(template.env, HashMap::default()); + assert_eq!(payload, expected_payload); + } + Ok(()) + } +} diff --git a/crates/remote_server/Cargo.toml b/crates/remote_server/Cargo.toml index ee62cc76187..30ca632b3f0 100644 --- a/crates/remote_server/Cargo.toml +++ b/crates/remote_server/Cargo.toml @@ -75,6 +75,9 @@ thiserror.workspace = true rayon.workspace = true uuid = { workspace = true, features = ["v4"] } +[target.'cfg(unix)'.dependencies] +libc.workspace = true + [target.'cfg(windows)'.dependencies] windows.workspace = true gpui = { workspace = true, features = ["windows-manifest"] } @@ -84,6 +87,7 @@ acp_thread.workspace = true action_log.workspace = true agent = { workspace = true, features = ["test-support"] } agent-client-protocol.workspace = true +agent_servers.workspace = true client = { workspace = true, features = ["test-support"] } clock = { workspace = true, features = ["test-support"] } editor = { workspace = true, features = ["test-support"] } diff --git a/crates/remote_server/src/main.rs b/crates/remote_server/src/main.rs index 5f0033f1a0a..58123ebb95b 100644 --- a/crates/remote_server/src/main.rs +++ b/crates/remote_server/src/main.rs @@ -56,7 +56,11 @@ fn main() -> anyhow::Result<()> { res } else { std::io::stderr() - .write_all(b"usage: remote \n") + .write_all(if cfg!(unix) { + b"usage: remote \n" + } else { + b"usage: remote \n" + }) .ok(); std::process::exit(1); } diff --git a/crates/remote_server/src/remote_editing_tests.rs b/crates/remote_server/src/remote_editing_tests.rs index 9b0deb09df6..7b34f7ae135 100644 --- a/crates/remote_server/src/remote_editing_tests.rs +++ b/crates/remote_server/src/remote_editing_tests.rs @@ -40,6 +40,11 @@ use lsp::{ LanguageServerId, LanguageServerName, }; use node_runtime::NodeRuntime; +#[cfg(not(windows))] +use project::{ + AgentId, + context_server_store::{ContextServerStatus, ServerStatusChangedEvent}, +}; use project::{ CompletionSource, LanguageServerLogType, ProgressToken, Project, ProjectPath, agent_server_store::AgentServerCommand, @@ -51,6 +56,8 @@ use project::{ search::{SearchQuery, SearchResult}, }; use remote::{ConnectionState, RemoteClient, RemoteClientEvent}; +#[cfg(not(windows))] +use remote::{MOCK_STDIO_FRAME_HEX, command::RemoteCommand}; use rpc::proto; use serde_json::json; use settings::{ @@ -5014,6 +5021,183 @@ async fn test_remote_external_agent_server( ); } +#[cfg(not(windows))] +#[gpui::test] +async fn test_remote_external_agent_receives_env_over_stdin( + cx: &mut TestAppContext, + server_cx: &mut TestAppContext, +) { + let fs = FakeFs::new(server_cx.executor()); + fs.insert_tree(path!("/project"), json!({})).await; + let (project, _headless_project) = init_test(&fs, cx, server_cx).await; + project + .update(cx, |project, cx| { + project.find_or_create_worktree(path!("/project"), true, cx) + }) + .await + .unwrap(); + cx.executor().allow_parking(); + + let output_dir = tempfile::tempdir().unwrap(); + let output_path = output_dir.path().join("launch"); + let args = stdio_launch_args( + &output_path, + r#"{"protocolVersion":1,"agentCapabilities":{},"authMethods":[]}"#, + ); + server_cx.update_global(|settings_store: &mut SettingsStore, cx| { + settings_store + .set_server_settings( + &json!({ + "agent_servers": { + "foo": { + "type": "custom", + "command": "/bin/sh", + "args": args, + "env": { "AGENT_TOKEN": "agent-secret-sentinel" } + } + } + }) + .to_string(), + cx, + ) + .unwrap(); + }); + server_cx.run_until_parked(); + cx.run_until_parked(); + + let agent_server_store = + project.read_with(cx, |project, _| project.agent_server_store().clone()); + let command = agent_server_store + .update(cx, |store, cx| { + store + .get_external_agent(&"foo".into()) + .unwrap() + .get_command(vec![], HashMap::default(), &mut cx.to_async()) + }) + .await + .unwrap(); + let connection = agent_servers::AcpConnection::stdio( + AgentId::new("foo"), + project.clone(), + command, + agent_server_store.downgrade(), + None, + HashMap::default(), + &mut cx.to_async(), + ) + .await + .unwrap(); + + let (launched, first_message) = read_stdio_launch(&output_path); + assert_eq!(launched.program, "/bin/sh"); + assert_eq!(launched.args, args); + assert_eq!( + launched.env, + HashMap::from_iter([ + (String::from("NO_BROWSER"), String::from("1")), + ( + String::from("AGENT_TOKEN"), + String::from("agent-secret-sentinel") + ), + ]) + ); + assert_eq!(launched.working_dir.as_deref(), Some(path!("/project"))); + assert_eq!(first_message["method"], "initialize"); + drop(connection); +} + +#[cfg(not(windows))] +#[gpui::test] +async fn test_remote_context_server_receives_env_over_stdin( + cx: &mut TestAppContext, + server_cx: &mut TestAppContext, +) { + let fs = FakeFs::new(server_cx.executor()); + fs.insert_tree(path!("/project"), json!({})).await; + let (project, _headless_project) = init_test(&fs, cx, server_cx).await; + project + .update(cx, |project, cx| { + project.find_or_create_worktree(path!("/project"), true, cx) + }) + .await + .unwrap(); + cx.executor().allow_parking(); + + let store = project.read_with(cx, |project, _| project.context_server_store()); + let (status_tx, mut status_rx) = futures::channel::mpsc::unbounded(); + let _subscription = cx.update(|cx| { + cx.subscribe(&store, move |_, event: &ServerStatusChangedEvent, _| { + status_tx.unbounded_send(event.status.clone()).ok(); + }) + }); + + let output_dir = tempfile::tempdir().unwrap(); + let output_path = output_dir.path().join("launch"); + let args = stdio_launch_args( + &output_path, + r#"{"protocolVersion":"2024-11-05","capabilities":{},"serverInfo":{"name":"test-server","version":"1.0.0"}}"#, + ); + let settings = json!({ + "context_servers": { + "mcp": { + "source": "custom", + "remote": true, + "command": "/bin/sh", + "args": args, + "env": { "MCP_TOKEN": "mcp-secret-sentinel" } + } + } + }) + .to_string(); + server_cx.update_global(|settings_store: &mut SettingsStore, cx| { + settings_store.set_server_settings(&settings, cx).unwrap(); + }); + server_cx.run_until_parked(); + cx.run_until_parked(); + cx.update_global(|settings_store: &mut SettingsStore, cx| { + settings_store.set_user_settings(&settings, cx).unwrap(); + }); + + let statuses = async { + let mut statuses = Vec::new(); + while let Some(status) = status_rx.next().await { + let starting = status == ContextServerStatus::Starting; + statuses.push(status); + if !starting { + break; + } + } + statuses + } + .fuse(); + let timeout = cx + .background_executor + .timer(std::time::Duration::from_secs(30)) + .fuse(); + futures::pin_mut!(statuses, timeout); + let statuses = futures::select! { + statuses = statuses => statuses, + _ = timeout => panic!("timed out waiting for the context server status"), + }; + assert_eq!( + statuses, + [ContextServerStatus::Starting, ContextServerStatus::Running] + ); + + let (launched, first_message) = read_stdio_launch(&output_path); + assert_eq!(launched.program, "/bin/sh"); + assert_eq!(launched.args, args); + assert_eq!( + launched.env, + HashMap::from_iter([( + String::from("MCP_TOKEN"), + String::from("mcp-secret-sentinel") + )]) + ); + assert_eq!(launched.working_dir.as_deref(), Some(path!("/project"))); + assert_eq!(first_message["method"], "initialize"); +} + #[gpui::test] async fn test_remote_apply_code_action_skips_unadvertised_command( cx: &mut TestAppContext, @@ -6095,6 +6279,39 @@ fn init_logger() { zlog::init_test(); } +#[cfg(not(windows))] +fn stdio_launch_args(output_path: &Path, initialize_result: &str) -> Vec { + let script = format!( + r#"printf '%s\n' "${MOCK_STDIO_FRAME_HEX}" > "$1" +IFS= read -r line +printf '%s\n' "$line" >> "$1" +id=$(printf '%s' "$line" | sed -nE 's/^\{{"jsonrpc":"2.0","id":("[^"]*"|[0-9]+),.*/\1/p') +printf '{{"jsonrpc":"2.0","id":%s,"result":%s}}\n' "$id" "$2" +exec cat >/dev/null"# + ); + vec![ + String::from("-c"), + script, + String::from("sh"), + output_path.display().to_string(), + String::from(initialize_result), + ] +} + +#[cfg(not(windows))] +fn read_stdio_launch(output_path: &Path) -> (RemoteCommand, serde_json::Value) { + let output = std::fs::read_to_string(output_path).unwrap(); + let (frame_hex, first_line) = output.trim_end().split_once('\n').unwrap(); + let frame = (0..frame_hex.len()) + .step_by(2) + .map(|index| u8::from_str_radix(&frame_hex[index..index + 2], 16).unwrap()) + .collect::>(); + let mut frame = &frame[..]; + let command = RemoteCommand::read(&mut frame).unwrap(); + assert_eq!(frame, b""); + (command, serde_json::from_str(first_line).unwrap()) +} + fn build_project(ssh: Entity, cx: &mut TestAppContext) -> Entity { cx.update(|cx| { if !cx.has_global::() { diff --git a/crates/remote_server/src/server.rs b/crates/remote_server/src/server.rs index 2263293899c..bcd3a2a1ead 100644 --- a/crates/remote_server/src/server.rs +++ b/crates/remote_server/src/server.rs @@ -30,6 +30,8 @@ use paths::logs_dir; use project::{project_settings::ProjectSettings, trusted_worktrees}; use proto::CrashReport; use release_channel::{AppCommitSha, AppVersion, RELEASE_CHANNEL, ReleaseChannel}; +#[cfg(unix)] +use remote::command::RemoteCommand; use remote::{ RemoteClient, json_log::LogRecord, @@ -56,6 +58,14 @@ use std::{ sync::{Arc, LazyLock}, time::Instant, }; +#[cfg(unix)] +use std::{ + ffi::OsString, + os::{ + fd::AsFd as _, + unix::{ffi::OsStrExt as _, process::CommandExt as _}, + }, +}; use thiserror::Error; use util::{ResultExt, command::new_command}; @@ -80,6 +90,8 @@ pub enum Commands { identifier: String, }, Version, + #[cfg(unix)] + Exec, } pub fn run(command: Commands) -> anyhow::Result<()> { @@ -104,6 +116,8 @@ pub fn run(command: Commands) -> anyhow::Result<()> { identifier, reconnect, } => execute_proxy(identifier, reconnect).context("running proxy on the remote server"), + #[cfg(unix)] + Commands::Exec => execute_command(), Commands::Version => { let release_channel = *RELEASE_CHANNEL; match release_channel { @@ -139,6 +153,105 @@ pub static VERSION: LazyLock = LazyLock::new(|| match *RELEASE_CHANNEL { } }); +#[cfg(unix)] +fn execute_command() -> Result<()> { + let mut stdin = File::from(std::io::stdin().as_fd().try_clone_to_owned()?); + let descriptor = RemoteCommand::read(&mut stdin)?; + drop(stdin); + + let working_dir = descriptor + .working_dir + .as_deref() + .map(|working_dir| shellexpand::tilde(working_dir).into_owned()); + if let Some(working_dir) = &working_dir { + std::env::set_current_dir(working_dir) + .with_context(|| format!("Failed to change directory to {working_dir}"))?; + } + let error = exec_with_script_fallback(&descriptor, working_dir.as_deref(), |mut command| { + command.exec() + }); + Err(error).with_context(|| format!("Failed to execute {}", descriptor.program)) +} + +#[cfg(unix)] +fn exec_with_script_fallback( + descriptor: &RemoteCommand, + working_dir: Option<&str>, + mut exec: impl FnMut(std::process::Command) -> std::io::Error, +) -> std::io::Error { + let error = exec(exec_command( + descriptor, + working_dir, + &descriptor.program, + &descriptor.args, + )); + if error.raw_os_error() != Some(libc::ENOEXEC) { + return error; + } + let path = descriptor + .env + .get("PATH") + .map(OsString::from) + .or_else(|| std::env::var_os("PATH")); + for candidate in script_candidates(&descriptor.program, path.as_deref()) { + let candidate_error = exec(exec_command( + descriptor, + working_dir, + &candidate, + &descriptor.args, + )); + match candidate_error.raw_os_error() { + Some(libc::ENOEXEC) => { + let args = std::iter::once(candidate.into_os_string()) + .chain(descriptor.args.iter().map(OsString::from)) + .collect::>(); + return exec(exec_command(descriptor, working_dir, "/bin/sh", &args)); + } + Some(libc::ENOENT | libc::ENOTDIR | libc::EACCES) => {} + _ => return candidate_error, + } + } + error +} + +#[cfg(unix)] +fn script_candidates(program: &str, path: Option<&OsStr>) -> Vec { + let explicit = |candidate: PathBuf| { + if candidate.is_absolute() { + candidate + } else { + Path::new(".").join(candidate) + } + }; + if program.contains('/') { + return vec![explicit(PathBuf::from(program))]; + } + path.unwrap_or(OsStr::new(DEFAULT_PATH)) + .as_bytes() + .split(|byte| *byte == b':') + .map(|directory| explicit(Path::new(OsStr::from_bytes(directory)).join(program))) + .collect() +} + +#[cfg(unix)] +const DEFAULT_PATH: &str = "/usr/local/bin:/bin:/usr/bin"; + +#[cfg(unix)] +fn exec_command( + descriptor: &RemoteCommand, + working_dir: Option<&str>, + program: impl AsRef, + args: impl IntoIterator>, +) -> std::process::Command { + let mut command = std::process::Command::new(program); + command.args(args); + if let Some(working_dir) = working_dir { + command.env("PWD", working_dir); + } + command.envs(&descriptor.env); + command +} + fn init_logging_proxy() { env_logger::builder() .format(|buf, record| { @@ -1557,4 +1670,137 @@ mod tests { new_contents ); } + + #[cfg(unix)] + #[test] + fn script_candidates_never_start_with_a_dash() { + assert_eq!( + script_candidates("agent", Some(OsStr::new("/usr/bin:bin::-bin"))), + [ + PathBuf::from("/usr/bin/agent"), + PathBuf::from("./bin/agent"), + PathBuf::from("./agent"), + PathBuf::from("./-bin/agent"), + ] + ); + assert_eq!( + script_candidates("-a", Some(OsStr::new("/opt/bin"))), + [PathBuf::from("/opt/bin/-a")] + ); + assert_eq!( + script_candidates("-x/agent", Some(OsStr::new("/opt/bin"))), + [PathBuf::from("./-x/agent")] + ); + assert_eq!( + script_candidates("/srv/agent", Some(OsStr::new("/opt/bin"))), + [PathBuf::from("/srv/agent")] + ); + assert_eq!( + script_candidates("agent", Some(OsStr::from_bytes(b"/not-utf8-\xff"))), + [PathBuf::from(OsStr::from_bytes(b"/not-utf8-\xff/agent"))] + ); + assert_eq!( + script_candidates("agent", None), + [ + PathBuf::from("/usr/local/bin/agent"), + PathBuf::from("/bin/agent"), + PathBuf::from("/usr/bin/agent"), + ] + ); + } + + #[cfg(unix)] + #[test] + fn script_fallback_runs_option_like_wrapper_instead_of_its_arguments() { + use std::cell::RefCell; + use std::os::unix::fs::PermissionsExt as _; + + let directory = tempfile::tempdir().expect("create temp dir"); + let missing_directory = directory.path().join("missing"); + let binary_directory = directory.path().join("bin"); + std::fs::create_dir(&binary_directory).expect("create bin dir"); + let wrapper = binary_directory.join("-a"); + std::fs::write( + &wrapper, + "printf 'WRAPPER_RAN|%s|%s|%s|%s\\n' \"$TOKEN\" \"$#\" \"$1\" \"$2\"\n", + ) + .expect("write wrapper"); + std::fs::set_permissions(&wrapper, std::fs::Permissions::from_mode(0o755)) + .expect("chmod wrapper"); + let descriptor = RemoteCommand { + program: String::from("-a"), + args: vec![ + String::from("spoofed-argv0"), + String::from("/usr/bin/printf"), + String::from("OPTION_INTERPRETED"), + ], + env: HashMap::from_iter([ + (String::from("TOKEN"), String::from("controlled")), + ( + String::from("PATH"), + format!( + "{}:{}", + missing_directory.display(), + binary_directory.display() + ), + ), + ]), + working_dir: None, + }; + + let attempts = RefCell::new(Vec::new()); + let shell_output = RefCell::new(None); + let error = exec_with_script_fallback(&descriptor, None, |command| { + let program = command.get_program().to_owned(); + if program == "/bin/sh" { + let output = smol::block_on(smol::process::Command::from(command).output()) + .expect("run /bin/sh"); + *shell_output.borrow_mut() = Some(output); + return std::io::Error::from_raw_os_error(libc::EIO); + } + attempts.borrow_mut().push(program.clone()); + if program == "-a" || program == wrapper.as_os_str() { + std::io::Error::from_raw_os_error(libc::ENOEXEC) + } else { + std::io::Error::from_raw_os_error(libc::ENOENT) + } + }); + assert_eq!(error.raw_os_error(), Some(libc::EIO)); + assert_eq!( + attempts.into_inner(), + [ + OsString::from("-a"), + missing_directory.join("-a").into_os_string(), + wrapper.into_os_string(), + ] + ); + let shell_output = shell_output.into_inner().expect("shell fallback ran"); + assert_eq!( + String::from_utf8_lossy(&shell_output.stderr), + "", + "{shell_output:?}" + ); + assert_eq!( + String::from_utf8_lossy(&shell_output.stdout), + "WRAPPER_RAN|controlled|3|spoofed-argv0|/usr/bin/printf\n" + ); + assert_eq!(shell_output.status.code(), Some(0)); + + let attempts = RefCell::new(0); + let error = exec_with_script_fallback(&descriptor, None, |_| { + *attempts.borrow_mut() += 1; + std::io::Error::from_raw_os_error(libc::EACCES) + }); + assert_eq!(error.raw_os_error(), Some(libc::EACCES)); + assert_eq!(attempts.into_inner(), 1); + + let error = exec_with_script_fallback(&descriptor, None, |command| { + if command.get_program() == "-a" { + std::io::Error::from_raw_os_error(libc::ENOEXEC) + } else { + std::io::Error::from_raw_os_error(libc::ELOOP) + } + }); + assert_eq!(error.raw_os_error(), Some(libc::ELOOP)); + } } diff --git a/crates/remote_server/tests/command.rs b/crates/remote_server/tests/command.rs new file mode 100644 index 00000000000..d77c9747abe --- /dev/null +++ b/crates/remote_server/tests/command.rs @@ -0,0 +1,776 @@ +#![cfg(unix)] + +use anyhow::{Context as _, Result}; +use collections::HashMap; +use futures::{AsyncReadExt as _, AsyncWriteExt as _}; +use remote::command::{ + RemoteCommand, home_relative_path, home_stdio_launcher_command, stdio_launcher_command, +}; +use smol::process::{Command, Stdio}; +use std::borrow::Cow; +use std::ffi::OsStr; +use std::os::unix::ffi::OsStrExt as _; +use std::os::unix::fs::PermissionsExt as _; +use util::shell::{Shell, ShellKind}; +use util::shell_builder::ShellBuilder; + +#[test] +fn test_remote_command_preserves_agent_stdin() -> Result<()> { + smol::block_on(async { + let descriptor = RemoteCommand { + program: String::from("/bin/cat"), + args: Vec::new(), + env: HashMap::default(), + working_dir: None, + }; + let mut child = launcher().spawn()?; + let mut stdin = child.stdin.take().context("missing stdin")?; + let mut stdout = child.stdout.take().context("missing stdout")?; + let message = b"{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"initialize\"}\n\0\xff"; + let mut input = descriptor.encode()?; + input.extend_from_slice(message); + stdin.write_all(&input).await?; + stdin.flush().await?; + let mut output = vec![0; message.len()]; + stdout.read_exact(&mut output).await?; + assert_eq!(output, message); + drop(stdin); + let output = child.output().await?; + assert!(output.status.success()); + assert_eq!(output.stderr, b""); + Ok(()) + }) +} + +#[test] +fn test_remote_command_preserves_environment() -> Result<()> { + smol::block_on(async { + let directory = tempfile::tempdir()?; + let marker = directory.path().join("injected"); + let environment = HashMap::from_iter([ + ( + String::from("TOKEN"), + String::from("'\"\n\r\t\\$()=secret\n"), + ), + (String::from("EMPTY"), String::new()), + (String::from("UNICODE"), String::from("钥匙🔑")), + (String::from("LARGE"), "s".repeat(32 * 1024)), + ( + format!("KEY$(touch {})", marker.display()), + String::from("literal"), + ), + (String::from("KEY-WITH-DASH"), String::from("preserved")), + ]); + for (key, value) in &environment { + let descriptor = RemoteCommand { + program: String::from("/usr/bin/printenv"), + args: vec![key.clone()], + env: environment.clone(), + working_dir: None, + }; + let mut child = launcher().spawn()?; + child + .stdin + .take() + .context("missing stdin")? + .write_all(&descriptor.encode()?) + .await?; + let output = child.output().await?; + assert!(output.status.success(), "{key}: {:?}", output.stderr); + assert_eq!(output.stdout, format!("{value}\n").as_bytes()); + assert_eq!(output.stderr, b""); + } + assert!(!marker.exists()); + Ok(()) + }) +} + +#[test] +fn test_remote_command_working_directory_and_exit_status() -> Result<()> { + smol::block_on(async { + let home = tempfile::tempdir()?; + let project = home.path().join("project 'with spaces'"); + std::fs::create_dir(&project)?; + for (env, expected_pwd) in [ + (HashMap::default(), project.display().to_string()), + ( + HashMap::from_iter([(String::from("PWD"), String::from("explicit"))]), + String::from("explicit"), + ), + ] { + let descriptor = RemoteCommand { + program: String::from("/usr/bin/printenv"), + args: vec![String::from("PWD")], + env, + working_dir: Some(String::from("~/project 'with spaces'")), + }; + let mut child = launcher().env("HOME", home.path()).spawn()?; + child + .stdin + .take() + .context("missing stdin")? + .write_all(&descriptor.encode()?) + .await?; + let output = child.output().await?; + assert!(output.status.success(), "{:?}", output.stderr); + assert_eq!(output.stdout, format!("{expected_pwd}\n").as_bytes()); + } + let descriptor = RemoteCommand { + program: String::from("/bin/sh"), + args: vec![String::from("-c"), String::from("pwd -P; exit 37")], + env: HashMap::from_iter([( + String::from("HOME"), + String::from("/not-the-bootstrap-home"), + )]), + working_dir: Some(String::from("~/project 'with spaces'")), + }; + let mut child = launcher().env("HOME", home.path()).spawn()?; + child + .stdin + .take() + .context("missing stdin")? + .write_all(&descriptor.encode()?) + .await?; + let output = child.output().await?; + assert_eq!(output.status.code(), Some(37)); + assert_eq!( + output.stdout, + format!("{}\n", project.canonicalize()?.display()).as_bytes() + ); + assert_eq!(output.stderr, b""); + Ok(()) + }) +} + +#[test] +fn test_remote_command_inherits_shell_environment() -> Result<()> { + smol::block_on(async { + let home = tempfile::tempdir()?; + let shell_only_binary = home.path().join("shell-only-printenv"); + std::os::unix::fs::symlink("/usr/bin/printenv", &shell_only_binary)?; + let home_path = home.path().display().to_string(); + let quoted_home = ShellKind::Posix + .try_quote(&home_path) + .context("shell quoting")?; + std::fs::write( + home.path().join(".bashrc"), + format!( + "export PATH={quoted_home}:/usr/bin:/bin\nexport SHELL_SECRET=only-in-shell-startup\n" + ), + )?; + for (program, env, expected) in [ + ( + "shell-only-printenv", + HashMap::default(), + "only-in-shell-startup", + ), + ( + "/usr/bin/printenv", + HashMap::from_iter([(String::from("SHELL_SECRET"), String::from("from-payload"))]), + "from-payload", + ), + ] { + let descriptor = RemoteCommand { + program: String::from(program), + args: vec![String::from("SHELL_SECRET")], + env, + working_dir: None, + }; + let exec = + stdio_launcher_command(ShellKind::Posix, env!("CARGO_BIN_EXE_remote_server"))?; + let mut child = shell_launcher(home.path(), &exec).spawn()?; + child + .stdin + .take() + .context("missing stdin")? + .write_all(&descriptor.encode()?) + .await?; + let output = child.output().await?; + assert!(output.status.success(), "{program}: {:?}", output.stderr); + assert_eq!(output.stdout, format!("{expected}\n").as_bytes()); + } + Ok(()) + }) +} + +#[test] +fn test_remote_command_inherits_default_shell_startup_environment() -> Result<()> { + smol::block_on(async { + let home = tempfile::tempdir()?; + let helper = install_helper(home.path())?; + let startup_script = home.path().join("startup env"); + std::fs::write( + &startup_script, + "export STARTUP_TOKEN=only-in-default-shell-startup\n", + )?; + let descriptor = RemoteCommand { + program: String::from("/usr/bin/printenv"), + args: vec![String::from("STARTUP_TOKEN")], + env: HashMap::default(), + working_dir: None, + }; + let (inner_shell, inner_args) = + ShellBuilder::new(&Shell::Program(String::from("/bin/bash")), false).build( + Some(stdio_launcher_command(ShellKind::Posix, &helper)?), + &[], + ); + let default_shell_command = std::iter::once(inner_shell) + .chain(inner_args) + .map(|argument| { + ShellKind::Posix + .try_quote(&argument) + .map(Cow::into_owned) + .context("shell quoting") + }) + .collect::>>()? + .join(" "); + let mut child = Command::new("/bin/bash") + .args(["-c", &default_shell_command]) + .env_clear() + .env("HOME", home.path()) + .env("PATH", "/usr/bin:/bin") + .env("BASH_ENV", &startup_script) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn()?; + child + .stdin + .take() + .context("missing stdin")? + .write_all(&descriptor.encode()?) + .await?; + let output = child.output().await?; + assert!(output.status.success(), "{:?}", output.stderr); + assert_eq!(output.stdout, b"only-in-default-shell-startup\n"); + Ok(()) + }) +} + +#[test] +fn test_remote_command_survives_shell_startup_changing_directory() -> Result<()> { + smol::block_on(async { + let home = tempfile::tempdir()?; + let project = home.path().join("project"); + std::fs::create_dir(&project)?; + let helper = install_helper(home.path())?; + std::fs::write( + home.path().join(".bashrc"), + "cd /tmp\nexport HOME=/not-the-real-home\n", + )?; + let descriptor = RemoteCommand { + program: String::from("/bin/pwd"), + args: vec![String::from("-P")], + env: HashMap::default(), + working_dir: Some(project.display().to_string()), + }; + let exec = stdio_launcher_command(ShellKind::Posix, &helper)?; + let mut child = shell_launcher(home.path(), &exec) + .current_dir(&project) + .spawn()?; + child + .stdin + .take() + .context("missing stdin")? + .write_all(&descriptor.encode()?) + .await?; + let output = child.output().await?; + assert!(output.status.success(), "{:?}", output.stderr); + assert_eq!( + output.stdout, + format!("{}\n", project.canonicalize()?.display()).as_bytes() + ); + Ok(()) + }) +} + +#[test] +fn test_remote_command_runs_shell_directory_hooks() -> Result<()> { + smol::block_on(async { + let home = tempfile::tempdir()?; + let project = home.path().join("project 'with spaces'"); + let application = project.join("application"); + std::fs::create_dir_all(&application)?; + let helper = install_helper(home.path())?; + let project_path = project.display().to_string(); + let quoted_project = ShellKind::Posix + .try_quote(&project_path) + .context("shell quoting")?; + std::fs::write( + home.path().join(".bashrc"), + format!( + "cd() {{ builtin cd \"$@\" || return; if [ \"$PWD\" = {quoted_project} ]; then export PROJECT_SECRET=project-only; builtin cd application; else unset PROJECT_SECRET; fi; }}\n" + ), + )?; + for (working_dir, expected) in [ + ( + Some(project_path.clone()), + format!("project-only\n{}\n", application.canonicalize()?.display()), + ), + ( + None, + format!("\n{}\n", home.path().canonicalize()?.display()), + ), + ] { + let descriptor = RemoteCommand { + program: String::from("/bin/sh"), + args: vec![ + String::from("-c"), + String::from("printf '%s\\n%s\\n' \"$PROJECT_SECRET\" \"$(pwd -P)\""), + ], + env: HashMap::default(), + working_dir: None, + }; + let prefix = match &working_dir { + Some(working_dir) => format!( + "cd {} && ", + ShellKind::Posix + .try_quote(working_dir) + .context("shell quoting")? + ), + None => String::from("cd && "), + }; + let exec = format!( + "{prefix}{}", + stdio_launcher_command(ShellKind::Posix, &helper)? + ); + let mut child = shell_launcher(home.path(), &exec) + .current_dir(home.path()) + .spawn()?; + child + .stdin + .take() + .context("missing stdin")? + .write_all(&descriptor.encode()?) + .await?; + let output = child.output().await?; + assert!( + output.status.success(), + "{working_dir:?}: {:?}", + output.stderr + ); + assert_eq!(output.stdout, expected.as_bytes(), "{working_dir:?}"); + } + Ok(()) + }) +} + +#[test] +fn test_remote_command_runs_scripts_without_shebang() -> Result<()> { + smol::block_on(async { + let directory = tempfile::tempdir()?; + let binary_directory = directory.path().join("bin"); + std::fs::create_dir(&binary_directory)?; + let script = binary_directory.join("agent-wrapper"); + for script in [&script, &binary_directory.join("-a")] { + std::fs::write( + script, + "printf '%s|%s|%s\\n' \"$WRAPPER_TOKEN\" \"$1\" \"$(pwd -P)\"\n", + )?; + std::fs::set_permissions(script, std::fs::Permissions::from_mode(0o755))?; + } + let stale_directory = directory.path().join("stale"); + std::fs::create_dir(&stale_directory)?; + let stale_script = stale_directory.join("agent-wrapper"); + std::fs::write( + &stale_script, + "#!/nonexistent/interpreter\nprintf 'stale\\n'\n", + )?; + std::fs::set_permissions(&stale_script, std::fs::Permissions::from_mode(0o755))?; + let expected = format!( + "wrapper-secret|--acp|{}\n", + directory.path().canonicalize()?.display() + ); + for program in [ + script.display().to_string(), + String::from("agent-wrapper"), + String::from("-a"), + ] { + let descriptor = RemoteCommand { + program, + args: vec![String::from("--acp")], + env: HashMap::from_iter([ + ( + String::from("WRAPPER_TOKEN"), + String::from("wrapper-secret"), + ), + ( + String::from("PATH"), + format!( + "{}:{}:/usr/bin:/bin", + stale_directory.display(), + binary_directory.display() + ), + ), + ]), + working_dir: Some(directory.path().display().to_string()), + }; + let mut child = launcher().env("PATH", "/usr/bin:/bin").spawn()?; + child + .stdin + .take() + .context("missing stdin")? + .write_all(&descriptor.encode()?) + .await?; + let output = child.output().await?; + assert!( + output.status.success(), + "{}: {:?}", + descriptor.program, + output.stderr + ); + assert_eq!(output.stdout, expected.as_bytes(), "{}", descriptor.program); + } + Ok(()) + }) +} + +#[test] +fn test_remote_command_matches_native_path_lookup() -> Result<()> { + smol::block_on(async { + let directory = tempfile::tempdir()?; + let binary_directory = directory.path().join("bin"); + std::fs::create_dir(&binary_directory)?; + let agent = binary_directory.join("agent"); + std::fs::write(&agent, "#!/bin/sh\nprintf 'found\\n'\n")?; + std::fs::set_permissions(&agent, std::fs::Permissions::from_mode(0o755))?; + let loop_directory = directory.path().join("loop"); + std::fs::create_dir(&loop_directory)?; + std::os::unix::fs::symlink("agent", loop_directory.join("agent"))?; + let binary_directory = binary_directory.as_os_str().as_bytes(); + let overlong_directory = [ + directory.path().as_os_str().as_bytes(), + b"/", + &[b'x'; 300][..], + ] + .concat(); + for (label, path, must_succeed) in [ + ( + "non-UTF-8 entry", + [&b"/not-present-\xff:"[..], binary_directory].concat(), + true, + ), + ( + "symlink loop", + [ + loop_directory.as_os_str().as_bytes(), + b":", + binary_directory, + ] + .concat(), + false, + ), + ( + "overlong entry", + [&overlong_directory[..], b":", binary_directory].concat(), + false, + ), + ] { + let path = OsStr::from_bytes(&path); + let native = Command::new("/usr/bin/env") + .arg("agent") + .env_clear() + .env("PATH", path) + .output() + .await?; + let descriptor = RemoteCommand { + program: String::from("agent"), + args: Vec::new(), + env: HashMap::default(), + working_dir: None, + }; + let mut child = launcher().env_clear().env("PATH", path).spawn()?; + child + .stdin + .take() + .context("missing stdin")? + .write_all(&descriptor.encode()?) + .await?; + let output = child.output().await?; + assert_eq!( + (output.status.success(), output.stdout.clone()), + (native.status.success(), native.stdout), + "{label}: {:?}", + output.stderr + ); + if must_succeed { + assert_eq!(output.stdout, b"found\n", "{label}"); + } + } + Ok(()) + }) +} + +#[test] +fn test_remote_command_launcher_expands_home_in_shell() -> Result<()> { + smol::block_on(async { + let home = tempfile::tempdir()?; + install_helper(home.path())?; + std::fs::write(home.path().join(".bashrc"), "cd /tmp\n")?; + let descriptor = RemoteCommand { + program: String::from("/usr/bin/printenv"), + args: vec![String::from("LINE\nBREAK")], + env: HashMap::from_iter([(String::from("LINE\nBREAK"), String::from("kept"))]), + working_dir: None, + }; + let exec = format!( + "cd && {}", + home_stdio_launcher_command(ShellKind::Posix, None, ".local/share/zed/remote server")? + ); + assert_eq!( + exec, + "cd && exec \"$HOME\"/'.local/share/zed/remote server' exec" + ); + let mut child = shell_launcher(home.path(), &exec).spawn()?; + child + .stdin + .take() + .context("missing stdin")? + .write_all(&descriptor.encode()?) + .await?; + let output = child.output().await?; + assert!(output.status.success(), "{:?}", output.stderr); + assert_eq!(output.stdout, b"kept\n"); + Ok(()) + }) +} + +#[test] +fn test_remote_command_launcher_quoting_in_tcsh() -> Result<()> { + let Some(tcsh) = ["/bin/tcsh", "/usr/bin/tcsh"] + .into_iter() + .find(|tcsh| std::path::Path::new(tcsh).exists()) + else { + return Ok(()); + }; + smol::block_on(async { + let directory = tempfile::tempdir()?; + let home = directory.path().join("bang!home"); + let project = home.join("O'Brien !1 project\\'; /usr/bin/printf INJECTED; true \\'"); + std::fs::create_dir_all(&project)?; + let helper = install_helper(&home)?; + let descriptor = RemoteCommand { + program: String::from("/bin/sh"), + args: vec![ + String::from("-c"), + String::from("printf '%s\\n%s\\n' \"$AGENT_TOKEN\" \"$(pwd -P)\""), + ], + env: HashMap::from_iter([(String::from("AGENT_TOKEN"), String::from("secret!"))]), + working_dir: None, + }; + let expected = format!("secret!\n{}\n", project.canonicalize()?.display()); + let project_path = project.display().to_string(); + for setting in [ + "", + "set backslash_quote\n", + "set histchars = ''\n", + "set backslash_quote\nset histchars = ''\n", + ] { + let exec = format!( + "{setting}cd {} && {}", + ShellKind::Tcsh + .try_quote(&project_path) + .context("shell quoting")?, + stdio_launcher_command(ShellKind::Tcsh, &helper)? + ); + let remote_shell = [String::from("-f"), String::from("-c"), exec.clone()]; + let direct = { + let mut command = Command::new(tcsh); + command.args(&remote_shell); + command + }; + let through_local_shell = Command::from( + ShellBuilder::new(&Shell::Program(String::from(tcsh)), false) + .non_interactive() + .build_std_command(Some(String::from(tcsh)), &remote_shell), + ); + for mut command in [direct, through_local_shell] { + let mut child = command + .env_clear() + .env("HOME", &home) + .env("PATH", "/usr/bin:/bin") + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn()?; + child + .stdin + .take() + .context("missing stdin")? + .write_all(&descriptor.encode()?) + .await?; + let output = child.output().await?; + assert!(output.status.success(), "{exec}: {:?}", output.stderr); + assert_eq!(String::from_utf8_lossy(&output.stdout), expected, "{exec}"); + assert_eq!(output.stderr, b""); + } + } + Ok(()) + }) +} + +#[test] +fn test_remote_command_launcher_expands_home_in_nushell() -> Result<()> { + let Some(nu) = ["/opt/homebrew/bin/nu", "/usr/local/bin/nu", "/usr/bin/nu"] + .into_iter() + .find(|nu| std::path::Path::new(nu).exists()) + else { + return Ok(()); + }; + smol::block_on(async { + let directory = tempfile::tempdir()?; + let home = directory.path().join("home a^b"); + let project = home.join("O'Brien project"); + std::fs::create_dir_all(&project)?; + install_helper(&home)?; + let descriptor = RemoteCommand { + program: String::from("/bin/sh"), + args: vec![ + String::from("-c"), + String::from("printf '%s\\n%s\\n' \"$AGENT_TOKEN\" \"$(pwd -P)\""), + ], + env: HashMap::from_iter([(String::from("AGENT_TOKEN"), String::from("secret!"))]), + working_dir: None, + }; + let expected = format!("secret!\n{}\n", project.canonicalize()?.display()); + let project_path = project.display().to_string(); + for (working_dir, home_dir) in [ + (Some("~/O'Brien project"), None), + (Some(project_path.as_str()), None), + (Some("~/O'Brien project"), Some(home.display().to_string())), + ] { + let exec = format!( + "cd {} {} {}", + match working_dir.and_then(|working_dir| working_dir.strip_prefix("~/")) { + Some(remainder) => home_relative_path(ShellKind::Nushell, Some(remainder))?, + None => ShellKind::Nushell + .try_quote(working_dir.context("working dir")?) + .context("shell quoting")? + .into_owned(), + }, + ShellKind::Nushell.sequential_and_commands_separator(), + home_stdio_launcher_command( + ShellKind::Nushell, + home_dir.as_deref(), + ".local/share/zed/remote server" + )? + ); + let mut child = Command::new(nu) + .args(["--no-config-file", "-c", &exec]) + .env_clear() + .env("HOME", &home) + .env("PATH", "/usr/bin:/bin") + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn()?; + child + .stdin + .take() + .context("missing stdin")? + .write_all(&descriptor.encode()?) + .await?; + let output = child.output().await?; + assert!(output.status.success(), "{exec}: {:?}", output.stderr); + assert_eq!(String::from_utf8_lossy(&output.stdout), expected, "{exec}"); + assert_eq!(output.stderr, b""); + } + Ok(()) + }) +} + +#[test] +fn test_remote_command_launcher_survives_local_nushell() -> Result<()> { + let Some(nu) = ["/opt/homebrew/bin/nu", "/usr/local/bin/nu", "/usr/bin/nu"] + .into_iter() + .find(|nu| std::path::Path::new(nu).exists()) + else { + return Ok(()); + }; + smol::block_on(async { + let directory = tempfile::tempdir()?; + let home = directory.path().join("home$REMOTE_PART"); + let project = home.join("project"); + std::fs::create_dir_all(&project)?; + install_helper(&home)?; + let descriptor = RemoteCommand { + program: String::from("/bin/sh"), + args: vec![ + String::from("-c"), + String::from("printf '%s\\n%s\\n' \"$AGENT_TOKEN\" \"$(pwd -P)\""), + ], + env: HashMap::from_iter([(String::from("AGENT_TOKEN"), String::from("secret"))]), + working_dir: None, + }; + let expected = format!("secret\n{}\n", project.canonicalize()?.display()); + for home_dir in [None, Some(home.display().to_string())] { + let exec = format!( + "cd {} && {}", + home_relative_path(ShellKind::Posix, Some("project"))?, + home_stdio_launcher_command( + ShellKind::Posix, + home_dir.as_deref(), + ".local/share/zed/remote server" + )? + ); + let remote_shell = [String::from("-c"), exec.clone()]; + let mut child = Command::from( + ShellBuilder::new(&Shell::Program(String::from(nu)), false) + .non_interactive() + .literal_args() + .build_std_command(Some(String::from("/bin/bash")), &remote_shell), + ) + .env_clear() + .env("HOME", &home) + .env("PATH", "/usr/bin:/bin") + .current_dir(directory.path()) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn()?; + child + .stdin + .take() + .context("missing stdin")? + .write_all(&descriptor.encode()?) + .await?; + let output = child.output().await?; + assert!(output.status.success(), "{exec}: {:?}", output.stderr); + assert_eq!(String::from_utf8_lossy(&output.stdout), expected, "{exec}"); + assert_eq!(output.stderr, b""); + } + Ok(()) + }) +} + +fn launcher() -> Command { + let mut command = Command::new(env!("CARGO_BIN_EXE_remote_server")); + command + .arg("exec") + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()); + command +} + +fn install_helper(home: &std::path::Path) -> Result { + let helper_directory = home.join(".local/share/zed"); + std::fs::create_dir_all(&helper_directory)?; + let helper = helper_directory.join("remote server"); + std::os::unix::fs::symlink(env!("CARGO_BIN_EXE_remote_server"), &helper)?; + Ok(helper.display().to_string()) +} + +fn shell_launcher(home: &std::path::Path, exec: &str) -> Command { + let mut command = Command::new("/bin/bash"); + command + .args(["-i", "-c", exec]) + .env_clear() + .env("HOME", home) + .env("PATH", "/usr/bin:/bin") + .env("TERM", "dumb") + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()); + command +} diff --git a/crates/util/Cargo.toml b/crates/util/Cargo.toml index 1db0915340f..571f21a4d86 100644 --- a/crates/util/Cargo.toml +++ b/crates/util/Cargo.toml @@ -72,3 +72,6 @@ rand.workspace = true tempfile.workspace = true util_macros.workspace = true path = { workspace = true, features = ["test-support"] } + +[target.'cfg(unix)'.dev-dependencies] +which.workspace = true diff --git a/crates/util/src/shell.rs b/crates/util/src/shell.rs index 5b91ec575a0..1c38707e7b2 100644 --- a/crates/util/src/shell.rs +++ b/crates/util/src/shell.rs @@ -438,12 +438,11 @@ impl ShellKind { ShellKind::PowerShell => Some(Self::quote_powershell(arg)), ShellKind::Pwsh => Some(Self::quote_pwsh(arg)), ShellKind::Cmd => Some(Self::quote_cmd(arg)), + ShellKind::Csh | ShellKind::Tcsh => Self::quote_csh(arg), + ShellKind::Nushell => Self::quote_nushell(arg), ShellKind::Posix - | ShellKind::Csh - | ShellKind::Tcsh | ShellKind::Rc | ShellKind::Fish - | ShellKind::Nushell | ShellKind::Xonsh | ShellKind::Elvish => shlex::try_quote(arg).ok(), } @@ -585,6 +584,75 @@ impl ShellKind { Cow::Owned(Self::escape_powershell_quotes(arg)) } + pub fn quote_csh(arg: &str) -> Option> { + if arg.contains('\0') { + return None; + } + if Self::is_unquoted_safe(arg) && !arg.starts_with('%') { + return Some(Cow::Borrowed(arg)); + } + if arg.is_empty() { + return Some(Cow::Borrowed("''")); + } + let mut quoted = String::with_capacity(arg.len() + 2); + let mut in_quotes = false; + for character in arg.chars() { + match character { + '\'' | '!' | '\\' => { + if in_quotes { + quoted.push('\''); + in_quotes = false; + } + quoted.push('\\'); + quoted.push(character); + } + character => { + if !in_quotes { + quoted.push('\''); + in_quotes = true; + } + if character == '\n' { + quoted.push('\\'); + } + quoted.push(character); + } + } + } + if in_quotes { + quoted.push('\''); + } + Some(Cow::Owned(quoted)) + } + + pub fn quote_nushell(arg: &str) -> Option> { + if arg.contains('\0') { + return None; + } + if Self::is_unquoted_safe(arg) { + return Some(Cow::Borrowed(arg)); + } + if !arg.contains('\'') { + return Some(Cow::Owned(format!("'{arg}'"))); + } + let mut quoted = String::with_capacity(arg.len() + 2); + quoted.push('"'); + for character in arg.chars() { + if character == '\\' || character == '"' { + quoted.push('\\'); + } + quoted.push(character); + } + quoted.push('"'); + Some(Cow::Owned(quoted)) + } + + fn is_unquoted_safe(arg: &str) -> bool { + !arg.is_empty() + && arg + .bytes() + .all(|byte| byte.is_ascii_alphanumeric() || b"-_./:,+@%".contains(&byte)) + } + pub fn quote_cmd(arg: &str) -> Cow<'_, str> { let crt_quoted = Self::quote_windows(arg, true); @@ -707,6 +775,8 @@ impl ShellKind { #[cfg(test)] mod tests { use super::*; + #[cfg(unix)] + use crate::shell_builder::ShellBuilder; // Examples // WSL @@ -875,7 +945,7 @@ mod tests { ); assert_eq!( shell_kind.try_quote("^'uname'").unwrap().into_owned(), - "'^'\"'uname\'\"".to_string() + "\"^'uname'\"".to_string() ); assert_eq!( shell_kind @@ -897,7 +967,7 @@ mod tests { ); assert_eq!( shell_kind.try_quote("^'uname a'").unwrap().into_owned(), - "'^'\"'uname a'\"".to_string() + "\"^'uname a'\"".to_string() ); assert_eq!( shell_kind @@ -919,6 +989,236 @@ mod tests { ); } + #[test] + fn test_try_quote_csh() { + for shell_kind in [ShellKind::Csh, ShellKind::Tcsh] { + assert_eq!(shell_kind.try_quote("plain").unwrap(), "plain"); + assert_eq!( + shell_kind.try_quote("--flag=a/b.c").unwrap(), + "'--flag=a/b.c'" + ); + assert_eq!(shell_kind.try_quote("=0").unwrap(), "'=0'"); + assert_eq!(shell_kind.try_quote("%agent").unwrap(), "'%agent'"); + assert_eq!(shell_kind.try_quote("50%").unwrap(), "50%"); + assert_eq!(shell_kind.try_quote("").unwrap(), "''"); + assert_eq!( + shell_kind.try_quote("/home/bang!home/x").unwrap(), + "'/home/bang'\\!'home/x'" + ); + assert_eq!(shell_kind.try_quote("a\\!b c").unwrap(), "'a'\\\\\\!'b c'"); + assert_eq!( + shell_kind.try_quote("O'Brien!").unwrap(), + "'O'\\''Brien'\\!" + ); + assert_eq!(shell_kind.try_quote("!").unwrap(), "\\!"); + assert_eq!(shell_kind.try_quote("\\'").unwrap(), "\\\\\\'"); + assert_eq!( + shell_kind + .try_quote("/tmp/a\\'; echo INJECTED; true \\'") + .unwrap(), + "'/tmp/a'\\\\\\''; echo INJECTED; true '\\\\\\'" + ); + assert_eq!( + shell_kind.try_quote("say \\\"hi\\\"").unwrap(), + "'say '\\\\'\"hi'\\\\'\"'" + ); + assert_eq!( + shell_kind.try_quote("line\nbreak").unwrap(), + "'line\\\nbreak'" + ); + assert_eq!( + shell_kind.try_quote("line\rbreak").unwrap(), + "'line\rbreak'" + ); + assert_eq!( + shell_kind.try_quote("~/*$HOME `id`").unwrap(), + "'~/*$HOME `id`'" + ); + assert_eq!(shell_kind.try_quote("nul\0byte"), None); + } + } + + #[test] + fn test_try_quote_nushell() { + let shell_kind = ShellKind::Nushell; + assert_eq!(shell_kind.try_quote("plain").unwrap(), "plain"); + assert_eq!(shell_kind.try_quote("").unwrap(), "''"); + assert_eq!(shell_kind.try_quote("=").unwrap(), "'='"); + assert_eq!(shell_kind.try_quote("=a").unwrap(), "'=a'"); + assert_eq!(shell_kind.try_quote("+=").unwrap(), "'+='"); + assert_eq!( + shell_kind.try_quote("--flag=a/b.c").unwrap(), + "'--flag=a/b.c'" + ); + assert_eq!( + shell_kind + .try_quote("/home/a^b/.zed_server/server") + .unwrap(), + "'/home/a^b/.zed_server/server'" + ); + assert_eq!( + shell_kind.try_quote("$env.HOME (1 + 1) `id` a\\b").unwrap(), + "'$env.HOME (1 + 1) `id` a\\b'" + ); + assert_eq!( + shell_kind.try_quote("O'Brien \\ \"x\" $y").unwrap(), + "\"O'Brien \\\\ \\\"x\\\" $y\"" + ); + assert_eq!(shell_kind.try_quote("nul\0byte"), None); + } + + #[cfg(unix)] + #[test] + fn test_quoted_arguments_survive_real_shells() { + let arguments = [ + "SAFE; /usr/bin/printf INJECTED\r", + "/tmp/a\\'; /usr/bin/printf INJECTED; true \\'", + "a\\\\b \\\"c\\\" \\'d\\' \\$e \\!f", + "line\nbreak", + "bang!\nnew\\line", + "O'Brien!", + "a\\!b", + "a\\\\!b", + "!1x", + "!", + "!!", + "!$ !* !:1 !-1 !# !?x?", + "ending!", + "trailing\\", + "~/*$HOME `id` $(id) (1 + 1) \"quoted\"", + "\t tab # hash", + "=", + "=a", + "=0", + "=1", + "+=", + "-=", + "/=", + "a=b", + "--flag=a/b.c", + "1..3", + "/home/a^b/.zed_server/server", + "钥匙🔑", + "", + ]; + let csh_settings = [ + "", + "set backslash_quote\n", + "set histchars = ''\n", + "set histchars = \\!^\n", + "set backslash_quote\nset histchars = ''\n", + "set backslash_quote\nunset histchars\nset history = 100\n", + ]; + let shells = [ + ("tcsh", ShellKind::Tcsh, &["-f"][..], &csh_settings[..]), + ("csh", ShellKind::Csh, &["-f"][..], &csh_settings[..]), + ( + "nu", + ShellKind::Nushell, + &["--no-config-file"][..], + &[""][..], + ), + ]; + for (shell_name, shell_kind, shell_options, settings) in shells { + let Ok(shell) = which::which(shell_name) else { + continue; + }; + let shell = shell.to_string_lossy().into_owned(); + let separator = shell_kind.sequential_and_commands_separator(); + let printf = shell_kind.prepend_command_prefix("/usr/bin/printf"); + for (setting, argument) in settings + .iter() + .flat_map(|setting| arguments.iter().map(move |argument| (*setting, *argument))) + { + let quoted = shell_kind.try_quote(argument).unwrap(); + let inner = format!("{setting}cd /{separator} {printf} %s {quoted}"); + let mut inner_arguments = shell_options.to_vec(); + inner_arguments.extend(["-c", &inner]); + let nested = format!( + "{setting}{} {}", + shell_kind.prepend_command_prefix(&shell), + inner_arguments + .iter() + .map(|inner_argument| shell_kind.try_quote(inner_argument).unwrap()) + .collect::>() + .join(" ") + ); + for script in [inner.clone(), nested] { + let output = smol::block_on( + smol::process::Command::new(&shell) + .args(shell_options) + .args(["-c", &script]) + .env_clear() + .env("HOME", "/") + .env("PATH", "/usr/bin:/bin") + .output(), + ) + .unwrap(); + assert_eq!( + ( + output.status.success(), + String::from_utf8_lossy(&output.stdout).into_owned(), + String::from_utf8_lossy(&output.stderr).into_owned(), + ), + (true, argument.to_owned(), String::new()), + "{shell} {script:?}" + ); + } + } + } + } + + #[cfg(unix)] + #[test] + fn test_quoted_csh_commands_survive_real_shells() { + use std::os::unix::fs::PermissionsExt as _; + + let directory = tempfile::tempdir().unwrap(); + let binary_directory = directory.path().join("bin"); + std::fs::create_dir(&binary_directory).unwrap(); + for program in ["%agent", "50%", "agent"] { + let script = binary_directory.join(program); + std::fs::write(&script, "#!/bin/sh\nprintf '%s' \"$1\"\n").unwrap(); + std::fs::set_permissions(&script, std::fs::Permissions::from_mode(0o755)).unwrap(); + } + for (shell_name, shell_kind) in [("tcsh", ShellKind::Tcsh), ("csh", ShellKind::Csh)] { + let Ok(shell) = which::which(shell_name) else { + continue; + }; + for program in ["%agent", "50%", "agent"] { + let (shell_program, shell_arguments) = + ShellBuilder::new(&Shell::Program(shell.display().to_string()), false) + .non_interactive() + .literal_args() + .build(Some(program.to_owned()), &[String::from("OK")]); + let script = shell_arguments.last().unwrap(); + assert_eq!( + script, + &format!("{} OK", shell_kind.try_quote(program).unwrap()) + ); + let output = smol::block_on( + smol::process::Command::new(&shell_program) + .arg("-f") + .args(&shell_arguments) + .env_clear() + .env("HOME", "/") + .env("PATH", &binary_directory) + .output(), + ) + .unwrap(); + assert_eq!( + ( + output.status.success(), + String::from_utf8_lossy(&output.stdout).into_owned(), + String::from_utf8_lossy(&output.stderr).into_owned(), + ), + (true, String::from("OK"), String::new()), + "{shell_program} {shell_arguments:?}" + ); + } + } + } + #[test] fn test_try_quote_single_quote_paths() { let path_with_quote = r"C:\Temp\O'Brien\repo"; diff --git a/crates/util/src/shell_builder.rs b/crates/util/src/shell_builder.rs index 870bf85caff..2859e9930ed 100644 --- a/crates/util/src/shell_builder.rs +++ b/crates/util/src/shell_builder.rs @@ -12,6 +12,7 @@ pub struct ShellBuilder { interactive: bool, /// Whether to redirect stdin to /dev/null for the spawned command as a subshell. redirect_stdin: bool, + substitute_variables: bool, kind: ShellKind, } @@ -31,6 +32,7 @@ impl ShellBuilder { interactive: true, kind, redirect_stdin: false, + substitute_variables: true, } } pub fn non_interactive(mut self) -> Self { @@ -38,6 +40,11 @@ impl ShellBuilder { self } + pub fn literal_args(mut self) -> Self { + self.substitute_variables = false; + self + } + /// Returns the label to show in the terminal tab pub fn command_label(&self, command_to_use_in_label: &str) -> String { if command_to_use_in_label.trim().is_empty() { @@ -90,10 +97,14 @@ impl ShellBuilder { }; let mut combined_command = task_args.iter().fold(task_command, |mut command, arg| { command.push(' '); - let shell_variable = self.kind.to_shell_variable(arg); + let shell_variable = if self.substitute_variables { + Cow::Owned(self.kind.to_shell_variable(arg)) + } else { + Cow::Borrowed(arg.as_str()) + }; command.push_str(&match self.kind.try_quote(&shell_variable) { Some(shell_variable) => shell_variable, - None => Cow::Owned(shell_variable), + None => shell_variable, }); command }); @@ -272,6 +283,31 @@ mod test { ); } + #[test] + fn test_literal_args_skip_variable_substitution() { + let shell = Shell::Program("nu".to_owned()); + let (program, args) = ShellBuilder::new(&shell, false) + .non_interactive() + .literal_args() + .build( + Some("ssh".into()), + &[ + "host".to_string(), + "cd \"$HOME\"/project && exec \"$HOME\"/'a b' exec".to_string(), + "${braced}".to_string(), + ], + ); + + assert_eq!(program, "nu"); + assert_eq!( + args, + vec![ + "-c", + "ssh host \"cd \\\"$HOME\\\"/project && exec \\\"$HOME\\\"/'a b' exec\" '${braced}'" + ] + ); + } + #[test] fn redirect_stdin_to_dev_null_precedence() { let shell = Shell::Program("nu".to_owned());