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 58c1ea7b0b
commit ab25662ddd
153 changed files with 16974 additions and 244 deletions
+6 -4
View File
@@ -36,8 +36,7 @@ func createFM(c *gin.Context) (*model.CreateFMResponse, error) {
if server == nil {
return nil, singleton.Localizer.ErrorT("server not found or not connected")
}
stream := server.GetTaskStream()
if stream == nil {
if server.GetTaskStream() == nil {
return nil, singleton.Localizer.ErrorT("server not found or not connected")
}
@@ -55,7 +54,7 @@ func createFM(c *gin.Context) (*model.CreateFMResponse, error) {
fmData, _ := json.Marshal(&model.TaskFM{
StreamID: streamId,
})
if err := stream.Send(&proto.Task{
if err := server.SendTask(&proto.Task{
Type: model.TaskTypeFM,
Data: string(fmData),
}); err != nil {
@@ -79,7 +78,7 @@ func fmStream(c *gin.Context) (any, error) {
// GHSA-style fix: io_stream sessions must be reachable only by their creator
// (or an admin). Without this, any authenticated user who learns a stream
// UUID can hijack a live file-manager session on the target server.
if !rpc.NezhaHandlerSingleton.IsStreamAuthorizedForUser(streamId, getUid(c), callerIsAdmin(c)) {
if !streamAttachAllowedForRequest(c, streamId) {
return nil, singleton.Localizer.ErrorT("permission denied")
}
if _, err := rpc.NezhaHandlerSingleton.GetStream(streamId); err != nil {
@@ -94,6 +93,9 @@ func fmStream(c *gin.Context) (any, error) {
defer wsConn.Close()
conn := websocketx.NewConn(wsConn)
deregisterPAT := registerPATConnection(c, func() { _ = wsConn.Close() })
defer deregisterPAT()
go func() {
// PING 保活
for {