diff --git a/integration-cli/docker_cli_run_test.go b/integration-cli/docker_cli_run_test.go index fc71f01820..9e5a90bedf 100644 --- a/integration-cli/docker_cli_run_test.go +++ b/integration-cli/docker_cli_run_test.go @@ -946,3 +946,17 @@ func TestModeHostname(t *testing.T) { logDone("run - hostname and several network modes") } + +func TestRootWorkdir(t *testing.T) { + s, _, err := cmd(t, "run", "--workdir", "/", "busybox", "pwd") + if err != nil { + t.Fatal(s, err) + } + if s != "/\n" { + t.Fatalf("pwd returned '%s' (expected /\\n)", s) + } + + deleteAllContainers() + + logDone("run - workdir /") +} diff --git a/pkg/symlink/fs.go b/pkg/symlink/fs.go index 257491f91b..da9c590675 100644 --- a/pkg/symlink/fs.go +++ b/pkg/symlink/fs.go @@ -13,8 +13,6 @@ const maxLoopCounter = 100 // FollowSymlink will follow an existing link and scope it to the root // path provided. func FollowSymlinkInScope(link, root string) (string, error) { - prev := "/" - root, err := filepath.Abs(root) if err != nil { return "", err @@ -25,10 +23,16 @@ func FollowSymlinkInScope(link, root string) (string, error) { return "", err } + if link == root { + return root, nil + } + if !strings.HasPrefix(filepath.Dir(link), root) { return "", fmt.Errorf("%s is not within %s", link, root) } + prev := "/" + for _, p := range strings.Split(link, "/") { prev = filepath.Join(prev, p) prev = filepath.Clean(prev)