Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
apply ratelimiting
Antonio Ojeab84e749d930c
diff --git a/cmd/sam-hub/main.go b/cmd/sam-hub/main.goindex 20047e52..2865ff56 100644--- a/cmd/sam-hub/main.go+++ b/cmd/sam-hub/main.go@@ -26,6 +26,7 @@ import ("github.com/biscuit-auth/biscuit-go/v2""github.com/biscuit-auth/biscuit-go/v2/parser"+ "golang.org/x/time/rate""github.com/coreos/go-oidc/v3/oidc""github.com/golang-jwt/jwt/v5""github.com/google/sam/api"@@ -39,6 +40,7 @@ import (libp2ptls "github.com/libp2p/go-libp2p/p2p/security/tls"libp2pquic "github.com/libp2p/go-libp2p/p2p/transport/quic""github.com/libp2p/go-msgio"+ "github.com/libp2p/go-libp2p/p2p/net/connmgr""github.com/libp2p/go-libp2p/p2p/transport/tcp""github.com/spf13/cobra"@@ -47,6 +49,10 @@ import (const (GracePeriod = 60 * time.Second++ // Rate limiting defaults+ EnrollRateLimit = 10+ EnrollBurst = 20)var (@@ -76,11 +82,19 @@ type Hub struct {EventTopic *pubsub.Topicgater *hubConnGatePolicy *api.PolicyConfig+ limiter *rate.Limiter}// NewHub starts a host supporting both QUIC and TCP (with TLS 1.3)func NewHub(ctx context.Context, policy *api.PolicyConfig) (*Hub, error) {gater := newHubConnGate()++ // Connection Manager for DoS protection+ cm, err := connmgr.NewConnManager(100, 400, connmgr.WithGracePeriod(time.Minute))+ if err != nil {+ return nil, fmt.Errorf("failed to create connection manager: %w", err)+ }+// Multi-transport setup for firewall traversalh, err := libp2p.New(libp2p.Transport(libp2pquic.NewTransport),@@ -91,6 +105,7 @@ func NewHub(ctx context.Context, policy *api.PolicyConfig) (*Hub, error) {libp2p.EnableRelayService(),libp2p.ConnectionGater(gater),libp2p.EnableNATService(),+ libp2p.ConnectionManager(cm),)if err != nil {return nil, err@@ -145,6 +160,7 @@ func NewHub(ctx context.Context, policy *api.PolicyConfig) (*Hub, error) {KeyRing: kr,MeshID: meshName,Policy: policy,+ limiter: rate.NewLimiter(rate.Limit(EnrollRateLimit), EnrollBurst),}h.Network().Notify(¬ifier{hub: hub})@@ -166,6 +182,13 @@ func (h *Hub) handleEnroll(s network.Stream) {_ = s.Close()}()remotePeer := s.Conn().RemotePeer()++ if !h.limiter.Allow() {+ logger.Warnf("[Enroll] Rate limit exceeded for %s, dropping connection", remotePeer)+ _ = s.Reset()+ return+ }+logger.Infof("[Enroll] New enrollment request from %s", remotePeer)reader := msgio.NewVarintReaderSize(s, 1024*64)diff --git a/cmd/sam-node/middleware.go b/cmd/sam-node/middleware.goindex e7f267d6..37bf0caf 100644--- a/cmd/sam-node/middleware.go+++ b/cmd/sam-node/middleware.go@@ -51,6 +51,12 @@ func (n *SamNode) WithBiscuitAuth(next network.StreamHandler) network.StreamHand}()remotePeer := s.Conn().RemotePeer()+ if !n.rateLimiter.Allow(remotePeer.String()) {+ logger.Warnf("[Auth] Rate limit exceeded for %s, dropping connection", remotePeer)+ _ = s.Reset()+ return+ }+// Read AuthFramereader := msgio.NewVarintReaderSize(s, 1024*64)msg, err := reader.ReadMsg()diff --git a/cmd/sam-node/node.go b/cmd/sam-node/node.goindex 3d2a6d75..0209647c 100644--- a/cmd/sam-node/node.go+++ b/cmd/sam-node/node.go@@ -29,6 +29,7 @@ import ("github.com/google/sam/api"lru "github.com/hashicorp/golang-lru/v2""github.com/libp2p/go-libp2p"+ "github.com/libp2p/go-libp2p/p2p/net/connmgr"dht "github.com/libp2p/go-libp2p-kad-dht"pubsub "github.com/libp2p/go-libp2p-pubsub""github.com/libp2p/go-libp2p/core/crypto"@@ -68,6 +69,7 @@ type SamNode struct {verificationCache *lru.Cache[string, bool]trustedKeys []TrustedKeykeysMu sync.RWMutex+ rateLimiter *PeerRateLimiter}// NewSamNode creates a new Agent instance secured with the 4-layer pipeline.@@ -87,6 +89,10 @@ func NewSamNode(ctx context.Context, privKey crypto.PrivKey, hubPubKey ed25519.P}var err error+ node.rateLimiter, err = NewPeerRateLimiter(1000)+ if err != nil {+ return nil, fmt.Errorf("failed to create rate limiter: %w", err)+ }node.revokedPeers, err = lru.New[string, int64](10000)if err != nil {return nil, fmt.Errorf("failed to create revocation cache: %w", err)@@ -108,6 +114,11 @@ func NewSamNode(ctx context.Context, privKey crypto.PrivKey, hubPubKey ed25519.P}}+ cm, err := connmgr.NewConnManager(100, 400, connmgr.WithGracePeriod(time.Minute))+ if err != nil {+ return nil, fmt.Errorf("failed to create connection manager: %w", err)+ }+// Layer 1: Establish FIPS-compliant Transports & NAT Servicesopts := []libp2p.Option{libp2p.Identity(privKey),@@ -117,6 +128,7 @@ func NewSamNode(ctx context.Context, privKey crypto.PrivKey, hubPubKey ed25519.Plibp2p.ConnectionGater(gater),libp2p.ListenAddrStrings(listenAddrs...),libp2p.EnableNATService(),+ libp2p.ConnectionManager(cm),}// If we have a Hub, configure it as our static fallback relay for NAT hole-punching@@ -224,6 +236,12 @@ func (n *SamNode) listenForHubEvents(ctx context.Context) {if err != nil {return}++ if !n.rateLimiter.Allow(msg.ReceivedFrom.String()) {+ logger.Warnf("[Mesh Event] Rate limit exceeded for %s, dropping message", msg.ReceivedFrom)+ continue+ }+var event api.MeshEventif err := proto.Unmarshal(msg.Data, &event); err != nil {logger.Errorf("[Mesh Event] Failed to unmarshal event from %s: %v", msg.ReceivedFrom, err)diff --git a/cmd/sam-node/rate_limiter.go b/cmd/sam-node/rate_limiter.gonew file mode 100644index 00000000..95c0bfb1--- /dev/null+++ b/cmd/sam-node/rate_limiter.go@@ -0,0 +1,57 @@+// Copyright 2026 Google LLC+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++package main++import (+ "sync"++ lru "github.com/hashicorp/golang-lru/v2"+ "golang.org/x/time/rate"+)++const (+ // Rate limiting defaults for peers+ PeerRateLimit = 5+ PeerBurst = 10+)++// PeerRateLimiter tracks rate limits per peer using an LRU cache.+type PeerRateLimiter struct {+ cache *lru.Cache[string, *rate.Limiter]+ mu sync.Mutex+}++// NewPeerRateLimiter creates a new PeerRateLimiter with specified cache size.+func NewPeerRateLimiter(size int) (*PeerRateLimiter, error) {+ cache, err := lru.New[string, *rate.Limiter](size)+ if err != nil {+ return nil, err+ }+ return &PeerRateLimiter{cache: cache}, nil+}++// Allow checks if the peer is allowed to perform an action.+func (prl *PeerRateLimiter) Allow(peerID string) bool {+ prl.mu.Lock()+ defer prl.mu.Unlock()++ limiter, ok := prl.cache.Get(peerID)+ if !ok {+ limiter = rate.NewLimiter(rate.Limit(PeerRateLimit), PeerBurst)+ prl.cache.Add(peerID, limiter)+ return limiter.Allow()+ }+ return limiter.Allow()+}diff --git a/cmd/sam-node/rate_limiter_test.go b/cmd/sam-node/rate_limiter_test.gonew file mode 100644index 00000000..c852016a--- /dev/null+++ b/cmd/sam-node/rate_limiter_test.go@@ -0,0 +1,74 @@+// Copyright 2026 Google LLC+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++package main++import (+ "testing"+)++func TestPeerRateLimiter(t *testing.T) {+ prl, err := NewPeerRateLimiter(2) // Small size to test eviction+ if err != nil {+ t.Fatal(err)+ }++ peer1 := "peer1"+ peer2 := "peer2"+ peer3 := "peer3"++ // Test new peer+ if !prl.Allow(peer1) {+ t.Error("Expected new peer to be allowed")+ }++ // Test burst+ for i := 0; i < PeerBurst-1; i++ {+ if !prl.Allow(peer1) {+ t.Errorf("Expected burst request %d to be allowed", i)+ }+ }++ // Exceed burst+ count := 0+ for i := 0; i < 20; i++ {+ if prl.Allow(peer1) {+ count+++ }+ }+ t.Logf("Allowed %d more requests after burst loop", count)++ if count > 0 {+ t.Errorf("Expected requests exceeding burst to be rejected, but allowed %d more", count)+ }++ // Test peer2+ if !prl.Allow(peer2) {+ t.Error("Expected peer2 to be allowed")+ }++ // Test eviction+ // Current cache: peer1, peer2+ if !prl.Allow(peer3) {+ t.Error("Expected peer3 to be allowed")+ }+ // Cache should have evicted peer1 or peer2 (LRU).+ // Since we used peer1 and then peer2, peer1 is the oldest!+ // So peer1 should be evicted!++ // Let's verify peer1 was evicted by checking if it gets a new limiter (allowed again!)+ if !prl.Allow(peer1) {+ t.Error("Expected evicted peer1 to be allowed again with new limiter")+ }+}diff --git a/tests/e2e/publish_ban/publish_ban.go b/tests/e2e/publish_ban/publish_ban.goindex 593cdacb..ec9d4604 100644--- a/tests/e2e/publish_ban/publish_ban.go+++ b/tests/e2e/publish_ban/publish_ban.go@@ -20,10 +20,14 @@ import (func main() {keyHex := flag.String("key", "", "Hub private key (hex)")peerToBan := flag.String("peer", "", "Peer ID to ban")- hubAddr := flag.String("hub", "", "Hub multiaddress")+ var addrs []string+ flag.Func("addr", "Multiaddresses to connect to", func(s string) error {+ addrs = append(addrs, s)+ return nil+ })flag.Parse()- if *keyHex == "" || *peerToBan == "" || *hubAddr == "" {+ if *keyHex == "" || *peerToBan == "" || len(addrs) == 0 {log.Fatal("Missing required flags")}@@ -46,17 +50,27 @@ func main() {log.Fatal(err)}- addr, err := multiaddr.NewMultiaddr(*hubAddr)- if err != nil {- log.Fatal(err)- }- addrInfo, err := peer.AddrInfoFromP2pAddr(addr)- if err != nil {- log.Fatal(err)+ connected := false+ for _, addrStr := range addrs {+ addr, err := multiaddr.NewMultiaddr(addrStr)+ if err != nil {+ log.Printf("Failed to parse addr %s: %v", addrStr, err)+ continue+ }+ addrInfo, err := peer.AddrInfoFromP2pAddr(addr)+ if err != nil {+ log.Printf("Failed to get addr info for %s: %v", addrStr, err)+ continue+ }+ if err := h.Connect(ctx, *addrInfo); err != nil {+ log.Printf("Failed to connect to %s: %v", addrStr, err)+ continue+ }+ fmt.Printf("Connected to %s\n", addrStr)+ connected = true}-- if err := h.Connect(ctx, *addrInfo); err != nil {- log.Fatal(err)+ if !connected {+ log.Fatal("Failed to connect to any peer")}topic, err := ps.Join(api.GossipEvents)@@ -85,14 +99,16 @@ func main() {// Wait for pubsub to discover peersfmt.Println("Waiting for pubsub peers...")- for i := 0; i < 30; i++ {- if len(topic.ListPeers()) > 0 {+ for i := 0; i < 60; i++ {+ peers := topic.ListPeers()+ if len(peers) > 0 {+ fmt.Printf("Found %d pubsub peers: %v\n", len(peers), peers)break}time.Sleep(500 * time.Millisecond)}if len(topic.ListPeers()) == 0 {- log.Fatal("No pubsub peers found")+ fmt.Println("Warning: No pubsub peers found, publishing anyway...")}if err := topic.Publish(ctx, eventData); err != nil {diff --git a/tests/e2e/revocation_test.bats b/tests/e2e/revocation_test.batsindex 655d9f12..37c8073a 100644--- a/tests/e2e/revocation_test.bats+++ b/tests/e2e/revocation_test.bats@@ -51,6 +51,9 @@ teardown() {[[ "$status" -eq 0 ]]local node1_name="${MESH_PREFIX}-node-1"mesh_wait_for_log "${node1_name}" "SAM Node Online" 20++ local node1_peer_id+ node1_peer_id=$(docker logs "${node1_name}" 2>&1 | grep -oE '12D3Koo[a-zA-Z0-9]+' | head -n 1)# Start Node 2run mesh_start_node 2@@ -77,7 +80,8 @@ teardown() {go run tests/e2e/publish_ban/publish_ban.go \--key "${hub_key}" \--peer "${node2_peer_id}" \- --hub "/dns4/sam-hub/tcp/4002/p2p/${hub_peer_id}"+ --addr "/dns4/sam-hub/tcp/4002/p2p/${hub_peer_id}" \+ --addr "/dns4/sam-node-1/tcp/5002/p2p/${node1_peer_id}"echo "publish_ban output: $output"[[ "$status" -eq 0 ]]@@ -85,6 +89,10 @@ teardown() {# Verify Node 1 receives the ban event and logs itrun mesh_wait_for_log "${node1_name}" "Peer banned: ${node2_peer_id}" 20+ if [[ "$status" -ne 0 ]]; then+ echo "Node 1 logs:"+ docker logs "${node1_name}"+ fi[[ "$status" -eq 0 ]]# Verify Node 1 disconnects from Node 2diff --git a/tests/integration/fallback_test.go b/tests/integration/fallback_test.goindex c5ceaadc..bba1e84c 100644--- a/tests/integration/fallback_test.go+++ b/tests/integration/fallback_test.go@@ -31,6 +31,7 @@ import ("github.com/google/sam/api""github.com/libp2p/go-libp2p"dht "github.com/libp2p/go-libp2p-kad-dht"+ "github.com/libp2p/go-libp2p/core/crypto""github.com/libp2p/go-libp2p/core/network""github.com/libp2p/go-libp2p/core/peer""github.com/libp2p/go-libp2p/core/protocol"@@ -39,52 +40,42 @@ import ("google.golang.org/protobuf/proto")+func init() {+ // Force initialization of parser to avoid data race in tests+ _, _ = parser.FromStringFact(`dummy("fact")`)+}+func TestFallbackReEnrollment(t *testing.T) {nodeBin := buildBinary(t, "./cmd/sam-node")- // 1. Generate Keys+ // 1. Generate Keys for BiscuitpubA, _, _ := ed25519.GenerateKey(nil)pubB, privB, _ := ed25519.GenerateKey(nil)- // 2. Start Mock Hub with dynamic key response- // First call returns Key A, subsequent calls return Key B- _, hubAddr := startMockHubDynamic(t, pubA, pubB)-- tmpHome := t.TempDir()- env := append(os.Environ(),- "HOME="+tmpHome,- "XDG_CONFIG_HOME="+filepath.Join(tmpHome, ".config"),- )-- // 4. Start a mock libp2p client in the test- clientHost, err := libp2p.New(libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))+ // 2. Generate Client Peer Identity BEFORE starting any libp2p hosts+ // This avoids data race with FromStringFact later!+ clientPriv, clientPub, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)if err != nil {t.Fatal(err)}- defer func() { _ = clientHost.Close() }()-- // Create a dummy JWT file for fallback- jwtPath := filepath.Join(tmpHome, "jwt.txt")- if err := os.WriteFile(jwtPath, []byte("dummy-jwt"), 0644); err != nil {+ clientID, err := peer.IDFromPublicKey(clientPub)+ if err != nil {t.Fatal(err)}- // 5. Create Biscuit token signed by Key B+ // 3. Create Biscuit token signed by Key B (using pre-computed clientID)builder := biscuit.NewBuilder(privB)- // Bind to client peer ID- nodeFact, _ := parser.FromStringFact(fmt.Sprintf(`node("%s")`, clientHost.ID()))+ nodeFact, _ := parser.FromStringFact(fmt.Sprintf(`node("%s")`, clientID))if err := builder.AddAuthorityFact(nodeFact); err != nil {t.Fatal(err)}- // Add client_peer_id fact for replay check- clientPeerFact, _ := parser.FromStringFact(fmt.Sprintf(`client_peer_id("%s")`, clientHost.ID()))+ clientPeerFact, _ := parser.FromStringFact(fmt.Sprintf(`client_peer_id("%s")`, clientID))if err := builder.AddAuthorityFact(clientPeerFact); err != nil {t.Fatal(err)}- // Add wildcard tool accesstoolFact, _ := parser.FromStringFact(`allow_mcp_tool("*")`)if err := builder.AddAuthorityFact(toolFact); err != nil {t.Fatal(err)@@ -105,8 +96,22 @@ func TestFallbackReEnrollment(t *testing.T) {tokenBytes, _ := b.Serialize()- // 6. Run Node in background- // It will enroll on startup and get Key A!+ // 4. Start Mock Hub with dynamic key response+ _, hubAddr := startMockHubDynamic(t, pubA, pubB)++ tmpHome := t.TempDir()+ env := append(os.Environ(),+ "HOME="+tmpHome,+ "XDG_CONFIG_HOME="+filepath.Join(tmpHome, ".config"),+ )++ // Create a dummy JWT file for fallback+ jwtPath := filepath.Join(tmpHome, "jwt.txt")+ if err := os.WriteFile(jwtPath, []byte("dummy-jwt"), 0644); err != nil {+ t.Fatal(err)+ }++ // 5. Run Node in backgroundcmd := exec.Command(nodeBin, "run", "--hub", hubAddr, "--listen", "/ip4/127.0.0.1/tcp/0", "--jwt-path", jwtPath)cmd.Dir = repoRoot(t)cmd.Env = env@@ -137,7 +142,7 @@ func TestFallbackReEnrollment(t *testing.T) {t.Fatalf("Node failed to start or didn't print address in time.\nOutput:\n%s", out)}- // 7. Extract Node address+ // 6. Extract Node addressvar nodeAddr stringlines := strings.Split(out, "\n")for _, line := range lines {@@ -178,7 +183,16 @@ func TestFallbackReEnrollment(t *testing.T) {nodeFullAddr := nodeAddr + "/p2p/" + nodePeerID- // 8. Connect to Node via libp2p+ // 7. Start client host with the pre-generated identity+ clientHost, err := libp2p.New(+ libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"),+ libp2p.Identity(clientPriv),+ )+ if err != nil {+ t.Fatal(err)+ }+ defer func() { _ = clientHost.Close() }()+targetAddr, err := multiaddr.NewMultiaddr(nodeFullAddr)if err != nil {t.Fatal(err)@@ -192,14 +206,14 @@ func TestFallbackReEnrollment(t *testing.T) {t.Fatal(err)}- // 9. Connect to Node via libp2p on api.MCPProtocolID+ // 8. Connect to Node via libp2p on api.MCPProtocolIDs, err := clientHost.NewStream(context.Background(), targetInfo.ID, api.MCPProtocolID)if err != nil {t.Fatal(err)}defer func() { _ = s.Close() }()- // 10. Send AuthFrame with token B+ // 9. Send AuthFrame with token Bwriter := msgio.NewVarintWriter(s)authFrame := &api.AuthFrame{Biscuit: tokenBytes}authFrameBytes, _ := proto.Marshal(authFrame)@@ -207,7 +221,7 @@ func TestFallbackReEnrollment(t *testing.T) {t.Fatal(err)}- // 11. Read response+ // 10. Read responsereader := msgio.NewVarintReaderSize(s, 1024*64)respMsg, err := reader.ReadMsg()if err != nil {