ARTEX/server/mcpdiscover_sandbox_test.go
BaaRai 518739be3b security(server): refuse stdio MCP discovery on the panel in sandbox mode
In ARTEX_SANDBOX=required the panel must not start agent-controlled local processes. discoverAndCacheMCP now refuses stdio before connectMCP, which covers startup auto-discovery, the operator add/refresh paths and ScopeSentry; the Auto agent's create_mcp and update_mcp reject stdio transports as well. HTTP and SSE MCP clients keep their existing checks. The canary test proves the command is not spawned.
2026-11-30 21:18:09 +01:00

69 lines
2.1 KiB
Go

package server
import (
"context"
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"github.com/Autumn-27/artex/agent"
"github.com/Autumn-27/artex/db"
"github.com/Autumn-27/artex/sandbox"
)
func withSandboxRequired(t *testing.T) {
t.Helper()
prev := agent.CurrentSandboxExecutor()
agent.SetSandboxExecutor(&agent.SandboxExecutor{Mode: sandbox.ModeRequired})
t.Cleanup(func() { agent.SetSandboxExecutor(prev) })
}
// The panel must never start a stdio MCP process in required mode. The canary
// command would create a file if connectMCP reached it; the guard returns before
// the process is spawned.
func TestSandboxSkipsStdioMCPDiscovery(t *testing.T) {
withSandboxRequired(t)
dir := t.TempDir()
canary := filepath.Join(dir, "stdio-mcp-ran")
args, _ := json.Marshal([]string{"-c", "touch " + canary})
m := &db.MCPServer{Name: "t-sandbox-stdio", Transport: "stdio", Command: "sh", Args: args, Enabled: true}
s := &Server{}
err := s.discoverAndCacheMCP(context.Background(), m)
if err == nil || !strings.Contains(err.Error(), "ARTEX_SANDBOX=required") {
t.Fatalf("discovery error = %v, want the sandbox refusal", err)
}
if _, statErr := os.Stat(canary); statErr == nil {
t.Fatal("stdio MCP command ran on the panel in required mode")
}
}
func TestSandboxMCPToolGuardsStdioOnly(t *testing.T) {
withSandboxRequired(t)
for _, tc := range []struct {
transport string
blocked bool
}{
{"", true},
{"stdio", true},
{"http", false},
{"sse", false},
} {
if got := sandboxBlocksStdioMCP(&db.MCPServer{Transport: tc.transport}); got != tc.blocked {
t.Errorf("transport %q blocked=%v, want %v", tc.transport, got, tc.blocked)
}
}
}
func TestSandboxCreateMCPRejectsStdio(t *testing.T) {
withSandboxRequired(t)
tool := (&Server{}).toolCreateMCP()
res, err := tool.Call(context.Background(), json.RawMessage(`{"name":"t-stdio","transport":"stdio","command":"sh"}`), nil)
if err != nil {
t.Fatal(err)
}
if !res.IsError || !strings.Contains(res.Flatten(), "ARTEX_SANDBOX=required") {
t.Fatalf("create_mcp result = %q, want the sandbox refusal", res.Flatten())
}
}