Skip to content

Commit f37c1c6

Browse files
authored
Merge pull request moby#50404 from thaJeztah/saner_copy_defaults
daemon: ContainerExtractToDir: make AllowOverwriteDirWithFile opt-in
2 parents 4929f52 + 9bcb12a commit f37c1c6

File tree

7 files changed

+20
-18
lines changed

7 files changed

+20
-18
lines changed

daemon/archive.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,16 @@ func (daemon *Daemon) ContainerArchivePath(name string, path string) (content io
5757
// ContainerExtractToDir extracts the given archive to the specified location
5858
// in the filesystem of the container identified by the given name. The given
5959
// path must be of a directory in the container. If it is not, the error will
60-
// be an errdefs.InvalidParameter. If noOverwriteDirNonDir is true then it will
61-
// be an error if unpacking the given content would cause an existing directory
62-
// to be replaced with a non-directory and vice versa.
63-
func (daemon *Daemon) ContainerExtractToDir(name, path string, copyUIDGID, noOverwriteDirNonDir bool, content io.Reader) error {
60+
// be an errdefs.InvalidParameter. It returns an error if unpacking the given
61+
// content would cause an existing directory to be replaced with a non-directory
62+
// or vice versa, unless allowOverwriteDirWithFile is set to true.
63+
func (daemon *Daemon) ContainerExtractToDir(name, path string, copyUIDGID, allowOverwriteDirWithFile bool, content io.Reader) error {
6464
ctr, err := daemon.GetContainer(name)
6565
if err != nil {
6666
return err
6767
}
6868

69-
err = daemon.containerExtractToDir(ctr, path, copyUIDGID, noOverwriteDirNonDir, content)
69+
err = daemon.containerExtractToDir(ctr, path, copyUIDGID, allowOverwriteDirWithFile, content)
7070
if err != nil {
7171
if os.IsNotExist(err) {
7272
return containerFileNotFound{path, name}

daemon/archive_tarcopyoptions.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66

77
// defaultTarCopyOptions is the setting that is used when unpacking an archive
88
// for a copy API event.
9-
func (daemon *Daemon) defaultTarCopyOptions(noOverwriteDirNonDir bool) *archive.TarOptions {
9+
func (daemon *Daemon) defaultTarCopyOptions(allowOverwriteDirWithFile bool) *archive.TarOptions {
1010
return &archive.TarOptions{
11-
NoOverwriteDirNonDir: noOverwriteDirNonDir,
11+
NoOverwriteDirNonDir: !allowOverwriteDirWithFile,
1212
IDMap: daemon.idMapping,
1313
}
1414
}

daemon/archive_tarcopyoptions_unix.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import (
1414
"github.com/moby/sys/user"
1515
)
1616

17-
func (daemon *Daemon) tarCopyOptions(ctr *container.Container, noOverwriteDirNonDir bool) (*archive.TarOptions, error) {
17+
func (daemon *Daemon) tarCopyOptions(ctr *container.Container, allowOverwriteDirWithFile bool) (*archive.TarOptions, error) {
1818
if ctr.Config.User == "" {
19-
return daemon.defaultTarCopyOptions(noOverwriteDirNonDir), nil
19+
return daemon.defaultTarCopyOptions(allowOverwriteDirWithFile), nil
2020
}
2121

2222
uid, gid, err := getUIDGID(ctr.Config.User)
@@ -25,7 +25,7 @@ func (daemon *Daemon) tarCopyOptions(ctr *container.Container, noOverwriteDirNon
2525
}
2626

2727
return &archive.TarOptions{
28-
NoOverwriteDirNonDir: noOverwriteDirNonDir,
28+
NoOverwriteDirNonDir: !allowOverwriteDirWithFile,
2929
ChownOpts: &archive.ChownOpts{UID: uid, GID: gid},
3030
}, nil
3131
}

daemon/archive_unix.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (daemon *Daemon) containerArchivePath(container *container.Container, path
9797
// noOverwriteDirNonDir is true then it will be an error if unpacking the
9898
// given content would cause an existing directory to be replaced with a non-
9999
// directory and vice versa.
100-
func (daemon *Daemon) containerExtractToDir(container *container.Container, path string, copyUIDGID, noOverwriteDirNonDir bool, content io.Reader) error {
100+
func (daemon *Daemon) containerExtractToDir(container *container.Container, path string, copyUIDGID, allowOverwriteDirWithFile bool, content io.Reader) error {
101101
container.Lock()
102102
defer container.Unlock()
103103

@@ -131,13 +131,13 @@ func (daemon *Daemon) containerExtractToDir(container *container.Container, path
131131
return err
132132
}
133133

134-
options := daemon.defaultTarCopyOptions(noOverwriteDirNonDir)
134+
options := daemon.defaultTarCopyOptions(allowOverwriteDirWithFile)
135135

136136
if copyUIDGID {
137137
var err error
138138
// tarCopyOptions will appropriately pull in the right uid/gid for the
139139
// user/group and will set the options.
140-
options, err = daemon.tarCopyOptions(container, noOverwriteDirNonDir)
140+
options, err = daemon.tarCopyOptions(container, allowOverwriteDirWithFile)
141141
if err != nil {
142142
return err
143143
}

daemon/archive_windows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func (daemon *Daemon) containerArchivePath(container *container.Container, path
148148
// directory and vice versa.
149149
//
150150
// FIXME(thaJeztah): copyUIDGID is not supported on Windows, but currently ignored silently
151-
func (daemon *Daemon) containerExtractToDir(container *container.Container, path string, copyUIDGID, noOverwriteDirNonDir bool, content io.Reader) error {
151+
func (daemon *Daemon) containerExtractToDir(container *container.Container, path string, copyUIDGID, allowOverwriteDirWithFile bool, content io.Reader) error {
152152
container.Lock()
153153
defer container.Unlock()
154154

@@ -213,7 +213,7 @@ func (daemon *Daemon) containerExtractToDir(container *container.Container, path
213213
// return err
214214
// }
215215

216-
options := daemon.defaultTarCopyOptions(noOverwriteDirNonDir)
216+
options := daemon.defaultTarCopyOptions(allowOverwriteDirWithFile)
217217
if err := chrootarchive.UntarWithRoot(content, resolvedPath, options, container.BaseFS); err != nil {
218218
return err
219219
}

daemon/server/router/container/backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type execBackend interface {
2323
type copyBackend interface {
2424
ContainerArchivePath(name string, path string) (content io.ReadCloser, stat *container.PathStat, err error)
2525
ContainerExport(ctx context.Context, name string, out io.Writer) error
26-
ContainerExtractToDir(name, path string, copyUIDGID, noOverwriteDirNonDir bool, content io.Reader) error
26+
ContainerExtractToDir(name, path string, copyUIDGID, allowOverwriteDirWithFile bool, content io.Reader) error
2727
ContainerStatPath(name string, path string) (stat *container.PathStat, err error)
2828
}
2929

daemon/server/router/container/copy.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,10 @@ func (c *containerRouter) putContainersArchive(ctx context.Context, w http.Respo
9292
return err
9393
}
9494

95-
noOverwriteDirNonDir := httputils.BoolValue(r, "noOverwriteDirNonDir")
95+
// TODO(thaJeztah): reverse the default: deprecate noOverwriteDirNonDir and add "allowOverwriteDirWithFile" (or similar) argument to opt-in.
96+
allowOverwriteDirWithFile := !r.Form.Has("noOverwriteDirNonDir") || !httputils.BoolValue(r, "noOverwriteDirNonDir")
97+
9698
copyUIDGID := httputils.BoolValue(r, "copyUIDGID")
9799

98-
return c.backend.ContainerExtractToDir(v.Name, v.Path, copyUIDGID, noOverwriteDirNonDir, r.Body)
100+
return c.backend.ContainerExtractToDir(v.Name, v.Path, copyUIDGID, allowOverwriteDirWithFile, r.Body)
99101
}

0 commit comments

Comments
 (0)