vcom/internal/config/session.go
vrubelroman c15ade247a 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>
2026-07-03 15:31:32 +00:00

86 lines
2.2 KiB
Go

package config
import (
"fmt"
"os"
"path/filepath"
toml "github.com/pelletier/go-toml/v2"
"vcom/internal/homedir"
)
// SessionState stores the UI state that should be restored on next launch.
type SessionState struct {
ActivePane string `toml:"active_pane"`
Left PaneSession `toml:"left"`
Right PaneSession `toml:"right"`
}
// PaneSession stores per-pane session state (path and selected entry name).
type PaneSession struct {
Path string `toml:"path"`
EntryName string `toml:"entry_name"`
CursorMemory map[string]string `toml:"cursor_memory"`
}
// DefaultSessionPath returns the path to the session file.
func DefaultSessionPath() (string, error) {
homeDir := homedir.Dir()
if homeDir == "" {
return "", fmt.Errorf("cannot determine home directory")
}
xdgDir := os.Getenv("XDG_CONFIG_HOME")
if xdgDir == "" {
xdgDir = filepath.Join(homeDir, ".config")
}
return filepath.Join(xdgDir, "vcom", "session.toml"), nil
}
// LoadSession reads the session state from disk.
// Returns (SessionState, nil) on success, (empty, error) if file doesn't exist.
func LoadSession() (SessionState, error) {
path, err := DefaultSessionPath()
if err != nil {
return SessionState{}, err
}
data, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return SessionState{}, nil
}
return SessionState{}, fmt.Errorf("read %s: %w", path, err)
}
var s SessionState
if err := toml.Unmarshal(data, &s); err != nil {
return SessionState{}, fmt.Errorf("parse %s: %w", path, err)
}
return s, nil
}
// SaveSession writes the session state to disk.
func SaveSession(s SessionState) error {
path, err := DefaultSessionPath()
if err != nil {
return err
}
absPath, err := filepath.Abs(path)
if err != nil {
return fmt.Errorf("resolve %s: %w", path, err)
}
if err := os.MkdirAll(filepath.Dir(absPath), 0o755); err != nil {
return fmt.Errorf("mkdir %s: %w", filepath.Dir(absPath), err)
}
data, err := toml.Marshal(s)
if err != nil {
return fmt.Errorf("marshal session: %w", err)
}
if err := os.WriteFile(absPath, data, 0o644); err != nil {
return fmt.Errorf("write %s: %w", path, err)
}
return nil
}