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>
This commit is contained in:
naiba
2026-05-30 15:56:44 +00:00
co-authored by cloudcode
parent 029695344c
commit e8dabf5bc6
153 changed files with 16974 additions and 244 deletions
+44 -7
View File
@@ -50,10 +50,18 @@ func createCron(c *gin.Context) (uint64, error) {
return 0, err
}
if !singleton.ServerShared.CheckPermission(c, slices.Values(cf.Servers)) {
if !isValidCronCover(cf.Cover) {
return 0, singleton.Localizer.ErrorT("permission denied")
}
if err := checkCronServerListPermission(c, cf.Cover, cf.Servers, getUid(c)); err != nil {
return 0, err
}
if err := rejectImplicitCoverForLimitedPAT(c, cf.Cover, cf.Servers); err != nil {
return 0, err
}
if err := assertOwnsNotificationGroup(c, cf.NotificationGroupID); err != nil {
return 0, err
}
@@ -111,12 +119,8 @@ func updateCron(c *gin.Context) (any, error) {
return 0, err
}
if !singleton.ServerShared.CheckPermission(c, slices.Values(cf.Servers)) {
return 0, singleton.Localizer.ErrorT("permission denied")
}
if err := assertOwnsNotificationGroup(c, cf.NotificationGroupID); err != nil {
return nil, err
if !isValidCronCover(cf.Cover) {
return nil, singleton.Localizer.ErrorT("permission denied")
}
var cr model.Cron
@@ -128,6 +132,18 @@ func updateCron(c *gin.Context) (any, error) {
return nil, singleton.Localizer.ErrorT("permission denied")
}
if err := checkCronServerListPermission(c, cf.Cover, cf.Servers, cr.GetUserID()); err != nil {
return nil, err
}
if err := rejectImplicitCoverForLimitedPATWithOwner(c, cf.Cover, cf.Servers, cr.GetUserID()); err != nil {
return nil, err
}
if err := assertOwnsNotificationGroup(c, cf.NotificationGroupID); err != nil {
return nil, err
}
cr.TaskType = cf.TaskType
cr.Name = cf.Name
cr.Scheduler = cf.Scheduler
@@ -183,6 +199,14 @@ func manualTriggerCron(c *gin.Context) (any, error) {
return nil, singleton.Localizer.ErrorT("permission denied")
}
// 运行时回放写侧 rejectImplicitCoverForLimitedPAT* 同一条 PAT 收口:
// 历史脏数据 / 旁路写入的 cron 仍可能携带「CronCoverAll + 不充分 deny-list」
// 的配置;CronTrigger 没有 PAT 上下文,manualTrigger 这里是唯一阻止
// 受限 PAT 触发 fan-out 到白名单外 owner servers 的同步入口。
if err := enforcePATCronDispatchScope(c, cr); err != nil {
return nil, err
}
singleton.ManualTrigger(cr)
return nil, nil
}
@@ -208,6 +232,19 @@ func batchDeleteCron(c *gin.Context) (any, error) {
return nil, singleton.Localizer.ErrorT("permission denied")
}
// 与 manualTriggerCron 对称:删除会改变 fan-out 范围本身,受限 PAT 不
// 应通过删除一个白名单内的「掩护」cron 间接放大对白名单外 owner servers
// 的影响。回放同一条 cover-fanout 收口。
for _, id := range cr {
existing, ok := singleton.CronShared.Get(id)
if !ok || existing == nil {
continue
}
if err := enforcePATCronDispatchScope(c, existing); err != nil {
return nil, err
}
}
if err := singleton.DB.Unscoped().Delete(&model.Cron{}, "id in (?)", cr).Error; err != nil {
return nil, newGormError("%v", err)
}