mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
feat(sandbox): scrub sensitive environment variables from package managers
Implements process-level environment variable protection per the spec. When the sandbox is enabled, credential-bearing variables are removed from the package manager child process before it is spawned, defending against supply chain attacks that harvest secrets from the environment. - DANGEROUS_ENV_VARS: curated default deny list of known secret names (no generic *_TOKEN/*_SECRET catch-alls); ScrubEnv matcher supports case- insensitive globs so profiles can opt into broader denies. - EnvironmentPolicy (environment.allow / environment.deny) on sandbox profiles, merged under inheritance; deep-copied on resolve. - npm/pypi profiles re-allow their own ecosystem's auth vars so package managers keep working; other ecosystems' and cloud creds stay scrubbed. - New 'env' --sandbox-allow type (and overlay support via the same path): allow-only, value kept verbatim (not path-resolved), governed by lockdown. - Enforced in executor.ApplySandbox as the last step before launch, after overlay and runtime overrides merge; scrubbed names logged for audit. https://claude.ai/code/session_017Da1sAYLYpeEgogm6f9VYW
This commit is contained in:
@@ -2,6 +2,7 @@ package executor
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
@@ -57,6 +58,72 @@ func TestApplyRuntimeOverrides_Exec(t *testing.T) {
|
||||
assert.Contains(t, policy.Process.AllowExec, "/usr/bin/curl")
|
||||
}
|
||||
|
||||
func TestApplyRuntimeOverrides_Env(t *testing.T) {
|
||||
policy := &sandbox.SandboxPolicy{
|
||||
Environment: sandbox.EnvironmentPolicy{
|
||||
Allow: []string{"NPM_TOKEN"},
|
||||
},
|
||||
}
|
||||
|
||||
applyRuntimeOverrides(policy, []config.SandboxAllowOverride{
|
||||
{Type: config.SandboxAllowEnv, Value: "AWS_PROFILE", Raw: "env=AWS_PROFILE"},
|
||||
})
|
||||
|
||||
assert.Contains(t, policy.Environment.Allow, "NPM_TOKEN")
|
||||
assert.Contains(t, policy.Environment.Allow, "AWS_PROFILE")
|
||||
}
|
||||
|
||||
func TestScrubEnv_RemovesDeniedKeepsAllowed(t *testing.T) {
|
||||
policy := &sandbox.SandboxPolicy{
|
||||
Name: "test",
|
||||
Environment: sandbox.EnvironmentPolicy{
|
||||
Allow: []string{"NPM_TOKEN"},
|
||||
},
|
||||
}
|
||||
|
||||
cmd := &exec.Cmd{Env: []string{
|
||||
"PATH=/usr/bin",
|
||||
"NPM_TOKEN=keep-me",
|
||||
"AWS_SECRET_ACCESS_KEY=scrub-me",
|
||||
"GITHUB_TOKEN=scrub-me-too",
|
||||
}}
|
||||
|
||||
scrubEnv(cmd, policy)
|
||||
|
||||
assert.Contains(t, cmd.Env, "PATH=/usr/bin")
|
||||
assert.Contains(t, cmd.Env, "NPM_TOKEN=keep-me")
|
||||
assert.NotContains(t, cmd.Env, "AWS_SECRET_ACCESS_KEY=scrub-me")
|
||||
assert.NotContains(t, cmd.Env, "GITHUB_TOKEN=scrub-me-too")
|
||||
}
|
||||
|
||||
func TestScrubEnv_AllowOverrideUnscrubs(t *testing.T) {
|
||||
policy := &sandbox.SandboxPolicy{Name: "test"}
|
||||
|
||||
// Simulate a --sandbox-allow env=AWS_PROFILE override having been merged.
|
||||
applyRuntimeOverrides(policy, []config.SandboxAllowOverride{
|
||||
{Type: config.SandboxAllowEnv, Value: "AWS_SESSION_TOKEN", Raw: "env=AWS_SESSION_TOKEN"},
|
||||
})
|
||||
|
||||
cmd := &exec.Cmd{Env: []string{"AWS_SESSION_TOKEN=kept"}}
|
||||
scrubEnv(cmd, policy)
|
||||
|
||||
assert.Contains(t, cmd.Env, "AWS_SESSION_TOKEN=kept")
|
||||
}
|
||||
|
||||
func TestScrubEnv_NilEnvPopulatedThenScrubbed(t *testing.T) {
|
||||
t.Setenv("GITHUB_TOKEN", "should-be-scrubbed")
|
||||
t.Setenv("PMG_ENV_SCRUB_MARKER", "kept")
|
||||
|
||||
policy := &sandbox.SandboxPolicy{Name: "test"}
|
||||
cmd := &exec.Cmd{Env: nil}
|
||||
|
||||
scrubEnv(cmd, policy)
|
||||
|
||||
require.NotNil(t, cmd.Env)
|
||||
assert.Contains(t, cmd.Env, "PMG_ENV_SCRUB_MARKER=kept")
|
||||
assert.NotContains(t, cmd.Env, "GITHUB_TOKEN=should-be-scrubbed")
|
||||
}
|
||||
|
||||
func TestApplyRuntimeOverrides_NetConnect(t *testing.T) {
|
||||
policy := &sandbox.SandboxPolicy{
|
||||
Network: sandbox.NetworkPolicy{
|
||||
@@ -250,7 +317,6 @@ func TestApplyRuntimeOverrides_VariableDenyNotRemovedByAbsoluteOverride(t *testi
|
||||
assert.Equal(t, []string{"${CWD}/blocked.txt"}, policy.Filesystem.DenyWrite)
|
||||
}
|
||||
|
||||
|
||||
func TestApplyProjectOverlayAppendsEntries(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
repo := "/repo/example"
|
||||
|
||||
Reference in New Issue
Block a user