crypto: Handle default SSH_AUTH_SOCK on Windows

On Windows, SSH Agents commonly use a named pipe with a global default
name. This patch is to give users a more streamline experience as they
will not need to set the environment variable manually anymore.
This commit is contained in:
Fred Gobry
2026-05-26 09:18:07 +02:00
committed by Lorenz Leutgeb
parent 067a9d13bf
commit 0a03f1c84e
+13 -7
View File
@@ -60,13 +60,19 @@ impl Agent {
pub fn connect() -> Result<Self, ConnectError> {
const SSH_AUTH_SOCK: &str = "SSH_AUTH_SOCK";
let path =
PathBuf::from(
std::env::var(SSH_AUTH_SOCK).map_err(|err| ConnectError::EnvVar {
var: SSH_AUTH_SOCK.to_string(),
source: err,
})?,
);
let path = match std::env::var(SSH_AUTH_SOCK) {
Ok(path) => Ok(PathBuf::from(path)),
#[cfg(windows)]
Err(VarError::NotPresent) => {
// Windows exposes the SSH Agent on a default named pipe.
const DEFAULT_SSH_AGENT_PIPE: &str = "\\\\.\\pipe\\openssh-ssh-agent";
Ok(PathBuf::from(DEFAULT_SSH_AGENT_PIPE))
}
Err(err) => Err(ConnectError::EnvVar {
var: SSH_AUTH_SOCK.to_string(),
source: err,
}),
}?;
let client = Client::new(
Stream::connect(&path).map_err(|err| ConnectError::Agent(AgentError::IO(err)))?,