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
7 changes: 4 additions & 3 deletions build/policy_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"time"

"github.com/docker/buildx/policy"
"github.com/docker/buildx/util/osutil"
"github.com/docker/buildx/util/sourcemeta"
"github.com/moby/buildkit/client/llb"
gwclient "github.com/moby/buildkit/frontend/gateway/client"
Expand Down Expand Up @@ -200,7 +201,7 @@ func (p *policyPathFSRef) resolve(name string) (fs.StatFS, string, error) {
if err != nil {
return nil, "", err
}
return cwd, filepath.Clean(v), nil
return cwd, osutil.SanitizePath(v), nil
}

contextFS, err := p.getContextFS()
Expand Down Expand Up @@ -278,11 +279,11 @@ func normalizeLocalPolicyPath(name, contextDir string) string {
if rel, err := filepath.Rel(contextDir, name); err == nil {
rel = filepath.Clean(rel)
if rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
return rel
return osutil.SanitizePath(rel)
}
}
}
return filepath.Clean(name)
return osutil.SanitizePath(name)
}

type memoizedPolicyFS struct {
Expand Down
49 changes: 49 additions & 0 deletions build/policy_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,55 @@ import (
"github.com/stretchr/testify/require"
)

func TestLoadPolicyDataLocalPaths(t *testing.T) {
dir := t.TempDir()
policyData := []byte("package docker\n")
policyRelPath := filepath.Join("policy", "allow.rego")
require.NoError(t, os.MkdirAll(filepath.Join(dir, "policy"), 0700))
require.NoError(t, os.WriteFile(filepath.Join(dir, policyRelPath), policyData, 0600))

t.Run("context-relative", func(t *testing.T) {
provider := newPolicyPathFS(context.Background(), nil, policyOpt{
ContextDir: dir,
})

dt, ok, err := loadPolicyData(provider, policyRelPath)
require.NoError(t, err)
require.True(t, ok)
require.Equal(t, policyData, dt)
})

t.Run("context-absolute", func(t *testing.T) {
provider := newPolicyPathFS(context.Background(), nil, policyOpt{
ContextDir: dir,
})

dt, ok, err := loadPolicyData(provider, filepath.Join(dir, policyRelPath))
require.NoError(t, err)
require.True(t, ok)
require.Equal(t, policyData, dt)
})

t.Run("cwd", func(t *testing.T) {
cwd, err := os.Getwd()
require.NoError(t, err)
require.NoError(t, os.Chdir(dir))
t.Cleanup(func() {
require.NoError(t, os.Chdir(cwd))
})

provider := newPolicyPathFS(context.Background(), nil, policyOpt{})
dt, ok, err := loadPolicyData(provider, "cwd://"+policyRelPath)
require.NoError(t, err)
require.True(t, ok)
require.Equal(t, policyData, dt)
})
}

func TestNormalizeLocalPolicyPath(t *testing.T) {
require.Equal(t, "policy/allow.rego", normalizeLocalPolicyPath(filepath.Join("policy", "allow.rego"), ""))
}

func TestMemoizedPolicyFSRefCountedClose(t *testing.T) {
var initCalls int
var closeCalls int
Expand Down
Loading