Apply deferred directory permissions children first when extracting tar

A parent directory without search permission prevents changing the
permissions of its children, so apply the deferred chmods in reverse
order, as GNU tar does. Also add a test and changelog entry for #10676.
This commit is contained in:
Kovid Goyal 2026-10-16 19:12:51 +05:30
parent 92a3723d42
commit f426c08f5d
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
4 changed files with 42 additions and 3 deletions

View file

@ -253,6 +253,9 @@ Detailed list of changes
- Remote control: Fix ``kitten @ action --match`` ignoring the match and
running the action on the active window (:iss:`10672`)
- diff kitten: Fix comparing remote directories over SSH failing when they
contain read-only directories (:pull:`10676`)
- Graphics protocol: Add two new :ref:`indexed color image formats
<indexed_color_data>` whose pixels are indices into the terminal color
palette, so that images automatically change color along with the terminal

View file

@ -153,7 +153,8 @@ func main(_ *cli.Command, opts_ *Options, args []string) (rc int, err error) {
init_caches()
defer func() {
for tdir := range remote_dirs {
// Directories extracted from the remote host can be read-only
// Directories extracted from the remote host can be read-only.
// This is best effort cleanup of a temp dir, so errors are ignored.
_ = filepath.WalkDir(tdir, func(path string, d fs.DirEntry, err error) error {
if err == nil && d.IsDir() {
_ = os.Chmod(path, 0o700)

View file

@ -400,8 +400,10 @@ func ExtractAllFromTar(tr *tar.Reader, dest_path string, optss ...TarExtractOpti
}
}
}
for _, chmod := range dir_chmods {
if err = chmod(); err != nil {
// Apply in reverse so that children are processed before their parents,
// as a parent without search permission prevents chmod of its children
for i := len(dir_chmods) - 1; i >= 0; i-- {
if err = dir_chmods[i](); err != nil {
return
}
}

View file

@ -276,3 +276,36 @@ func TestTarExtractReadOnlyDirectory(t *testing.T) {
t.Fatalf("Permissions of read-only directory not preserved: %v", st.Mode())
}
}
func TestTarExtractUnsearchableParentDirectory(t *testing.T) {
dest := t.TempDir()
parent := filepath.Join(dest, "a")
t.Cleanup(func() {
_ = os.Chmod(parent, 0700)
_ = os.Chmod(filepath.Join(parent, "b"), 0700)
})
if _, err := ExtractAllFromTar(make_tar(t,
tar_entry{name: "a/", typeflag: tar.TypeDir, mode: 0600},
tar_entry{name: "a/b/", typeflag: tar.TypeDir, mode: 0555},
tar_entry{name: "a/b/file.txt", body: "contents"},
), dest); err != nil {
t.Fatal(err)
}
st, err := os.Stat(parent)
if err != nil {
t.Fatal(err)
}
if st.Mode().Perm() != 0600 {
t.Fatalf("Permissions of parent directory not preserved: %v", st.Mode())
}
if err = os.Chmod(parent, 0700); err != nil {
t.Fatal(err)
}
assert_file_contents(t, filepath.Join(parent, "b", "file.txt"), "contents")
if st, err = os.Stat(filepath.Join(parent, "b")); err != nil {
t.Fatal(err)
}
if st.Mode().Perm() != 0555 {
t.Fatalf("Permissions of child directory not preserved: %v", st.Mode())
}
}