From ac9098aba6568028c267837b737dc990318f0d94 Mon Sep 17 00:00:00 2001 From: Jovi Hsu Date: Tue, 14 Nov 2023 15:45:28 +0000 Subject: [PATCH] Fix: do not panic when connect to non-ssh server; send our version string first after connection setup --- src/config/version.rs | 2 +- src/session/mod.rs | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/config/version.rs b/src/config/version.rs index 32ca679..dd253d0 100644 --- a/src/config/version.rs +++ b/src/config/version.rs @@ -58,7 +58,7 @@ impl SshVersion { S: Read, { let buf = read_version(stream, timeout)?; - if &buf[0..4] != SSH_MAGIC { + if buf.len() < 4 || &buf[0..4] != SSH_MAGIC { error!("SSH version magic doesn't match"); error!("Probably not an ssh server"); } diff --git a/src/session/mod.rs b/src/session/mod.rs index 29a0e55..28a2341 100644 --- a/src/session/mod.rs +++ b/src/session/mod.rs @@ -50,14 +50,15 @@ where .connect(), SessionState::Version(mut config, mut stream) => { info!("start for version negotiation."); + // Send Client version + config.ver.send_our_version(&mut stream)?; + // Receive the server version config .ver .read_server_version(&mut stream, config.timeout)?; // Version validate config.ver.validate()?; - // Send Client version - config.ver.send_our_version(&mut stream)?; // from now on // each step of the interaction is subject to the ssh constraints on the packet