mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-09-19 09:40:12 +00: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>
43 lines
1.8 KiB
Go
43 lines
1.8 KiB
Go
package model
|
|
|
|
import "time"
|
|
|
|
// MCPAuditLog 记录每一次 MCP tool 调用,用于事后追责与异常检测。
|
|
// 写入是 best-effort:失败仅打日志,不阻塞业务请求。
|
|
type MCPAuditLog struct {
|
|
ID uint64 `gorm:"primaryKey" json:"id"`
|
|
CreatedAt time.Time `gorm:"index" json:"created_at"`
|
|
UserID uint64 `gorm:"index" json:"user_id"`
|
|
TokenID uint64 `gorm:"index" json:"token_id"`
|
|
Tool string `gorm:"type:varchar(64);index" json:"tool"`
|
|
ServerID uint64 `gorm:"index" json:"server_id,omitempty"`
|
|
ArgsHash string `gorm:"type:char(64)" json:"args_hash"`
|
|
ArgsPeek string `gorm:"type:varchar(512)" json:"args_peek,omitempty"`
|
|
Outcome string `gorm:"type:varchar(32);index" json:"outcome"`
|
|
ErrorCode string `gorm:"type:varchar(32)" json:"error_code,omitempty"`
|
|
ErrorMsg string `gorm:"type:varchar(512)" json:"error_msg,omitempty"`
|
|
DurationMs int64 `json:"duration_ms"`
|
|
IP string `gorm:"type:varchar(64)" json:"ip"`
|
|
}
|
|
|
|
func (MCPAuditLog) TableName() string {
|
|
return "mcp_audit_logs"
|
|
}
|
|
|
|
const (
|
|
MCPOutcomeOK = "ok"
|
|
MCPOutcomeScopeDenied = "scope_denied"
|
|
MCPOutcomePermDenied = "permission_denied"
|
|
MCPOutcomeServerOffline = "server_offline"
|
|
MCPOutcomeAgentTimeout = "agent_timeout"
|
|
MCPOutcomeAgentError = "agent_error"
|
|
// MCPOutcomeMCPDisabled 区分 “管理员按下 kill switch 把 MCP 关了” 与
|
|
// “agent 真出故障” 两种语义:前者属 forbidden 类、不应该触发 agent
|
|
// 故障告警;详见 service/rpc/mcp_rpc.go 里 ErrMCPDisabled 的注释。
|
|
MCPOutcomeMCPDisabled = "mcp_disabled"
|
|
MCPOutcomeInvalidArgs = "invalid_args"
|
|
MCPOutcomeRateLimited = "rate_limited"
|
|
MCPOutcomeUnsupportedAgent = "unsupported_agent"
|
|
MCPOutcomeInternalError = "internal_error"
|
|
)
|