Skip to content

Agent Mesh

fic race

Antonio Ojea49c2c85cef3c
diff --git a/cmd/sam-node/mcp_test.go b/cmd/sam-node/mcp_test.go
index 20ef4bc1..1143589d 100644
--- a/cmd/sam-node/mcp_test.go
+++ b/cmd/sam-node/mcp_test.go
@@ -19,6 +19,7 @@ import (
"testing"
"time"
+ lru "github.com/hashicorp/golang-lru/v2"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
@@ -101,7 +102,14 @@ func TestZeroTrustMCPServer(t *testing.T) {
serverStream := &mockStream{r: pr1, w: pw2, protocol: protocol.ID("/sam/mcp/1.0.0")}
clientStream := &mockStream{r: pr2, w: pw1, protocol: protocol.ID("/sam/mcp/1.0.0")}
- node := &SamNode{}
+ rl, _ := NewPeerRateLimiter(100)
+ rp, _ := lru.New[string, int64](100)
+ vc, _ := lru.New[string, string](100)
+ node := &SamNode{
+ rateLimiter: rl,
+ revokedPeers: rp,
+ verificationCache: vc,
+ }
go func() {
handler := node.WithBiscuitAuth(node.HandleMCPStream)
diff --git a/cmd/sam-node/middleware.go b/cmd/sam-node/middleware.go
index 37bf0caf..68386919 100644
--- a/cmd/sam-node/middleware.go
+++ b/cmd/sam-node/middleware.go
@@ -96,21 +96,34 @@ func (n *SamNode) WithBiscuitAuth(next network.StreamHandler) network.StreamHand
tokenHash := sha256.Sum256(authFrame.Biscuit)
hashStr := hex.EncodeToString(tokenHash[:]) + ":" + remotePeer.String()
- if valid, ok := n.verificationCache.Get(hashStr); ok && valid {
- logger.Infof("[Auth] Token cache hit for %s", remotePeer)
- // Valid
- resp := &api.AuthResponse{Success: true}
- respBytes, err := proto.Marshal(resp)
- if err != nil {
- logger.Errorf("[Auth] Failed to marshal ACK response: %v", err)
- return
+ if pubKeyStr, ok := n.verificationCache.Get(hashStr); ok {
+ n.keysMu.RLock()
+ keys := n.trustedKeys
+ n.keysMu.RUnlock()
+
+ trusted := false
+ for _, tk := range keys {
+ if hex.EncodeToString(tk.Key) == pubKeyStr {
+ trusted = true
+ break
+ }
}
- if err := writer.WriteMsg(respBytes); err != nil {
- logger.Errorf("[Auth] Failed to write ACK to %s: %v", remotePeer, err)
+ if trusted {
+ logger.Infof("[Auth] Token cache hit for %s", remotePeer)
+ // Valid
+ resp := &api.AuthResponse{Success: true}
+ respBytes, err := proto.Marshal(resp)
+ if err != nil {
+ logger.Errorf("[Auth] Failed to marshal ACK response: %v", err)
+ return
+ }
+ if err := writer.WriteMsg(respBytes); err != nil {
+ logger.Errorf("[Auth] Failed to write ACK to %s: %v", remotePeer, err)
+ return
+ }
+ next(s)
return
}
- next(s)
- return
}
n.keysMu.RLock()
@@ -119,10 +132,12 @@ func (n *SamNode) WithBiscuitAuth(next network.StreamHandler) network.StreamHand
var authorized bool
var lastErr error
+ var successfulKey ed25519.PublicKey
for _, pubKey := range keys {
logger.Infof("[Auth] Trying key: %x", pubKey.Key)
if err := n.Authorize(authFrame.Biscuit, reqCtx, pubKey.Key); err == nil {
authorized = true
+ successfulKey = pubKey.Key
break
} else {
lastErr = err
@@ -183,7 +198,7 @@ func (n *SamNode) WithBiscuitAuth(next network.StreamHandler) network.StreamHand
}
// Cache successful verification
- n.verificationCache.Add(hashStr, true)
+ n.verificationCache.Add(hashStr, hex.EncodeToString(successfulKey))
// Valid
resp := &api.AuthResponse{Success: true}
@@ -198,6 +213,9 @@ func (n *SamNode) WithBiscuitAuth(next network.StreamHandler) network.StreamHand
}
func (n *SamNode) Authorize(rawToken []byte, req RequestContext, pubKey ed25519.PublicKey) error {
+ if len(pubKey) != ed25519.PublicKeySize {
+ return fmt.Errorf("invalid public key size: %d", len(pubKey))
+ }
b, err := biscuit.Unmarshal(rawToken)
if err != nil {
return fmt.Errorf("invalid biscuit: %w", err)
diff --git a/cmd/sam-node/middleware_test.go b/cmd/sam-node/middleware_test.go
index 2bc0e5e9..c5dce03d 100644
--- a/cmd/sam-node/middleware_test.go
+++ b/cmd/sam-node/middleware_test.go
@@ -52,8 +52,6 @@ func TestAuthorize(t *testing.T) {
_ = store.Close()
}()
-
-
// Create a biscuit token
pub, priv, err := ed25519.GenerateKey(nil)
if err != nil {
@@ -101,8 +99,8 @@ func TestAuthorize(t *testing.T) {
}
node := &SamNode{
- Store: store,
- trustedKeys: []TrustedKey{{Key: pub, ReceivedAt: time.Now()}},
+ Store: store,
+ trustedKeys: []TrustedKey{{Key: pub, ReceivedAt: time.Now()}},
}
req := RequestContext{
@@ -204,7 +202,7 @@ attenuation:
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
builder := biscuit.NewBuilder(priv)
-
+
err := builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{
Name: "node",
IDs: []biscuit.Term{biscuit.String(dummyPeer.String())},
@@ -249,8 +247,8 @@ attenuation:
}
node := &SamNode{
- trustedKeys: []TrustedKey{{Key: pub, ReceivedAt: time.Now()}},
- LocalPolicy: localPolicy,
+ trustedKeys: []TrustedKey{{Key: pub, ReceivedAt: time.Now()}},
+ LocalPolicy: localPolicy,
}
req := RequestContext{
@@ -307,9 +305,11 @@ func TestRevocation(t *testing.T) {
}
cache, _ := lru.New[string, int64](10000)
+ rl, _ := NewPeerRateLimiter(100)
node := &SamNode{
trustedKeys: []TrustedKey{{Key: pub, ReceivedAt: time.Now()}},
revokedPeers: cache,
+ rateLimiter: rl,
}
// Mark as revoked
@@ -319,7 +319,7 @@ func TestRevocation(t *testing.T) {
pr2, pw2 := io.Pipe()
serverStream := &mockStream{r: pr1, w: pw2, protocol: protocol.ID("/test/proto")}
-
+
// Run handler in goroutine
go func() {
handler := node.WithBiscuitAuth(func(s network.Stream) {
@@ -342,7 +342,7 @@ func TestRevocation(t *testing.T) {
if err != nil {
t.Fatalf("failed to read response: %v", err)
}
-
+
var resp api.AuthResponse
if err := proto.Unmarshal(msg, &resp); err != nil {
t.Fatal(err)
@@ -425,8 +425,8 @@ func TestHandleAuthHandshakeCache(t *testing.T) {
t.Fatal(err)
}
- cache, _ := lru.New[string, bool](10)
-
+ cache, _ := lru.New[string, string](10)
+
dir := t.TempDir()
store, err := NewStore(dir)
if err != nil {
@@ -443,7 +443,7 @@ func TestHandleAuthHandshakeCache(t *testing.T) {
pr1, pw1 := io.Pipe()
serverStream := &mockStream{r: pr1, w: io.Discard, protocol: protocol.ID("/sam/auth/1.0.0")}
-
+
go func() {
node.HandleAuthHandshake(serverStream)
}()
@@ -456,30 +456,32 @@ func TestHandleAuthHandshakeCache(t *testing.T) {
}
time.Sleep(1 * time.Second)
-
+
_, err = store.GetVerifiedIdentity(dummyPeer)
if err != nil {
t.Fatalf("Expected identity to be saved, got err: %v", err)
}
tokenHash := sha256.Sum256(tokenBytes)
- hashStr := hex.EncodeToString(tokenHash[:])
-
- if valid, ok := cache.Get(hashStr); !ok || !valid {
- t.Fatal("Expected token to be in verification cache")
+ hashStr := hex.EncodeToString(tokenHash[:]) + ":" + dummyPeer.String()
+
+ if pubKeyStr, ok := cache.Get(hashStr); !ok || pubKeyStr != hex.EncodeToString(pub) {
+ t.Fatal("Expected token to be in verification cache with correct key")
}
// Now corrupt keys and try again.
node.keysMu.Lock()
- node.trustedKeys = []TrustedKey{{Key: []byte("invalid-key"), ReceivedAt: time.Now()}}
+ invalidKey := make([]byte, ed25519.PublicKeySize)
+ copy(invalidKey, []byte("invalid-key"))
+ node.trustedKeys = []TrustedKey{{Key: invalidKey, ReceivedAt: time.Now()}}
node.keysMu.Unlock()
dir2 := t.TempDir()
store2, _ := NewStore(dir2)
defer func() { _ = store2.Close() }()
-
+
node.Store = store2
-
+
pr5, pw5 := io.Pipe()
serverStream3 := &mockStream{r: pr5, w: io.Discard, protocol: protocol.ID("/sam/auth/1.0.0")}
@@ -493,9 +495,9 @@ func TestHandleAuthHandshakeCache(t *testing.T) {
}
time.Sleep(1 * time.Second)
-
+
_, err = store2.GetVerifiedIdentity(dummyPeer)
- if err != nil {
- t.Fatalf("Expected identity to be saved via cache hit, got err: %v", err)
+ if err == nil {
+ t.Fatal("Expected identity to NOT be saved via cache hit with invalid key")
}
}
diff --git a/cmd/sam-node/node.go b/cmd/sam-node/node.go
index 0209647c..2648ac81 100644
--- a/cmd/sam-node/node.go
+++ b/cmd/sam-node/node.go
@@ -66,7 +66,7 @@ type SamNode struct {
mu sync.Mutex
LocalPolicy *CompiledLocalPolicy
revokedPeers *lru.Cache[string, int64]
- verificationCache *lru.Cache[string, bool]
+ verificationCache *lru.Cache[string, string]
trustedKeys []TrustedKey
keysMu sync.RWMutex
rateLimiter *PeerRateLimiter
@@ -98,7 +98,7 @@ func NewSamNode(ctx context.Context, privKey crypto.PrivKey, hubPubKey ed25519.P
return nil, fmt.Errorf("failed to create revocation cache: %w", err)
}
- node.verificationCache, err = lru.New[string, bool](1000)
+ node.verificationCache, err = lru.New[string, string](1000)
if err != nil {
return nil, fmt.Errorf("failed to create verification cache: %w", err)
}
@@ -445,9 +445,22 @@ func (n *SamNode) HandleAuthHandshake(s network.Stream) {
tokenHash := sha256.Sum256(exchange.Biscuit)
hashStr := hex.EncodeToString(tokenHash[:]) + ":" + remotePeer.String()
- if valid, ok := n.verificationCache.Get(hashStr); ok && valid {
- logger.Infof("[AuthN] Token cache hit for %s", remotePeer)
- return
+ if pubKeyStr, ok := n.verificationCache.Get(hashStr); ok {
+ n.keysMu.RLock()
+ keys := n.trustedKeys
+ n.keysMu.RUnlock()
+
+ trusted := false
+ for _, tk := range keys {
+ if hex.EncodeToString(tk.Key) == pubKeyStr {
+ trusted = true
+ break
+ }
+ }
+ if trusted {
+ logger.Infof("[AuthN] Token cache hit for %s", remotePeer)
+ return
+ }
}
// 2. Unmarshal and verify token format
@@ -458,12 +471,16 @@ func (n *SamNode) HandleAuthHandshake(s network.Stream) {
}
authorized := false
+ var successfulKey ed25519.PublicKey
// 3. Verify signature chain against the trusted Hub keys.
n.keysMu.RLock()
keys := n.trustedKeys
n.keysMu.RUnlock()
for _, tk := range keys {
+ if len(tk.Key) != ed25519.PublicKeySize {
+ continue
+ }
authorizer, err := b.Authorizer(tk.Key)
if err != nil {
continue
@@ -479,13 +496,14 @@ func (n *SamNode) HandleAuthHandshake(s network.Stream) {
if err := authorizer.Authorize(); err == nil {
authorized = true
+ successfulKey = tk.Key
break
}
}
if authorized {
// Cache successful verification
- n.verificationCache.Add(hashStr, true)
+ n.verificationCache.Add(hashStr, hex.EncodeToString(successfulKey))
}
if !authorized {
diff --git a/tests/integration/fallback_test.go b/tests/integration/fallback_test.go
index bba1e84c..97849ccf 100644
--- a/tests/integration/fallback_test.go
+++ b/tests/integration/fallback_test.go
@@ -23,6 +23,7 @@ import (
"os/exec"
"path/filepath"
"strings"
+ "sync"
"testing"
"time"
@@ -40,9 +41,11 @@ import (
"google.golang.org/protobuf/proto"
)
+// init forces the biscuit-go parser to build its underlying participle
+// lexer and reflection caches synchronously at startup. This prevents
+// a known data race when multiple goroutines parse facts concurrently.
func init() {
- // Force initialization of parser to avoid data race in tests
- _, _ = parser.FromStringFact(`dummy("fact")`)
+ _, _ = parser.FromStringFact(`warmup("cache")`)
}
func TestFallbackReEnrollment(t *testing.T) {
@@ -116,8 +119,8 @@ func TestFallbackReEnrollment(t *testing.T) {
cmd.Dir = repoRoot(t)
cmd.Env = env
- var stdout bytes.Buffer
- var stderr bytes.Buffer
+ var stdout safeBuffer
+ var stderr safeBuffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
@@ -287,3 +290,20 @@ func startMockHubDynamic(t *testing.T, pubA, pubB ed25519.PublicKey) (peer.ID, s
return h.ID(), h.Addrs()[0].String() + "/p2p/" + h.ID().String()
}
+
+type safeBuffer struct {
+ mu sync.Mutex
+ buf bytes.Buffer
+}
+
+func (s *safeBuffer) Write(p []byte) (n int, err error) {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ return s.buf.Write(p)
+}
+
+func (s *safeBuffer) String() string {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ return s.buf.String()
+}