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

+8 -3
View File
@@ -13,7 +13,8 @@ import (
// EditFileTool performs a string-replace edit on a file.
type EditFileTool struct {
WorkDir string // if set, restricts edits to this directory
ExactPath bool // preserve admitted path spelling; no home or Unicode recovery
WorkDir string // if set, restricts edits to this directory
}
type editFileInput struct {
@@ -62,11 +63,15 @@ func (t *EditFileTool) Execute(ctx context.Context, input json.RawMessage) (tool
return tool.ToolResult{Error: "path is required"}, nil
}
in.Path = tool.ExpandHome(in.Path)
if !t.ExactPath {
in.Path = tool.ExpandHome(in.Path)
}
if t.WorkDir != "" && !filepath.IsAbs(in.Path) {
in.Path = filepath.Join(t.WorkDir, in.Path)
}
in.Path = tool.ResolveExistingPath(in.Path)
if !t.ExactPath {
in.Path = tool.ResolveExistingPath(in.Path)
}
if t.WorkDir != "" {
if err := tool.ValidatePathInWorkDir(in.Path, t.WorkDir); err != nil {
+8 -3
View File
@@ -15,7 +15,8 @@ import (
// ReadFileTool reads the contents of a file.
type ReadFileTool struct {
WorkDir string // if set, restricts reads to this directory
ExactPath bool // preserve admitted path spelling; no home or Unicode recovery
WorkDir string // if set, restricts reads to this directory
}
type readFileInput struct {
@@ -82,11 +83,15 @@ func (t *ReadFileTool) Execute(ctx context.Context, input json.RawMessage) (tool
return tool.ToolResult{Error: "path is required"}, nil
}
in.Path = tool.ExpandHome(in.Path)
if !t.ExactPath {
in.Path = tool.ExpandHome(in.Path)
}
if t.WorkDir != "" && !filepath.IsAbs(in.Path) {
in.Path = filepath.Join(t.WorkDir, in.Path)
}
in.Path = tool.ResolveExistingPath(in.Path)
if !t.ExactPath {
in.Path = tool.ResolveExistingPath(in.Path)
}
if t.WorkDir != "" {
if err := tool.ValidatePathInWorkDir(in.Path, t.WorkDir); err != nil {
+20 -4
View File
@@ -3,15 +3,17 @@ package file
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/sausheong/harness/tool"
"os"
"path/filepath"
"github.com/sausheong/harness/tool"
)
// WriteFileTool creates or overwrites a file.
type WriteFileTool struct {
WorkDir string // if set, restricts writes to this directory
ExactPath bool // preserve admitted path spelling; no home or Unicode recovery
WorkDir string // if set, restricts writes to this directory
}
type writeFileInput struct {
@@ -55,7 +57,9 @@ func (t *WriteFileTool) Execute(_ context.Context, input json.RawMessage) (tool.
return tool.ToolResult{Error: "path is required"}, nil
}
in.Path = tool.ExpandHome(in.Path)
if !t.ExactPath {
in.Path = tool.ExpandHome(in.Path)
}
if t.WorkDir != "" && !filepath.IsAbs(in.Path) {
in.Path = filepath.Join(t.WorkDir, in.Path)
}
@@ -72,7 +76,19 @@ func (t *WriteFileTool) Execute(_ context.Context, input json.RawMessage) (tool.
return tool.ToolResult{Error: fmt.Sprintf("failed to create directory: %v", err)}, nil
}
if err := tool.WriteFileAtomic(in.Path, []byte(in.Content), 0o600); err != nil {
// Retain existing ordinary permission bits, including executable scripts.
// New files remain private. Inspection failures must not silently replace
// a file with restrictive defaults and lose its original mode.
mode := os.FileMode(0600)
if info, err := os.Stat(in.Path); err == nil {
if !info.Mode().IsRegular() {
return tool.ToolResult{Error: "destination is not a regular file"}, nil
}
mode = info.Mode().Perm()
} else if !errors.Is(err, os.ErrNotExist) {
return tool.ToolResult{Error: fmt.Sprintf("inspect destination: %v", err)}, nil
}
if err := tool.WriteFileAtomic(in.Path, []byte(in.Content), mode); err != nil {
return tool.ToolResult{Error: fmt.Sprintf("failed to write file: %v", err)}, nil
}
+41
View File
@@ -24,3 +24,44 @@ func TestWriteFileIsRestrictive(t *testing.T) {
require.NoError(t, err)
require.Equal(t, os.FileMode(0o600), info.Mode().Perm())
}
func TestWriteFilePreservesExecutableMode(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "script.sh")
require.NoError(t, os.WriteFile(path, []byte("old"), 0751))
require.NoError(t, os.Chmod(path, 0751))
writer := &WriteFileTool{WorkDir: dir}
input, _ := json.Marshal(map[string]string{"path": path, "content": "#!/bin/sh\nexit 0\n"})
result, err := writer.Execute(context.Background(), input)
require.NoError(t, err)
require.Empty(t, result.Error)
info, err := os.Stat(path)
require.NoError(t, err)
require.Equal(t, os.FileMode(0751), info.Mode().Perm())
}
func TestFailedWritePreservesExistingContentAndMode(t *testing.T) {
if os.Geteuid() == 0 {
t.Fatal("permission failure fixture requires an unprivileged user")
}
dir := t.TempDir()
path := filepath.Join(dir, "script.sh")
require.NoError(t, os.WriteFile(path, []byte("original"), 0751))
require.NoError(t, os.Chmod(path, 0751))
require.NoError(t, os.Chmod(dir, 0500))
defer os.Chmod(dir, 0700)
writer := &WriteFileTool{WorkDir: dir}
input, _ := json.Marshal(map[string]string{"path": path, "content": "replacement"})
result, err := writer.Execute(context.Background(), input)
require.NoError(t, err)
require.NotEmpty(t, result.Error)
data, err := os.ReadFile(path)
require.NoError(t, err)
require.Equal(t, "original", string(data))
info, err := os.Stat(path)
require.NoError(t, err)
require.Equal(t, os.FileMode(0751), info.Mode().Perm())
entries, err := os.ReadDir(dir)
require.NoError(t, err)
require.Len(t, entries, 1)
}