Files
nezha_domains/service/singleton/testhelpers.go
T
naibaandcloudcode ab25662ddd feat(auth): add PAT auth, scoped REST/MCP access, CSRF, and tenant isolation
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>
2026-05-30 15:56:44 +00:00

66 lines
1.9 KiB
Go

package singleton
import "github.com/nezhahq/nezha/model"
// NewEmptyServerClassForTest 构造一个不依赖 DB 的空 ServerClass,仅用于单测。
// 生产路径请用 NewServerClass。
func NewEmptyServerClassForTest() *ServerClass {
sc := &ServerClass{
class: class[uint64, *model.Server]{
list: make(map[uint64]*model.Server),
},
uuidToID: make(map[string]uint64),
}
model.OwnerServerIDsLookup = sc.ownerServerIDs
model.AllServerIDsLookup = sc.allServerIDs
model.OwnerIsAdminLookup = ownerIsAdmin
return sc
}
// InsertForTest 把一个 server 直接塞进内存表与排序快照,跳过 DB & InitServer 逻辑。
// 调用方需保证 server.ID 已经设置。
func (c *ServerClass) InsertForTest(s *model.Server) {
c.listMu.Lock()
c.list[s.ID] = s
if s.UUID != "" {
c.uuidToID[s.UUID] = s.ID
}
c.listMu.Unlock()
c.sortList()
}
// NewEmptyDDNSClassForTest 构造一个不依赖 DB 的空 DDNSClass,仅用于单测。
func NewEmptyDDNSClassForTest() *DDNSClass {
return &DDNSClass{
class: class[uint64, *model.DDNSProfile]{
list: make(map[uint64]*model.DDNSProfile),
},
}
}
// InsertForTest 把一个 DDNS profile 直接塞进内存表,跳过 DB。
func (c *DDNSClass) InsertForTest(p *model.DDNSProfile) {
c.listMu.Lock()
c.list[p.ID] = p
c.listMu.Unlock()
}
// NewEmptyNotificationClassForTest 构造空 NotificationClass。
func NewEmptyNotificationClassForTest() *NotificationClass {
return &NotificationClass{
class: class[uint64, *model.Notification]{
list: make(map[uint64]*model.Notification),
},
groupToIDList: make(map[uint64]map[uint64]*model.Notification),
idToGroupList: make(map[uint64]map[uint64]struct{}),
groupList: make(map[uint64]string),
}
}
// InsertForTest 把一个 Notification 直接塞进内存表。
func (c *NotificationClass) InsertForTest(n *model.Notification) {
c.listMu.Lock()
c.list[n.ID] = n
c.listMu.Unlock()
}