Skip to content
Draft
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
2 changes: 1 addition & 1 deletion build/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func runExec(a *goyek.A, cmdLine string, opts ...cmd.Option) bool {
a.Helper()
a.Log("Exec: ", cmdLine)
a.Log("Exec: ", cmd.Mask(cmdLine))
return cmd.Exec(a, cmdLine, opts...)
}

Expand Down
41 changes: 41 additions & 0 deletions build/security_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"context"
"io"
"strings"
"testing"

"github.com/goyek/goyek/v3"
)

func TestRunExec_Masking(t *testing.T) {
sb := &strings.Builder{}

// Middleware to capture output
mw := func(next goyek.Runner) goyek.Runner {
return func(in goyek.Input) goyek.Result {
in.Output = io.MultiWriter(in.Output, sb)
return next(in)
}
}

f := &goyek.Flow{}
f.Define(goyek.Task{
Name: "test",
Action: func(a *goyek.A) {
runExec(a, "SECRET=password echo hello")
},
})
f.Use(mw)

_ = f.Execute(context.Background(), []string{"test"})

got := sb.String()
if strings.Contains(got, "password") {
t.Errorf("Secret 'password' found in logs: %s", got)
}
if !strings.Contains(got, "SECRET=[MASKED]") {
t.Errorf("Masked secret not found in logs: %s", got)
}
}
32 changes: 32 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,38 @@ func Exec(a *goyek.A, cmdLine string, opts ...Option) bool {
return true
}

// Mask returns a masked command line where the values of leading environment variables are replaced with [MASKED].
func Mask(cmdLine string) string {
envs, args, err := shellwords.ParseWithEnvs(cmdLine)
if err != nil || len(envs) == 0 {
return cmdLine
}

var sb strings.Builder
for i, env := range envs {
if i > 0 {
sb.WriteByte(' ')
}
key, _, _ := strings.Cut(env, "=")
sb.WriteString(key)
sb.WriteString("=[MASKED]")
}

for _, arg := range args {
if sb.Len() > 0 {
sb.WriteByte(' ')
}
if strings.Contains(arg, " ") {
sb.WriteByte('"')
sb.WriteString(arg)
sb.WriteByte('"')
} else {
sb.WriteString(arg)
}
}
return sb.String()
}

// Dir is an option to set the working directory.
func Dir(s string) Option {
return func(_ *goyek.A, cmd *exec.Cmd) {
Expand Down
61 changes: 61 additions & 0 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,64 @@ func TestExec_EnvOnly(t *testing.T) {
}()
Exec(&goyek.A{}, "FOO=bar")
}

func TestMask(t *testing.T) {
tests := []struct {
name string
cmdLine string
want string
}{
{
name: "empty",
cmdLine: "",
want: "",
},
{
name: "no env vars",
cmdLine: "ls -l",
want: "ls -l",
},
{
name: "one env var",
cmdLine: "FOO=bar ls -l",
want: "FOO=[MASKED] ls -l",
},
{
name: "multiple env vars",
cmdLine: "FOO=bar BAZ=qux ls -l",
want: "FOO=[MASKED] BAZ=[MASKED] ls -l",
},
{
name: "env var with space",
cmdLine: `FOO="bar baz" ls -l`,
want: "FOO=[MASKED] ls -l",
},
{
name: "quoted arg",
cmdLine: `echo "hello world"`,
want: `echo "hello world"`,
},
{
name: "env var and quoted arg",
cmdLine: `FOO=bar echo "hello world"`,
want: `FOO=[MASKED] echo "hello world"`,
},
{
name: "parse error",
cmdLine: `FOO="bar`,
want: `FOO="bar`,
},
{
name: "no trailing space",
cmdLine: "FOO=bar ls",
want: "FOO=[MASKED] ls",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Mask(tt.cmdLine); got != tt.want {
t.Errorf("Mask() = %q, want %q", got, tt.want)
}
})
}
}