diff --git a/docs/changelog.rst b/docs/changelog.rst index fa14c09c8..2af6ecc80 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -395,6 +395,10 @@ Detailed list of changes downloaded only once and images are decoded only once for all such outputs. Specifying the same output file twice is now an error (:pull:`10744`) +- Clipboard kitten: Correctly detect duplicate output files when a path contains + a symlink followed by ``..``, avoiding overwrites of the same target and + erroneous rejection of distinct output files + - Kittens: When saving files, such as themes, config files or clipboard output, write to the target of a symlink even when the target does not yet exist, instead of replacing the symlink with a regular file (:pull:`10740`) diff --git a/kittens/clipboard/read.go b/kittens/clipboard/read.go index a548bed11..cff17b86b 100644 --- a/kittens/clipboard/read.go +++ b/kittens/clipboard/read.go @@ -424,11 +424,14 @@ func (self read_outputs) cleanup() { } } -// Return the path an output file will be written to, following symlinks the -// same way utils.AtomicUpdateFile does. The path is made absolute first so -// that symlinks in the working directory are resolved as well. +// Resolve the path in the same order as utils.AtomicUpdateFile, then resolve +// symlinks in the absolute working-directory prefix as well. func resolved_output_path(path string) (string, error) { - path, err := filepath.Abs(path) + path, err := utils.EvalSymlinksThatExist(path) + if err != nil { + return "", err + } + path, err = filepath.Abs(path) if err != nil { return "", err } diff --git a/kittens/clipboard/read_test.go b/kittens/clipboard/read_test.go index 885dd1e27..b2d5270e8 100644 --- a/kittens/clipboard/read_test.go +++ b/kittens/clipboard/read_test.go @@ -260,6 +260,90 @@ func TestClipboardReadDuplicateOutputFiles(t *testing.T) { } } +func TestClipboardReadDuplicateOutputFilesSymlinkParent(t *testing.T) { + for _, existing := range []bool{false, true} { + for _, absolute_link := range []bool{false, true} { + t.Run(fmt.Sprintf("existing=%t/absolute_link=%t", existing, absolute_link), func(t *testing.T) { + dir := t.TempDir() + t.Chdir(dir) + if err := os.MkdirAll("real/sub", 0o755); err != nil { + t.Fatal(err) + } + link := "real/sub" + if absolute_link { + link = filepath.Join(dir, link) + } + if err := os.Symlink(link, "link"); err != nil { + t.Fatal(err) + } + if existing { + for _, path := range []string{"target", "real/target"} { + if err := os.WriteFile(path, []byte("old contents"), 0o600); err != nil { + t.Fatal(err) + } + } + } + // Concatenate to preserve link/.. in the absolute path as well. + for _, path := range []string{"link/../target", dir + "/link/../target"} { + outputs := []*Output{{arg: path}, {arg: "real/target"}} + if err := check_for_duplicate_output_files(outputs); err == nil { + t.Errorf("Duplicate output files not rejected: %s and real/target", path) + } + outputs[1].arg = "target" + if err := check_for_duplicate_output_files(outputs); err != nil { + t.Errorf("Distinct output files rejected: %s and target: %s", path, err) + } + } + }) + } + } +} + +func TestClipboardReadDuplicateOutputFilesSymlinkCwd(t *testing.T) { + for _, existing := range []bool{false, true} { + t.Run(fmt.Sprintf("existing=%t", existing), func(t *testing.T) { + dir := t.TempDir() + if err := os.MkdirAll(filepath.Join(dir, "real", "sub"), 0o755); err != nil { + t.Fatal(err) + } + link := filepath.Join(dir, "link") + if err := os.Symlink("real/sub", link); err != nil { + t.Fatal(err) + } + t.Chdir(link) + t.Setenv("PWD", link) + paths := []string{filepath.Join(dir, "target"), filepath.Join(dir, "real", "target")} + if existing { + for _, path := range paths { + if err := os.WriteFile(path, []byte("old contents"), 0o600); err != nil { + t.Fatal(err) + } + } + } + output := &Output{arg: "../target"} + defer output.cleanup() + output.add_data([]byte("clipboard contents")) + output.commit() + if output.err != nil { + t.Fatal(output.err) + } + // Compare detection with the actual write, including when PWD + // names a symlink rather than the physical working directory. + for _, path := range paths { + data, err := os.ReadFile(path) + if err != nil && !os.IsNotExist(err) { + t.Fatal(err) + } + written := string(data) == "clipboard contents" + err = check_for_duplicate_output_files([]*Output{output, {arg: path}}) + if (err != nil) != written { + t.Errorf("Duplicate detection disagrees with write to %s: written=%t error=%v", path, written, err) + } + } + }) + } +} + func TestClipboardOutputCommitAtomic(t *testing.T) { dest := filepath.Join(t.TempDir(), "output") old_data := bytes.Repeat([]byte("old contents\n"), 1024)