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
@@ -1,6 +1,9 @@
package controller
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
@@ -120,3 +123,80 @@ func TestListServerGroupAdminSeesAllGroupsIncludingEmpty(t *testing.T) {
assert.ElementsMatch(t, []string{"Public Group", "Empty Group"}, names,
"admin must keep full visibility, including empty groups")
}
func newServerGroupCtxWithPAT(viewer *model.User, tok *model.APIToken) *gin.Context {
c := newServerGroupCtx(viewer)
if tok != nil {
c.Set(model.CtxKeyAPIToken, tok)
}
return c
}
// PAT scoped to server_ids must hide groups whose membership is entirely
// outside the whitelist and must strip out-of-whitelist server IDs from
// remaining groups. Otherwise admin-issued limited PATs still enumerate
// every group name + server id via /api/v1/server-group.
func TestListServerGroupPATWhitelistFiltersGroupsAndServerIDs(t *testing.T) {
setupServerGroupVisibilityFixture(t)
require.NoError(t, singleton.DB.Create(&model.ServerGroupServer{
Common: model.Common{UserID: 1}, ServerGroupId: 10, ServerId: 2,
}).Error)
tok := &model.APIToken{ID: 77, UserID: 1}
tok.SetServerIDs([]uint64{1})
items, err := listServerGroup(newServerGroupCtxWithPAT(&model.User{
Common: model.Common{ID: 1}, Role: model.RoleAdmin,
}, tok))
require.NoError(t, err)
names := collectGroupNames(items)
assert.ElementsMatch(t, []string{"Public Group"}, names,
"PAT scoped to {1} must drop the empty group and not surface group names containing only server 2")
if assert.Len(t, items, 1) {
assert.ElementsMatch(t, []uint64{1}, items[0].Servers,
"server IDs outside the PAT whitelist must be redacted from the response")
}
}
func TestListServerGroupPATWithDisjointWhitelistReturnsEmpty(t *testing.T) {
setupServerGroupVisibilityFixture(t)
tok := &model.APIToken{ID: 78, UserID: 1}
tok.SetServerIDs([]uint64{9999})
items, err := listServerGroup(newServerGroupCtxWithPAT(&model.User{
Common: model.Common{ID: 1}, Role: model.RoleAdmin,
}, tok))
require.NoError(t, err)
assert.Empty(t, items, "PAT scoped to a server it cannot reach must see no groups, not all of them")
}
// batchDeleteServerGroup must refuse to delete a group whose members are not
// entirely covered by the PAT whitelist; otherwise an admin's limited PAT can
// drop groups that touch servers outside its scope.
func TestBatchDeleteServerGroupRejectsPATOutsideWhitelist(t *testing.T) {
setupServerGroupVisibilityFixture(t)
require.NoError(t, singleton.DB.Create(&model.ServerGroupServer{
Common: model.Common{UserID: 1}, ServerGroupId: 10, ServerId: 2,
}).Error)
tok := &model.APIToken{ID: 79, UserID: 1}
tok.SetServerIDs([]uint64{1})
c := newServerGroupCtxWithPAT(&model.User{
Common: model.Common{ID: 1}, Role: model.RoleAdmin,
}, tok)
body, _ := json.Marshal([]uint64{10})
c.Request = httptest.NewRequest(http.MethodPost, "/api/v1/batch-delete/server-group", bytes.NewReader(body))
c.Request.Header.Set("Content-Type", "application/json")
_, err := batchDeleteServerGroup(c)
require.Error(t, err, "PAT scoped to {1} must not delete group 10 which still contains server 2")
var remaining int64
require.NoError(t, singleton.DB.Model(&model.ServerGroup{}).Where("id = ?", 10).Count(&remaining).Error)
assert.Equal(t, int64(1), remaining, "group 10 must remain after refused PAT delete")
}