From e3845fe0f620582b4ea31f4d3646ba3ec3e8aa5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Almeida?= Date: Sun, 17 May 2026 19:05:31 +0100 Subject: [PATCH] fix(ssh): re-read ~/.ssh/config on every resolve, no process-wide cache MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit kevinburke/ssh_config's package-level Get/GetStrict use a sync.Once to load $HOME/.ssh/config once per process and cache the result. That breaks two things: * users who edit their config mid-session don't see updates until kandev restarts; * e2e tests that write a fresh ~/.ssh/config per test all see the state captured by whichever test triggered the first lookup. Open and Decode $HOME/.ssh/config on demand from a local inheritFromSSHConfig helper. The library still parses the file the same way — we're just dropping the global cache. Unblocks the four ssh/config-inheritance.spec.ts cases that wrote a per-test ~/.ssh/config. --- .../lifecycle/executor_ssh_connection.go | 47 +++++++++++++++++-- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/apps/backend/internal/agent/runtime/lifecycle/executor_ssh_connection.go b/apps/backend/internal/agent/runtime/lifecycle/executor_ssh_connection.go index c683199724..5d8bbb564e 100644 --- a/apps/backend/internal/agent/runtime/lifecycle/executor_ssh_connection.go +++ b/apps/backend/internal/agent/runtime/lifecycle/executor_ssh_connection.go @@ -98,9 +98,15 @@ func initialTargetFromConfig(cfg SSHConnConfig) *SSHTarget { // inheritFromSSHConfig fills empty fields on target from the matching Host // block in ~/.ssh/config. Explicit form values are never overwritten. +// +// Reads $HOME/.ssh/config on every call (no caching). The kevinburke/ssh_config +// package's package-level Get/GetStrict use a sync.Once to load the config once +// per process, which breaks tests (and any user who edits their config) — each +// resolve should see the current on-disk state. func inheritFromSSHConfig(alias string, t *SSHTarget) { + cfg := loadUserSSHConfig() if t.Host == "" { - if v, _ := ssh_config.GetStrict(alias, "HostName"); v != "" { + if v := lookupSSHConfig(cfg, alias, "HostName"); v != "" { t.Host = strings.TrimSpace(v) } } @@ -108,30 +114,61 @@ func inheritFromSSHConfig(alias string, t *SSHTarget) { t.Host = alias } if t.Port == 0 { - if v, _ := ssh_config.GetStrict(alias, "Port"); v != "" { + if v := lookupSSHConfig(cfg, alias, "Port"); v != "" { if n, err := strconv.Atoi(v); err == nil { t.Port = n } } } if t.User == "" { - if v, _ := ssh_config.GetStrict(alias, "User"); v != "" { + if v := lookupSSHConfig(cfg, alias, "User"); v != "" { t.User = v } } if t.IdentitySource == "" { - if v, _ := ssh_config.GetStrict(alias, "IdentityFile"); v != "" { + if v := lookupSSHConfig(cfg, alias, "IdentityFile"); v != "" { t.IdentitySource = SSHIdentitySourceFile t.IdentityFile = expandHome(v) } } if t.ProxyJump == "" { - if v, _ := ssh_config.GetStrict(alias, "ProxyJump"); v != "" { + if v := lookupSSHConfig(cfg, alias, "ProxyJump"); v != "" { t.ProxyJump = v } } } +// loadUserSSHConfig parses $HOME/.ssh/config on demand. Returns nil if the +// file is absent or unreadable; lookupSSHConfig handles a nil config by +// returning the empty string. +func loadUserSSHConfig() *ssh_config.Config { + home, err := os.UserHomeDir() + if err != nil || home == "" { + return nil + } + f, err := os.Open(filepath.Join(home, ".ssh", "config")) + if err != nil { + return nil + } + defer func() { _ = f.Close() }() + cfg, err := ssh_config.Decode(f) + if err != nil { + return nil + } + return cfg +} + +func lookupSSHConfig(cfg *ssh_config.Config, alias, key string) string { + if cfg == nil { + return "" + } + v, err := cfg.Get(alias, key) + if err != nil { + return "" + } + return v +} + // applyTargetDefaults fills in port/user/identity defaults and validates that // host is set. Identity defaults: ssh-agent if SSH_AUTH_SOCK is set, otherwise // fall back to ~/.ssh/id_ed25519.