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>
91 lines
3.3 KiB
Go
91 lines
3.3 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/nezhahq/nezha/model"
|
|
"github.com/nezhahq/nezha/service/rpc"
|
|
)
|
|
|
|
func ensureNezhaSingleton(t *testing.T) {
|
|
t.Helper()
|
|
if rpc.NezhaHandlerSingleton == nil {
|
|
rpc.NezhaHandlerSingleton = rpc.NewNezhaHandler()
|
|
}
|
|
}
|
|
|
|
// H2 regression: terminal/FM stream attachment must respect the caller PAT's
|
|
// server_ids whitelist. The existing IsStreamAuthorizedForUser only gates on
|
|
// creator-id / admin role, so an admin's server-limited PAT could attach to
|
|
// a stream targeting any server simply by knowing the streamId.
|
|
func TestStreamAttachAllowedForRequest_DeniesPATOutsideWhitelist(t *testing.T) {
|
|
ensureNezhaSingleton(t)
|
|
streamId := "stream-h2-deny"
|
|
rpc.NezhaHandlerSingleton.CreateStream(streamId, 1, 99)
|
|
t.Cleanup(func() { _ = rpc.NezhaHandlerSingleton.CloseStream(streamId) })
|
|
|
|
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"}) // does NOT include 99
|
|
|
|
if streamAttachAllowedForRequest(ctx, streamId) {
|
|
t.Fatal("admin PAT scoped to [1] must NOT attach to a stream targeting server 99")
|
|
}
|
|
}
|
|
|
|
func TestStreamAttachAllowedForRequest_AllowsPATInsideWhitelist(t *testing.T) {
|
|
ensureNezhaSingleton(t)
|
|
streamId := "stream-h2-allow"
|
|
rpc.NezhaHandlerSingleton.CreateStream(streamId, 1, 5)
|
|
t.Cleanup(func() { _ = rpc.NezhaHandlerSingleton.CloseStream(streamId) })
|
|
|
|
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: "5"})
|
|
|
|
if !streamAttachAllowedForRequest(ctx, streamId) {
|
|
t.Fatal("PAT scoped to [5] must attach to a stream targeting server 5")
|
|
}
|
|
}
|
|
|
|
func TestStreamAttachAllowedForRequest_JWTAdminUnchanged(t *testing.T) {
|
|
ensureNezhaSingleton(t)
|
|
streamId := "stream-h2-jwt"
|
|
rpc.NezhaHandlerSingleton.CreateStream(streamId, 1, 99)
|
|
t.Cleanup(func() { _ = rpc.NezhaHandlerSingleton.CloseStream(streamId) })
|
|
|
|
ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
|
|
ctx.Set(model.CtxKeyAuthorizedUser, &model.User{Common: model.Common{ID: 1}, Role: model.RoleAdmin})
|
|
|
|
if !streamAttachAllowedForRequest(ctx, streamId) {
|
|
t.Fatal("JWT admin (no PAT) must continue to attach via the existing admin branch")
|
|
}
|
|
}
|
|
|
|
func TestStreamAttachAllowedForRequest_DeniesNonCreatorMember(t *testing.T) {
|
|
ensureNezhaSingleton(t)
|
|
streamId := "stream-h2-foreign"
|
|
rpc.NezhaHandlerSingleton.CreateStream(streamId, 1, 5)
|
|
t.Cleanup(func() { _ = rpc.NezhaHandlerSingleton.CloseStream(streamId) })
|
|
|
|
ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
|
|
ctx.Set(model.CtxKeyAuthorizedUser, &model.User{Common: model.Common{ID: 2}, Role: model.RoleMember})
|
|
|
|
if streamAttachAllowedForRequest(ctx, streamId) {
|
|
t.Fatal("non-creator non-admin member must remain denied (pre-existing GHSA gate)")
|
|
}
|
|
}
|
|
|
|
func TestStreamAttachAllowedForRequest_UnknownStreamRejected(t *testing.T) {
|
|
ensureNezhaSingleton(t)
|
|
ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
|
|
ctx.Set(model.CtxKeyAuthorizedUser, &model.User{Common: model.Common{ID: 1}, Role: model.RoleAdmin})
|
|
|
|
if streamAttachAllowedForRequest(ctx, "does-not-exist") {
|
|
t.Fatal("unknown streamId must remain rejected")
|
|
}
|
|
}
|