Cleanup previous PR

Walk up from the drop destination using .. relative to its file
descriptor instead of its path, so the check is not fooled if the
destination is moved or replaced while the kitten is running. Also skip
the staging directory when copying, which guards against routes to it
the up-front check cannot see, such as bind mounts. Use readable
subtest names, check the chmod error in test cleanup and link the PR in
the changelog.
This commit is contained in:
Kovid Goyal 2026-10-29 17:32:35 +05:30
parent a7b391a875
commit 40562972f4
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
3 changed files with 141 additions and 19 deletions

View file

@ -397,7 +397,7 @@ Detailed list of changes
- Drag and drop kitten: Reject copying or moving a directory into itself or
one of its subdirectories before staging files, preventing recursive
copying of the staging directory.
copying of the staging directory (:pull:`10769`)
- Clipboard kitten: Fix a race when replacing an existing output file and
losing the original file if writing it fails. Also preserve the permissions

View file

@ -14,8 +14,10 @@ import (
"slices"
"strconv"
"strings"
"syscall"
"github.com/emmansun/base64"
"golang.org/x/sys/unix"
"github.com/kovidgoyal/go-parallel"
"github.com/kovidgoyal/kitty/tools/utils"
@ -132,6 +134,12 @@ func uniqify_child_names(names []string, is_case_sensitive_filesystem bool) []st
func do_local_copy(ctx context.Context, dest_dir *os.File, uri_list []string, action int, copy_mode string) (err error) {
disallow_hardlinks := action == copy_on_drop && copy_mode == "independent"
// Never copy the staging directory into itself, even if it is reachable
// from a source by a route the up-front check cannot see, such as a bind mount.
staging_info, err := dest_dir.Stat()
if err != nil {
return err
}
var src_file *os.File
defer func() {
if src_file != nil {
@ -179,7 +187,10 @@ func do_local_copy(ctx context.Context, dest_dir *os.File, uri_list []string, ac
err = utils.CopyFolderContents(ctx, src_file, d, utils.CopyFolderOptions{
Disallow_hardlinks: disallow_hardlinks,
Filter_files: func(parent *os.File, child os.FileInfo) bool {
return child.IsDir() || child.Mode().IsRegular() || child.Mode()&fs.ModeSymlink != 0
if child.IsDir() {
return !os.SameFile(child, staging_info)
}
return child.Mode().IsRegular() || child.Mode()&fs.ModeSymlink != 0
},
})
d.Close()
@ -645,32 +656,51 @@ func (dnd *dnd) new_tdir() (dir_file *os.File, err error) {
return utils.CreateDirAt(dnd.base_tempdir, name, 0o700)
}
func destination_is_within_source(source_info os.FileInfo, dest_dir *os.File) (bool, error) {
path, err := filepath.EvalSymlinks(dest_dir.Name())
// Identify a file by device and inode. Unlike os.SameFile this also works
// with the os.FileInfo objects returned by utils.StatAt.
func file_identity(info os.FileInfo) (dev, ino uint64, err error) {
switch s := info.Sys().(type) {
case *syscall.Stat_t:
return uint64(s.Dev), uint64(s.Ino), nil
case *unix.Stat_t:
return uint64(s.Dev), uint64(s.Ino), nil
}
return 0, 0, fmt.Errorf("unknown stat result type for %s: %T", info.Name(), info.Sys())
}
func same_file(a, b os.FileInfo) (bool, error) {
adev, aino, err := file_identity(a)
if err != nil {
return false, err
}
if path, err = filepath.Abs(path); err != nil {
bdev, bino, err := file_identity(b)
if err != nil {
return false, err
}
return adev == bdev && aino == bino, nil
}
// Walk up from dest_dir via .. relative to its file descriptor rather than
// its path, so that the result is correct even if dest_dir has been moved or
// its path contains symlinks. Comparing identities handles case aliases and
// stat only requires search permission on the ancestors.
func destination_is_within_source(source_info os.FileInfo, dest_dir *os.File) (bool, error) {
info, err := dest_dir.Stat()
if err != nil {
return false, err
}
// Compare directory identities to handle case aliases, without opening
// ancestors for reading: traversal only requires search permission.
for {
if os.SameFile(source_info, info) {
return true, nil
for rel := ".."; ; rel = filepath.Join(rel, "..") {
if same, err := same_file(source_info, info); err != nil || same {
return same, err
}
parent := filepath.Dir(path)
if parent == path {
return false, nil
}
path = parent
if info, err = os.Stat(path); err != nil {
parent, err := utils.StatAt(dest_dir, rel)
if err != nil {
return false, err
}
if at_root, err := same_file(parent, info); err != nil || at_root {
return false, err
}
info = parent
}
}

View file

@ -107,6 +107,13 @@ func TestLocalCopyHardlinkPolicy(t *testing.T) {
}
func runLocalDrop(t *testing.T, dest string, uris []parsed_uri, action int, copy_mode string) (*dnd, error) {
t.Helper()
return runLocalDropAfter(t, dest, uris, action, copy_mode, nil)
}
// Like runLocalDrop but calls before_drop, if not nil, after the kitten has
// opened dest and before the drop is processed.
func runLocalDropAfter(t *testing.T, dest string, uris []parsed_uri, action int, copy_mode string, before_drop func()) (*dnd, error) {
t.Helper()
lp, err := loop.New()
if err != nil {
@ -121,6 +128,9 @@ func runLocalDrop(t *testing.T, dest string, uris []parsed_uri, action int, copy
d.remove_tdir()
d.drop_output_dir.Close()
})
if before_drop != nil {
before_drop()
}
d.drop_status = drop_status{action: action, reading_data: true, uri_list: uris}
if err := d.all_mime_data_dropped(); err != nil {
return d, err
@ -261,12 +271,14 @@ func TestLocalMoveMissingSource(t *testing.T) {
}
}
var action_names = map[int]string{copy_on_drop: "copy", move_on_drop: "move"}
func TestLocalDropIntoSourceDirectory(t *testing.T) {
for _, action := range []int{copy_on_drop, move_on_drop} {
for _, copy_mode := range []string{"auto", "independent"} {
for _, location := range []string{"self", "descendant"} {
for _, alias := range []string{"none", "source-parent", "destination", "case"} {
t.Run(fmt.Sprintf("%d/%s/%s/%s", action, copy_mode, location, alias), func(t *testing.T) {
t.Run(fmt.Sprintf("%s/%s/%s/%s", action_names[action], copy_mode, location, alias), func(t *testing.T) {
root := t.TempDir()
source := filepath.Join(root, "source")
files := map[string]string{"data.txt": "source data", "child/nested/keep.txt": "destination data"}
@ -334,7 +346,7 @@ func TestLocalDropOutsideSourceDirectory(t *testing.T) {
for _, action := range []int{copy_on_drop, move_on_drop} {
for _, copy_mode := range []string{"auto", "independent"} {
for _, location := range []string{"sibling", "symlink", "search-only-parent"} {
t.Run(fmt.Sprintf("%d/%s/%s", action, copy_mode, location), func(t *testing.T) {
t.Run(fmt.Sprintf("%s/%s/%s", action_names[action], copy_mode, location), func(t *testing.T) {
root := t.TempDir()
source := filepath.Join(root, "source")
buildTree(t, source, map[string]string{"data.txt": "source data", "child/keep.txt": "destination data"}, nil)
@ -353,7 +365,11 @@ func TestLocalDropOutsideSourceDirectory(t *testing.T) {
}
if location == "search-only-parent" {
parent := filepath.Dir(dest)
t.Cleanup(func() { os.Chmod(parent, 0755) })
t.Cleanup(func() {
if err := os.Chmod(parent, 0755); err != nil {
t.Error(err)
}
})
if err := os.Chmod(parent, 0111); err != nil {
t.Fatal(err)
}
@ -384,6 +400,82 @@ func TestLocalDropOutsideSourceDirectory(t *testing.T) {
}
}
func TestLocalDropIntoMovedDestination(t *testing.T) {
for _, action := range []int{copy_on_drop, move_on_drop} {
for _, location := range []string{"outside", "inside"} {
t.Run(action_names[action]+"/"+location, func(t *testing.T) {
root := t.TempDir()
source := filepath.Join(root, "source")
buildTree(t, source, map[string]string{"data.txt": "source data", "child/keep.txt": "keep"}, nil)
dest, moved_to := filepath.Join(root, "dest"), filepath.Join(root, "moved")
if location == "inside" {
dest, moved_to = filepath.Join(root, "dest"), filepath.Join(source, "child", "moved")
}
if err := os.Mkdir(dest, 0755); err != nil {
t.Fatal(err)
}
// Replace the original path so that a path based check would
// look at the wrong directory.
d, err := runLocalDropAfter(t, dest, []parsed_uri{{kind: parsed_uri_file, path: source}}, action, "auto", func() {
if err := os.Rename(dest, moved_to); err != nil {
t.Fatal(err)
}
if err := os.Mkdir(dest, 0755); err != nil {
t.Fatal(err)
}
})
if location == "inside" {
if err == nil {
t.Fatal("dropping a directory into a destination moved inside it must fail")
}
if d.tdir_counter != 0 {
t.Errorf("invalid drop started staging before failing: %v", err)
}
if data, err := os.ReadFile(filepath.Join(source, "data.txt")); err != nil || string(data) != "source data" {
t.Errorf("source changed: %q, %v", data, err)
}
return
}
if err != nil {
t.Fatal(err)
}
if data, err := os.ReadFile(filepath.Join(moved_to, "source", "data.txt")); err != nil || string(data) != "source data" {
t.Errorf("copied data: %q, %v", data, err)
}
})
}
}
}
func TestLocalCopySkipsStagingDirectory(t *testing.T) {
// Simulate a route to the staging directory that the up-front check
// cannot detect, such as a bind mount, by bypassing that check.
for _, action := range []int{copy_on_drop, move_on_drop} {
t.Run(action_names[action], func(t *testing.T) {
source := filepath.Join(t.TempDir(), "source")
buildTree(t, source, map[string]string{"data.txt": "source data"}, nil)
staging_path := filepath.Join(source, "staging")
if err := os.Mkdir(staging_path, 0700); err != nil {
t.Fatal(err)
}
staging, err := os.Open(staging_path)
if err != nil {
t.Fatal(err)
}
defer staging.Close()
if err := do_local_copy(context.Background(), staging, []string{source}, action, "auto"); err != nil {
t.Fatal(err)
}
if data, err := os.ReadFile(filepath.Join(staging_path, "source", "data.txt")); err != nil || string(data) != "source data" {
t.Errorf("copied data: %q, %v", data, err)
}
if _, err := os.Lstat(filepath.Join(staging_path, "source", "staging")); !os.IsNotExist(err) {
t.Errorf("staging directory was copied into itself: %v", err)
}
})
}
}
func TestLocalCopyDuplicateBasenames(t *testing.T) {
for _, tc := range []struct {
name string