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>
118 lines
3.4 KiB
Go
118 lines
3.4 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/nezhahq/nezha/model"
|
|
"github.com/nezhahq/nezha/service/singleton"
|
|
)
|
|
|
|
// H1 regression: updateServerGroup must reject a server-limited PAT whose
|
|
// whitelist does not cover the group's CURRENT membership. Today the
|
|
// handler only checks the incoming sg.Servers list and then unconditionally
|
|
// `DELETE FROM server_group_server WHERE server_group_id = ?`, so a PAT
|
|
// scoped to [X] can remove server Y (owned by another tenant or just
|
|
// outside the whitelist) from a group it shares with X.
|
|
func TestPatHasGroupMembershipAccess_DeniesGroupContainingOutsideServer(t *testing.T) {
|
|
db := newTestDB(t)
|
|
swap := swapSingletonDB(t, db)
|
|
defer swap()
|
|
|
|
if err := db.Create(&model.ServerGroupServer{
|
|
Common: model.Common{ID: 1, UserID: 1},
|
|
ServerGroupId: 42,
|
|
ServerId: 9, // outside whitelist
|
|
}).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.Create(&model.ServerGroupServer{
|
|
Common: model.Common{ID: 2, UserID: 1},
|
|
ServerGroupId: 42,
|
|
ServerId: 1, // inside whitelist
|
|
}).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
|
|
ctx.Set(model.CtxKeyAuthorizedUser, &model.User{Common: model.Common{ID: 1}, Role: model.RoleAdmin})
|
|
ctx.Set(model.CtxKeyAPIToken, &model.APIToken{ServersCSV: "1"})
|
|
|
|
if patGroupMembershipAccessAllowed(ctx, 42) {
|
|
t.Fatal("PAT scoped to [1] must NOT be allowed to mutate a group whose current members include server 9; " +
|
|
"transactional DELETE+INSERT would drop server 9 from the group")
|
|
}
|
|
}
|
|
|
|
func TestPatHasGroupMembershipAccess_AllowsGroupFullyInsideWhitelist(t *testing.T) {
|
|
db := newTestDB(t)
|
|
swap := swapSingletonDB(t, db)
|
|
defer swap()
|
|
|
|
if err := db.Create(&model.ServerGroupServer{
|
|
Common: model.Common{ID: 1, UserID: 1},
|
|
ServerGroupId: 7,
|
|
ServerId: 1,
|
|
}).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
|
|
ctx.Set(model.CtxKeyAuthorizedUser, &model.User{Common: model.Common{ID: 1}, Role: model.RoleAdmin})
|
|
ctx.Set(model.CtxKeyAPIToken, &model.APIToken{ServersCSV: "1,2"})
|
|
|
|
if !patGroupMembershipAccessAllowed(ctx, 7) {
|
|
t.Fatal("PAT whitelist [1,2] covers all current members → must allow update")
|
|
}
|
|
}
|
|
|
|
func TestPatHasGroupMembershipAccess_JWTAlwaysAllowed(t *testing.T) {
|
|
db := newTestDB(t)
|
|
swap := swapSingletonDB(t, db)
|
|
defer swap()
|
|
|
|
if err := db.Create(&model.ServerGroupServer{
|
|
Common: model.Common{ID: 1, UserID: 1},
|
|
ServerGroupId: 100,
|
|
ServerId: 99,
|
|
}).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
|
|
ctx.Set(model.CtxKeyAuthorizedUser, &model.User{Common: model.Common{ID: 1}, Role: model.RoleAdmin})
|
|
|
|
if !patGroupMembershipAccessAllowed(ctx, 100) {
|
|
t.Fatal("JWT requests (no PAT) must always pass — the existing admin/owner check stands")
|
|
}
|
|
}
|
|
|
|
func newTestDB(t *testing.T) *gorm.DB {
|
|
t.Helper()
|
|
db, err := gorm.Open(sqlite.Open("file:"+t.Name()+"?mode=memory&cache=shared"), &gorm.Config{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.AutoMigrate(&model.ServerGroupServer{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() {
|
|
sqlDB, _ := db.DB()
|
|
if sqlDB != nil {
|
|
_ = sqlDB.Close()
|
|
}
|
|
})
|
|
return db
|
|
}
|
|
|
|
func swapSingletonDB(t *testing.T, db *gorm.DB) func() {
|
|
t.Helper()
|
|
original := singleton.DB
|
|
singleton.DB = db
|
|
return func() { singleton.DB = original }
|
|
}
|