Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
security: fix authorization bypass via middleware cache
Antonio Ojea06bcd9538d0a
diff --git a/cmd/sam-node/middleware.go b/cmd/sam-node/middleware.goindex 174d7dc8..0c26cbc0 100644--- a/cmd/sam-node/middleware.go+++ b/cmd/sam-node/middleware.go@@ -130,7 +130,7 @@ func (n *SamNode) WithBiscuitAuth(next func(network.Stream, RequestContext)) net// Check verification cachetokenHash := sha256.Sum256(authFrame.Biscuit)- hashStr := hex.EncodeToString(tokenHash[:]) + ":" + remotePeer.String()+ hashStr := hex.EncodeToString(tokenHash[:]) + ":" + remotePeer.String() + ":" + reqCtx.Protocol + ":" + reqCtx.Targetif pubKeyStr, ok := n.verificationCache.Get(hashStr); ok {n.keysMu.RLock()diff --git a/cmd/sam-node/middleware_test.go b/cmd/sam-node/middleware_test.goindex 0ad1e749..bc5814e2 100644--- a/cmd/sam-node/middleware_test.go+++ b/cmd/sam-node/middleware_test.go@@ -508,3 +508,83 @@ func TestVerifyBiscuitCache(t *testing.T) {t.Fatal("Expected verifyBiscuit to FAIL after key rotation, but it succeeded")}}++func TestAuthorizationCacheBypass(t *testing.T) {+ pub, priv, err := ed25519.GenerateKey(nil)+ if err != nil {+ t.Fatal(err)+ }++ dummyPeer := peer.ID("dummy-peer-id")++ // Mint token allowing ONLY query_db+ builder := biscuit.NewBuilder(priv)+ _ = builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{+ Name: "node",+ IDs: []biscuit.Term{biscuit.String(dummyPeer.String())},+ }})+ _ = builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{+ Name: "client_peer_id",+ IDs: []biscuit.Term{biscuit.String(dummyPeer.String())},+ }})+ _ = builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{+ Name: "allow_mcp_server",+ IDs: []biscuit.Term{biscuit.String("query_db")},+ }})++ b, _ := builder.Build()+ tokenBytes, _ := b.Serialize()++ cache, _ := lru.New[string, string](10)+ revCache, _ := lru.New[string, int64](10)+ rl, _ := NewPeerRateLimiter(100)+ node := &SamNode{+ trustedKeys: []TrustedKey{{Key: pub, ReceivedAt: time.Now()}},+ verificationCache: cache,+ revokedPeers: revCache,+ rateLimiter: rl,+ TrustHubRBAC: true,+ }++ // Helper to simulate request+ doRequest := func(target string) bool {+ pr1, pw1 := io.Pipe()+ pr2, pw2 := io.Pipe()+ serverStream := &mockStream{r: pr1, w: pw2, protocol: protocol.ID("mcp"), conn: &mockConn{remotePeer: dummyPeer}}++ done := make(chan bool, 1)+ go func() {+ handler := node.WithBiscuitAuth(func(s network.Stream, reqCtx RequestContext) {+ done <- true+ })+ handler(serverStream)+ close(done)+ }()++ writer := msgio.NewVarintWriter(pw1)+ authFrame := &api.AuthFrame{Biscuit: tokenBytes, TargetService: target}+ data, _ := proto.Marshal(authFrame)+ _ = writer.WriteMsg(data)++ reader := msgio.NewVarintReaderSize(pr2, 1024*64)+ msg, err := reader.ReadMsg()+ if err != nil {+ return false+ }+ var resp api.AuthResponse+ _ = proto.Unmarshal(msg, &resp)++ success := <-done+ return resp.Success && success+ }++ // 1. Authorized target should succeed+ if !doRequest("query_db") {+ t.Fatal("Expected authorized target to succeed")+ }++ // 2. Unauthorized target with the same token should FAIL+ if doRequest("reboot_server") {+ t.Fatal("SECURITY BUG: Unauthorized target succeeded due to cache bypass!")+ }+}