Harden model results with lossless isolated repair and scoped permissions
This commit is contained in:
+34
-5
@@ -3,6 +3,7 @@ package stage
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -50,16 +51,27 @@ func (c *AgentClient) permissionRules() []map[string]string {
|
||||
case "websearch", "webfetch", "read", "glob", "grep":
|
||||
allow(tool, "*")
|
||||
case "edit":
|
||||
if len(c.WritePaths) == 0 {
|
||||
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 {
|
||||
for _, path := range c.WritePaths {
|
||||
allow("edit", filepath.ToSlash(path))
|
||||
if c.Directory != "" {
|
||||
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)))
|
||||
}
|
||||
}
|
||||
@@ -68,7 +80,13 @@ func (c *AgentClient) permissionRules() []map[string]string {
|
||||
rules = append(rules, map[string]string{"action": "edit", "resource": pattern, "effect": "deny"})
|
||||
}
|
||||
case "shell":
|
||||
allow("shell", "*")
|
||||
if c.Policy.Shell == nil {
|
||||
allow("shell", "*")
|
||||
} else {
|
||||
for _, command := range c.Policy.Shell {
|
||||
allow("shell", command)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, pattern := range []string{"*.env", "*.env.*", "pipelines.db*"} {
|
||||
@@ -76,3 +94,14 @@ func (c *AgentClient) permissionRules() []map[string]string {
|
||||
}
|
||||
return rules
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user