Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(ssh): fall back to the default identity file when key auth has none

HARRISON698121569bfd
diff --git a/crates/dbx-driver-support/src/ssh_config.rs b/crates/dbx-driver-support/src/ssh_config.rs
index 9f24a080a9..49a8419be9 100644
--- a/crates/dbx-driver-support/src/ssh_config.rs
+++ b/crates/dbx-driver-support/src/ssh_config.rs
@@ -54,11 +54,50 @@ pub fn find_host(alias: &str) -> Option<SshConfigHostEntry> {
/// `auth_method` retain the legacy default-filling behavior so imported
/// connections continue to resolve their aliases. Payloads produced by the
/// current connection form keep all user and port values as entered.
+///
+/// A key-auth request with no `key_path` (the user picked "key" but left the
+/// path blank, expecting the usual `~/.ssh/id_*` default) falls back to the
+/// first existing default key, the way OpenSSH does. Without this the tunnel
+/// builder rejects the empty path with "File path cannot be empty" (#11839).
pub fn resolve_ssh_tunnel_config(ssh: &SshTunnelConfig) -> SshTunnelConfig {
- match find_host(&ssh.host) {
+ let resolved = match find_host(&ssh.host) {
Some(entry) => apply_host_entry(ssh, entry),
None => ssh.clone(),
+ };
+ apply_default_identity_file(resolved)
+}
+
+/// OpenSSH's default identity files, in its own preference order.
+const DEFAULT_IDENTITY_FILE_NAMES: [&str; 4] = ["id_ed25519", "id_ecdsa", "id_ecdsa_sk", "id_rsa"];
+
+/// Fills an empty `key_path` when the connection is set up for key auth, using
+/// the first default identity file that exists.
+///
+/// Only key-based methods are considered: for `password`/`agent`/`none` an
+/// empty path is meaningful (there is no key to load), and filling one in would
+/// silently change which credential the user asked DBX to use. A path is only
+/// supplied when the file is actually present, so a user with no keys still
+/// gets the original "no key configured" behavior instead of a path that
+/// cannot be read.
+fn apply_default_identity_file(ssh: SshTunnelConfig) -> SshTunnelConfig {
+ apply_default_identity_file_with(ssh, |candidate| Path::new(&expand_tilde(candidate)).is_file())
+}
+
+/// Same as [`apply_default_identity_file`] with an injectable existence probe,
+/// so tests do not depend on the developer's real `~/.ssh`.
+fn apply_default_identity_file_with(mut ssh: SshTunnelConfig, exists: impl Fn(&str) -> bool) -> SshTunnelConfig {
+ if !ssh.key_path.trim().is_empty() {
+ return ssh;
+ }
+ if ssh.auth_method != "key" && ssh.auth_method != "key+password" {
+ return ssh;
+ }
+ if let Some(path) =
+ DEFAULT_IDENTITY_FILE_NAMES.iter().map(|name| format!("~/.ssh/{name}")).find(|candidate| exists(candidate))
+ {
+ ssh.key_path = path;
}
+ ssh
}
/// Resolves `ssh.host` plus, when its `~/.ssh/config` entry (or an ancestor
@@ -79,7 +118,9 @@ fn resolve_chain_from_entries(ssh: &SshTunnelConfig, entries: &[SshConfigHostEnt
let find = |alias: &str| entries.iter().find(|entry| entry.alias == alias).cloned();
let Some(target_entry) = find(&ssh.host) else {
- return vec![ssh.clone()];
+ // Same default-key fallback as `resolve_ssh_tunnel_config`, so a host
+ // with no `~/.ssh/config` block still gets an identity file.
+ return vec![apply_default_identity_file(ssh.clone())];
};
let resolved_target = apply_host_entry(ssh, target_entry.clone());
@@ -100,7 +141,10 @@ fn resolve_chain_from_entries(ssh: &SshTunnelConfig, entries: &[SshConfigHostEnt
jump_hops.reverse();
jump_hops.push(resolved_target);
- jump_hops
+ // Any hop still without a key path (no explicit form value, no
+ // `IdentityFile`) falls back to the user's default key. Applied last so an
+ // `IdentityFile` from `~/.ssh/config` always wins.
+ jump_hops.into_iter().map(apply_default_identity_file).collect()
}
/// Builds a synthetic hop for a `ProxyJump` host. Unlike the leaf host in
@@ -550,6 +594,69 @@ mod tests {
assert_eq!(resolved, ssh);
}
+ // #11839: picking "key" auth without typing a path must fall back to the
+ // usual `~/.ssh/id_*` defaults instead of reaching the tunnel builder with
+ // an empty path ("File path cannot be empty").
+ #[test]
+ fn key_auth_without_a_path_falls_back_to_the_existing_default_key() {
+ let mut ssh = config("host");
+ ssh.auth_method = "key".to_string();
+ // No id_ed25519 on this imaginary machine: the next candidate wins.
+ let resolved = apply_default_identity_file_with(ssh, |candidate| candidate.ends_with("id_rsa"));
+ assert_eq!(resolved.key_path, "~/.ssh/id_rsa");
+ }
+
+ #[test]
+ fn default_key_fallback_prefers_ed25519_over_rsa() {
+ let mut ssh = config("host");
+ ssh.auth_method = "key+password".to_string();
+ let resolved = apply_default_identity_file_with(ssh, |_| true);
+ assert_eq!(resolved.key_path, "~/.ssh/id_ed25519");
+ }
+
+ #[test]
+ fn default_key_fallback_leaves_non_key_methods_untouched() {
+ // Empty is meaningful for these methods; supplying a key path would
+ // silently change the credential the user selected.
+ for method in ["password", "agent", "none", ""] {
+ let mut ssh = config("host");
+ ssh.auth_method = method.to_string();
+ let resolved = apply_default_identity_file_with(ssh, |_| true);
+ assert_eq!(resolved.key_path, "", "method {method:?} must not get a default key");
+ }
+ }
+
+ #[test]
+ fn default_key_fallback_keeps_an_explicit_path() {
+ let mut ssh = config("host");
+ ssh.auth_method = "key".to_string();
+ ssh.key_path = "/explicit/key".to_string();
+ let resolved = apply_default_identity_file_with(ssh, |_| true);
+ assert_eq!(resolved.key_path, "/explicit/key");
+ }
+
+ #[test]
+ fn default_key_fallback_is_skipped_when_no_candidate_exists() {
+ // A user with no keys keeps the previous behavior (and its clearer
+ // error) rather than getting an unreadable path.
+ let mut ssh = config("host");
+ ssh.auth_method = "key".to_string();
+ let resolved = apply_default_identity_file_with(ssh, |_| false);
+ assert_eq!(resolved.key_path, "");
+ }
+
+ #[test]
+ fn an_ssh_config_identity_file_still_wins_over_the_default_key() {
+ let mut ssh = config("myserver");
+ ssh.auth_method = "key".to_string();
+ let mut config_entry = entry("myserver");
+ config_entry.identity_file = Some("~/.ssh/config_declared_key".to_string());
+ // The config's own IdentityFile is filled first, so the later
+ // default-key pass must leave it alone.
+ let resolved = apply_default_identity_file_with(apply_host_entry(&ssh, config_entry), |_| true);
+ assert_eq!(resolved.key_path, "~/.ssh/config_declared_key");
+ }
+
fn jump_entry(alias: &str, host_name: &str, proxy_jump: Option<&str>) -> SshConfigHostEntry {
SshConfigHostEntry {
alias: alias.to_string(),