package agent import ( "context" "encoding/json" "fmt" "path/filepath" "strings" "github.com/Autumn-27/artex/internal/sandbox" "github.com/Autumn-27/norma/llm" actool "github.com/Autumn-27/norma/tool" ) // wrapSandboxFileTools confines norma's built-in filesystem tools (Read, Write, // Edit, MultiEdit, LS, Glob, Grep) to hostRoot and translates paths between // the container view (/workspace/...) and the host directory, enforcing symlink // escape protection on every call. func wrapSandboxFileTools(tools []actool.CoreTool, hostRoot, defaultSubdir string) []actool.CoreTool { out := make([]actool.CoreTool, len(tools)) for i, t := range tools { switch t.Name() { case "Read", "Write", "Edit", "MultiEdit", "LS", "Glob", "Grep": out[i] = &sandboxFileTool{ CoreTool: t, hostRoot: filepath.Clean(hostRoot), defaultSubdir: defaultSubdir, } default: out[i] = t } } return out } type sandboxFileTool struct { actool.CoreTool hostRoot string defaultSubdir string } func (s *sandboxFileTool) Call(ctx context.Context, in json.RawMessage, tc *actool.ToolContext) (actool.Result, error) { rewritten, err := s.rewriteInput(in) if err != nil { return actool.Errorf("Error: " + err.Error()), nil } // Adjust ToolContext.WorkingDir to the confined default directory on host so // relative operations (LS/Glob/Grep without explicit path) never escape hostRoot. subTC := tc if tc != nil { cp := *tc if s.defaultSubdir != "" { cp.WorkingDir = filepath.Join(s.hostRoot, s.defaultSubdir) } else { cp.WorkingDir = s.hostRoot } subTC = &cp } res, err := s.CoreTool.Call(ctx, rewritten, subTC) if err != nil { return res, err } return s.rewriteResult(res), nil } func (s *sandboxFileTool) rewriteInput(in json.RawMessage) (json.RawMessage, error) { var m map[string]any if err := json.Unmarshal(in, &m); err != nil { return in, nil } changed := false for _, key := range []string{"file_path", "path"} { rawVal, exists := m[key] if !exists { continue } strVal, ok := rawVal.(string) if !ok { continue } if strings.TrimSpace(strVal) == "" && (s.Name() == "LS" || s.Name() == "Glob" || s.Name() == "Grep") { if s.defaultSubdir != "" { m[key] = filepath.Join(s.hostRoot, s.defaultSubdir) } else { m[key] = s.hostRoot } changed = true continue } resolved, allowed := sandbox.ResolveConfined(s.hostRoot, s.defaultSubdir, strVal) if !allowed { return nil, fmt.Errorf("path %q is outside sandbox workspace (%s)", strVal, sandbox.ContainerWorkDir) } m[key] = resolved changed = true } // If LS/Glob/Grep omitted path entirely, anchor it inside the sandbox root. if !changed && (s.Name() == "LS" || s.Name() == "Glob" || s.Name() == "Grep") { if _, hasPath := m["path"]; !hasPath { if s.defaultSubdir != "" { m["path"] = filepath.Join(s.hostRoot, s.defaultSubdir) } else { m["path"] = s.hostRoot } changed = true } } if !changed { return in, nil } b, err := json.Marshal(m) if err != nil { return in, err } return b, nil } func (s *sandboxFileTool) rewriteResult(res actool.Result) actool.Result { rootReal, err := filepath.EvalSymlinks(s.hostRoot) if err != nil { rootReal = s.hostRoot } for i := range res.Content { if res.Content[i].Type == llm.BlockText && res.Content[i].Text != "" { txt := res.Content[i].Text txt = strings.ReplaceAll(txt, rootReal, sandbox.ContainerWorkDir) if s.hostRoot != rootReal { txt = strings.ReplaceAll(txt, s.hostRoot, sandbox.ContainerWorkDir) } res.Content[i].Text = txt } } return res }