From 509439a772df4984acdd30deb2e76686a3bcbe5c Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Thu, 24 Aug 2023 11:43:11 +0200 Subject: [PATCH] bake: deny access to local dockerfile for remote invocation with local context Signed-off-by: CrazyMax --- bake/bake.go | 4 ++++ tests/bake.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/bake/bake.go b/bake/bake.go index 982bfb7b..f169a869 100644 --- a/bake/bake.go +++ b/bake/bake.go @@ -1060,6 +1060,10 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) { if err != nil { return nil, err } + } else if !build.IsRemoteURL(bi.DockerfilePath) && strings.HasPrefix(bi.ContextPath, "cwd://") && (inp != nil && build.IsRemoteURL(inp.URL)) { + if _, err := os.Stat(filepath.Join(path.Clean(strings.TrimPrefix(bi.ContextPath, "cwd://")), bi.DockerfilePath)); err == nil { + return nil, errors.Errorf("reading a dockerfile for a remote build invocation is currently not supported") + } } if strings.HasPrefix(bi.ContextPath, "cwd://") { bi.ContextPath = path.Clean(strings.TrimPrefix(bi.ContextPath, "cwd://")) diff --git a/tests/bake.go b/tests/bake.go index 975e4528..33d53461 100644 --- a/tests/bake.go +++ b/tests/bake.go @@ -27,6 +27,7 @@ var bakeTests = []func(t *testing.T, sb integration.Sandbox){ testBakeRemoteCmdContextEscapeRoot, testBakeRemoteCmdContextEscapeRelative, testBakeRemoteDockerfileCwd, + testBakeRemoteLocalContextRemoteDockerfile, } func testBakeLocal(t *testing.T, sb integration.Sandbox) { @@ -348,3 +349,42 @@ COPY foo /foo ) require.Error(t, err, out) } + +func testBakeRemoteLocalContextRemoteDockerfile(t *testing.T, sb integration.Sandbox) { + bakefile := []byte(` +target "default" { + context = BAKE_CMD_CONTEXT + dockerfile = "Dockerfile.app" +} +`) + dockerfileApp := []byte(` +FROM scratch +COPY foo /foo + `) + + dirSpec := tmpdir( + t, + fstest.CreateFile("docker-bake.hcl", bakefile, 0600), + ) + dirSrc := tmpdir( + t, + fstest.CreateFile("Dockerfile.app", dockerfileApp, 0600), + fstest.CreateFile("foo", []byte("foo"), 0600), + ) + + git, err := gitutil.New(gitutil.WithWorkingDir(dirSpec)) + require.NoError(t, err) + + gitutil.GitInit(git, t) + gitutil.GitAdd(git, t, "docker-bake.hcl") + gitutil.GitCommit(git, t, "initial commit") + addr := gitutil.GitServeHTTP(git, t) + + out, err := bakeCmd( + sb, + withDir(dirSrc), + withArgs(addr, "--set", "*.output=type=cacheonly"), + ) + require.Error(t, err, out) + require.Contains(t, out, "reading a dockerfile for a remote build invocation is currently not supported") +}