Файли
openidea/crates/ide-ssh/tests/session.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

522 рядки
20 KiB
Rust

//! Sessions against an in-process russh server that runs commands (a canned
//! reply) and forwards `direct-tcpip` channels to a local echo server.
use ide_core::error::ErrorKind;
use ide_core::remote_settings::SshAuth;
use ide_ssh::{ConnectError, Output, Session, SshFiles, SshTarget, Tunnel};
use parking_lot::Mutex;
use russh::keys::{PrivateKey, PublicKey, decode_secret_key};
use russh::server::{self, Auth, ChannelOpenHandle, Msg, Server as _, Session as ServerSession};
use russh::{Channel, ChannelId};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpListener, TcpStream};
fn fixture(name: &str) -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/ssh").join(name)
}
fn key(name: &str, passphrase: Option<&str>) -> PrivateKey {
decode_secret_key(&std::fs::read_to_string(fixture(name)).unwrap(), passphrase).unwrap()
}
#[derive(Clone)]
struct Sshd {
client_key: PublicKey,
}
impl server::Server for Sshd {
type Handler = Sshd;
fn new_client(&mut self, _: Option<std::net::SocketAddr>) -> Sshd {
self.clone()
}
}
impl server::Handler for Sshd {
type Error = russh::Error;
async fn auth_none(&mut self, user: &str) -> Result<Auth, Self::Error> {
Ok(if user == "coder" { Auth::Accept } else { Auth::reject() })
}
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 auth_publickey(&mut self, user: &str, key: server::VerifiedPublicKey<'_>) -> Result<Auth, Self::Error> {
let accept = user == "dev" && key.public_key.key_data() == self.client_key.key_data();
Ok(if accept { Auth::Accept } else { Auth::reject() })
}
async fn channel_open_session(
&mut self,
_channel: Channel<Msg>,
reply: ChannelOpenHandle,
_session: &mut ServerSession,
) -> Result<(), Self::Error> {
reply.accept().await;
Ok(())
}
/// `ran: <command as sent>` on stdout, `oops` on stderr, exit status 3. `linger` exits but keeps
/// the channel open, as when a child it started still holds stdout.
async fn exec_request(
&mut self,
channel: ChannelId,
data: &[u8],
session: &mut ServerSession,
) -> Result<(), Self::Error> {
session.channel_success(channel)?;
session.data(channel, format!("ran: {}", String::from_utf8_lossy(data)).into_bytes())?;
session.extended_data(channel, 1, b"oops".to_vec())?;
session.exit_status_request(channel, 3)?;
if data != b"sh -c 'linger'" {
session.eof(channel)?;
session.close(channel)?;
}
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 ServerSession,
) -> Result<(), Self::Error> {
let Ok(mut upstream) = 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(())
}
/// `RemoteForward`: listens on 127.0.0.1:`port` and forwards each connection back to the client.
async fn tcpip_forward(
&mut self,
address: &str,
port: &mut u32,
session: &mut ServerSession,
) -> Result<bool, Self::Error> {
let Ok(listener) = TcpListener::bind(("127.0.0.1", *port as u16)).await else { return Ok(false) };
let (handle, address, port) = (session.handle(), address.to_owned(), *port);
tokio::spawn(async move {
while let Ok((mut socket, peer)) = listener.accept().await {
let Ok(channel) = handle
.channel_open_forwarded_tcpip(address.clone(), port, "127.0.0.1", u32::from(peer.port()))
.await
else {
continue;
};
tokio::spawn(async move {
let mut stream = channel.into_stream();
let _ = tokio::io::copy_bidirectional(&mut stream, &mut socket).await;
});
}
});
Ok(true)
}
}
/// Echo server standing in for a remote service.
async fn echo() -> u16 {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let port = listener.local_addr().unwrap().port();
tokio::spawn(async move {
while let Ok((mut s, _)) = listener.accept().await {
tokio::spawn(async move {
let (mut r, mut w) = s.split();
let _ = tokio::io::copy(&mut r, &mut w).await;
});
}
});
port
}
async fn sshd() -> u16 {
let config = Arc::new(server::Config {
keys: vec![key("host", None)],
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.unwrap();
let port = listener.local_addr().unwrap().port();
let mut server = Sshd { client_key: key("client", None).public_key().clone() };
tokio::spawn(async move {
let _ = server.run_on_socket(config, &listener).await;
});
port
}
struct Env {
_tmp: tempfile::TempDir,
files: SshFiles,
ssh_port: u16,
echo_port: u16,
}
async fn env() -> Env {
let tmp = tempfile::tempdir().unwrap();
let files = SshFiles { known_hosts: tmp.path().join("known_hosts"), config: None };
Env { files, _tmp: tmp, ssh_port: sshd().await, echo_port: echo().await }
}
impl Env {
fn target(&self, auth: SshAuth) -> SshTarget {
SshTarget {
host: "127.0.0.1".into(),
port: Some(self.ssh_port),
user: "dev".into(),
auth,
..Default::default()
}
}
async fn connect(&self, target: &SshTarget, secret: Option<&str>) -> Result<Session, ConnectError> {
Session::connect(target, secret, &self.files).await
}
/// Trusts the server's key on the first connection, as the UI does.
async fn connect_trusted(&self, target: &SshTarget, secret: Option<&str>) -> Result<Session, ConnectError> {
match self.connect(target, secret).await {
Err(ConnectError::UnknownHostKey(k)) => {
k.trust(&self.files.known_hosts).unwrap();
self.connect(target, secret).await
}
other => other,
}
}
}
async fn round_trip(t: &Tunnel) {
let mut s = TcpStream::connect(("127.0.0.1", t.port())).await.unwrap();
s.write_all(b"SELECT 1").await.unwrap();
let mut buf = [0u8; 8];
s.read_exact(&mut buf).await.unwrap();
assert_eq!(&buf, b"SELECT 1");
}
fn denied(r: Result<Session, ConnectError>) -> String {
match r {
Err(ConnectError::Other(e)) if e.kind == ErrorKind::PermissionDenied => e.message,
Err(ConnectError::Other(e)) => panic!("expected permission denied, got {e:?}"),
Err(ConnectError::UnknownHostKey(k)) => panic!("unexpected unknown host key {}", k.fingerprint()),
Ok(_) => panic!("expected permission denied, connected"),
}
}
#[tokio::test]
async fn unknown_host_key_must_be_trusted_and_changed_key_is_refused() {
let env = env().await;
let target = env.target(SshAuth::Password);
let Err(ConnectError::UnknownHostKey(k)) = env.connect(&target, Some("secret")).await else {
panic!("expected an unknown host key");
};
assert!(k.fingerprint().starts_with("ssh-ed25519 SHA256:"), "{}", k.fingerprint());
assert!(k.error().message.starts_with("Unknown SSH host key for 127.0.0.1:"));
k.trust(&env.files.known_hosts).unwrap();
assert!(env.connect(&target, Some("secret")).await.is_ok());
// Another key recorded for the same host: refused, never offered for trust.
let other = key("client", None).public_key().to_openssh().unwrap();
std::fs::write(&env.files.known_hosts, format!("[127.0.0.1]:{} {other}\n", env.ssh_port)).unwrap();
let msg = denied(env.connect(&target, Some("secret")).await);
assert!(msg.contains("has changed"), "{msg}");
}
#[tokio::test]
async fn password_authentication_and_forwarding() {
let env = env().await;
let target = env.target(SshAuth::Password);
let session = Arc::new(env.connect_trusted(&target, Some("secret")).await.ok().unwrap());
let tunnel = Tunnel::open(session.clone(), "127.0.0.1", env.echo_port).await.unwrap();
round_trip(&tunnel).await;
round_trip(&tunnel).await;
assert!(!tunnel.is_closed());
assert!(denied(env.connect(&target, None).await).starts_with("SSH password required"));
assert!(denied(env.connect(&target, Some("wrong")).await).starts_with("SSH authentication failed"));
let closed = TcpListener::bind("127.0.0.1:0").await.unwrap().local_addr().unwrap().port();
let Err(e) = Tunnel::open(session, "127.0.0.1", closed).await else { panic!("expected an error") };
assert!(e.message.contains(&format!("cannot reach 127.0.0.1:{closed}")), "{}", e.message);
}
#[tokio::test]
async fn command_tunnel_runs_the_command_per_connection() {
let env = env().await;
let session = Arc::new(env.connect_trusted(&env.target(SshAuth::Password), Some("secret")).await.ok().unwrap());
let tunnel = Tunnel::command(session, "docker system dial-stdio").await.unwrap();
for _ in 0..2 {
let mut s = TcpStream::connect(("127.0.0.1", tunnel.port())).await.unwrap();
let mut out = String::new();
s.read_to_string(&mut out).await.unwrap();
assert_eq!(out, "ran: sh -c 'docker system dial-stdio'");
}
}
#[tokio::test]
async fn forwarding_keeps_the_port_number_when_it_is_free() {
let env = env().await;
let target = env.target(SshAuth::Password);
let session = Arc::new(env.connect_trusted(&target, Some("secret")).await.ok().unwrap());
let free = TcpListener::bind("127.0.0.1:0").await.unwrap().local_addr().unwrap().port();
let tunnel = Tunnel::listen_on(session.clone(), Some(free), "127.0.0.1", env.echo_port).await.unwrap();
assert_eq!(tunnel.port(), free);
round_trip(&tunnel).await;
// Taken here: another free port.
let other = Tunnel::listen_on(session, Some(free), "127.0.0.1", env.echo_port).await.unwrap();
assert_ne!(other.port(), free);
round_trip(&other).await;
}
#[tokio::test]
async fn dropping_a_tunnel_closes_active_connections() {
let env = env().await;
let session = Arc::new(env.connect_trusted(&env.target(SshAuth::Password), Some("secret")).await.ok().unwrap());
let tunnel = Tunnel::listen(session.clone(), "127.0.0.1", env.echo_port).await.unwrap();
let mut socket = TcpStream::connect(("127.0.0.1", tunnel.port())).await.unwrap();
socket.write_all(b"ping").await.unwrap();
let mut reply = [0; 4];
socket.read_exact(&mut reply).await.unwrap();
assert_eq!(&reply, b"ping");
drop(tunnel);
let mut byte = [0];
assert_eq!(
tokio::time::timeout(std::time::Duration::from_secs(5), socket.read(&mut byte)).await.unwrap().unwrap(),
0
);
assert!(!session.is_closed());
}
#[tokio::test]
async fn closing_a_tunnel_lets_go_of_the_session_and_its_connections() {
let env = env().await;
let session = Arc::new(env.connect_trusted(&env.target(SshAuth::Password), Some("secret")).await.ok().unwrap());
let tunnel = Tunnel::listen(session.clone(), "127.0.0.1", env.echo_port).await.unwrap();
let port = tunnel.port();
let mut socket = TcpStream::connect(("127.0.0.1", port)).await.unwrap();
socket.write_all(b"ping").await.unwrap();
let mut reply = [0; 4];
socket.read_exact(&mut reply).await.unwrap();
assert!(Arc::strong_count(&session) > 2, "the tunnel and its connection hold the session");
tunnel.close().await;
// Nothing is left behind to wait for: only this test holds the session now.
assert_eq!(Arc::strong_count(&session), 1);
let mut byte = [0];
assert_eq!(socket.read(&mut byte).await.unwrap(), 0);
assert!(TcpStream::connect(("127.0.0.1", port)).await.is_err());
assert!(!session.is_closed());
}
#[tokio::test]
async fn closing_an_idle_tunnel_returns_at_once() {
let env = env().await;
let session = Arc::new(env.connect_trusted(&env.target(SshAuth::Password), Some("secret")).await.ok().unwrap());
let tunnel = Tunnel::command(session.clone(), "docker system dial-stdio").await.unwrap();
tokio::time::timeout(std::time::Duration::from_secs(5), tunnel.close()).await.unwrap();
assert_eq!(Arc::strong_count(&session), 1);
}
#[tokio::test]
async fn key_pair_authentication_with_and_without_passphrase() {
let env = env().await;
let target = SshTarget { key_file: fixture("client").to_string_lossy().into(), ..env.target(SshAuth::KeyPair) };
assert!(env.connect_trusted(&target, None).await.is_ok());
// An encrypted key the server does not accept: passphrase first, then the key is refused.
let locked = SshTarget { key_file: fixture("locked").to_string_lossy().into(), ..target.clone() };
assert!(denied(env.connect(&locked, None).await).starts_with("SSH key passphrase required"));
assert!(denied(env.connect(&locked, Some("nope")).await).contains("wrong passphrase"));
assert!(denied(env.connect(&locked, Some("pw")).await).starts_with("SSH authentication failed"));
}
#[tokio::test]
async fn runs_commands_and_streams_processes() {
let env = env().await;
let session = env.connect_trusted(&env.target(SshAuth::Password), Some("secret")).await.ok().unwrap();
let out = session.exec("uname -a", None).await.unwrap();
assert_eq!(
(out.code, out.stdout_str().as_str(), out.stderr.as_slice()),
(Some(3), "ran: sh -c 'uname -a'", &b"oops"[..])
);
assert!(out.into_stdout("uname").unwrap_err().message.contains("oops"));
let seen: Arc<Mutex<(String, String)>> = Arc::default();
let s = seen.clone();
let process = session
.spawn(
"python3 main.py",
None,
Box::new(move |o| match o {
Output::Stdout(b) => s.lock().0.push_str(&String::from_utf8_lossy(b)),
Output::Stderr(b) => s.lock().1.push_str(&String::from_utf8_lossy(b)),
}),
)
.await
.unwrap();
assert_eq!(process.wait().await, Some(3));
assert_eq!(*seen.lock(), ("ran: sh -c 'python3 main.py'".to_owned(), "oops".to_owned()));
}
#[tokio::test]
async fn a_process_that_exits_finishes_even_while_its_channel_stays_open() {
let env = env().await;
let session = env.connect_trusted(&env.target(SshAuth::Password), Some("secret")).await.ok().unwrap();
let seen: Arc<Mutex<String>> = Arc::default();
let s = seen.clone();
let process = session
.spawn(
"linger",
None,
Box::new(move |o| {
if let Output::Stdout(b) = o {
s.lock().push_str(&String::from_utf8_lossy(b));
}
}),
)
.await
.unwrap();
let code = tokio::time::timeout(std::time::Duration::from_secs(10), process.wait())
.await
.expect("the exit status must end the process");
assert_eq!((code, seen.lock().as_str()), (Some(3), "ran: sh -c 'linger'"));
}
#[tokio::test]
async fn a_command_that_exits_returns_even_while_its_channel_stays_open() {
let env = env().await;
let session = env.connect_trusted(&env.target(SshAuth::Password), Some("secret")).await.ok().unwrap();
let out = tokio::time::timeout(std::time::Duration::from_secs(10), session.exec("linger", None))
.await
.expect("the exit status must end the command")
.unwrap();
assert_eq!(
(out.code, out.stdout.as_slice(), out.stderr.as_slice()),
(Some(3), b"ran: sh -c 'linger'".as_slice(), b"oops".as_slice())
);
}
#[tokio::test]
async fn upload_reports_a_command_that_stops_reading() {
let env = env().await;
let session = env.connect_trusted(&env.target(SshAuth::Password), Some("secret")).await.ok().unwrap();
// Incompressible and larger than the channel window: the command exits (status 3, `oops`)
// without reading it, as when the host has no gzip or the folder is not writable.
let mut state = 0x9e37_79b9_7f4a_7c15_u64;
let data: Vec<u8> = (0..8 << 20)
.map(|_| {
state ^= state << 13;
state ^= state >> 7;
state ^= state << 17;
state as u8
})
.collect();
let put = session.put("/home/dev/.openidea/server/openidea-server", &data);
let err = tokio::time::timeout(std::time::Duration::from_secs(20), put)
.await
.expect("the upload must end when the command exits")
.unwrap_err();
assert!(err.message.contains("oops"), "{}", err.message);
}
#[cfg(unix)]
#[tokio::test]
async fn openssh_proxy_command_and_none_authentication() {
let mut env = env().await;
let config = env._tmp.path().join("ssh_config");
std::fs::write(
&config,
format!(
"Host *.coder\n\tStrictHostKeyChecking=no\n\tUserKnownHostsFile=/dev/null\n\tProxyCommand nc 127.0.0.1 {}\n",
env.ssh_port
),
)
.unwrap();
env.files.config = Some(config);
let target = SshTarget {
host: "deng-devbox.coder".into(),
port: Some(22),
user: "coder".into(),
auth: SshAuth::OpenSsh,
..Default::default()
};
let session = env.connect(&target, None).await.expect("ProxyCommand + none auth should connect");
let out = session.exec("uname -srm", None).await.unwrap();
assert_eq!(out.stdout_str(), "ran: sh -c 'uname -srm'");
}
#[tokio::test]
async fn accept_new_records_an_unknown_key_and_still_refuses_a_changed_one() {
let mut env = env().await;
let config = env._tmp.path().join("ssh_config");
std::fs::write(&config, "Host 127.0.0.1\n\tStrictHostKeyChecking accept-new\n").unwrap();
env.files.config = Some(config);
let target = SshTarget { user: "coder".into(), ..env.target(SshAuth::OpenSsh) };
assert!(env.connect(&target, None).await.is_ok());
let recorded = std::fs::read_to_string(&env.files.known_hosts).unwrap();
assert!(recorded.trim_start().starts_with(&format!("[127.0.0.1]:{} ssh-ed25519 ", env.ssh_port)), "{recorded}");
let other = key("client", None).public_key().to_openssh().unwrap();
std::fs::write(&env.files.known_hosts, format!("[127.0.0.1]:{} {other}\n", env.ssh_port)).unwrap();
let msg = denied(env.connect(&target, None).await);
assert!(msg.contains("has changed"), "{msg}");
}
/// A free local port (bound and released).
fn free_port() -> u16 {
std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port()
}
async fn echoes(port: u16) {
let mut s = TcpStream::connect(("127.0.0.1", port)).await.expect("forwarded port listens");
s.write_all(b"ping").await.unwrap();
let mut buf = [0u8; 4];
s.read_exact(&mut buf).await.unwrap();
assert_eq!(&buf, b"ping");
}
/// `RemoteForward` and `LocalForward` of `~/.ssh/config` are forwarded while the session lives (#6031).
#[tokio::test]
async fn config_port_forwards_follow_the_session() {
let mut env = env().await;
let (remote, local) = (free_port(), free_port());
let config = env._tmp.path().join("ssh_config");
std::fs::write(
&config,
format!(
"Host box\n\tHostName 127.0.0.1\n\tStrictHostKeyChecking=no\n\tUserKnownHostsFile=/dev/null\n\
\tRemoteForward {remote} 127.0.0.1:{echo}\n\tLocalForward 127.0.0.1:{local} localhost:{echo}\n\
\tRemoteForward /tmp/remote.sock /tmp/local.sock\n",
echo = env.echo_port
),
)
.unwrap();
env.files.config = Some(config);
let target = SshTarget {
host: "box".into(),
port: Some(env.ssh_port),
user: "coder".into(),
auth: SshAuth::OpenSsh,
..Default::default()
};
let session = Arc::new(env.connect(&target, None).await.expect("connects"));
session.start_forwards().await;
// The SSH host's port reaches the echo server here; the local port reaches it from the SSH host.
echoes(remote).await;
echoes(local).await;
drop(session);
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
assert!(TcpStream::connect(("127.0.0.1", local)).await.is_err(), "a closed session stops its LocalForward");
}