Files
nezha_domains/cmd/dashboard/controller/mcp_transfer_audit_throttle_test.go
T
naibaandcloudcode ab25662ddd 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>
2026-05-30 15:56:44 +00:00

69 lines
1.8 KiB
Go

package controller
import (
"testing"
"time"
)
// H8 regression: anonymous transfer failures (entry=nil — token was bogus
// or already consumed) must be sampled, not written to audit one-for-one.
// Otherwise any unauthenticated attacker can flood the audit table by
// repeatedly POSTing /mcp/upload/garbage.
func TestTransferAnonAuditThrottle_FirstRequestPasses(t *testing.T) {
th := newTransferAnonAuditThrottle(10*time.Second, 5)
if !th.shouldRecord("1.2.3.4") {
t.Fatal("first anon failure from an IP must be recorded")
}
}
func TestTransferAnonAuditThrottle_BurstCappedPerWindow(t *testing.T) {
th := newTransferAnonAuditThrottle(time.Minute, 3)
const ip = "5.6.7.8"
recorded := 0
for i := 0; i < 20; i++ {
if th.shouldRecord(ip) {
recorded++
}
}
if recorded > 3 {
t.Fatalf("burst of 20 anon failures must be capped at 3 per window, got %d", recorded)
}
if recorded == 0 {
t.Fatal("burst must record at least one sample")
}
}
func TestTransferAnonAuditThrottle_IndependentPerIP(t *testing.T) {
th := newTransferAnonAuditThrottle(time.Minute, 1)
if !th.shouldRecord("a") {
t.Fatal("first request from IP a must be recorded")
}
if !th.shouldRecord("b") {
t.Fatal("first request from a different IP must not share IP a's budget")
}
if th.shouldRecord("a") {
t.Fatal("second request from IP a within window must be dropped")
}
}
func TestTransferAnonAuditThrottle_WindowResets(t *testing.T) {
th := newTransferAnonAuditThrottle(20*time.Millisecond, 1)
const ip = "9.9.9.9"
if !th.shouldRecord(ip) {
t.Fatal("first request must be recorded")
}
if th.shouldRecord(ip) {
t.Fatal("second request inside window must be dropped")
}
time.Sleep(40 * time.Millisecond)
if !th.shouldRecord(ip) {
t.Fatal("request after window expiry must be recorded again")
}
}