Harden Harness runtime, persistence and execution for Hand

This commit is contained in:
sausheong committed 2026-09-09 22:55:55 +08:00
1 parent 09c30fe2d5
commit 0972eed3ea
164 files changed
+15912 -506

No files matched your search

+30
View File
@@ -0,0 +1,30 @@
package tool
import (
"os"
"path/filepath"
"testing"
)
func TestCreationPathResolvesExistingAncestors(t *testing.T) {
work, outside := t.TempDir(), t.TempDir()
alias := filepath.Join(t.TempDir(), "workspace-link")
if err := os.Symlink(work, alias); err != nil {
t.Fatal(err)
}
if err := ValidatePathInWorkDir(filepath.Join(alias, "new/deep/file"), alias); err != nil {
t.Fatal("safe missing parents rejected", err)
}
if err := os.Symlink(outside, filepath.Join(work, "escape")); err != nil {
t.Fatal(err)
}
if err := ValidatePathInWorkDir(filepath.Join(work, "escape/new/deep/file"), work); err == nil {
t.Fatal("missing parents hid outside ancestor")
}
if err := os.Symlink(filepath.Join(outside, "missing"), filepath.Join(work, "dangling")); err != nil {
t.Fatal(err)
}
if err := ValidatePathInWorkDir(filepath.Join(work, "dangling/new/file"), work); err == nil {
t.Fatal("dangling symlink accepted")
}
}
+55 -20
View File
@@ -285,30 +285,14 @@ func ValidatePathInWorkDir(path, workDir string) error {
return fmt.Errorf("invalid path: %w", err)
}
// Resolve symlinks to prevent symlink-based traversal
realWork, err := filepath.EvalSymlinks(absWork)
realWork, err := resolveCreationPath(absWork)
if err != nil {
realWork = absWork // workspace might not exist yet
return fmt.Errorf("resolve workspace: %w", err)
}
// Resolve an existing target itself so a final-component symlink cannot
// escape the workspace. For a path that does not exist yet, resolve its
// parent directory instead so callers can safely create a new file.
realPath, targetErr := filepath.EvalSymlinks(absPath)
if targetErr == nil {
if !strings.HasPrefix(realPath, realWork+string(filepath.Separator)) && realPath != realWork {
return fmt.Errorf("path %q is outside workspace %q", path, workDir)
}
return nil
}
parentDir := filepath.Dir(absPath)
realParent, err := filepath.EvalSymlinks(parentDir)
realPath, err := resolveCreationPath(absPath)
if err != nil {
// Parent doesn't exist — use the unresolved absolute path
realParent = parentDir
return fmt.Errorf("resolve target: %w", err)
}
realPath = filepath.Join(realParent, filepath.Base(absPath))
if !strings.HasPrefix(realPath, realWork+string(filepath.Separator)) && realPath != realWork {
return fmt.Errorf("path %q is outside workspace %q", path, workDir)
@@ -317,9 +301,60 @@ func ValidatePathInWorkDir(path, workDir string) error {
return nil
}
// resolveCreationPath resolves every existing ancestor before appending missing
// components. A dangling symlink is not a missing ordinary path: refuse it.
func resolveCreationPath(path string) (string, error) {
candidate := path
var suffix []string
for {
_, err := os.Lstat(candidate)
if err == nil {
resolved, e := filepath.EvalSymlinks(candidate)
if e != nil {
return "", e
}
for i := len(suffix) - 1; i >= 0; i-- {
resolved = filepath.Join(resolved, suffix[i])
}
return resolved, nil
}
if !os.IsNotExist(err) {
return "", err
}
parent := filepath.Dir(candidate)
if parent == candidate {
return "", err
}
suffix = append(suffix, filepath.Base(candidate))
candidate = parent
}
}
// RegisterCron registers the cron tool with the given scheduler. agentID is
// baked into the tool so jobs the agent schedules later run as the same
// agent that scheduled them.
func RegisterCron(reg *Registry, agentID string, scheduler JobScheduler) {
reg.Register(&CronTool{AgentID: agentID, Scheduler: scheduler})
}
// RegisterUnique atomically installs a batch only if every name is new and
// unique within the batch. This supports complete dynamic tool catalogues.
func (r *Registry) RegisterUnique(batch []Tool) error {
r.mu.Lock()
defer r.mu.Unlock()
names := make(map[string]bool, len(batch))
for _, t := range batch {
if t == nil {
return fmt.Errorf("nil tool in batch")
}
name := t.Name()
if name == "" || names[name] || r.tools[name] != nil {
return fmt.Errorf("tool name collision: %q", name)
}
names[name] = true
}
for _, t := range batch {
r.tools[t.Name()] = t
}
return nil
}
+27 -4
View File
@@ -13,10 +13,10 @@ type stubTool struct {
name string
}
func (s *stubTool) Name() string { return s.name }
func (s *stubTool) Description() string { return s.name + " description" }
func (s *stubTool) Parameters() json.RawMessage { return json.RawMessage(`{}`) }
func (s *stubTool) IsConcurrencySafe(_ json.RawMessage) bool { return false }
func (s *stubTool) Name() string { return s.name }
func (s *stubTool) Description() string { return s.name + " description" }
func (s *stubTool) Parameters() json.RawMessage { return json.RawMessage(`{}`) }
func (s *stubTool) IsConcurrencySafe(_ json.RawMessage) bool { return false }
func (s *stubTool) Execute(ctx context.Context, in json.RawMessage) (ToolResult, error) {
return ToolResult{}, nil
}
@@ -57,3 +57,26 @@ func TestNamesAreDeterministic(t *testing.T) {
assert.Equal(t, []string{"alpha", "mango", "zebra"}, got,
"Names() must return tools sorted by name")
}
func TestRegisterUniqueRejectsWholeConflictingBatch(t *testing.T) {
reg := NewRegistry()
reg.Register(&stubTool{name: "existing"})
for _, batch := range [][]Tool{
{&stubTool{name: "new"}, &stubTool{name: "existing"}},
{&stubTool{name: "new"}, &stubTool{name: "new"}},
{&stubTool{name: "new"}, nil},
} {
if err := reg.RegisterUnique(batch); err == nil {
t.Fatal("invalid batch accepted")
}
if len(reg.Names()) != 1 {
t.Fatal("partial batch published")
}
}
if err := reg.RegisterUnique([]Tool{&stubTool{name: "new"}, &stubTool{name: "other"}}); err != nil {
t.Fatal(err)
}
if len(reg.Names()) != 3 {
t.Fatal("valid batch missing")
}
}