From 662e9b0c7ba0169f7c2b5dc187fbec9d47e7e62e Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 24 May 2025 07:25:44 -0700 Subject: [PATCH] wezterm-ssh: expand a couple more ssh_config tokens A couple of these are for things that we don't support; the intent is not to support the associated features so much as avoid emitting warnings when loading an ssh config that references them, especially if the offending section of the config isn't actually being used by wezterm. refs: https://github.com/wezterm/wezterm/issues/6975 --- Cargo.lock | 2 + wezterm-ssh/Cargo.toml | 2 + wezterm-ssh/src/config.rs | 79 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 327daacb3..355c93f24 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6928,6 +6928,7 @@ dependencies = [ "filedescriptor", "filenamegen", "gethostname", + "hex", "k9", "libc", "libssh-rs", @@ -6937,6 +6938,7 @@ dependencies = [ "predicates", "regex", "rstest", + "sha2", "shell-words", "smol", "socket2", diff --git a/wezterm-ssh/Cargo.toml b/wezterm-ssh/Cargo.toml index 6da327ffa..b1ee7410b 100644 --- a/wezterm-ssh/Cargo.toml +++ b/wezterm-ssh/Cargo.toml @@ -25,6 +25,7 @@ dirs-next.workspace = true filedescriptor.workspace = true filenamegen.workspace = true gethostname.workspace = true +hex.workspace = true libc.workspace = true libssh-rs = {workspace=true, features=["vendored"], optional = true} #libssh-rs = {path="../../libssh-rs/libssh-rs", features=["vendored"], optional = true} @@ -33,6 +34,7 @@ portable-pty.workspace = true regex.workspace = true smol.workspace = true socket2.workspace = true +sha2.workspace = true ssh2 = {workspace=true, features=["openssl-on-win32"], optional = true} thiserror.workspace = true wezterm-uds.workspace = true diff --git a/wezterm-ssh/src/config.rs b/wezterm-ssh/src/config.rs index 7483ab9ca..41db0c3d9 100644 --- a/wezterm-ssh/src/config.rs +++ b/wezterm-ssh/src/config.rs @@ -643,12 +643,37 @@ impl Config { None } + fn resolve_uid(&self) -> String { + #[cfg(test)] + if let Some(env) = self.environment.as_ref() { + // For testing purposes only, allow pretending that we + // have a specific fixed UID so that test expectations + // are easier to handle with snapshots + if let Some(uid) = env.get("WEZTERM_SSH_UID") { + return uid.to_string(); + } + } + + #[cfg(unix)] + { + let uid = unsafe { libc::getuid() }; + return uid.to_string(); + } + + #[cfg(not(unix))] + { + String::new() + } + } + /// Perform token substitution fn expand_tokens(&self, value: &mut String, tokens: &[&str], token_map: &ConfigMap) { let orig_value = value.to_string(); for &t in tokens { if let Some(v) = token_map.get(t) { *value = value.replace(t, v); + } else if t == "%i" { + *value = value.replace(t, &self.resolve_uid()); } else if t == "%u" { *value = value.replace(t, &self.resolve_local_user()); } else if t == "%l" { @@ -670,6 +695,27 @@ impl Config { } *value = items.join(" "); } + } else if t == "%j" { + // %j: The contents of the ProxyJump option, or the empty string if this option is unset + // We don't directly support ProxyJump, and this %j token referencing + // may technically put this into two-phase evaluation territory which + // we don't support. + // Let's silently gloss over this and treat this token as the empty + // string. + // Someone in the future will probably curse this. + *value = value.replace(t, ""); + } else if t == "%T" { + // %T: The local tun(4) or tap(4) network interface assigned if tunnel + // forwarding was requested, or "NONE" otherwise. + // We don't support this function, so it is always NONE + *value = value.replace(t, "NONE"); + } else if t == "%C" && value.contains("%C") { + // %C: Hash of %l%h%p%r%j + use sha2::Digest; + let mut c_value = "%l%h%p%r%j".to_string(); + self.expand_tokens(&mut c_value, tokens, token_map); + let hashed = hex::encode(sha2::Sha256::digest(&c_value.as_bytes())); + *value = value.replace("%C", &hashed); } else if value.contains(t) { log::warn!("Unsupported token {t} when evaluating `{orig_value}`"); } @@ -859,6 +905,39 @@ Config { ); } + #[test] + fn misc_tokens() { + let mut config = Config::new(); + + let mut fake_env = ConfigMap::new(); + fake_env.insert("HOME".to_string(), "/home/me".to_string()); + fake_env.insert("USER".to_string(), "me".to_string()); + fake_env.insert("WEZTERM_SSH_UID".to_string(), "1000".to_string()); + config.assign_environment(fake_env); + + config.add_config_string( + r#" + Host foo + LocalCommand C=%C d=%d h=%h i=%i L=%L l=%L n=%n p=%p r=%r T=%T u=%u + "#, + ); + + let opts = config.for_host("foo"); + snapshot!( + opts, + r#" +{ + "hostname": "foo", + "identityfile": "/home/me/.ssh/id_dsa /home/me/.ssh/id_ecdsa /home/me/.ssh/id_ed25519 /home/me/.ssh/id_rsa", + "localcommand": "C=69be18e71cb78246b4a5b6f25eee51a05908894d1ca556a9df61d854f57db753 d=/home/me h=foo i=1000 L=foo l=foo n=foo p=22 r=me T=NONE u=me", + "port": "22", + "user": "me", + "userknownhostsfile": "/home/me/.ssh/known_hosts /home/me/.ssh/known_hosts2", +} +"# + ); + } + #[test] fn parse_user() { let mut config = Config::new();