fix: enforce MCP operation boundaries
This commit is contained in:
1 parent
ce65fe386e
commit
4c7b4bb052
4 files changed
+110
-10
No files matched your search
@@ -186,8 +186,6 @@ func (s *SSH) Exec(ctx context.Context, vm, address, username, command, cwd stri
|
||||
return ExecResult{}, err
|
||||
}
|
||||
defer client.Close()
|
||||
stopOnContext := closeOnContext(ctx, client)
|
||||
defer stopOnContext()
|
||||
session, err := client.NewSession()
|
||||
if err != nil {
|
||||
return ExecResult{}, err
|
||||
@@ -229,7 +227,7 @@ func (s *SSH) Exec(ctx context.Context, vm, address, username, command, cwd stri
|
||||
if closeErr != nil {
|
||||
return ExecResult{}, closeErr
|
||||
}
|
||||
if err != nil && !timedOut {
|
||||
if err != nil && !timedOut && !cancelled {
|
||||
var exitErr *ssh.ExitError
|
||||
if !errors.As(err, &exitErr) {
|
||||
return ExecResult{}, err
|
||||
|
||||
+27
-4
@@ -131,8 +131,19 @@ func (s *Server) Handle(ctx context.Context, raw json.RawMessage) (json.RawMessa
|
||||
}
|
||||
defer s.finishRequest(req.ID, cancel)
|
||||
ctx = requestCtx
|
||||
s.callMu.Lock()
|
||||
defer s.callMu.Unlock()
|
||||
serialized := true
|
||||
if req.Method == "tools/call" {
|
||||
var tool struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
if json.Unmarshal(req.Params, &tool) == nil && tool.Name == "test_wait" {
|
||||
serialized = false
|
||||
}
|
||||
}
|
||||
if serialized {
|
||||
s.callMu.Lock()
|
||||
defer s.callMu.Unlock()
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return marshal(response{JSONRPC: "2.0", ID: req.ID, Error: &rpcError{Code: -32800, Message: "request cancelled"}})
|
||||
}
|
||||
@@ -512,8 +523,8 @@ func (s *Server) guestAddress(ctx context.Context, ref string) (string, string,
|
||||
if strings.TrimSpace(ref) == "" {
|
||||
return "", "", fmt.Errorf("%w: vm is required", errInvalidParams)
|
||||
}
|
||||
var vm map[string]any
|
||||
if err := s.host.Call(ctx, "host/vm.get", map[string]string{"vm": ref}, &vm); err != nil {
|
||||
vm, err := s.vmInfo(ctx, ref)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
portValue, ok := vm["ssh_port"].(float64)
|
||||
@@ -524,6 +535,18 @@ func (s *Server) guestAddress(ctx context.Context, ref string) (string, string,
|
||||
return net.JoinHostPort("127.0.0.1", fmt.Sprint(int(portValue))), username, nil
|
||||
}
|
||||
|
||||
func (s *Server) vmInfo(ctx context.Context, ref string) (map[string]any, error) {
|
||||
var vm map[string]any
|
||||
if err := s.host.Call(ctx, "host/vm.get", map[string]string{"vm": ref}, &vm); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
phase, _ := vm["phase"].(string)
|
||||
if phase != "" && !strings.EqualFold(phase, "normal") {
|
||||
return nil, fmt.Errorf("confirmation_required: VM %q is in %s phase", ref, phase)
|
||||
}
|
||||
return vm, nil
|
||||
}
|
||||
|
||||
func (s *Server) vmStart(ctx context.Context, raw json.RawMessage) (any, error) {
|
||||
var args struct {
|
||||
VM string `json:"vm"`
|
||||
|
||||
@@ -28,6 +28,8 @@ type testHost struct {
|
||||
frames []string
|
||||
frameIndex int
|
||||
secretValue string
|
||||
vmPhase string
|
||||
deleteErr error
|
||||
}
|
||||
|
||||
type blockingHost struct{ started chan struct{} }
|
||||
@@ -95,9 +97,16 @@ func (h *testHost) Call(_ context.Context, method string, params any, out any) e
|
||||
return nil
|
||||
}
|
||||
if strings.HasSuffix(method, "vm.get") {
|
||||
*out.(*map[string]any) = map[string]any{"vm": "driver:macos1", "name": "macos1", "state": "running"}
|
||||
*out.(*map[string]any) = map[string]any{"vm": "driver:macos1", "name": "macos1", "state": "running", "phase": h.vmPhase}
|
||||
return nil
|
||||
}
|
||||
if strings.HasSuffix(method, "vm.clone") {
|
||||
*out.(*map[string]any) = map[string]any{"vm": "__agent-test"}
|
||||
return nil
|
||||
}
|
||||
if strings.HasSuffix(method, "vm.delete") && h.deleteErr != nil {
|
||||
return h.deleteErr
|
||||
}
|
||||
if strings.HasSuffix(method, "vm.list") {
|
||||
*out.(*hostReply) = hostReply{VMs: []any{map[string]any{"vm": "driver:macos1", "name": "macos1"}}}
|
||||
return nil
|
||||
@@ -274,6 +283,62 @@ func TestCancellationNotificationStopsActiveToolCall(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGuestAccessRejectsNonNormalVMPhase(t *testing.T) {
|
||||
server := New()
|
||||
host := &testHost{vmPhase: "installer"}
|
||||
server.BindHost(host)
|
||||
_, _, err := server.guestAddress(context.Background(), "macos1")
|
||||
if err == nil || !strings.Contains(err.Error(), "confirmation_required") {
|
||||
t.Fatalf("guestAddress error = %v, want confirmation_required", err)
|
||||
}
|
||||
|
||||
_, err = server.startTestRun(context.Background(), json.RawMessage(`{"config":{"vm":"macos1","steps":[{"run":"true"}]}}`))
|
||||
if err == nil || !strings.Contains(err.Error(), "confirmation_required") {
|
||||
t.Fatalf("startTestRun error = %v, want confirmation_required", err)
|
||||
}
|
||||
if len(host.calls) != 2 || host.calls[0].method != "host/vm.get" || host.calls[1].method != "host/vm.get" {
|
||||
t.Fatalf("host calls = %+v, want phase checks before side effects", host.calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTestWaitDoesNotBlockBehindSerializedToolCall(t *testing.T) {
|
||||
server := New()
|
||||
run := &testRun{ID: "run-test", Status: "running", Done: make(chan struct{})}
|
||||
server.state.runs[run.ID] = run
|
||||
server.callMu.Lock()
|
||||
result := make(chan json.RawMessage, 1)
|
||||
go func() {
|
||||
response, _ := server.Handle(context.Background(), json.RawMessage(`{"jsonrpc":"2.0","id":45,"method":"tools/call","params":{"name":"test_wait","arguments":{"run_id":"run-test","timeout_ms":20}}}`))
|
||||
result <- response
|
||||
}()
|
||||
select {
|
||||
case raw := <-result:
|
||||
server.callMu.Unlock()
|
||||
if response := parseResponse(t, raw); response["error"] != nil {
|
||||
t.Fatalf("test_wait response = %s", raw)
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
server.callMu.Unlock()
|
||||
t.Fatal("test_wait blocked behind another tool call")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTestRunReportsTemporaryVMCleanupFailure(t *testing.T) {
|
||||
server := New()
|
||||
host := &testHost{deleteErr: errors.New("delete failed")}
|
||||
server.BindHost(host)
|
||||
run := &testRun{
|
||||
ID: "run-cleanup", VM: "macos1", Config: testConfig{VM: "macos1", Isolation: "clone"}, Done: make(chan struct{}),
|
||||
}
|
||||
server.executeTestRun(context.Background(), run)
|
||||
if run.Status != "failed" {
|
||||
t.Fatalf("run status = %q, want failed", run.Status)
|
||||
}
|
||||
if run.Result["cleanup_error"] != "delete failed" || run.Result["kept_vm"] != "__agent-test" {
|
||||
t.Fatalf("run result = %+v, want cleanup failure and kept VM", run.Result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCancelledDragReleasesMouseButton(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
host := &dragCancellationHost{cancel: cancel}
|
||||
|
||||
@@ -213,6 +213,9 @@ func (s *Server) startTestRun(parent context.Context, raw json.RawMessage) (any,
|
||||
if strings.TrimSpace(cfg.VM) == "" || len(cfg.Steps) == 0 {
|
||||
return nil, fmt.Errorf("%w: vm and at least one step are required", errInvalidParams)
|
||||
}
|
||||
if _, err := s.vmInfo(parent, cfg.VM); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if cfg.Isolation == "" {
|
||||
cfg.Isolation = "clone"
|
||||
}
|
||||
@@ -306,7 +309,15 @@ func (s *Server) executeTestRun(ctx context.Context, run *testRun) {
|
||||
}
|
||||
if err == nil || !keepOnFailure {
|
||||
if sessionID != "" {
|
||||
_ = s.disposeSession(context.Background(), sessionID)
|
||||
if cleanupErr := s.disposeSession(context.Background(), sessionID); cleanupErr != nil {
|
||||
result["cleanup_error"] = cleanupErr.Error()
|
||||
result["kept_vm"] = vmName
|
||||
if status == "passed" {
|
||||
status = "failed"
|
||||
result["status"] = status
|
||||
result["error"] = "temporary VM cleanup failed"
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
result["kept_vm"] = vmName
|
||||
@@ -438,7 +449,10 @@ func (s *Server) executeStep(ctx context.Context, run *testRun, step testStep, l
|
||||
return err
|
||||
}
|
||||
logLine(fmt.Sprintf("step %s exit=%d: %s", step.Name, result.ExitCode, result.Output))
|
||||
if result.ExitCode != 0 || result.TimedOut || result.Cancelled {
|
||||
if result.Cancelled {
|
||||
return context.Canceled
|
||||
}
|
||||
if result.ExitCode != 0 || result.TimedOut {
|
||||
return fmt.Errorf("step %q failed with exit code %d", step.Name, result.ExitCode)
|
||||
}
|
||||
return nil
|
||||
|
||||
Reference in new issue
Block a user