Files
pipelines/internal/stage/permissions.go
T
alan 87016ef5dc Engine: check stages, harness-agnostic delivery, read-only enforcement, complete captures
- New `type=check` stage: the server runs a `commands` block (sh -c, per
  command_timeout, workspace env) and routes on pass/fail; per-command status,
  exit code, duration and bounded output are recorded and the failing output is
  the handoff. With delivery=true it is the landing gate (commit leftovers,
  merge moved main, checks in a fresh candidate worktree, fast-forward main).
  Parser/registry, lint (silent guards), format docs, CLI inspect, app graph and
  run detail rendering.
- delivery=true is accepted on every harness (and on check stages).
- permissions.shell on claude stages maps to Bash(cmd) / Bash(prefix:*) rules
  instead of a bare Bash allow; stages without edit that change the workspace
  fail the visit with the changed files listed (allow_workspace_changes=true
  opts out; artifacts, public and harness state dirs are ignored).
- File-change capture: symlinks compared by target, untouched protected files no
  longer make captures partial, untracked caches/dependency/build output left
  out, lockfiles listed without patch, huge files compared by metadata, raised
  patch/line limits. Outcome repair follow-ups are logged and counted on the
  step (control.outcome_repairs).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 12:52:16 +00:00

145 lines
5.0 KiB
Go

package stage
import (
"encoding/json"
"fmt"
"io"
"path/filepath"
"strings"
)
// Nil means normal stage defaults; an explicit empty list denies that action.
type ToolPolicy struct {
Edit []string `json:"edit"`
Shell []string `json:"shell"`
}
func StagePermissions(def Definition) (ToolPolicy, error) {
text, present := def.Blocks["permissions"]
if !present {
return ToolPolicy{}, nil
}
if err := validateUniqueJSON(text); err != nil {
return ToolPolicy{}, fmt.Errorf("invalid permissions JSON: %w", err)
}
var fields map[string]json.RawMessage
if json.Unmarshal([]byte(text), &fields) == nil {
for name, value := range fields {
if string(value) == "null" {
return ToolPolicy{}, fmt.Errorf("permissions %s must be an array, not null", name)
}
}
}
decoder := json.NewDecoder(strings.NewReader(text))
decoder.DisallowUnknownFields()
var policy *ToolPolicy
if err := decoder.Decode(&policy); err != nil {
return ToolPolicy{}, fmt.Errorf("permissions must be a JSON object with edit/shell lists: %w", err)
}
var extra any
if err := decoder.Decode(&extra); err != io.EOF || policy == nil {
return ToolPolicy{}, fmt.Errorf("permissions must be one non-null JSON object")
}
tools, err := effectiveTools(def)
if err != nil {
return ToolPolicy{}, err
}
has := func(action string) bool {
for _, tool := range tools {
if tool == action {
return true
}
}
return false
}
if policy.Edit != nil && !has("edit") {
return ToolPolicy{}, fmt.Errorf("edit permissions require the edit tool")
}
if policy.Shell != nil && !has("shell") {
return ToolPolicy{}, fmt.Errorf("shell permissions require the shell tool")
}
if len(policy.Edit) > 20 || len(policy.Shell) > 20 {
return ToolPolicy{}, fmt.Errorf("permissions allow at most 20 resources per action")
}
seen := map[string]bool{}
for _, pattern := range policy.Edit {
if pattern == "" || strings.TrimSpace(pattern) != pattern || filepath.IsAbs(pattern) || strings.ContainsAny(pattern, "\\\r\n") {
return ToolPolicy{}, fmt.Errorf("edit permissions must be nonempty run-directory-relative patterns")
}
for _, part := range strings.Split(pattern, "/") {
if part == ".." || part == "." || part == "" {
return ToolPolicy{}, fmt.Errorf("edit patterns must not traverse directories")
}
}
if seen[pattern] {
return ToolPolicy{}, fmt.Errorf("duplicate edit permission")
}
seen[pattern] = true
}
seen = map[string]bool{}
claude := def.Configs["harness"] == ClaudeBackend
for _, command := range policy.Shell {
exact, prefix := ShellPermissionPrefix(command)
if prefix && !claude {
return ToolPolicy{}, fmt.Errorf("shell prefix permissions (%q) are supported only by the claude harness; list exact commands", command)
}
if exact == "" || strings.TrimSpace(exact) != exact || strings.ContainsAny(exact, "*?;|&<>`$\r\n\\") {
return ToolPolicy{}, fmt.Errorf("shell permissions must be exact commands (or, on the claude harness, a command prefix ending in \" *\") without other wildcards, chaining, redirects, or expansion")
}
if claude && strings.ContainsAny(exact, "()") {
return ToolPolicy{}, fmt.Errorf("claude shell permissions cannot contain parentheses")
}
if seen[command] {
return ToolPolicy{}, fmt.Errorf("duplicate shell permission")
}
seen[command] = true
}
// An unrestricted shell can bypass any edit-tool path restriction.
if policy.Edit != nil && has("shell") && policy.Shell == nil {
return ToolPolicy{}, fmt.Errorf("restricted edit paths require an explicit shell command list (or no shell tool)")
}
return *policy, nil
}
// ShellPermissionPrefix splits a permissions.shell entry. "go test *" is a
// prefix entry (the command with any arguments; claude harness only) and
// returns ("go test", true); any other entry is an exact command.
func ShellPermissionPrefix(entry string) (string, bool) {
if base, ok := strings.CutSuffix(entry, " *"); ok {
return base, true
}
return entry, false
}
// claudePermissionsSupported limits a claude stage's permissions block to a
// shell allow-list (mapped to Bash(...) rules). Claude Code's edit-path rules
// are not audited, so restricted edits stay with the other harnesses.
func claudePermissionsSupported(def Definition) error {
if _, present := def.Blocks["permissions"]; !present {
return nil
}
policy, err := StagePermissions(def)
if err != nil {
return err
}
if policy.Edit != nil {
return fmt.Errorf("the claude harness enforces only permissions.shell; remove permissions.edit or use the opencode harness for this stage")
}
return nil
}
// claudeShellRules maps a shell allow-list to Claude Code permission rules:
// an exact command to Bash(command) and a prefix entry to Bash(prefix:*).
// Claude Code checks each part of a chained command against the rules.
func claudeShellRules(shell []string) []string {
rules := make([]string, 0, len(shell))
for _, entry := range shell {
if base, prefix := ShellPermissionPrefix(entry); prefix {
rules = append(rules, "Bash("+base+":*)")
} else {
rules = append(rules, "Bash("+base+")")
}
}
return rules
}