-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsandbox_env_test.go
More file actions
104 lines (95 loc) · 2.79 KB
/
Copy pathsandbox_env_test.go
File metadata and controls
104 lines (95 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package claude
import (
"testing"
agentadaptor "github.com/agent-dance/agent-adaptor/driver"
)
func withGeteuid(t *testing.T, uid int) {
t.Helper()
prev := geteuid
geteuid = func() int { return uid }
t.Cleanup(func() { geteuid = prev })
}
func envContains(env []agentadaptor.EnvBinding, name, value string) bool {
for _, b := range env {
if b.Name == name && b.Value == value {
return true
}
}
return false
}
func envHas(env []agentadaptor.EnvBinding, name string) bool {
for _, b := range env {
if b.Name == name {
return true
}
}
return false
}
func TestEnsureRootSandboxEnvInjectsUnderRootWithSkipPermissions(t *testing.T) {
withGeteuid(t, 0)
env := []agentadaptor.EnvBinding{{Name: "HOME", Value: "/root"}}
out := ensureRootSandboxEnv([]string{"--print", "--dangerously-skip-permissions"}, env)
if !envContains(out, "IS_SANDBOX", "1") {
t.Fatalf("expected IS_SANDBOX=1 to be injected, got %#v", out)
}
if len(env) != 1 || env[0].Name != "HOME" {
t.Fatalf("input env must not be mutated, got %#v", env)
}
}
func TestEnsureRootSandboxEnvNoOpWhenNotRoot(t *testing.T) {
withGeteuid(t, 1000)
env := []agentadaptor.EnvBinding{{Name: "HOME", Value: "/home/dev"}}
out := ensureRootSandboxEnv([]string{"--dangerously-skip-permissions"}, env)
if envHas(out, "IS_SANDBOX") {
t.Fatalf("non-root should not receive IS_SANDBOX, got %#v", out)
}
}
func TestEnsureRootSandboxEnvNoOpWithoutSkipPermissions(t *testing.T) {
withGeteuid(t, 0)
env := []agentadaptor.EnvBinding{{Name: "HOME", Value: "/root"}}
out := ensureRootSandboxEnv([]string{"--print", "--output-format", "stream-json"}, env)
if envHas(out, "IS_SANDBOX") {
t.Fatalf("no skip-permissions flag, no injection expected, got %#v", out)
}
}
func TestEnsureRootSandboxEnvRespectsHostIntent(t *testing.T) {
withGeteuid(t, 0)
cases := []struct {
name string
env []agentadaptor.EnvBinding
}{
{
name: "host set IS_SANDBOX=0 keeps it",
env: []agentadaptor.EnvBinding{{Name: "IS_SANDBOX", Value: "0"}},
},
{
name: "host set IS_SANDBOX=1 keeps it",
env: []agentadaptor.EnvBinding{{Name: "IS_SANDBOX", Value: "1"}},
},
{
name: "host set CLAUDE_CODE_BUBBLEWRAP takes precedence",
env: []agentadaptor.EnvBinding{{Name: "CLAUDE_CODE_BUBBLEWRAP", Value: "1"}},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
out := ensureRootSandboxEnv([]string{"--dangerously-skip-permissions"}, tc.env)
isSandboxCount := 0
bubblewrapCount := 0
for _, b := range out {
if b.Name == "IS_SANDBOX" {
isSandboxCount++
}
if b.Name == "CLAUDE_CODE_BUBBLEWRAP" {
bubblewrapCount++
}
}
if isSandboxCount > 1 {
t.Fatalf("IS_SANDBOX duplicated, got %#v", out)
}
if bubblewrapCount > 1 {
t.Fatalf("CLAUDE_CODE_BUBBLEWRAP duplicated, got %#v", out)
}
})
}
}