From 49e569518895e15b0a2de8ba3b664aa8eb0b0f12 Mon Sep 17 00:00:00 2001 From: Zhizhen He Date: Mon, 26 Oct 2026 13:12:42 +0800 Subject: [PATCH 1/2] clipboard: Resolve symlinks before cleaning output paths --- docs/changelog.rst | 4 ++++ kittens/clipboard/read.go | 13 +++++++----- kittens/clipboard/read_test.go | 39 ++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 5 deletions(-) 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..6bde0e02f 100644 --- a/kittens/clipboard/read.go +++ b/kittens/clipboard/read.go @@ -425,12 +425,15 @@ 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. +// same way utils.AtomicUpdateFile does. Make it absolute without cleaning it +// so symlinks are resolved before any following ".." components. func resolved_output_path(path string) (string, error) { - path, err := filepath.Abs(path) - if err != nil { - return "", err + if !filepath.IsAbs(path) { + wd, err := os.Getwd() + if err != nil { + return "", err + } + path = wd + string(os.PathSeparator) + path } return utils.EvalSymlinksThatExist(path) } diff --git a/kittens/clipboard/read_test.go b/kittens/clipboard/read_test.go index 885dd1e27..95eff1c8b 100644 --- a/kittens/clipboard/read_test.go +++ b/kittens/clipboard/read_test.go @@ -260,6 +260,45 @@ 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 TestClipboardOutputCommitAtomic(t *testing.T) { dest := filepath.Join(t.TempDir(), "output") old_data := bytes.Repeat([]byte("old contents\n"), 1024) From 4596a9baab799f162bf36e6c0f733c21ab1efd81 Mon Sep 17 00:00:00 2001 From: Zhizhen He Date: Mon, 26 Oct 2026 13:36:01 +0800 Subject: [PATCH 2/2] clipboard: Match duplicate checks to atomic write resolution --- kittens/clipboard/read.go | 18 +++++++------- kittens/clipboard/read_test.go | 45 ++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/kittens/clipboard/read.go b/kittens/clipboard/read.go index 6bde0e02f..cff17b86b 100644 --- a/kittens/clipboard/read.go +++ b/kittens/clipboard/read.go @@ -424,16 +424,16 @@ func (self read_outputs) cleanup() { } } -// Return the path an output file will be written to, following symlinks the -// same way utils.AtomicUpdateFile does. Make it absolute without cleaning it -// so symlinks are resolved before any following ".." components. +// 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) { - if !filepath.IsAbs(path) { - wd, err := os.Getwd() - if err != nil { - return "", err - } - path = wd + string(os.PathSeparator) + path + path, err := utils.EvalSymlinksThatExist(path) + if err != nil { + return "", err + } + path, err = filepath.Abs(path) + if err != nil { + return "", err } return utils.EvalSymlinksThatExist(path) } diff --git a/kittens/clipboard/read_test.go b/kittens/clipboard/read_test.go index 95eff1c8b..b2d5270e8 100644 --- a/kittens/clipboard/read_test.go +++ b/kittens/clipboard/read_test.go @@ -299,6 +299,51 @@ func TestClipboardReadDuplicateOutputFilesSymlinkParent(t *testing.T) { } } +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)