mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-09-19 09:40: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>
93 lines
2.4 KiB
Go
93 lines
2.4 KiB
Go
package rpc
|
|
|
|
import (
|
|
"io"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// nopRWC is a minimal io.ReadWriteCloser used for race tests; Close is a
|
|
// no-op so the racer goroutines do not panic on shared state.
|
|
type nopRWC struct{}
|
|
|
|
func (nopRWC) Read(p []byte) (int, error) { return 0, io.EOF }
|
|
func (nopRWC) Write(p []byte) (int, error) { return len(p), nil }
|
|
func (nopRWC) Close() error { return nil }
|
|
|
|
// H10 regression: UserConnected/AgentConnected mutate stream.userIo /
|
|
// stream.agentIo without holding ioStreamMutex, while WaitForAgent /
|
|
// RevokeStreamsForPurpose / RevokeStreamsForServer read & close the same
|
|
// fields under the lock. The go race detector catches it deterministically
|
|
// under -race; without the fix this test fails.
|
|
func TestIOStream_AgentConnectedIsRaceFreeUnderLock(t *testing.T) {
|
|
h := NewNezhaHandler()
|
|
const streamId = "race-test"
|
|
h.CreateStream(streamId, 1, 1)
|
|
t.Cleanup(func() { _ = h.CloseStream(streamId) })
|
|
|
|
var wg sync.WaitGroup
|
|
wg.Add(3)
|
|
|
|
go func() {
|
|
defer wg.Done()
|
|
// repeatedly attach an agent
|
|
for i := 0; i < 200; i++ {
|
|
_ = h.AgentConnected(streamId, nopRWC{})
|
|
}
|
|
}()
|
|
|
|
go func() {
|
|
defer wg.Done()
|
|
// concurrently attach a user
|
|
for i := 0; i < 200; i++ {
|
|
_ = h.UserConnected(streamId, nopRWC{})
|
|
}
|
|
}()
|
|
|
|
go func() {
|
|
defer wg.Done()
|
|
// Revoker takes the write lock and reads the same userIo/agentIo
|
|
// fields the unsynchronised writers above are setting. Use a real
|
|
// targetServerID (1) so RevokeStreamsForServer actually inspects
|
|
// the entry's userIo/agentIo before deleting.
|
|
for i := 0; i < 200; i++ {
|
|
h.RevokeStreamsForServer(1)
|
|
h.CreateStream(streamId, 1, 1)
|
|
}
|
|
}()
|
|
|
|
wg.Wait()
|
|
}
|
|
|
|
// StartStream reads stream.userIo/agentIo while it waits for both endpoints.
|
|
// Those reads must be lock-protected against the concurrent writes done by
|
|
// UserConnected/AgentConnected; otherwise -race flags the data race on the
|
|
// interface fields.
|
|
func TestIOStream_StartStreamReadsAreRaceFree(t *testing.T) {
|
|
h := NewNezhaHandler()
|
|
const streamId = "startstream-race"
|
|
h.CreateStream(streamId, 2, 2)
|
|
t.Cleanup(func() { _ = h.CloseStream(streamId) })
|
|
|
|
var wg sync.WaitGroup
|
|
wg.Add(3)
|
|
|
|
go func() {
|
|
defer wg.Done()
|
|
_ = h.StartStream(streamId, 50*time.Millisecond)
|
|
}()
|
|
go func() {
|
|
defer wg.Done()
|
|
time.Sleep(5 * time.Millisecond)
|
|
_ = h.AgentConnected(streamId, nopRWC{})
|
|
}()
|
|
go func() {
|
|
defer wg.Done()
|
|
time.Sleep(5 * time.Millisecond)
|
|
_ = h.UserConnected(streamId, nopRWC{})
|
|
}()
|
|
|
|
wg.Wait()
|
|
}
|