fix: verify SSH host keys, support ssh-agent, fix remote dir walk bugs

- Replace ssh.InsecureIgnoreHostKey() with TOFU verification against
  ~/.ssh/known_hosts (accept-new for unknown hosts, hard reject on a
  changed key) to close a MITM hole on every SFTP connection.
- Actually connect to a running ssh-agent (SSH_AUTH_SOCK) for auth
  instead of only scanning default key files on disk; give a clear
  error when an explicit IdentityFile is passphrase-protected.
- Fix SSHClient.walk()/DirectorySize: the internal filepathSkipDir
  sentinel leaked out as a real error on stat/ReadDir failures instead
  of being swallowed, aborting size calculation on the first
  unreadable subdirectory instead of skipping it.
- Skip symlink-to-directory entries in remote directory copies instead
  of failing the whole transfer trying to Open() them as regular files.
- Consolidate 5 duplicated sudo-aware home-dir lookups into
  internal/homedir, adding a missing os.Geteuid()==0 check before
  trusting SUDO_USER.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
vrubelroman 2026-07-03 15:31:32 +00:00
parent 3cf9582147
commit c15ade247a
16 changed files with 2580 additions and 65 deletions

View file

@ -0,0 +1,26 @@
// Package homedir resolves the real user's home directory, including when
// the process is running under sudo.
package homedir
import (
"os"
"os/user"
)
// Dir returns the real user's home directory. os.UserHomeDir() resolves to
// /root when the process is running as root via sudo, which points config,
// SSH, and trash paths at the wrong place. When the effective UID is 0 and
// SUDO_USER is set, it resolves the invoking user's home instead.
func Dir() string {
if os.Geteuid() == 0 {
if sudoUser := os.Getenv("SUDO_USER"); sudoUser != "" {
if u, err := user.Lookup(sudoUser); err == nil {
return u.HomeDir
}
}
}
if h, err := os.UserHomeDir(); err == nil {
return h
}
return ""
}