Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions client/container_copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -86,6 +87,13 @@ func (cli *Client) CopyFromContainer(ctx context.Context, containerID, srcPath s
if err != nil {
return nil, stat, fmt.Errorf("unable to get resource stat from response: %s", err)
}

// Strict validation: if path ends with "/", it must be a directory
if strings.HasSuffix(srcPath, "/") && stat.Mode&os.ModeDir == 0 {
resp.Body.Close()
return nil, stat, fmt.Errorf("path %q ends with '/', but is not a directory. The filename, directory name, or volume label syntax is incorrect", srcPath)
}

return resp.Body, stat, err
}

Expand Down
14 changes: 12 additions & 2 deletions integration/container/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ func TestCopyFromContainerPathDoesNotExist(t *testing.T) {
}

func TestCopyFromContainerPathIsNotDir(t *testing.T) {
skip.If(t, testEnv.UsingSnapshotter(), "FIXME: https://github.com/moby/moby/issues/47107")
ctx := setupTest(t)

apiClient := testEnv.APIClient()
Expand All @@ -47,7 +46,18 @@ func TestCopyFromContainerPathIsNotDir(t *testing.T) {
path = "c:/windows/system32/drivers/etc/hosts/"
expected = "The filename, directory name, or volume label syntax is incorrect."
}
_, _, err := apiClient.CopyFromContainer(ctx, cid, path)

rc, _, err := apiClient.CopyFromContainer(ctx, cid, path)
if err == nil {
if rc != nil {
defer rc.Close()
content, _ := io.ReadAll(rc)
t.Logf("Expected an error, but got nil. Received data: %s", string(content))
} else {
t.Logf("Expected an error, but got nil. Received no data.")
}
}

assert.ErrorContains(t, err, expected)
}

Expand Down
Loading