mirror of
https://github.com/tiennm99/goclaw.git
synced 2026-08-08 02:24:50 +00:00
* fix(sandbox): avoid shell in FsBridge writes Replace sh -c with interpolated path by shell-free 'tee -- <path>' argv form, piping content via stdin. Prevents command injection through filenames containing shell metacharacters inside the sandbox container. Co-authored-by: evgyur <evgyur@gmail.com> * fix(security): fail-closed on pairing DB errors across channels On IsPaired lookup error, deny instead of granting access. Covers the shared CheckDMPolicy/CheckGroupPolicy helpers (Slack/Discord/Feishu/WhatsApp/Zalo) and the four inline Telegram pairing checks. Co-authored-by: Srini <srinis.k@gmail.com> * fix(security): harden provider URL validation against SSRF Enforce scheme check for all provider types; restrict local types (ollama, claude_cli, acp) to an explicit localhost allowlist instead of skipping checks; resolve remote hostnames and reject any IP in a private/reserved range via the shared security.IsBlocked CIDR list (covers loopback, link-local, metadata, multicast, and unspecified 0.0.0.0/::). Closes the wildcard-DNS bypass and the local-type escape hatch. Operator opt-in via GOCLAW_ALLOW_PRIVATE_PROVIDER_URLS. Exports security.IsBlocked as the single source of truth for blocked ranges. Co-authored-by: Linh Vo Van <linh.vo@e-cq.net> * feat(pipeline): add fail-closed tool call authorization gate Gate tool execution against the server-side AllowedTools allowlist built from the RBAC/tenant-aware filtered tool set. Resolve the tool-call prefix before the allowlist lookup so prefixed agents are not wrongly blocked, re-check deny on lazy MCP activation, and expand IsDenied to cover aliased tool names. Co-authored-by: Huy Doan <tui@pm.me> * fix(security): expand file-serve deny-list defense-in-depth Add absolute-path deny prefixes (/home, /Users, /srv, /var/lib, /var/www, /opt) and an explicit fail-closed log when no file-serving boundary is configured. Co-authored-by: Linh Vo Van <linh.vo@e-cq.net> * fix(providers): allow claude cli executable paths Refs: #1185 --------- Co-authored-by: evgyur <evgyur@gmail.com> Co-authored-by: Srini <srinis.k@gmail.com> Co-authored-by: Linh Vo Van <linh.vo@e-cq.net> Co-authored-by: Huy Doan <tui@pm.me>
175 lines
4.8 KiB
Go
175 lines
4.8 KiB
Go
package sandbox
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestLimitedBuffer_UnderLimit(t *testing.T) {
|
|
lb := &limitedBuffer{max: 100}
|
|
n, err := lb.Write([]byte("hello"))
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if n != 5 {
|
|
t.Errorf("expected 5, got %d", n)
|
|
}
|
|
if lb.String() != "hello" {
|
|
t.Errorf("expected 'hello', got %q", lb.String())
|
|
}
|
|
if lb.truncated {
|
|
t.Error("should not be truncated")
|
|
}
|
|
}
|
|
|
|
func TestLimitedBuffer_AtLimit(t *testing.T) {
|
|
lb := &limitedBuffer{max: 5}
|
|
lb.Write([]byte("hello"))
|
|
if lb.truncated {
|
|
t.Error("exactly at limit should not be truncated")
|
|
}
|
|
if lb.String() != "hello" {
|
|
t.Errorf("expected 'hello', got %q", lb.String())
|
|
}
|
|
}
|
|
|
|
func TestLimitedBuffer_OverLimit(t *testing.T) {
|
|
lb := &limitedBuffer{max: 5}
|
|
n, err := lb.Write([]byte("hello world"))
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
// Should report all bytes as "written" (consumed) even though truncated
|
|
if n != 11 {
|
|
t.Errorf("expected 11 (full input consumed), got %d", n)
|
|
}
|
|
if lb.String() != "hello" {
|
|
t.Errorf("expected 'hello', got %q", lb.String())
|
|
}
|
|
if !lb.truncated {
|
|
t.Error("should be truncated")
|
|
}
|
|
}
|
|
|
|
func TestLimitedBuffer_MultipleWrites(t *testing.T) {
|
|
lb := &limitedBuffer{max: 10}
|
|
lb.Write([]byte("aaaa"))
|
|
lb.Write([]byte("bbbb"))
|
|
lb.Write([]byte("cccc")) // should be partially truncated
|
|
|
|
if lb.buf.Len() != 10 {
|
|
t.Errorf("expected 10 bytes, got %d", lb.buf.Len())
|
|
}
|
|
if !lb.truncated {
|
|
t.Error("should be truncated after exceeding max")
|
|
}
|
|
if lb.String() != "aaaabbbbcc" {
|
|
t.Errorf("expected 'aaaabbbbcc', got %q", lb.String())
|
|
}
|
|
}
|
|
|
|
func TestLimitedBuffer_DiscardAfterTruncation(t *testing.T) {
|
|
lb := &limitedBuffer{max: 3}
|
|
lb.Write([]byte("abc"))
|
|
lb.Write([]byte("def")) // should be silently discarded
|
|
|
|
if lb.String() != "abc" {
|
|
t.Errorf("expected 'abc', got %q", lb.String())
|
|
}
|
|
if !lb.truncated {
|
|
t.Error("should be truncated")
|
|
}
|
|
}
|
|
|
|
func TestDefaultConfig_MaxOutputBytes(t *testing.T) {
|
|
cfg := DefaultConfig()
|
|
if cfg.MaxOutputBytes != 1<<20 {
|
|
t.Errorf("expected 1MB default, got %d", cfg.MaxOutputBytes)
|
|
}
|
|
}
|
|
|
|
func TestSanitizeKey(t *testing.T) {
|
|
tests := []struct {
|
|
input string
|
|
expected string
|
|
}{
|
|
{"agent:main:telegram:direct:123", "agent-main-telegram-direct-123"},
|
|
{"simple", "simple"},
|
|
{"has/slash", "has-slash"},
|
|
{"has space", "has-space"},
|
|
{strings.Repeat("x", 100), strings.Repeat("x", 50)},
|
|
}
|
|
for _, tc := range tests {
|
|
got := sanitizeKey(tc.input)
|
|
if got != tc.expected {
|
|
t.Errorf("sanitizeKey(%q) = %q, want %q", tc.input, got, tc.expected)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestResolveScopeKey(t *testing.T) {
|
|
tests := []struct {
|
|
scope Scope
|
|
key string
|
|
expected string
|
|
}{
|
|
{ScopeShared, "agent:main:telegram:direct:123", "shared"},
|
|
{ScopeAgent, "agent:main:telegram:direct:123", "agent:main"},
|
|
{ScopeSession, "agent:main:telegram:direct:123", "agent:main:telegram:direct:123"},
|
|
{ScopeSession, "", "default"},
|
|
}
|
|
for _, tc := range tests {
|
|
cfg := Config{Scope: tc.scope}
|
|
got := cfg.ResolveScopeKey(tc.key)
|
|
if got != tc.expected {
|
|
t.Errorf("scope=%s key=%q → %q, want %q", tc.scope, tc.key, got, tc.expected)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFsBridgeResolvePathRejectsWorkspaceEscapes(t *testing.T) {
|
|
bridge := NewFsBridge("container-test", "/workspace/agent-a")
|
|
|
|
tests := []struct {
|
|
name string
|
|
path string
|
|
want string
|
|
}{
|
|
{name: "inside relative", path: "notes/a.txt", want: "/workspace/agent-a/notes/a.txt"},
|
|
{name: "inside absolute", path: "/workspace/agent-a/notes/a.txt", want: "/workspace/agent-a/notes/a.txt"},
|
|
{name: "relative parent escape", path: "../agent-b/secret.txt", want: "/workspace/agent-a"},
|
|
{name: "absolute sibling escape", path: "/workspace/agent-b/secret.txt", want: "/workspace/agent-a"},
|
|
{name: "root escape", path: "/etc/passwd", want: "/workspace/agent-a"},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := bridge.resolvePath(tt.path); got != tt.want {
|
|
t.Fatalf("resolvePath(%q) = %q, want %q", tt.path, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestFsBridgePathWithinUsesPathBoundaries(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
root string
|
|
target string
|
|
want bool
|
|
}{
|
|
{name: "root itself", root: "/workspace/agent-a", target: "/workspace/agent-a", want: true},
|
|
{name: "child path", root: "/workspace/agent-a", target: "/workspace/agent-a/file.txt", want: true},
|
|
{name: "sibling with shared prefix", root: "/workspace/agent-a", target: "/workspace/agent-a-b/file.txt", want: false},
|
|
{name: "parent path", root: "/workspace/agent-a", target: "/workspace", want: false},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := fsBridgePathWithin(tt.root, tt.target); got != tt.want {
|
|
t.Fatalf("fsBridgePathWithin(%q, %q) = %v, want %v", tt.root, tt.target, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|