| 1 | //go:build linux |
| 2 | |
| 3 | package containment |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "encoding/json" |
| 8 | "fmt" |
| 9 | "io" |
| 10 | "os" |
| 11 | "strconv" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | |
| 15 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 16 | "amadan.net/oficina/equipo/internal/runner/provider/claude" |
| 17 | "amadan.net/oficina/equipo/internal/runner/provider/codex" |
| 18 | ) |
| 19 | |
| 20 | // Fake CLIs run Bash through the pinned builders' namespace and mask layout. |
| 21 | // No model endpoint, real credential, or permissive sandbox fallback is used. |
| 22 | func TestEngineeringShellNesting(t *testing.T) { |
| 23 | for _, name := range []string{"claude", "codex"} { |
| 24 | t.Run(name, func(t *testing.T) { |
| 25 | m, l := fixture(t) |
| 26 | b := provider.Binding{Provider: name, Home: "/fake-runtime", CredentialHome: "/fake-source", RuntimeRoot: "/fake-homes"} |
| 27 | profile, _ := provider.Profile("implement") |
| 28 | in := provider.PolicyInputs{Action: "implement", Tools: profile.MCPTools()} |
| 29 | var err error |
| 30 | var prefix []string |
| 31 | switch name { |
| 32 | case "claude": |
| 33 | in, err = claude.PolicyInputs(b, in, "Engineering", "claude-opus-5-5") |
| 34 | if err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | var settings struct { |
| 38 | Sandbox struct { |
| 39 | Enabled, FailIfUnavailable bool |
| 40 | AllowUnsandboxedCommands, EnableWeakerNestedSandbox bool |
| 41 | Filesystem struct{ DenyRead []string } |
| 42 | } |
| 43 | } |
| 44 | if err = json.Unmarshal(in.Settings, &settings); err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | if !settings.Sandbox.Enabled || !settings.Sandbox.FailIfUnavailable || settings.Sandbox.AllowUnsandboxedCommands || settings.Sandbox.EnableWeakerNestedSandbox { |
| 48 | t.Fatal("unsafe Bash settings") |
| 49 | } |
| 50 | prefix = []string{"/usr/bin/bwrap", "--new-session", "--die-with-parent", "--unshare-net", "--ro-bind", "/", "/", "--bind", "/workspace", "/workspace", "--bind", "/tmp/claude-" + strconv.Itoa(os.Getuid()), "/tmp/claude-" + strconv.Itoa(os.Getuid())} |
| 51 | // The pinned CLI applies deny masks before its fresh /dev and /proc. |
| 52 | for _, path := range settings.Sandbox.Filesystem.DenyRead { |
| 53 | if path == "/home" || path == "/proc" || path == "/config" || path == "/tool" || path == "/egress" { |
| 54 | prefix = append(prefix, "--tmpfs", path, "--remount-ro", path) |
| 55 | } |
| 56 | } |
| 57 | prefix = append(prefix, "--dev", "/dev", "--unshare-pid", "--unshare-user", "--proc", "/proc", "--disable-userns", "--") |
| 58 | case "codex": |
| 59 | in, err = codex.PolicyInputs(b, in, "gpt-6-luna") |
| 60 | if err != nil { |
| 61 | t.Fatal(err) |
| 62 | } |
| 63 | var settings struct { |
| 64 | Permissions map[string]struct { |
| 65 | Filesystem map[string]string |
| 66 | Network struct{ Enabled bool } |
| 67 | } |
| 68 | } |
| 69 | if err = json.Unmarshal(in.Settings, &settings); err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | policy := settings.Permissions["equipo"] |
| 73 | if policy.Network.Enabled || policy.Filesystem["/proc"] != "deny" || policy.Filesystem["/tool"] != "deny" { |
| 74 | t.Fatal("unsafe shell policy") |
| 75 | } |
| 76 | prefix = []string{"/usr/bin/bwrap", "--unshare-user", "--unshare-pid", "--unshare-net", "--ro-bind", "/", "/", "--bind", "/workspace", "/workspace", "--bind", "/tmp", "/tmp"} |
| 77 | for _, path := range []string{"/home/runner", "/proc", "/tool", "/config", "/egress"} { |
| 78 | prefix = append(prefix, "--tmpfs", path, "--remount-ro", path) |
| 79 | } |
| 80 | prefix = append(prefix, "--") |
| 81 | } |
| 82 | raw, _ := json.Marshal(prefix) |
| 83 | p := launch(t, m, l, fmt.Sprintf(`import json,subprocess,os |
| 84 | os.makedirs("/tmp/claude-"+str(os.getuid()),exist_ok=True) |
| 85 | raw=json.loads(%q) or [] |
| 86 | prefix=[]; i=0 |
| 87 | while i<len(raw): |
| 88 | if raw[i]=='--tmpfs' and not os.path.exists(raw[i+1]): i+=4; continue |
| 89 | prefix.append(raw[i]); i+=1 |
| 90 | r=subprocess.run(prefix+['/usr/bin/bash','-c','printf shell-started; test -x /usr/bin/make; test -x /usr/bin/python3'],capture_output=True,text=True) |
| 91 | assert r.returncode==0,(r.returncode,r.stderr) |
| 92 | print(r.stdout,flush=True) |
| 93 | `, string(raw))) |
| 94 | out, _ := io.ReadAll(p.Stdout()) |
| 95 | stderr, _ := io.ReadAll(p.Stderr()) |
| 96 | exit, err := p.Wait(context.Background()) |
| 97 | if err != nil || exit.Code != 0 || !strings.Contains(string(out), "shell-started") { |
| 98 | t.Fatalf("shell: %+v %v %s %s", exit, err, out, stderr) |
| 99 | } |
| 100 | }) |
| 101 | } |
| 102 | } |
| 103 | |