mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
Verify SFTP host keys
This commit is contained in:
parent
dbb0cc0d55
commit
5c5fc61360
1 changed files with 25 additions and 4 deletions
|
|
@ -1,8 +1,8 @@
|
||||||
use std::{io, sync::Arc, time::{Duration, SystemTime}};
|
use std::{io, sync::Arc, time::{Duration, SystemTime}};
|
||||||
|
|
||||||
use chrono::DateTime;
|
use chrono::DateTime;
|
||||||
use russh::keys::{PrivateKeyWithHashAlg, agent::AgentIdentity};
|
use russh::keys::{PrivateKeyWithHashAlg, agent::AgentIdentity, known_hosts};
|
||||||
use yazi_fs::provider::local::Local;
|
use yazi_fs::{provider::local::Local, Xdg};
|
||||||
|
|
||||||
use crate::config::ServiceSftp;
|
use crate::config::ServiceSftp;
|
||||||
|
|
||||||
|
|
@ -23,9 +23,30 @@ impl russh::client::Handler for Conn {
|
||||||
|
|
||||||
async fn check_server_key(
|
async fn check_server_key(
|
||||||
&mut self,
|
&mut self,
|
||||||
_server_public_key: &russh::keys::PublicKey,
|
server_public_key: &russh::keys::PublicKey,
|
||||||
) -> Result<bool, Self::Error> {
|
) -> 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue