Файли
openidea/crates/ide-ssh/src/testing.rs
T
l0ng-ai fb0b2d6a5f fix(ssh): run remote commands with sh whatever the login shell (#6612)
sshd runs every exec request through the account's login shell. The commands the IDE sends (server probe and install, connection test, run targets, command tunnels) are POSIX shell, so with fish as the login shell they failed with stderr only and opening a remote project never got past the probe.

Exec requests now send sh -c '<script>', a plain command to every shell, quoted so that fish reads the same text as a POSIX shell (quotes and backslashes written outside the single quotes). The login shell still starts first, so its environment is inherited; interactive terminals keep the login shell itself.

Fixes #6200
2026-11-28 08:44:47 +08:00

412 рядки
15 KiB
Rust

//! An in-process SSH server for tests (feature `testing`): password `dev` /
//! `secret`, commands answered by a closure or an explicitly allowed process, SFTP served from a local
//! folder (remote `/x` is `<root>/x`), and a shell that echoes its input
//! (`exit` ends it with status 0). `direct-tcpip` channels are forwarded to local TCP
//! ports, and refused when nothing listens there.
use russh::server::{self, Auth, ChannelOpenHandle, Msg, Server as _, Session};
use russh::{Channel, ChannelId};
use russh_sftp::protocol::{Attrs, Data, File, FileAttributes, Handle, Name, OpenFlags, Status, StatusCode, Version};
use std::collections::HashMap;
use std::io::{Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use std::process::Stdio;
use std::sync::Arc;
use tokio::io::AsyncWriteExt;
use tokio::net::TcpListener;
use tokio::process::Command;
use tokio::sync::Mutex;
/// Reply of a command: (stdout, exit status).
pub type ExecFn = Arc<dyn Fn(&str) -> (String, u32) + Send + Sync>;
type SpawnFn = Arc<dyn Fn(&str) -> Option<Command> + Send + Sync>;
#[derive(Clone)]
enum Exec {
Reply(ExecFn),
Process(SpawnFn),
}
/// Starts the server; returns its port. `root` backs SFTP.
pub async fn sshd(root: PathBuf, exec: ExecFn) -> u16 {
start(root, Exec::Reply(exec)).await
}
/// Serves stdio of an explicitly chosen command over an SSH exec channel.
pub async fn sshd_with_process(root: PathBuf, exec: impl Fn(&str) -> Option<Command> + Send + Sync + 'static) -> u16 {
start(root, Exec::Process(Arc::new(exec))).await
}
async fn start(root: PathBuf, exec: Exec) -> u16 {
let config = Arc::new(server::Config {
keys: vec![russh::keys::decode_secret_key(include_str!("../tests/fixtures/ssh/host"), None).expect("host key")],
auth_rejection_time: std::time::Duration::from_millis(10),
auth_rejection_time_initial: Some(std::time::Duration::ZERO),
..Default::default()
});
let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind");
let port = listener.local_addr().expect("addr").port();
let mut server = Sshd { root, exec };
tokio::spawn(async move {
let _ = server.run_on_socket(config, &listener).await;
});
port
}
#[derive(Clone)]
struct Sshd {
root: PathBuf,
exec: Exec,
}
impl server::Server for Sshd {
type Handler = Conn;
fn new_client(&mut self, _: Option<std::net::SocketAddr>) -> Conn {
Conn { root: self.root.clone(), exec: self.exec.clone(), channels: Arc::default(), shells: Default::default() }
}
}
struct Conn {
root: PathBuf,
exec: Exec,
channels: Arc<Mutex<HashMap<ChannelId, Channel<Msg>>>>,
shells: std::collections::HashSet<ChannelId>,
}
impl server::Handler for Conn {
type Error = russh::Error;
async fn auth_password(&mut self, user: &str, password: &str) -> Result<Auth, Self::Error> {
Ok(if user == "dev" && password == "secret" { Auth::Accept } else { Auth::reject() })
}
async fn channel_open_session(
&mut self,
channel: Channel<Msg>,
reply: ChannelOpenHandle,
_session: &mut Session,
) -> Result<(), Self::Error> {
self.channels.lock().await.insert(channel.id(), channel);
reply.accept().await;
Ok(())
}
async fn channel_open_direct_tcpip(
&mut self,
channel: Channel<Msg>,
host: &str,
port: u32,
_originator_address: &str,
_originator_port: u32,
reply: ChannelOpenHandle,
_session: &mut Session,
) -> Result<(), Self::Error> {
let Ok(mut upstream) = tokio::net::TcpStream::connect((host, port as u16)).await else {
return Ok(());
};
reply.accept().await;
tokio::spawn(async move {
let mut stream = channel.into_stream();
let _ = tokio::io::copy_bidirectional(&mut stream, &mut upstream).await;
});
Ok(())
}
async fn exec_request(
&mut self,
channel: ChannelId,
data: &[u8],
session: &mut Session,
) -> Result<(), Self::Error> {
// The client wraps every command for the login shell; tests see the script it runs.
let raw = String::from_utf8_lossy(data);
let command = crate::shell::posix_script(&raw).unwrap_or_else(|| raw.into_owned());
let Exec::Reply(exec) = &self.exec else {
let Exec::Process(spawn) = &self.exec else { unreachable!() };
let Some(ch) = self.channels.lock().await.remove(&channel) else {
session.channel_failure(channel)?;
return Ok(());
};
let Some(mut cmd) = spawn(&command) else {
session.channel_failure(channel)?;
return Ok(());
};
let mut child = cmd.stdin(Stdio::piped()).stdout(Stdio::piped()).kill_on_drop(true).spawn()?;
let mut input = child.stdin.take().expect("piped stdin");
let mut output = child.stdout.take().expect("piped stdout");
session.channel_success(channel)?;
tokio::spawn(async move {
let (mut reader, mut writer) = tokio::io::split(ch.into_stream());
let feed = async {
let _ = tokio::io::copy(&mut reader, &mut input).await;
drop(input);
};
let drain = async {
let _ = tokio::io::copy(&mut output, &mut writer).await;
let _ = writer.shutdown().await;
};
tokio::pin!(drain);
// EOF from the SSH client closes the child stdin; EOF from the child completes the channel.
tokio::select! {
_ = &mut drain => {}
_ = feed => {
let _ = tokio::time::timeout(std::time::Duration::from_secs(5), &mut drain).await;
}
}
let _ = tokio::time::timeout(std::time::Duration::from_secs(5), child.wait()).await;
});
return Ok(());
};
let (out, code) = exec(&command);
session.channel_success(channel)?;
session.data(channel, out.into_bytes())?;
session.exit_status_request(channel, code)?;
session.eof(channel)?;
session.close(channel)?;
Ok(())
}
async fn pty_request(
&mut self,
channel: ChannelId,
_term: &str,
cols: u32,
rows: u32,
_pix_width: u32,
_pix_height: u32,
_modes: &[(russh::Pty, u32)],
session: &mut Session,
) -> Result<(), Self::Error> {
session.channel_success(channel)?;
session.data(channel, format!("pty {cols}x{rows}\r\n").into_bytes())?;
Ok(())
}
async fn shell_request(&mut self, channel: ChannelId, session: &mut Session) -> Result<(), Self::Error> {
self.shells.insert(channel);
session.channel_success(channel)?;
session.data(channel, b"$ ".to_vec())?;
Ok(())
}
async fn window_change_request(
&mut self,
channel: ChannelId,
cols: u32,
rows: u32,
_pix_width: u32,
_pix_height: u32,
session: &mut Session,
) -> Result<(), Self::Error> {
session.data(channel, format!("resized {cols}x{rows}\r\n").into_bytes())?;
Ok(())
}
async fn data(&mut self, channel: ChannelId, data: &[u8], session: &mut Session) -> Result<(), Self::Error> {
if !self.shells.contains(&channel) {
return Ok(());
}
session.data(channel, data.to_vec())?;
if String::from_utf8_lossy(data).contains("exit") {
self.shells.remove(&channel);
session.exit_status_request(channel, 0)?;
session.eof(channel)?;
session.close(channel)?;
}
Ok(())
}
async fn channel_close(&mut self, channel: ChannelId, _session: &mut Session) -> Result<(), Self::Error> {
self.shells.remove(&channel);
Ok(())
}
async fn subsystem_request(
&mut self,
channel: ChannelId,
name: &str,
session: &mut Session,
) -> Result<(), Self::Error> {
let Some(ch) = self.channels.lock().await.remove(&channel).filter(|_| name == "sftp") else {
session.channel_failure(channel)?;
return Ok(());
};
session.channel_success(channel)?;
let sftp = Sftp { root: self.root.clone(), files: HashMap::new(), dirs: HashMap::new(), next: 0 };
tokio::spawn(russh_sftp::server::run(ch.into_stream(), sftp));
Ok(())
}
}
struct Sftp {
root: PathBuf,
files: HashMap<String, std::fs::File>,
/// Open directories: entries not yet sent.
dirs: HashMap<String, Option<Vec<File>>>,
next: u64,
}
fn ok(id: u32) -> Status {
Status { id, status_code: StatusCode::Ok, error_message: "Ok".into(), language_tag: "en-US".into() }
}
fn code(e: std::io::Error) -> StatusCode {
match e.kind() {
std::io::ErrorKind::NotFound => StatusCode::NoSuchFile,
std::io::ErrorKind::PermissionDenied => StatusCode::PermissionDenied,
_ => StatusCode::Failure,
}
}
impl Sftp {
fn local(&self, path: &str) -> PathBuf {
path.split('/').filter(|s| !s.is_empty() && *s != "." && *s != "..").fold(self.root.clone(), |p, s| p.join(s))
}
fn handle(&mut self) -> String {
self.next += 1;
format!("h{}", self.next)
}
fn attrs(path: &Path) -> Result<FileAttributes, StatusCode> {
std::fs::symlink_metadata(path).map(|m| FileAttributes::from(&m)).map_err(code)
}
}
impl russh_sftp::server::Handler for Sftp {
type Error = StatusCode;
fn unimplemented(&self) -> Self::Error {
StatusCode::OpUnsupported
}
async fn init(&mut self, _version: u32, _ext: HashMap<String, String>) -> Result<Version, Self::Error> {
Ok(Version::new())
}
async fn realpath(&mut self, id: u32, path: String) -> Result<Name, Self::Error> {
let p = if path == "." || path.is_empty() { "/home/dev".to_owned() } else { path };
Ok(Name { id, files: vec![File::dummy(p)] })
}
async fn open(
&mut self,
id: u32,
filename: String,
pflags: OpenFlags,
_attrs: FileAttributes,
) -> Result<Handle, Self::Error> {
let path = self.local(&filename);
let file = std::fs::OpenOptions::new()
.read(pflags.contains(OpenFlags::READ))
.write(pflags.contains(OpenFlags::WRITE))
.create(pflags.contains(OpenFlags::CREATE))
.create_new(pflags.contains(OpenFlags::EXCLUDE))
.truncate(pflags.contains(OpenFlags::TRUNCATE))
.open(path)
.map_err(code)?;
let handle = self.handle();
self.files.insert(handle.clone(), file);
Ok(Handle { id, handle })
}
async fn close(&mut self, id: u32, handle: String) -> Result<Status, Self::Error> {
self.files.remove(&handle);
self.dirs.remove(&handle);
Ok(ok(id))
}
async fn read(&mut self, id: u32, handle: String, offset: u64, len: u32) -> Result<Data, Self::Error> {
let file = self.files.get_mut(&handle).ok_or(StatusCode::Failure)?;
file.seek(SeekFrom::Start(offset)).map_err(code)?;
let mut buf = vec![0; len as usize];
let n = file.read(&mut buf).map_err(code)?;
if n == 0 {
return Err(StatusCode::Eof);
}
buf.truncate(n);
Ok(Data { id, data: buf })
}
async fn write(&mut self, id: u32, handle: String, offset: u64, data: Vec<u8>) -> Result<Status, Self::Error> {
let file = self.files.get_mut(&handle).ok_or(StatusCode::Failure)?;
file.seek(SeekFrom::Start(offset)).map_err(code)?;
file.write_all(&data).map_err(code)?;
Ok(ok(id))
}
async fn stat(&mut self, id: u32, path: String) -> Result<Attrs, Self::Error> {
let p = self.local(&path);
let attrs = std::fs::metadata(&p).map(|m| FileAttributes::from(&m)).map_err(code)?;
Ok(Attrs { id, attrs })
}
async fn lstat(&mut self, id: u32, path: String) -> Result<Attrs, Self::Error> {
Ok(Attrs { id, attrs: Self::attrs(&self.local(&path))? })
}
async fn fstat(&mut self, id: u32, handle: String) -> Result<Attrs, Self::Error> {
let file = self.files.get(&handle).ok_or(StatusCode::Failure)?;
Ok(Attrs { id, attrs: FileAttributes::from(&file.metadata().map_err(code)?) })
}
async fn setstat(&mut self, id: u32, path: String, attrs: FileAttributes) -> Result<Status, Self::Error> {
#[cfg(unix)]
if let Some(mode) = attrs.permissions {
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(self.local(&path), std::fs::Permissions::from_mode(mode)).map_err(code)?;
}
if let Some(t) = attrs.mtime {
let time = std::time::UNIX_EPOCH + std::time::Duration::from_secs(t.into());
let f = std::fs::File::options().write(true).open(self.local(&path)).map_err(code)?;
f.set_modified(time).map_err(code)?;
}
Ok(ok(id))
}
async fn opendir(&mut self, id: u32, path: String) -> Result<Handle, Self::Error> {
let dir = self.local(&path);
let mut files = Vec::new();
for e in std::fs::read_dir(&dir).map_err(code)?.flatten() {
let name = e.file_name().to_string_lossy().into_owned();
files.push(File::new(name, Self::attrs(&e.path())?));
}
let handle = self.handle();
self.dirs.insert(handle.clone(), Some(files));
Ok(Handle { id, handle })
}
async fn readdir(&mut self, id: u32, handle: String) -> Result<Name, Self::Error> {
match self.dirs.get_mut(&handle).and_then(Option::take) {
Some(files) if !files.is_empty() => Ok(Name { id, files }),
_ => Err(StatusCode::Eof),
}
}
async fn remove(&mut self, id: u32, filename: String) -> Result<Status, Self::Error> {
std::fs::remove_file(self.local(&filename)).map_err(code)?;
Ok(ok(id))
}
async fn mkdir(&mut self, id: u32, path: String, _attrs: FileAttributes) -> Result<Status, Self::Error> {
std::fs::create_dir(self.local(&path)).map_err(code)?;
Ok(ok(id))
}
async fn rmdir(&mut self, id: u32, path: String) -> Result<Status, Self::Error> {
std::fs::remove_dir(self.local(&path)).map_err(code)?;
Ok(ok(id))
}
#[cfg(unix)]
async fn symlink(&mut self, id: u32, target: String, link: String) -> Result<Status, Self::Error> {
std::os::unix::fs::symlink(target, self.local(&link)).map_err(code)?;
Ok(ok(id))
}
async fn rename(&mut self, id: u32, oldpath: String, newpath: String) -> Result<Status, Self::Error> {
std::fs::rename(self.local(&oldpath), self.local(&newpath)).map_err(code)?;
Ok(ok(id))
}
}