31aec05f99
Derive validated same-run visit artifact capabilities in the runner and pass them to OpenCode and Pi clients. Permit supported file tools to read completed prior visits and write only the current visit, preserving workspace, secret, database, and shell restrictions.\n\nChecked packaged templates and authoring skill references: neither needs updating in this item; docs/notes/scoped-artifact-access.md records the shared documentation staleness for the docs-fold writer.
181 lines
6.1 KiB
Go
181 lines
6.1 KiB
Go
package stage
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
// ArtifactAccess is runtime-only, server-derived access to artifact visits.
|
|
// Empty fields deliberately grant nothing outside the workspace.
|
|
type ArtifactAccess struct {
|
|
Write string
|
|
Read []string
|
|
}
|
|
|
|
// ToolNames validates model-stage permissions, not general graph semantics.
|
|
// A present empty block explicitly denies all tools.
|
|
func ToolNames(def Definition) ([]string, bool, error) {
|
|
text, present := def.Blocks["tools"]
|
|
if !present {
|
|
return nil, false, nil
|
|
}
|
|
tools := []string{}
|
|
seen := map[string]bool{}
|
|
for _, line := range strings.Split(text, "\n") {
|
|
name := strings.TrimSpace(line)
|
|
if name == "" {
|
|
continue
|
|
}
|
|
switch name {
|
|
case "websearch", "webfetch", "read", "glob", "grep", "edit", "shell":
|
|
default:
|
|
return nil, true, fmt.Errorf("unsupported tool %q", name)
|
|
}
|
|
if seen[name] {
|
|
return nil, true, fmt.Errorf("duplicate tool %s", name)
|
|
}
|
|
seen[name] = true
|
|
tools = append(tools, name)
|
|
}
|
|
return tools, true, nil
|
|
}
|
|
|
|
// legacyTools is the single declaration of the implicit tool set for a stage
|
|
// with no tools block: the mutating read/glob/grep/edit/shell bundle. It is
|
|
// never nil or empty, so an absent block is never represented as a read-only or
|
|
// text-only stage.
|
|
var legacyTools = []string{"read", "glob", "grep", "edit", "shell"}
|
|
|
|
// effectiveTools is the single materialisation point for the tools a model
|
|
// stage is permitted. A present tools block (including an explicit empty block)
|
|
// is returned exactly as parsed by ToolNames; an absent block yields the legacy
|
|
// mutating bundle so it is never silently narrowed to an empty or read-only set.
|
|
func effectiveTools(def Definition) ([]string, error) {
|
|
tools, explicit, err := ToolNames(def)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !explicit {
|
|
return legacyTools, nil
|
|
}
|
|
return tools, nil
|
|
}
|
|
|
|
// safeToReplay is the single definition of which post-admission work may be
|
|
// automatically retried. Only an explicit tools block that selects neither
|
|
// "edit" nor "shell" is side-effect free. An absent block is the legacy
|
|
// read/glob/grep/edit/shell bundle and is treated as mutating, matching the
|
|
// runner's useAgent/permissionRules semantics.
|
|
func safeToReplay(def Definition) bool {
|
|
tools, explicit, err := ToolNames(def)
|
|
if err != nil || !explicit {
|
|
return false
|
|
}
|
|
for _, tool := range tools {
|
|
if tool == "edit" || tool == "shell" {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (c *AgentClient) permissionRules() []map[string]string {
|
|
rules := []map[string]string{{"action": "*", "resource": "*", "effect": "deny"}}
|
|
tools := c.Tools
|
|
// A missing block gets normal local agent tools. Explicit stage blocks still
|
|
// select which tools are available, including an empty block for text-only work.
|
|
if tools == nil {
|
|
tools = legacyTools
|
|
}
|
|
allow := func(action, resource string) {
|
|
rules = append(rules, map[string]string{"action": action, "resource": resource, "effect": "allow"})
|
|
}
|
|
for _, tool := range tools {
|
|
switch tool {
|
|
case "websearch", "webfetch", "read", "glob", "grep":
|
|
allow(tool, "*")
|
|
case "edit":
|
|
if len(c.WritePaths) == 0 && c.Policy.Edit == nil {
|
|
allow("edit", "*")
|
|
rules = append(rules, map[string]string{"action": "edit", "resource": "/*", "effect": "deny"})
|
|
if c.Directory != "" {
|
|
allow("edit", filepath.ToSlash(filepath.Join(c.Directory, "*")))
|
|
}
|
|
} else {
|
|
paths := c.WritePaths
|
|
if len(paths) == 0 {
|
|
paths = c.Policy.Edit
|
|
}
|
|
// Relative patterns may also match absolute paths. Deny those first,
|
|
// then grant only absolute equivalents inside the run directory.
|
|
for _, path := range paths {
|
|
if c.Policy.Edit == nil || matchesEditPolicy(c.Policy.Edit, path) {
|
|
allow("edit", filepath.ToSlash(path))
|
|
}
|
|
}
|
|
rules = append(rules, map[string]string{"action": "edit", "resource": "/*", "effect": "deny"})
|
|
for _, path := range paths {
|
|
if c.Directory != "" && (c.Policy.Edit == nil || matchesEditPolicy(c.Policy.Edit, path)) {
|
|
allow("edit", filepath.ToSlash(filepath.Join(c.Directory, path)))
|
|
}
|
|
}
|
|
}
|
|
for _, pattern := range []string{"../*", "*/../*"} {
|
|
rules = append(rules, map[string]string{"action": "edit", "resource": pattern, "effect": "deny"})
|
|
}
|
|
case "shell":
|
|
if c.Policy.Shell == nil {
|
|
allow("shell", "*")
|
|
} else {
|
|
for _, command := range c.Policy.Shell {
|
|
allow("shell", command)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// OpenCode V2 resolves an external file to its containing directory and
|
|
// checks external_directory against <directory>/*. Do not grant a parent:
|
|
// each visit is deliberately an independent capability.
|
|
readArtifacts := false
|
|
writeArtifacts := false
|
|
for _, tool := range tools {
|
|
readArtifacts = readArtifacts || tool == "read" || tool == "glob" || tool == "grep"
|
|
writeArtifacts = writeArtifacts || tool == "edit"
|
|
}
|
|
if readArtifacts {
|
|
for _, directory := range c.ArtifactAccess.Read {
|
|
if artifactDirectory(directory) {
|
|
allow("external_directory", filepath.ToSlash(filepath.Join(directory, "*")))
|
|
}
|
|
}
|
|
}
|
|
if writeArtifacts && artifactDirectory(c.ArtifactAccess.Write) {
|
|
allow("external_directory", filepath.ToSlash(filepath.Join(c.ArtifactAccess.Write, "*")))
|
|
allow("edit", filepath.ToSlash(filepath.Join(c.ArtifactAccess.Write, "*")))
|
|
}
|
|
for _, pattern := range []string{"../*", "*/../*"} {
|
|
rules = append(rules, map[string]string{"action": "external_directory", "resource": pattern, "effect": "deny"})
|
|
}
|
|
for _, pattern := range []string{"*.env", "*.env.*", "pipelines.db*"} {
|
|
rules = append(rules, map[string]string{"action": "read", "resource": pattern, "effect": "deny"})
|
|
}
|
|
return rules
|
|
}
|
|
|
|
func artifactDirectory(directory string) bool {
|
|
return directory != "" && filepath.IsAbs(directory) && filepath.Clean(directory) == directory && !strings.ContainsAny(directory, "*?\\")
|
|
}
|
|
|
|
func matchesEditPolicy(patterns []string, path string) bool {
|
|
for _, pattern := range patterns {
|
|
// OpenCode resource wildcards match across directory separators.
|
|
re := strings.ReplaceAll(strings.ReplaceAll(regexp.QuoteMeta(pattern), `\*`, ".*"), `\?`, ".")
|
|
if ok, _ := regexp.MatchString("^"+re+"$", filepath.ToSlash(path)); ok {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|