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
6 changes: 6 additions & 0 deletions pkg/skaffold/parser/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,12 @@ func processEachDependency(ctx context.Context, d latest.ConfigDependency, cfgOp
cfgOpts.file = path
cfgOpts.selection = d.Names
cfgOpts.isRemote = isRemoteCfg
// Disable the `cmd` template function for remote/untrusted config dependencies
// to prevent arbitrary command execution via malicious skaffold.yaml files.
if isRemoteCfg {
util.CmdAllowed = false
defer func() { util.CmdAllowed = true }()
}
depConfigs, _, err := getConfigs(ctx, cfgOpts, opts, r)
if err != nil {
return nil, err
Expand Down
7 changes: 7 additions & 0 deletions pkg/skaffold/util/env_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ var (
funcsMap = template.FuncMap{
"cmd": runCmdFunc,
}
// CmdAllowed controls whether the `cmd` template function is permitted.
// Set to false when processing remote/untrusted config dependencies to
// prevent arbitrary command execution.
CmdAllowed = true
)

// ExpandEnvTemplate parses and executes template s with an optional environment map
Expand Down Expand Up @@ -140,6 +144,9 @@ func MapToFlag(m map[string]*string, flag string) ([]string, error) {
}

func runCmdFunc(name string, args ...string) (string, error) {
if !CmdAllowed {
return "", fmt.Errorf("the 'cmd' template function is not allowed in remote dependency configs for security reasons")
}
cmd := exec.Command(name, args...)
out, err := RunCmdOut(context.TODO(), cmd)
return strings.TrimSpace(string(out)), err
Expand Down
20 changes: 20 additions & 0 deletions pkg/skaffold/util/env_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package util

import (
"fmt"
"strings"
"testing"

"github.com/GoogleContainerTools/skaffold/v2/testutil"
Expand Down Expand Up @@ -180,6 +181,25 @@ func TestMapToFlag(t *testing.T) {
}
}

func TestCmdBlockedWhenNotAllowed(t *testing.T) {
testutil.Run(t, "cmd blocked in remote config context", func(t *testutil.T) {
t.Override(&CmdAllowed, false)
_, err := ExpandEnvTemplate(`{{cmd "echo" "hello"}}`, nil)
t.CheckError(true, err)
if err != nil && !strings.Contains(err.Error(), "not allowed in remote dependency") {
t.Errorf("expected 'not allowed in remote dependency' error, got: %v", err)
}
})

testutil.Run(t, "cmd allowed in local config context", func(t *testutil.T) {
t.Override(&CmdAllowed, true)
t.Override(&DefaultExecCommand, testutil.CmdRunOut("echo hello", "hello"))
out, err := ExpandEnvTemplate(`{{cmd "echo" "hello"}}`, nil)
t.CheckNoError(err)
t.CheckDeepEqual("hello", out)
})
}

func TestRunCmdFunc(t *testing.T) {
tests := []struct {
description string
Expand Down