mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-09-19 17:50: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>
56 lines
2.1 KiB
Go
56 lines
2.1 KiB
Go
package model
|
|
|
|
import (
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// Regression: NAT had no HasPermission override, so it fell back to
|
|
// Common.HasPermission (owner/admin only). listHandler/CheckPermission gate
|
|
// on NAT.HasPermission, meaning a server-limited PAT could list/update/delete
|
|
// NAT records bound to off-whitelist servers of the same owner. NAT now
|
|
// applies CanAccessServer(NAT.ServerID) like Server/Service/Cron.
|
|
func TestNATHasPermission_DeniesOffWhitelistServerForLimitedPAT(t *testing.T) {
|
|
nat := &NAT{Common: Common{ID: 1, UserID: 100}, ServerID: 2}
|
|
ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
|
|
ctx.Set(CtxKeyAuthorizedUser, &User{Common: Common{ID: 100}, Role: RoleMember})
|
|
ctx.Set(CtxKeyAPIToken, &stubPATAccessor{ids: []uint64{1}}) // whitelist excludes server 2
|
|
|
|
if nat.HasPermission(ctx) {
|
|
t.Fatal("server-limited PAT must not reach a NAT bound to an off-whitelist server")
|
|
}
|
|
}
|
|
|
|
func TestNATHasPermission_AllowsWhitelistedServerForLimitedPAT(t *testing.T) {
|
|
nat := &NAT{Common: Common{ID: 1, UserID: 100}, ServerID: 1}
|
|
ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
|
|
ctx.Set(CtxKeyAuthorizedUser, &User{Common: Common{ID: 100}, Role: RoleMember})
|
|
ctx.Set(CtxKeyAPIToken, &stubPATAccessor{ids: []uint64{1}})
|
|
|
|
if !nat.HasPermission(ctx) {
|
|
t.Fatal("PAT whitelisted to the NAT's server must be allowed")
|
|
}
|
|
}
|
|
|
|
func TestNATHasPermission_NoPATPassesViaCommonHasPermission(t *testing.T) {
|
|
nat := &NAT{Common: Common{ID: 1, UserID: 100}, ServerID: 2}
|
|
ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
|
|
ctx.Set(CtxKeyAuthorizedUser, &User{Common: Common{ID: 100}, Role: RoleMember})
|
|
|
|
if !nat.HasPermission(ctx) {
|
|
t.Fatal("owner without PAT must keep the existing owner/admin pass")
|
|
}
|
|
}
|
|
|
|
func TestNATHasPermission_DeniesNonOwner(t *testing.T) {
|
|
nat := &NAT{Common: Common{ID: 1, UserID: 100}, ServerID: 1}
|
|
ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
|
|
ctx.Set(CtxKeyAuthorizedUser, &User{Common: Common{ID: 200}, Role: RoleMember})
|
|
|
|
if nat.HasPermission(ctx) {
|
|
t.Fatal("a different non-admin user must not reach another owner's NAT")
|
|
}
|
|
}
|