diff --git a/fileutils/copy_test.go b/fileutils/copy_test.go index ccdcc901..6899e032 100644 --- a/fileutils/copy_test.go +++ b/fileutils/copy_test.go @@ -2,6 +2,7 @@ package fileutils import ( "os" + "path" "path/filepath" "testing" @@ -9,6 +10,54 @@ import ( "github.com/spf13/afero" ) +// failingOpenFs wraps an afero.Fs and makes Open fail for one specific path, +// while every other operation (including Stat) is delegated unchanged. It +// simulates a directory that can be stat-ed but not opened/read — for example +// an unreadable sub-directory, or one whose permissions changed or that was +// removed after its parent was listed (a TOCTOU race) — encountered during a +// recursive copy. +type failingOpenFs struct { + afero.Fs + failOpen string +} + +func (f *failingOpenFs) Open(name string) (afero.File, error) { + if path.Clean(name) == path.Clean(f.failOpen) { + return nil, os.ErrPermission + } + return f.Fs.Open(name) +} + +// CopyDir is documented to keep going when it hits an error and to report the +// error afterwards. A sub-directory that cannot be opened must therefore yield +// an error (and leave the other, readable entries copied) rather than +// panicking on a nil directory handle. +func TestCopyDirUnreadableSubdirReturnsError(t *testing.T) { + mem := afero.NewMemMapFs() + if err := mem.MkdirAll("/srcdir/sub", 0o755); err != nil { + t.Fatal(err) + } + if err := afero.WriteFile(mem, "/srcdir/ok.txt", []byte("readable"), 0o644); err != nil { + t.Fatal(err) + } + + afs := &failingOpenFs{Fs: mem, failOpen: "/srcdir/sub"} + + err := Copy(afs, "/srcdir", "/dstdir", 0o644, 0o755) + if err == nil { + t.Fatal("expected an error when a sub-directory cannot be opened") + } + + // The readable sibling must still have been copied (continue-on-error). + data, readErr := afero.ReadFile(afs, "/dstdir/ok.txt") + if readErr != nil { + t.Fatalf("readable sibling was not copied: %v", readErr) + } + if string(data) != "readable" { + t.Fatalf("unexpected copied content: %q", string(data)) + } +} + // Copying an in-scope directory that contains a symlink whose target escapes // the user's scope must not dereference that symlink into the destination. // Otherwise a scoped user could exfiltrate out-of-scope file content via the diff --git a/fileutils/dir.go b/fileutils/dir.go index e0b049db..4bd7c925 100644 --- a/fileutils/dir.go +++ b/fileutils/dir.go @@ -23,7 +23,12 @@ func CopyDir(afs afero.Fs, source, dest string, fileMode, dirMode fs.FileMode) e return err } - dir, _ := afs.Open(source) + dir, err := afs.Open(source) + if err != nil { + return err + } + defer dir.Close() + obs, err := dir.Readdir(-1) if err != nil { return err