Merge PR #10737: ssh: Fix HOME prefix matching for copied paths

This commit is contained in:
Kovid Goyal 2026-10-25 19:07:17 +05:30
commit 2b39989766
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
4 changed files with 58 additions and 5 deletions

View file

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

View file

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

View file

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

View file

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