windows: Encrypt SSH passwords stored in memory (#38427)

Release Notes:

- N/A

---------

Co-authored-by: Julia <julia@zed.dev>
This commit is contained in:
Piotr Osiewicz
2025-09-23 18:58:46 +02:00
committed by GitHub
co-authored by Julia
parent 6a24ad7d39
commit dc20a41e0d
14 changed files with 232 additions and 37 deletions
+9 -2
View File
@@ -8,6 +8,7 @@ use crate::{
},
};
use anyhow::{Context as _, Result, anyhow};
use askpass::EncryptedPassword;
use async_trait::async_trait;
use collections::HashMap;
use futures::{
@@ -60,7 +61,12 @@ pub struct CommandTemplate {
}
pub trait RemoteClientDelegate: Send + Sync {
fn ask_password(&self, prompt: String, tx: oneshot::Sender<String>, cx: &mut AsyncApp);
fn ask_password(
&self,
prompt: String,
tx: oneshot::Sender<EncryptedPassword>,
cx: &mut AsyncApp,
);
fn get_download_params(
&self,
platform: RemotePlatform,
@@ -1373,6 +1379,7 @@ mod fake {
use super::{ChannelClient, RemoteClientDelegate, RemoteConnection, RemotePlatform};
use crate::remote_client::{CommandTemplate, RemoteConnectionOptions};
use anyhow::Result;
use askpass::EncryptedPassword;
use async_trait::async_trait;
use collections::HashMap;
use futures::{
@@ -1517,7 +1524,7 @@ mod fake {
pub(super) struct Delegate;
impl RemoteClientDelegate for Delegate {
fn ask_password(&self, _: String, _: oneshot::Sender<String>, _: &mut AsyncApp) {
fn ask_password(&self, _: String, _: oneshot::Sender<EncryptedPassword>, _: &mut AsyncApp) {
unreachable!()
}
+18 -3
View File
@@ -74,6 +74,8 @@ struct SshSocket {
#[cfg(not(target_os = "windows"))]
socket_path: PathBuf,
envs: HashMap<String, String>,
#[cfg(target_os = "windows")]
password: askpass::EncryptedPassword,
}
macro_rules! shell_script {
@@ -347,7 +349,13 @@ impl SshRemoteConnection {
#[cfg(not(target_os = "windows"))]
let socket = SshSocket::new(connection_options, socket_path)?;
#[cfg(target_os = "windows")]
let socket = SshSocket::new(connection_options, &temp_dir, askpass.get_password())?;
let socket = SshSocket::new(
connection_options,
&temp_dir,
askpass
.get_password()
.context("Failed to fetch askpass password")?,
)?;
drop(askpass);
let ssh_platform = socket.platform().await?;
@@ -674,16 +682,21 @@ impl SshSocket {
}
#[cfg(target_os = "windows")]
fn new(options: SshConnectionOptions, temp_dir: &TempDir, secret: String) -> Result<Self> {
fn new(
options: SshConnectionOptions,
temp_dir: &TempDir,
password: askpass::EncryptedPassword,
) -> Result<Self> {
let askpass_script = temp_dir.path().join("askpass.bat");
std::fs::write(&askpass_script, "@ECHO OFF\necho %ZED_SSH_ASKPASS%")?;
let mut envs = HashMap::default();
envs.insert("SSH_ASKPASS_REQUIRE".into(), "force".into());
envs.insert("SSH_ASKPASS".into(), askpass_script.display().to_string());
envs.insert("ZED_SSH_ASKPASS".into(), secret);
Ok(Self {
connection_options: options,
envs,
password,
})
}
@@ -737,12 +750,14 @@ impl SshSocket {
#[cfg(target_os = "windows")]
fn ssh_options<'a>(&self, command: &'a mut process::Command) -> &'a mut process::Command {
use askpass::ProcessExt;
command
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.args(self.connection_options.additional_args())
.envs(self.envs.clone())
.encrypted_env("ZED_SSH_ASKPASS", self.password.clone())
}
// On Windows, we need to use `SSH_ASKPASS` to provide the password to ssh.