diff --git a/docs/changelog.rst b/docs/changelog.rst index e92660dfd..0a411d763 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -351,6 +351,9 @@ Detailed list of changes remote control authentication by masquerading as a cancellation of an asynchronous request +- ssh kitten: Fix copying files outside HOME whose paths share its prefix, + such as :file:`/home/alice-backup` when HOME is :file:`/home/alice` + - ssh kitten: Preserve the permissions of copied directories, including nested directories, instead of using the permissions of the top level directory and the remote umask (:pull:`10727`) diff --git a/kittens/ssh/config.go b/kittens/ssh/config.go index 89af0339a..f9455b7c7 100644 --- a/kittens/ssh/config.go +++ b/kittens/ssh/config.go @@ -201,11 +201,8 @@ func get_arcname(loc, dest, home string) (arcname string) { arcname = dest } else { arcname = filepath.Clean(loc) - if strings.HasPrefix(arcname, home) { - ra, err := filepath.Rel(home, arcname) - if err == nil { - arcname = ra - } + if ra, err := filepath.Rel(home, arcname); err == nil && filepath.IsLocal(ra) { + arcname = ra } } prefix := "home/" diff --git a/kittens/ssh/config_test.go b/kittens/ssh/config_test.go index 9115527f7..557e090f7 100644 --- a/kittens/ssh/config_test.go +++ b/kittens/ssh/config_test.go @@ -161,6 +161,29 @@ func TestSSHConfigParsing(t *testing.T) { } +func TestSSHCopyArcnames(t *testing.T) { + for _, tc := range []struct { + name, loc, dest, home, expected string + }{ + {"home", "/home/alice", "", "/home/alice", "home/."}, + {"inside home", "/home/alice/.config/app", "", "/home/alice", "home/.config/app"}, + {"sibling directory", "/home/alice-backup/config", "", "/home/alice", "root/home/alice-backup/config"}, + {"sibling user", "/home/alice2/config", "", "/home/alice", "root/home/alice2/config"}, + {"outside home", "/etc/config", "", "/home/alice", "root/etc/config"}, + {"cleaned path", "/home/alice/../alice-backup/config", "", "/home/alice", "root/home/alice-backup/config"}, + {"trailing slash", "/home/alice/.config/app", "", "/home/alice/", "home/.config/app"}, + {"root home", "/etc/config", "", "/", "home/etc/config"}, + {"relative destination", "/home/alice-backup/config", "config", "/home/alice", "home/config"}, + {"absolute destination", "/home/alice/config", "/etc/config", "/home/alice", "root/etc/config"}, + } { + t.Run(tc.name, func(t *testing.T) { + if actual := get_arcname(tc.loc, tc.dest, tc.home); actual != tc.expected { + t.Fatalf("Incorrect arcname: %q != %q", actual, tc.expected) + } + }) + } +} + func TestSSHCopyDirectoryMetadata(t *testing.T) { root := filepath.Join(t.TempDir(), "tree") dirs := []struct { diff --git a/kitty_tests/ssh.py b/kitty_tests/ssh.py index 01b9f7144..f8aee6e9c 100644 --- a/kitty_tests/ssh.py +++ b/kitty_tests/ssh.py @@ -160,6 +160,36 @@ copy --exclude **/w.* --exclude **/r d1 ) self.ae(len(glob.glob(f'{remote_home}/{tname}/*/xterm-kitty')), 2) + def test_ssh_copy_home_prefix(self): + for sh in self.all_possible_sh: + with self.subTest(interpreter=sh), tempfile.TemporaryDirectory() as tdir: + local_home = os.path.join(tdir, 'alice') + remote_home = os.path.join(tdir, 'remote') + sibling = local_home + '-backup' + for d in (local_home, remote_home, sibling): + os.mkdir(d) + source = os.path.join(sibling, 'config') + with open(source, 'w') as f: + f.write('copied contents') + # Remove the local file after creating the archive so that the + # bootstrap must restore it at the same absolute path. + launcher = os.path.join(tdir, 'launcher') + with open(launcher, 'w') as f: + f.write(f'#!/bin/sh\nrm -f {shlex.quote(source)} || exit 1\nexec sh "$@"\n') + os.chmod(launcher, 0o755) + self.check_bootstrap( + sh, + remote_home, + test_script='env; exit 0', + SHELL_INTEGRATION_VALUE='', + conf=f'copy {shlex.quote(source)}', + home=local_home, + launcher=launcher, + ) + with open(source) as f: + self.ae(f.read(), 'copied contents') + self.ae(glob.glob(os.path.join(remote_home, '.kitty-ssh-kitten-untar-*')), []) + def test_ssh_copy_directory_merge(self): bootstrap_utils = os.path.join(kitty_base_dir, 'shell-integration', 'ssh', 'bootstrap-utils.sh') for sh in self.all_possible_sh: