mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-09-19 09:40:12 +00:00
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:
@@ -0,0 +1,62 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// The per-token limiter map had no eviction: every distinct token ID ever
|
||||
// seen left a permanent entry. A user churning PATs (create/use/delete in a
|
||||
// loop) grows the map without bound. Allow must opportunistically prune
|
||||
// windows idle past the minute bucket so memory stays proportional to the
|
||||
// active token set, not the historical one.
|
||||
func TestMCPRateLimiter_PrunesStaleTokenWindows(t *testing.T) {
|
||||
rl := newMCPRateLimiter(10, 120)
|
||||
|
||||
stale := time.Now().Add(-10 * time.Minute)
|
||||
for i := uint64(1); i <= 500; i++ {
|
||||
rl.mu.Lock()
|
||||
rl.perToken[i] = &tokenWindow{
|
||||
secBucketStart: stale,
|
||||
minBucketStart: stale,
|
||||
}
|
||||
rl.mu.Unlock()
|
||||
}
|
||||
|
||||
// A fresh request triggers a prune sweep of idle windows.
|
||||
if !rl.Allow(99999) {
|
||||
t.Fatal("fresh token must be allowed")
|
||||
}
|
||||
|
||||
rl.mu.Lock()
|
||||
size := len(rl.perToken)
|
||||
rl.mu.Unlock()
|
||||
|
||||
// Only the just-active token (99999) should remain; the 500 stale ones
|
||||
// must have been evicted.
|
||||
if size > 1 {
|
||||
t.Fatalf("stale token windows were not pruned: map still holds %d entries", size)
|
||||
}
|
||||
}
|
||||
|
||||
// Pruning must NOT evict tokens that are still within their active window,
|
||||
// otherwise an in-flight client loses its accumulated count and effectively
|
||||
// resets its budget.
|
||||
func TestMCPRateLimiter_KeepsActiveTokenWindows(t *testing.T) {
|
||||
rl := newMCPRateLimiter(10, 120)
|
||||
|
||||
if !rl.Allow(1) {
|
||||
t.Fatal("token 1 must be allowed")
|
||||
}
|
||||
if !rl.Allow(2) {
|
||||
t.Fatal("token 2 must be allowed")
|
||||
}
|
||||
|
||||
rl.mu.Lock()
|
||||
size := len(rl.perToken)
|
||||
rl.mu.Unlock()
|
||||
|
||||
if size != 2 {
|
||||
t.Fatalf("active token windows must be retained, got %d entries", size)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user