From 8d6fe48536050b72b64f474d4f452ba1f312d338 Mon Sep 17 00:00:00 2001 From: vrubelroman Date: Sat, 31 Oct 2026 23:07:16 +0300 Subject: [PATCH] fix(remote): never follow a symlink when deleting recursively (N-P0-3) RemoveRecursive used Stat on the root, so deleting a remote symlink to a directory walked into the target and deleted its contents. The root is now examined with Lstat (links are removed with Remove), and Walk also Lstats its root. Ports the analysts' TestRemoveRecursiveSymlinkToDir onto the sshtest server. Co-Authored-By: Claude Opus 5.5 --- internal/fs/remote/client.go | 8 +++- internal/fs/remote/client_test.go | 71 ++++++++++++++++++++++++++++++ internal/fs/remote/harness_test.go | 28 ++++++++++++ 3 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 internal/fs/remote/client_test.go create mode 100644 internal/fs/remote/harness_test.go diff --git a/internal/fs/remote/client.go b/internal/fs/remote/client.go index abf41ee..cfd4d5e 100644 --- a/internal/fs/remote/client.go +++ b/internal/fs/remote/client.go @@ -476,7 +476,9 @@ func (c *SSHClient) RemoveRecursive(path string) error { return fmt.Errorf("not connected") } - info, err := c.sftpCli.Stat(path) + // Lstat, not Stat: a symlink to a directory must be removed as a link. + // Following it would delete the contents of the link's target (N-P0-3). + info, err := c.sftpCli.Lstat(path) if err != nil { return err } @@ -781,8 +783,10 @@ func (c *SSHClient) Walk(root string, walkFn walkFunc) error { func (c *SSHClient) walk(dirPath string, walkFn walkFunc, info os.FileInfo) error { if info == nil { + // The root is examined with Lstat like every other entry, so a + // symlink root is reported as a leaf and never descended into. var statErr error - info, statErr = c.sftpCli.Stat(dirPath) + info, statErr = c.sftpCli.Lstat(dirPath) if statErr != nil { if fnErr := walkFn(dirPath, nil, statErr); fnErr != nil && fnErr != filepathSkipDir { return fnErr diff --git a/internal/fs/remote/client_test.go b/internal/fs/remote/client_test.go new file mode 100644 index 0000000..8587717 --- /dev/null +++ b/internal/fs/remote/client_test.go @@ -0,0 +1,71 @@ +package remote + +import ( + "os" + "path/filepath" + "testing" +) + +// B1 / N-P0-3: deleting a remote symlink that points to a directory wiped +// the target directory. +func TestRemoveRecursiveSymlinkToDir(t *testing.T) { + _, c, dir := setup(t) + target := filepath.Join(dir, "precious") + writeFile(t, filepath.Join(target, "a.txt"), 10, 0o644) + writeFile(t, filepath.Join(target, "sub", "b.txt"), 10, 0o644) + link := filepath.Join(dir, "link") + if err := os.Symlink(target, link); err != nil { + t.Fatal(err) + } + + if err := c.RemoveRecursive(link); err != nil { + t.Fatalf("RemoveRecursive(link): %v", err) + } + for _, p := range []string{"a.txt", "sub/b.txt"} { + if _, err := os.Stat(filepath.Join(target, p)); err != nil { + t.Errorf("deleting the link removed target content %s: %v", p, err) + } + } + if _, err := os.Lstat(link); !os.IsNotExist(err) { + t.Errorf("the link itself must be gone, Lstat err=%v", err) + } +} + +func TestRemoveRecursiveDirKeepsLinkedTarget(t *testing.T) { + _, c, dir := setup(t) + outside := filepath.Join(dir, "outside") + writeFile(t, filepath.Join(outside, "keep.txt"), 10, 0o644) + tree := filepath.Join(dir, "tree") + writeFile(t, filepath.Join(tree, "f.txt"), 10, 0o644) + if err := os.Symlink(outside, filepath.Join(tree, "to-outside")); err != nil { + t.Fatal(err) + } + if err := c.RemoveRecursive(tree); err != nil { + t.Fatalf("RemoveRecursive(tree): %v", err) + } + if _, err := os.Lstat(tree); !os.IsNotExist(err) { + t.Errorf("tree should be gone: %v", err) + } + if _, err := os.Stat(filepath.Join(outside, "keep.txt")); err != nil { + t.Errorf("symlink inside the tree was followed: %v", err) + } +} + +func TestRemoveRecursiveFileAndDir(t *testing.T) { + _, c, dir := setup(t) + f := filepath.Join(dir, "f.bin") + writeFile(t, f, 100, 0o644) + if err := c.RemoveRecursive(f); err != nil { + t.Fatal(err) + } + d := filepath.Join(dir, "d") + writeFile(t, filepath.Join(d, "x", "y", "z.bin"), 100, 0o644) + if err := c.RemoveRecursive(d); err != nil { + t.Fatal(err) + } + for _, p := range []string{f, d} { + if _, err := os.Lstat(p); !os.IsNotExist(err) { + t.Errorf("%s still exists", p) + } + } +} diff --git a/internal/fs/remote/harness_test.go b/internal/fs/remote/harness_test.go new file mode 100644 index 0000000..982e38a --- /dev/null +++ b/internal/fs/remote/harness_test.go @@ -0,0 +1,28 @@ +package remote + +import ( + "os" + "testing" + + "vcom/internal/testutil/sshtest" +) + +// setup starts an in-process SSH server (serving the real filesystem), +// connects an SSHClient to it with an isolated HOME and returns the client +// and a scratch directory. +func setup(t *testing.T) (*sshtest.Server, *SSHClient, string) { + t.Helper() + sshtest.IsolatedHome(t) + srv := sshtest.Start(t, sshtest.Options{}) + c, err := Connect(SSHHost{Name: "test", HostName: "127.0.0.1", Port: srv.Port(), User: "u", Password: srv.Password}) + if err != nil { + t.Fatalf("connect: %v", err) + } + t.Cleanup(func() { c.Close() }) + return srv, c, t.TempDir() +} + +func writeFile(t *testing.T, p string, size int, mode os.FileMode) { + t.Helper() + sshtest.WriteRandom(t, p, size, mode) +}