mirror of
https://github.com/nezhahq/nezha.git
synced 2026-11-10 21:21:06 +08:00
Introduce Personal Access Tokens (nzp_*) as a stateless auth path alongside
JWT, gated per-endpoint by a scope middleware (nezha:{resource}:{verb}) with
fail-closed empty-scope defaults and a server-id whitelist. Self-management
endpoints (profile, api-tokens, oauth2 bind, refresh-token) explicitly reject
PATs to block privilege-escalation chains. A revoke registry tears down active
long-lived connections (terminal, fm, ws, transfer, mcp) the moment a PAT is
deleted, with a tombstone closing the revoke->register race.
Add an MCP endpoint that proxies tool calls (exec, fs read/write/delete,
transfer) to agents over gRPC, guarded by origin/DNS-rebinding checks, a
per-token rate limiter, audit logging, and a kill switch. Serialize all
sends through the IOStream wrapper to honour grpc-go's concurrency contract.
Add CSRF double-submit protection on unsafe cookie-authenticated methods,
exempting authenticated PAT requests by context identity (not a forgeable
Authorization header). Apply visibility/whitelist filtering consistently
across list, get-by-id, and mutate paths to enforce tenant isolation.
Migrate legacy mcp:* scopes: rewrite read/exec to nezha:* equivalents and
drop dangerous write/delete/wildcard grants.
Co-authored-by: cloudcode <cloudcode@users.noreply.github.com>
92 lines
3.3 KiB
Go
92 lines
3.3 KiB
Go
package controller
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
"github.com/nezhahq/nezha/model"
|
|
pb "github.com/nezhahq/nezha/proto"
|
|
"github.com/nezhahq/nezha/service/rpc"
|
|
"github.com/nezhahq/nezha/service/singleton"
|
|
)
|
|
|
|
// execErrorStream replies every Task with a TaskResult whose Successful=true
|
|
// but whose Data carries a model.ExecResult{Error: ...}. This is exactly what
|
|
// the real agent does for "agent disabled command execution" / "cmd required"
|
|
// / pre-start failures.
|
|
type execErrorStream struct {
|
|
errMsg string
|
|
}
|
|
|
|
func (s *execErrorStream) Send(t *pb.Task) error {
|
|
go func(taskID uint64) {
|
|
b, _ := json.Marshal(model.ExecResult{ExitCode: -1, Error: s.errMsg})
|
|
rpc.DeliverMCPResultForTest(&pb.TaskResult{
|
|
Id: taskID,
|
|
Type: model.TaskTypeExec,
|
|
Successful: true,
|
|
Data: string(b),
|
|
})
|
|
}(t.GetId())
|
|
return nil
|
|
}
|
|
|
|
func (s *execErrorStream) Recv() (*pb.TaskResult, error) { return nil, context.Canceled }
|
|
func (s *execErrorStream) SetHeader(metadata.MD) error { return nil }
|
|
func (s *execErrorStream) SendHeader(metadata.MD) error { return nil }
|
|
func (s *execErrorStream) SetTrailer(metadata.MD) {}
|
|
func (s *execErrorStream) Context() context.Context { return context.Background() }
|
|
func (s *execErrorStream) SendMsg(any) error { return nil }
|
|
func (s *execErrorStream) RecvMsg(any) error { return context.Canceled }
|
|
|
|
// TestServerExec_AgentReportedErrorBecomesToolError pins the protocol contract
|
|
// that fs.* tools already obey: when the agent returns a structured result
|
|
// with a non-empty Error field, MCP tools/call must surface isError=true and
|
|
// audit must record agent_error — not MCPOutcomeOK with a quietly-failed
|
|
// structuredContent. The previous handler ignored ExecResult.Error and
|
|
// returned res, nil, which made the LLM and the audit log both believe the
|
|
// command succeeded while the agent had actually refused it.
|
|
func TestServerExec_AgentReportedErrorBecomesToolError(t *testing.T) {
|
|
cleanup, uid := setupMCPTest(t)
|
|
defer cleanup()
|
|
|
|
srv, _ := singleton.ServerShared.Get(7)
|
|
srv.SetTaskStream(&execErrorStream{errMsg: "agent disabled command execution"})
|
|
|
|
tok, _ := mkToken(t, uid, []string{model.ScopeServerExec}, nil)
|
|
|
|
c, w := mcpCallCtx(t, tok, uid, jsonRPCRequest{
|
|
JSONRPC: "2.0", ID: json.RawMessage("1"), Method: "tools/call",
|
|
Params: jsonObj(t, toolCallParams{
|
|
Name: "server.exec",
|
|
Arguments: jsonRaw(map[string]any{
|
|
"server_id": 7,
|
|
"cmd": "echo",
|
|
"timeout_seconds": 2,
|
|
}),
|
|
}),
|
|
})
|
|
mcpEndpoint(c)
|
|
_, tcr := decodeRPC(w)
|
|
require.NotNil(t, tcr, "tools/call must return a tool result envelope")
|
|
require.True(t, tcr.IsError,
|
|
"agent ExecResult.Error must propagate as MCP tool error; got %+v", tcr)
|
|
require.Contains(t, tcr.Content[0].Text, "agent disabled command execution",
|
|
"tool error text must surface the agent-reported error message")
|
|
|
|
require.Eventually(t, func() bool {
|
|
var got model.MCPAuditLog
|
|
err := singleton.DB.Where("token_id = ?", tok.ID).First(&got).Error
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return got.Outcome == model.MCPOutcomeAgentError
|
|
}, 2*time.Second, 20*time.Millisecond,
|
|
"audit row must record agent_error, not ok, when ExecResult.Error is set")
|
|
}
|