claude: a step runs sandboxed shell commands; refusals name the rule
Production 2026-11-22 (task tkwkpraqeeer, claude-2, a token connection): Engineering's Bash was refused with provider_policy_rejected for nearly every command, so it could not cd, run Go or read /opt. Cause: token launches set CLAUDE_CODE_SUBPROCESS_ENV_SCRUB=1 (provider/token.go). In that mode the pinned CLI 2.1.312 turns sandbox auto-approval off (isAutoAllowSupported is !scrub). Only its read-only allowlist still passes (ls/cat/sed/grep/pwd in the working directory); everything else is an ask, which reaches StepPermission, and that allowed only file tools. The transcript's own reason codes: cd-compound-read (cd ...; grep relative), too-complex (export PATH=...$PATH), no-rule-match (/opt/go/bin/go ...), and outside the working directory (ls /opt/go/bin). Reproduced with the pinned binary against a local fake API: scrub on refuses exactly that set, scrub off auto-allows it. StepPermission (11-07) predates token connections (11-10); the policy's intent is "ordinary sandboxed commands retain automatic approval". Fix: StepPermission allows Bash when the policy keeps every command in the CLI's sandbox (enabled, failIfUnavailable, no unsandboxed fallback, no excluded commands, no weaker nesting), the request has only the pinned fields (no dangerouslyDisableSandbox/sandbox), and the command is not one an exact Bash ask rule keeps for a person (the certification controls). Verified with the pinned CLI in scrub mode: an approved command still cannot read /config or /home/runner, sees no credential variables, has no network, and its write to /config stays in the sandbox. ReadPermission (Review, answers, Copy) still runs no shell through its callback. Every refusal is now "provider_policy_rejected: <fixed rule and what to do>" (absolute path under /workspace, protected path, outside /workspace, /research read-only, field, sandbox, kept for a person, tool unavailable, read-only step). It never repeats the request. Model-facing text only; no user-facing copy changes. Tests first: TestStepPermissionRunsSandboxedShell, TestStepPermissionNamesTheRule (and TestStepPermission, TestStepPermissionReadsResearch updated) failed before the fix. Mutation checks (go test -run TestStepPermission ./internal/runner/provider/claude/), each failed, then restored: - ignore the sandbox settings: "sandbox off: shell allowed" - drop the ask-rule reservation: "allowed printf equipo-cert-allow" - drop the weaker-nesting check: "weaker nested: shell allowed" - drop the dangerouslyDisableSandbox key check: message lacks "sandbox" - deny all Bash (old behaviour): "refused curl example.com" - refusal echoes the path: message contains the request text - generic refusal: message "provider_policy_rejected" - ReadPermission routes Bash: "read-only step allowed a shell read"
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+255
−13
internal/runner/provider/claude/answers.go+118 −10internal/runner/provider/claude/answers_test.go+137 −3
diff --git a/internal/runner/provider/claude/answers.go b/internal/runner/provider/claude/answers.go| index cebe0c5f..69b76220 100644 |
| --- a/internal/runner/provider/claude/answers.go |
| +++ b/internal/runner/provider/claude/answers.go |
| @@ -173,28 +173,137 @@ func permissionResult(q permissionRequest, a provider.Answer, policy provider.Po |
| return json.Marshal(map[string]any{"behavior": "allow", "updatedInput": input}) |
| } |
| +// A step's permission callback refuses with a fixed reason: the rule |
| +// that refused and what to do instead. It never repeats the request. |
| +const ( |
| + refusePath = "file tools need an absolute path under /workspace; for Glob and Grep, pass path, such as /workspace" |
| + refuseOutside = "this step can use files only under /workspace (and read /research)" |
| + refuseProtected = "that path is protected: credentials, runtime files and links are never available" |
| + refuseResearch = "/research is read-only: use Read, Glob and Grep there" |
| + refuseFields = "the request has a field this step does not accept" |
| + refuseSandbox = "commands run only inside the sandbox; dangerouslyDisableSandbox is not available" |
| + refusePerson = "this command is kept for a person's approval, and nobody is asked during a step" |
| + refuseTool = "this tool is not available to this step" |
| + refuseReadOnly = "this step only reads: use Read, Glob and Grep under /workspace" |
| + refuseShell = "this step's policy does not keep commands in the sandbox, so it runs none" |
| +) |
| + |
| +func refusal(rule string) json.RawMessage { |
| + out, _ := json.Marshal(map[string]string{"behavior": "deny", "message": provider.ErrPolicy.Error() + ": " + rule}) |
| + return out |
| +} |
| + |
| // StepPermission answers an Engineering step's permission callback when |
| -// nobody is asked: a workspace file action passes the same protected-path |
| -// and field checks as an approved one and is allowed; everything else is |
| -// denied. The result is the callback's JSON text. |
| +// nobody is asked. A workspace file action passes the same protected-path |
| +// and field checks as an approved one and is allowed. A shell command is |
| +// allowed when the policy confines every command to the CLI's sandbox |
| +// (sandboxedShell): the CLI asks for one only where its own automatic |
| +// approval is off or declines, as it always is for a token connection |
| +// (CLAUDE_CODE_SUBPROCESS_ENV_SCRUB). Everything else is denied with the |
| +// rule that refused it. The result is the callback's JSON text. |
| func StepPermission(raw json.RawMessage, policy provider.Policy) json.RawMessage { |
| - deny, _ := permissionResult(permissionRequest{}, provider.Answer{Behavior: "deny"}, policy) |
| var q permissionRequest |
| d := json.NewDecoder(bytes.NewReader(raw)) |
| d.DisallowUnknownFields() |
| if !uniqueJSON(raw) || d.Decode(&q) != nil || q.Tool == "" || !uniqueJSON(q.Input) || len(raw) > MaxFrameBytes { |
| - return deny |
| + return refusal(refuseFields) |
| } |
| - if protectedNativePath(q, policy) || !workspaceNativePath(q) && !researchNativePath(q) { |
| - return deny |
| + if q.Tool == "Bash" { |
| + if rule := sandboxedShell(q, policy); rule != "" { |
| + return refusal(rule) |
| + } |
| + } else if rule := stepPathRule(q, policy); rule != "" { |
| + return refusal(rule) |
| } |
| out, err := permissionResult(q, provider.Answer{Behavior: "allow"}, policy) |
| if err != nil { |
| - return deny |
| + return refusal(refuseFields) |
| } |
| return out |
| } |
| +// stepPathRule names the rule refusing a native file action in a step, or |
| +// "" when the action is inside the step's profile. |
| +func stepPathRule(q permissionRequest, policy provider.Policy) string { |
| + if !slices.Contains([]string{"Read", "Write", "Edit", "Glob", "Grep"}, q.Tool) { |
| + return refuseTool |
| + } |
| + var input map[string]json.RawMessage |
| + if json.Unmarshal(q.Input, &input) != nil { |
| + return refuseFields |
| + } |
| + path := "" |
| + for _, key := range []string{"file_path", "path"} { |
| + if raw, ok := input[key]; ok { |
| + if json.Unmarshal(raw, &path) != nil { |
| + return refuseFields |
| + } |
| + break |
| + } |
| + } |
| + switch { |
| + case path == "" || strings.HasPrefix(path, "~") || !filepath.IsAbs(path): |
| + return refusePath |
| + case protectedNativePath(q, policy): |
| + return refuseProtected |
| + case workspaceNativePath(q) || researchNativePath(q): |
| + return "" |
| + case filepath.Clean(path) == "/research" || strings.HasPrefix(filepath.Clean(path), "/research/"): |
| + return refuseResearch |
| + } |
| + return refuseOutside |
| +} |
| + |
| +// sandboxedShell names the rule refusing a shell command in a step, or "" |
| +// when the step may run it: the policy keeps every command in the CLI's |
| +// sandbox (enabled, no unconfined fallback or exemption, no weaker nesting), |
| +// the request cannot ask to leave it, and the command is not one an |
| +// explicit ask rule keeps for a person (the certification controls). |
| +func sandboxedShell(q permissionRequest, policy provider.Policy) string { |
| + var input map[string]json.RawMessage |
| + var command string |
| + if json.Unmarshal(q.Input, &input) != nil { |
| + return refuseFields |
| + } |
| + for _, key := range []string{"dangerouslyDisableSandbox", "sandbox"} { |
| + if _, ok := input[key]; ok { |
| + return refuseSandbox |
| + } |
| + } |
| + if json.Unmarshal(input["command"], &command) != nil || strings.TrimSpace(command) == "" { |
| + return refuseFields |
| + } |
| + var m manifest |
| + var settings struct { |
| + Sandbox *struct { |
| + Enabled, FailIfUnavailable, AllowUnsandboxedCommands *bool |
| + ExcludedCommands []string |
| + EnableWeakerNestedSandbox *bool |
| + } |
| + Permissions struct{ Ask []string } |
| + } |
| + if json.Unmarshal(policy.Manifest, &m) != nil || json.Unmarshal(m.Settings, &settings) != nil { |
| + return refuseShell |
| + } |
| + s := settings.Sandbox |
| + if s == nil || s.Enabled == nil || !*s.Enabled || s.FailIfUnavailable == nil || !*s.FailIfUnavailable || s.AllowUnsandboxedCommands == nil || *s.AllowUnsandboxedCommands || len(s.ExcludedCommands) != 0 || s.EnableWeakerNestedSandbox != nil && *s.EnableWeakerNestedSandbox { |
| + return refuseShell |
| + } |
| + for _, rule := range settings.Permissions.Ask { |
| + reserved, ok := strings.CutPrefix(rule, "Bash(") |
| + if !ok { |
| + continue |
| + } |
| + reserved, ok = strings.CutSuffix(reserved, ")") |
| + // Only exact command rules are known here; any other Bash ask |
| + // rule keeps every command for a person. |
| + if !ok || strings.ContainsAny(reserved, "*:") || strings.TrimSpace(command) == reserved { |
| + return refusePerson |
| + } |
| + } |
| + return "" |
| +} |
| + |
| // ReadPermission is StepPermission for a read-only step: only Read, Glob |
| // and Grep inside the workspace; every write, shell or other tool is |
| // denied. |
| @@ -203,8 +312,7 @@ func ReadPermission(raw json.RawMessage, policy provider.Policy) json.RawMessage |
| Tool string `json:"tool_name"` |
| } |
| if json.Unmarshal(raw, &q) != nil || !slices.Contains([]string{"Read", "Glob", "Grep"}, q.Tool) { |
| - deny, _ := permissionResult(permissionRequest{}, provider.Answer{Behavior: "deny"}, policy) |
| - return deny |
| + return refusal(refuseReadOnly) |
| } |
| return StepPermission(raw, policy) |
| } |
diff --git a/internal/runner/provider/claude/answers_test.go b/internal/runner/provider/claude/answers_test.go| index 7ca6c4f3..17aa7798 100644 |
| --- a/internal/runner/provider/claude/answers_test.go |
| +++ b/internal/runner/provider/claude/answers_test.go |
| @@ -174,7 +174,8 @@ func TestClaudePermissionResponsePinnedWire(t *testing.T) { |
| } |
| // TestStepPermission: with nobody to ask, an Engineering step may use its |
| -// file tools inside /workspace under the same checks; nothing else. |
| +// file tools inside /workspace under the same checks, and its shell, which |
| +// the policy confines to the CLI's sandbox; nothing else. |
| func TestStepPermission(t *testing.T) { |
| f := fixtureFor(t, "Engineering") |
| allowed := func(raw string) bool { |
| @@ -190,6 +191,7 @@ func TestStepPermission(t *testing.T) { |
| `{"tool_name":"Write","input":{"file_path":"/workspace/panes/new.go","content":"package panes"}}`, |
| `{"tool_name":"Read","input":{"file_path":"/workspace/README.md"}}`, |
| `{"tool_name":"Grep","input":{"pattern":"swipe","path":"/workspace"}}`, |
| + `{"tool_name":"Bash","input":{"command":"curl example.com"}}`, |
| } { |
| if !allowed(raw) { |
| t.Fatalf("refused %s", raw) |
| @@ -200,7 +202,7 @@ func TestStepPermission(t *testing.T) { |
| `{"tool_name":"Read","input":{"file_path":"/workspace/../home/runner/.credentials.json"}}`, |
| `{"tool_name":"Write","input":{"file_path":"/tool/equipo.sock","content":"x"}}`, |
| `{"tool_name":"Write","input":{"file_path":"/workspace/a","content":"x","dangerouslyDisableSandbox":true}}`, |
| - `{"tool_name":"Bash","input":{"command":"curl example.com"}}`, |
| + `{"tool_name":"Bash","input":{"command":"ls","dangerouslyDisableSandbox":true}}`, |
| `{"tool_name":"mcp__equipo__submit_candidate","input":{}}`, |
| `{"tool_name":"Read","input":{"file_path":"README.md"}}`, |
| `{"tool_name":"Edit","input":{"file_path":"/workspace/a"},"extra":1}`, |
| @@ -242,11 +244,143 @@ func TestStepPermissionReadsResearch(t *testing.T) { |
| `{"tool_name":"Read","input":{"file_path":"/researchers/a"}}`, |
| `{"tool_name":"Read","input":{"file_path":"research/img1-logo.png"}}`, |
| `{"tool_name":"Glob","input":{"pattern":"../home/*","path":"/research"}}`, |
| - `{"tool_name":"Bash","input":{"command":"cat /research/img1-logo.png"}}`, |
| } { |
| if allowed(check, raw) { |
| t.Fatalf("allowed %s", raw) |
| } |
| } |
| } |
| + // Engineering's shell runs in the sandbox, which binds /research |
| + // read-only; a read-only step runs no shell through its callback. |
| + if allowed(ReadPermission, `{"tool_name":"Bash","input":{"command":"cat /research/img1-logo.png"}}`) { |
| + t.Fatal("read-only step allowed a shell read") |
| + } |
| +} |
| + |
| +// TestStepPermissionRunsSandboxedShell: production 2026-11-22, a token |
| +// connection (claude-2). Token launches set CLAUDE_CODE_SUBPROCESS_ENV_SCRUB, |
| +// and in that mode the pinned CLI turns sandbox auto-approval off, so its |
| +// ordinary shell commands reached this callback and every one was refused. |
| +// The policy forces every command into the CLI's sandbox, so the step |
| +// allows them exactly as the CLI's auto-approval would; only a command an |
| +// explicit ask rule reserves for a person stays refused. |
| +func TestStepPermissionRunsSandboxedShell(t *testing.T) { |
| + f := fixtureFor(t, "Engineering") |
| + // The commands the CLI escalated in that step (its reason codes: |
| + // cd-compound-read, too-complex, no-rule-match, outside the working |
| + // directory). |
| + for _, command := range []string{ |
| + `cd internal/home; grep -ln "operator/confirm" *_test.go`, |
| + `cd /workspace/internal/home; ls *operator*`, |
| + `export PATH=/opt/go/bin:$PATH GOMODCACHE=/opt/go-mod GOTOOLCHAIN=local; go test ./internal/home/`, |
| + `GOTOOLCHAIN=local CGO_ENABLED=0 /opt/go/bin/go test -count=1 ./internal/home/ 2>&1 | tail -30`, |
| + `/opt/go/bin/go version`, |
| + `ls /opt/go/bin`, |
| + `which go gofmt; ls /opt`, |
| + } { |
| + input, _ := json.Marshal(map[string]any{"command": command, "description": "d", "timeout": 600000}) |
| + raw, _ := json.Marshal(map[string]any{"tool_name": "Bash", "tool_use_id": "toolu_x", "input": json.RawMessage(input)}) |
| + var v struct { |
| + Behavior string |
| + UpdatedInput json.RawMessage |
| + } |
| + if json.Unmarshal(StepPermission(raw, f.launch.Policy), &v) != nil || v.Behavior != "allow" || !sameJSON(v.UpdatedInput, input) { |
| + t.Fatalf("sandboxed command refused or changed: %s -> %+v", command, v) |
| + } |
| + } |
| + for _, raw := range []string{ |
| + // The certification controls exercise the person's approval path. |
| + `{"tool_name":"Bash","input":{"command":"printf equipo-cert-allow"}}`, |
| + `{"tool_name":"Bash","input":{"command":" printf equipo-cert-deny "}}`, |
| + `{"tool_name":"Bash","input":{"command":"ls","dangerouslyDisableSandbox":false}}`, |
| + `{"tool_name":"Bash","input":{"command":"ls","sandbox":false}}`, |
| + `{"tool_name":"Bash","input":{"command":7}}`, |
| + `{"tool_name":"Bash","input":{}}`, |
| + } { |
| + if stepBehavior(t, StepPermission, raw, f.launch.Policy) == "allow" { |
| + t.Fatalf("allowed %s", raw) |
| + } |
| + } |
| + // Only a policy that keeps every command in the sandbox lets the step |
| + // approve shell commands itself. |
| + for name, mutate := range map[string]func(map[string]any){ |
| + "sandbox off": func(s map[string]any) { s["enabled"] = false }, |
| + "unsandboxed allowed": func(s map[string]any) { s["allowUnsandboxedCommands"] = true }, |
| + "may run unconfined": func(s map[string]any) { s["failIfUnavailable"] = false }, |
| + "excluded commands": func(s map[string]any) { s["excludedCommands"] = []string{"go"} }, |
| + "weaker nested": func(s map[string]any) { s["enableWeakerNestedSandbox"] = true }, |
| + "no sandbox key": nil, |
| + } { |
| + p := withSandboxSettings(t, f.launch.Policy, mutate) |
| + if stepBehavior(t, StepPermission, `{"tool_name":"Bash","input":{"command":"ls"}}`, p) == "allow" { |
| + t.Fatalf("%s: shell allowed", name) |
| + } |
| + } |
| + // Review keeps its read-only callback: no shell through it. |
| + r := fixtureFor(t, "Review") |
| + if stepBehavior(t, ReadPermission, `{"tool_name":"Bash","input":{"command":"ls"}}`, r.launch.Policy) == "allow" { |
| + t.Fatal("read-only step allowed a shell command") |
| + } |
| +} |
| + |
| +// TestStepPermissionNamesTheRule: a refusal tells the model which rule |
| +// refused it and what to do instead, in fixed words that never repeat |
| +// the request. |
| +func TestStepPermissionNamesTheRule(t *testing.T) { |
| + f := fixtureFor(t, "Engineering") |
| + secret := "sk-ant-oat01-zzzz" |
| + for _, tc := range []struct{ raw, want string }{ |
| + {`{"tool_name":"Glob","input":{"pattern":"internal/home/*` + secret + `*"}}`, "absolute path"}, |
| + {`{"tool_name":"Read","input":{"file_path":"README-` + secret + `.md"}}`, "absolute path"}, |
| + {`{"tool_name":"Read","input":{"file_path":"~/` + secret + `"}}`, "absolute path"}, |
| + {`{"tool_name":"Read","input":{"file_path":"/etc/` + secret + `"}}`, "/workspace"}, |
| + {`{"tool_name":"Write","input":{"file_path":"/tool/equipo.sock","content":"` + secret + `"}}`, "protected"}, |
| + {`{"tool_name":"Write","input":{"file_path":"/research/a","content":"` + secret + `"}}`, "read"}, |
| + {`{"tool_name":"Bash","input":{"command":"printf equipo-cert-deny"}}`, "person"}, |
| + {`{"tool_name":"Bash","input":{"command":"ls ` + secret + `","dangerouslyDisableSandbox":true}}`, "sandbox"}, |
| + {`{"tool_name":"WebFetch","input":{"url":"https://` + secret + `"}}`, "not available"}, |
| + {`{"tool_name":"Edit","input":{"file_path":"/workspace/a","old_string":"` + secret + `","new_string":"b","extra":1}}`, "field"}, |
| + } { |
| + out := StepPermission(json.RawMessage(tc.raw), f.launch.Policy) |
| + var v struct{ Behavior, Message string } |
| + if json.Unmarshal(out, &v) != nil || v.Behavior != "deny" { |
| + t.Fatalf("not denied: %s -> %s", tc.raw, out) |
| + } |
| + if !strings.HasPrefix(v.Message, provider.ErrPolicy.Error()+": ") || !strings.Contains(v.Message, tc.want) || strings.Contains(v.Message, secret) { |
| + t.Fatalf("%s: message %q, want the rule naming %q and no request text", tc.raw, v.Message, tc.want) |
| + } |
| + } |
| + r := fixtureFor(t, "Review") |
| + var v struct{ Behavior, Message string } |
| + if json.Unmarshal(ReadPermission(json.RawMessage(`{"tool_name":"Bash","input":{"command":"ls"}}`), r.launch.Policy), &v) != nil || v.Behavior != "deny" || !strings.Contains(v.Message, "Read, Glob and Grep") { |
| + t.Fatalf("read-only refusal does not name its rule: %+v", v) |
| + } |
| +} |
| + |
| +func stepBehavior(t *testing.T, check func(json.RawMessage, provider.Policy) json.RawMessage, raw string, p provider.Policy) string { |
| + t.Helper() |
| + var v struct{ Behavior string } |
| + if out := check(json.RawMessage(raw), p); json.Unmarshal(out, &v) != nil { |
| + t.Fatalf("not JSON: %s", out) |
| + } |
| + return v.Behavior |
| +} |
| + |
| +// withSandboxSettings returns p with its manifest's sandbox settings |
| +// changed by mutate (nil removes the sandbox key). |
| +func withSandboxSettings(t *testing.T, p provider.Policy, mutate func(map[string]any)) provider.Policy { |
| + t.Helper() |
| + var m map[string]json.RawMessage |
| + var settings map[string]any |
| + if json.Unmarshal(p.Manifest, &m) != nil || json.Unmarshal(m["Settings"], &settings) != nil { |
| + t.Fatal("manifest settings unreadable") |
| + } |
| + if mutate == nil { |
| + delete(settings, "sandbox") |
| + } else { |
| + mutate(settings["sandbox"].(map[string]any)) |
| + } |
| + m["Settings"], _ = json.Marshal(settings) |
| + p.Manifest, _ = json.Marshal(m) |
| + return p |
| } |