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 <noreply@anthropic.com>
This commit is contained in:
vrubelroman 2026-10-31 23:07:16 +03:00
parent 2f3491c1db
commit 8d6fe48536
3 changed files with 105 additions and 2 deletions

View file

@ -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

View file

@ -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)
}
}
}

View file

@ -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)
}