fix: harden runtime against panics/races, close path-traversal and fail-open gaps
- Runtime.Run rejects concurrent invocations instead of racing on shared state; panics in tools, hooks, and permission checkers are recovered and surfaced as errors/denials instead of crashing the loop - AgentSpec.Loop now actually overrides RuntimeDeps.AgentLoop per-agent, as documented - session.Store validates agent ID / session key as single path components, preventing traversal outside the session directory - ValidatePathInWorkDir resolves symlinks on the target itself, not just its parent, closing a workspace-escape gap - bash tool now fails closed on an unrecognized ExecPolicy.Level instead of allowing everything - browser tool rejects unknown actions instead of silently no-op'ing Bump README to v0.3.4 and document the AgentSpec.Loop override semantics.
This commit is contained in:
1 parent
445614757b
commit
eb4fa67976
15 files changed
+421
-37
No files matched your search
+7
-4
@@ -11,6 +11,7 @@ import (
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/sausheong/harness/tool"
|
||||
)
|
||||
|
||||
@@ -57,8 +58,7 @@ func resolveBashCommandPaths(cmd string) (string, [][2]string) {
|
||||
return out, subs
|
||||
}
|
||||
|
||||
// shellSingleQuote wraps s in single quotes, escaping any embedded single
|
||||
// quotes via the standard '\'' dance.
|
||||
// shellSingleQuote wraps s in single quotes, safely escaping embedded quotes.
|
||||
func shellSingleQuote(s string) string {
|
||||
return "'" + strings.ReplaceAll(s, "'", `'\''`) + "'"
|
||||
}
|
||||
@@ -83,7 +83,7 @@ const defaultBashTimeout = 120 * time.Second
|
||||
|
||||
// ExecPolicy controls which commands the bash tool is allowed to execute.
|
||||
type ExecPolicy struct {
|
||||
Level string // "deny", "allowlist", "full"
|
||||
Level string // "deny", "allowlist", "full"; any other value denies execution
|
||||
Allowlist []string // command basenames allowed when Level is "allowlist"
|
||||
}
|
||||
|
||||
@@ -214,8 +214,11 @@ func (t *BashTool) Execute(ctx context.Context, input json.RawMessage) (tool.Too
|
||||
return tool.ToolResult{Error: fmt.Sprintf("command %q is not in the exec allowlist", cmd)}, nil
|
||||
}
|
||||
}
|
||||
case "full":
|
||||
// Explicitly unrestricted.
|
||||
default:
|
||||
return tool.ToolResult{Error: fmt.Sprintf("invalid bash execution policy %q; execution denied", t.ExecPolicy.Level)}, nil
|
||||
}
|
||||
// "full" or unrecognized: allow everything
|
||||
}
|
||||
|
||||
timeout := defaultBashTimeout
|
||||
|
||||
+19
-4
@@ -43,6 +43,21 @@ func TestExtractCommands_BackgroundAmpersand(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnknownExecPolicyFailsClosed(t *testing.T) {
|
||||
bt := &BashTool{ExecPolicy: &ExecPolicy{Level: "allowlits", Allowlist: []string{"echo"}}}
|
||||
in, err := json.Marshal(bashInput{Command: "echo should-not-run"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
res, err := bt.Execute(context.Background(), in)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(res.Error, "invalid bash execution policy") {
|
||||
t.Fatalf("unknown policy must fail closed, got %q", res.Error)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizeLLMText(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -86,10 +101,10 @@ func TestResolveBashCommandPaths(t *testing.T) {
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
cmd string
|
||||
wantCmd string
|
||||
wantSubs int
|
||||
name string
|
||||
cmd string
|
||||
wantCmd string
|
||||
wantSubs int
|
||||
}{
|
||||
{
|
||||
name: "backslash-escaped path with ascii spaces resolves to nbsp file",
|
||||
|
||||
@@ -289,6 +289,10 @@ func (t *BrowserTool) Execute(ctx context.Context, input json.RawMessage) (tool.
|
||||
if in.Script == "" {
|
||||
return tool.ToolResult{Error: "script is required for evaluate action"}, nil
|
||||
}
|
||||
case "navigate", "get_text", "screenshot":
|
||||
// No additional required fields beyond the checks above.
|
||||
default:
|
||||
return tool.ToolResult{Error: fmt.Sprintf("unknown action: %q (valid: navigate, click, type, get_text, screenshot, evaluate, close)", in.Action)}, nil
|
||||
}
|
||||
|
||||
// Catch the about:blank trap: any post-navigation action (screenshot,
|
||||
@@ -390,7 +394,7 @@ func (t *BrowserTool) getOrCreateSession(name string) (*browserSession, error) {
|
||||
// both — wrap it.
|
||||
sess := &browserSession{
|
||||
taskCtx: taskCtx,
|
||||
taskCancel: cleanup, // cleanup tears down both task + alloc
|
||||
taskCancel: cleanup, // cleanup tears down both task + alloc
|
||||
allocCancel: func() {}, // no-op; cleanup already covers alloc
|
||||
lastUsed: time.Now(),
|
||||
}
|
||||
|
||||
@@ -48,6 +48,30 @@ func TestReadFileToolWorkDirClamps(t *testing.T) {
|
||||
"reads outside the workspace must be rejected when WorkDir is set")
|
||||
}
|
||||
|
||||
func TestReadFileToolWorkDirRejectsEscapingFileSymlink(t *testing.T) {
|
||||
workspace := t.TempDir()
|
||||
outside := filepath.Join(t.TempDir(), "secret.txt")
|
||||
require.NoError(t, os.WriteFile(outside, []byte("nope"), 0o644))
|
||||
link := filepath.Join(workspace, "secret-link")
|
||||
require.NoError(t, os.Symlink(outside, link))
|
||||
|
||||
readTool := &ReadFileTool{WorkDir: workspace}
|
||||
input, _ := json.Marshal(readFileInput{Path: link})
|
||||
result, err := readTool.Execute(context.Background(), input)
|
||||
require.NoError(t, err)
|
||||
assert.Contains(t, result.Error, "outside workspace")
|
||||
assert.Empty(t, result.Output)
|
||||
|
||||
writeTool := &WriteFileTool{WorkDir: workspace}
|
||||
writeInput, _ := json.Marshal(writeFileInput{Path: link, Content: "overwritten"})
|
||||
result, err = writeTool.Execute(context.Background(), writeInput)
|
||||
require.NoError(t, err)
|
||||
assert.Contains(t, result.Error, "outside workspace")
|
||||
data, readErr := os.ReadFile(outside)
|
||||
require.NoError(t, readErr)
|
||||
assert.Equal(t, "nope", string(data))
|
||||
}
|
||||
|
||||
func TestWriteFileTool(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "subdir", "output.txt")
|
||||
|
||||
Reference in new issue
Block a user