From d34103b0d967e8f6da448d1dae42d95ce75c80ac Mon Sep 17 00:00:00 2001 From: Justin Chadwell Date: Tue, 6 Jun 2023 16:40:18 +0200 Subject: [PATCH] bake: fix potential context entitlements escape Signed-off-by: Justin Chadwell --- bake/bake.go | 3 +- tests/bake.go | 102 +++++++++++++++++++++++++++++++++++++++++++ tests/integration.go | 6 +++ 3 files changed, 110 insertions(+), 1 deletion(-) diff --git a/bake/bake.go b/bake/bake.go index 8dd3b979..9a507cd6 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 a462be70..130fb0bd 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 = <