Verify SFTP host keys

This commit is contained in:
adiraj66132 2026-07-12 03:09:24 +05:30
parent dbb0cc0d55
commit 5c5fc61360

View file

@ -1,8 +1,8 @@
use std::{io, sync::Arc, time::{Duration, SystemTime}};
use chrono::DateTime;
use russh::keys::{PrivateKeyWithHashAlg, agent::AgentIdentity};
use yazi_fs::provider::local::Local;
use russh::keys::{PrivateKeyWithHashAlg, agent::AgentIdentity, known_hosts};
use yazi_fs::{provider::local::Local, Xdg};
use crate::config::ServiceSftp;
@ -23,9 +23,30 @@ impl russh::client::Handler for Conn {
async fn check_server_key(
&mut self,
_server_public_key: &russh::keys::PublicKey,
server_public_key: &russh::keys::PublicKey,
) -> Result<bool, Self::Error> {
Ok(true)
let path = Xdg::config_dir().join("known_hosts");
match known_hosts::check_known_hosts_path(&self.config.host, self.config.port, server_public_key, &path) {
Ok(true) => Ok(true),
Ok(false) => {
if let Err(e) = known_hosts::learn_known_hosts_path(&self.config.host, self.config.port, server_public_key, &path) {
tracing::warn!("Failed to record host key in known_hosts: {e}");
}
Ok(true)
}
Err(russh::keys::Error::KeyChanged { line }) => {
tracing::error!(
"Host key for `{}` has changed (known_hosts:{}). Possible MITM attack!",
self.config.host, line
);
Ok(false)
}
Err(e) => {
tracing::warn!("Could not verify host key for `{}`: {e}", self.config.host);
Ok(true)
}
}
}
}