mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-09-21 10:40:13 +00:00
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:
@@ -0,0 +1,116 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// C1 regression: an admin-owned CronCoverAll fans out to every server in the
|
||||
// system at runtime (CronTrigger gates on userIsAdmin(cr.UserID)). A
|
||||
// server-limited PAT created by that admin must therefore only pass
|
||||
// HasPermission when its deny-list covers EVERY server outside its whitelist
|
||||
// system-wide — not just the admin's own servers, which is the visibly
|
||||
// degenerate set that OwnerServerIDsLookup returns today.
|
||||
//
|
||||
// Without this regression, an admin with a PAT scoped to server X can create
|
||||
// a CronCoverAll cron with deny-list = [X] and the dashboard will cheerfully
|
||||
// dispatch the command to every OTHER user's server.
|
||||
func TestCronHasPermission_AdminOwnerCoverAllDeniesUntilDenyListCoversAllOtherServers(t *testing.T) {
|
||||
saved := OwnerServerIDsLookup
|
||||
savedAdmin := OwnerIsAdminLookup
|
||||
savedAll := AllServerIDsLookup
|
||||
t.Cleanup(func() {
|
||||
OwnerServerIDsLookup = saved
|
||||
OwnerIsAdminLookup = savedAdmin
|
||||
AllServerIDsLookup = savedAll
|
||||
})
|
||||
|
||||
// Admin (uid=1) owns only server 1. Member (uid=200) owns server 2.
|
||||
OwnerServerIDsLookup = func(ownerUID uint64) []uint64 {
|
||||
switch ownerUID {
|
||||
case 1:
|
||||
return []uint64{1}
|
||||
case 200:
|
||||
return []uint64{2}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
OwnerIsAdminLookup = func(uid uint64) bool { return uid == 1 }
|
||||
AllServerIDsLookup = func() []uint64 { return []uint64{1, 2} }
|
||||
|
||||
// Limited PAT (admin's) — scoped to server 1 only.
|
||||
pat := &stubPATAccessor{ids: []uint64{1}}
|
||||
|
||||
t.Run("deny_list_missing_other_owner_server_must_reject", func(t *testing.T) {
|
||||
cron := &Cron{
|
||||
Common: Common{ID: 9, UserID: 1}, // admin-owned
|
||||
Cover: CronCoverAll,
|
||||
Servers: []uint64{1}, // deny self, but NOT server 2 (member's)
|
||||
}
|
||||
ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
|
||||
ctx.Set(CtxKeyAuthorizedUser, &User{Common: Common{ID: 1}, Role: RoleAdmin})
|
||||
ctx.Set(CtxKeyAPIToken, pat)
|
||||
|
||||
if cron.HasPermission(ctx) {
|
||||
t.Fatal("admin-owned CronCoverAll fans out to ALL servers at runtime; " +
|
||||
"deny-list missing server 2 must reject a PAT scoped to [1] — otherwise " +
|
||||
"the cron will execute on a foreign user's server")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("deny_list_covers_all_other_servers_passes", func(t *testing.T) {
|
||||
cron := &Cron{
|
||||
Common: Common{ID: 10, UserID: 1},
|
||||
Cover: CronCoverAll,
|
||||
Servers: []uint64{2}, // deny the only server outside PAT whitelist
|
||||
}
|
||||
ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
|
||||
ctx.Set(CtxKeyAuthorizedUser, &User{Common: Common{ID: 1}, Role: RoleAdmin})
|
||||
ctx.Set(CtxKeyAPIToken, pat)
|
||||
|
||||
if !cron.HasPermission(ctx) {
|
||||
t.Fatal("deny-list covering every non-whitelisted server must pass: fan-out " +
|
||||
"is now contained inside the PAT whitelist")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Companion: member-owned crons must NOT use the system-wide fan-out set.
|
||||
// Runtime CronTrigger only ships to servers whose UserID matches the
|
||||
// member-owner; HasPermission must mirror that to avoid false rejects on
|
||||
// completely legitimate configs.
|
||||
func TestCronHasPermission_MemberOwnerCoverAllStillUsesOwnerSet(t *testing.T) {
|
||||
saved := OwnerServerIDsLookup
|
||||
savedAdmin := OwnerIsAdminLookup
|
||||
savedAll := AllServerIDsLookup
|
||||
t.Cleanup(func() {
|
||||
OwnerServerIDsLookup = saved
|
||||
OwnerIsAdminLookup = savedAdmin
|
||||
AllServerIDsLookup = savedAll
|
||||
})
|
||||
|
||||
OwnerServerIDsLookup = func(ownerUID uint64) []uint64 {
|
||||
if ownerUID == 100 {
|
||||
return []uint64{1}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
OwnerIsAdminLookup = func(uid uint64) bool { return false }
|
||||
AllServerIDsLookup = func() []uint64 { return []uint64{1, 2, 3} }
|
||||
|
||||
cron := &Cron{
|
||||
Common: Common{ID: 9, UserID: 100},
|
||||
Cover: CronCoverAll,
|
||||
Servers: []uint64{}, // empty deny-list: fan-out = owner-set = [1]
|
||||
}
|
||||
ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
|
||||
ctx.Set(CtxKeyAuthorizedUser, &User{Common: Common{ID: 100}, Role: RoleMember})
|
||||
// PAT whitelist = [1] — covers everything the member owner can fan out to.
|
||||
ctx.Set(CtxKeyAPIToken, &stubPATAccessor{ids: []uint64{1}})
|
||||
|
||||
if !cron.HasPermission(ctx) {
|
||||
t.Fatal("member-owned CronCoverAll fans out to owner servers only; PAT [1] covers them all")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user