Files
pmg/sandbox/util/env_test.go
T
Abhisek DattaandGitHub c7244f921a feat: Add support for environment protection (scrubbing) (#327)
* feat: Add support for environment variable protection for sandbox

* chore: Update dangerous env var list

* fix: Split profiles for improved environment protection

* fix: pipx sandbox profile separation

* chore: Show sandbox scrub info on error exit

* fix: Code review fixes

* test: Add e2e for sandbox environment scrubbing
2026-06-11 11:40:33 +05:30

97 lines
3.0 KiB
Go

package util
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestScrubEnv(t *testing.T) {
tests := []struct {
name string
env []string
opts EnvScrubOptions
wantKept []string
wantRemoved []string
}{
{
name: "built-in deny scrubs known secret",
env: []string{"PATH=/usr/bin", "AWS_SECRET_ACCESS_KEY=abc"},
wantKept: []string{"PATH=/usr/bin"},
wantRemoved: []string{"AWS_SECRET_ACCESS_KEY"},
},
{
name: "case-insensitive match",
env: []string{"aws_secret_access_key=abc"},
wantKept: []string{},
wantRemoved: []string{"aws_secret_access_key"},
},
{
name: "allow suppresses built-in deny",
env: []string{"NPM_TOKEN=secret"},
opts: EnvScrubOptions{Allow: []string{"NPM_TOKEN"}},
wantKept: []string{"NPM_TOKEN=secret"},
wantRemoved: nil,
},
{
name: "profile deny glob scrubs",
env: []string{"MY_CUSTOM_TOKEN=x", "OTHER=y"},
opts: EnvScrubOptions{Deny: []string{"*_TOKEN"}},
wantKept: []string{"OTHER=y"},
wantRemoved: []string{"MY_CUSTOM_TOKEN"},
},
{
name: "allow glob wins over profile deny glob",
env: []string{"npm_config_registry=x", "npm_config_token=y"},
opts: EnvScrubOptions{Deny: []string{"*_TOKEN", "npm_config_*"}, Allow: []string{"npm_config_*"}},
wantKept: []string{"npm_config_registry=x", "npm_config_token=y"},
wantRemoved: nil,
},
{
name: "protected essential never scrubbed even under broad deny",
env: []string{"PATH=/usr/bin", "HOME=/home/u", "LC_ALL=en_US.UTF-8"},
opts: EnvScrubOptions{Deny: []string{"*"}},
wantKept: []string{"PATH=/usr/bin", "HOME=/home/u", "LC_ALL=en_US.UTF-8"},
wantRemoved: nil,
},
{
name: "non-sensitive variables are kept",
env: []string{"FOO=bar", "EDITOR=vim"},
wantKept: []string{"FOO=bar", "EDITOR=vim"},
wantRemoved: nil,
},
{
name: "entry without equals is treated as a name",
env: []string{"GITHUB_TOKEN", "PLAINNAME"},
wantKept: []string{"PLAINNAME"},
wantRemoved: []string{"GITHUB_TOKEN"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ScrubEnv(tt.env, tt.opts)
assert.Equal(t, tt.wantKept, got.Env)
assert.Equal(t, tt.wantRemoved, got.Removed)
})
}
}
func TestScrubEnv_RemovesEntirelyNotBlanked(t *testing.T) {
got := ScrubEnv([]string{"GITHUB_TOKEN=secret"}, EnvScrubOptions{})
require.Empty(t, got.Env)
assert.NotContains(t, got.Env, "GITHUB_TOKEN=")
assert.Equal(t, []string{"GITHUB_TOKEN"}, got.Removed)
}
func TestScrubEnv_NoCatchAllsInBuiltinList(t *testing.T) {
// A novel token name must NOT be scrubbed by the default list (catch-alls
// are opt-in per profile, never built in).
got := ScrubEnv([]string{"SOME_RANDOM_TOKEN=x"}, EnvScrubOptions{})
assert.Equal(t, []string{"SOME_RANDOM_TOKEN=x"}, got.Env)
assert.Empty(t, got.Removed)
}