diff --git a/bake/bake.go b/bake/bake.go index 8dd3b979aeb..9a507cd6e83 100644 --- a/bake/bake.go +++ b/bake/bake.go @@ -1012,7 +1012,8 @@ func checkPath(p string) error { if err != nil { return err } - if strings.HasPrefix(rel, ".."+string(os.PathSeparator)) { + parts := strings.Split(rel, string(os.PathSeparator)) + if parts[0] == ".." { return errors.Errorf("path %s is outside of the working directory, please set BAKE_ALLOW_REMOTE_FS_ACCESS=1", p) } return nil diff --git a/tests/bake.go b/tests/bake.go index a462be70962..130fb0bdf15 100644 --- a/tests/bake.go +++ b/tests/bake.go @@ -22,6 +22,8 @@ var bakeTests = []func(t *testing.T, sb integration.Sandbox){ testBakeRemoteCmdContext, testBakeRemoteCmdContextOverride, testBakeRemoteContextSubdir, + testBakeRemoteCmdContextEscapeRoot, + testBakeRemoteCmdContextEscapeRelative, } func testBakeRemote(t *testing.T, sb integration.Sandbox) { @@ -161,3 +163,103 @@ COPY super-cool.txt / require.FileExists(t, filepath.Join(dirDest, "super-cool.txt")) } + +func testBakeRemoteCmdContextEscapeRoot(t *testing.T, sb integration.Sandbox) { + dirSrc := tmpdir( + t, + fstest.CreateFile("foo", []byte("foo"), 0600), + ) + dirSrc, err := filepath.Abs(dirSrc) + require.NoError(t, err) + + dirCurrent := tmpdir(t) + dirCurrent, err = filepath.Abs(dirCurrent) + require.NoError(t, err) + + bakefile := []byte(` +target "default" { + context = "cwd://` + dirSrc + `" + dockerfile-inline = <