Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
identity: enforce biscuit expiry when extracting a peer ID, and drop banned peers from the relay ACL
Antonio Ojea64530d272118
diff --git a/internal/controlplane/biscuit_ttl_test.go b/internal/controlplane/biscuit_ttl_test.goindex 9c4c2fc6..66115dbf 100644--- a/internal/controlplane/biscuit_ttl_test.go+++ b/internal/controlplane/biscuit_ttl_test.go@@ -58,12 +58,12 @@ func biscuitExpiration(t *testing.T, tokenBytes []byte, cpPubKey ed25519.PublicKif err != nil {t.Fatalf("query: %v", err)}- if len(facts) != 1 || len(facts[0].Predicate.IDs) != 1 {+ if len(facts) != 1 || len(facts[0].IDs) != 1 {t.Fatalf("expected exactly one expiration fact, got %v", facts)}- date, ok := facts[0].Predicate.IDs[0].(biscuit.Date)+ date, ok := facts[0].IDs[0].(biscuit.Date)if !ok {- t.Fatalf("expiration term is %T, want biscuit.Date", facts[0].Predicate.IDs[0])+ t.Fatalf("expiration term is %T, want biscuit.Date", facts[0].IDs[0])}return time.Time(date)}diff --git a/internal/controlplane/server.go b/internal/controlplane/server.goindex 292bbae6..5b428233 100644--- a/internal/controlplane/server.go+++ b/internal/controlplane/server.go@@ -626,8 +626,10 @@ func (s *Server) HandleRefresh(w http.ResponseWriter, r *http.Request) {trustedKeys = append(trustedKeys, k.Public)}- // Verify current biscuit signature and extract peer ID- pID, err := identity.VerifyAndExtractPeerID(trustedKeys, currentBiscuitBytes, s.config.BiscuitTimeout)+ // Verify current biscuit signature and extract peer ID. Expiry is not+ // enforced here: a node refreshes because its token lapsed. The session+ // record and the signed challenge below are what bound this request.+ pID, err := identity.VerifyExpiredAndExtractPeerID(trustedKeys, currentBiscuitBytes, s.config.BiscuitTimeout)if err != nil {logger.Warnw("Invalid biscuit presented for refresh", "error", err)http.Error(w, "Invalid biscuit: "+err.Error(), http.StatusUnauthorized)diff --git a/internal/identity/biscuit.go b/internal/identity/biscuit.goindex 49455d0b..2a4e6bf2 100644--- a/internal/identity/biscuit.go+++ b/internal/identity/biscuit.go@@ -369,9 +369,23 @@ func MintBootstrapBiscuitToken(signingKey ed25519.PrivateKey, remotePeer peer.IDreturn mintBiscuit(signingKey, remotePeer, []string{role}, expiration, nil, policyRoles, labels)}-// VerifyAndExtractPeerID checks that the biscuit is signed by one of the trusted keys and returns the peer ID.-// This function does NOT perform time checks, making it suitable for token refresh flows.+// VerifyExpiredAndExtractPeerID checks that the biscuit is signed by one of the+// trusted keys and returns the peer ID, deliberately WITHOUT enforcing expiry.+// Only the refresh flow may use it: a node refreshes precisely because its token+// lapsed, so it has nothing unexpired to present. Callers must bound the request+// some other way (the refresh handler gates on the session record and a signed+// challenge). Everywhere else, use VerifyAndExtractPeerID.+func VerifyExpiredAndExtractPeerID(trustedPublicKeys []ed25519.PublicKey, biscuitData []byte, timeout time.Duration) (peer.ID, error) {+ return extractPeerID(trustedPublicKeys, biscuitData, timeout, false)+}++// VerifyAndExtractPeerID checks that the biscuit is signed by one of the trusted+// keys and is unexpired, and returns the peer ID.func VerifyAndExtractPeerID(trustedPublicKeys []ed25519.PublicKey, biscuitData []byte, timeout time.Duration) (peer.ID, error) {+ return extractPeerID(trustedPublicKeys, biscuitData, timeout, true)+}++func extractPeerID(trustedPublicKeys []ed25519.PublicKey, biscuitData []byte, timeout time.Duration, enforceExpiry bool) (peer.ID, error) {b, err := biscuit.Unmarshal(biscuitData)if err != nil {return "", fmt.Errorf("malformed biscuit: %w", err)@@ -389,6 +403,12 @@ func VerifyAndExtractPeerID(trustedPublicKeys []ed25519.PublicKey, biscuitData [lastErr = errcontinue}+ if enforceExpiry {+ EnforceExpiration(auth)+ }+ // No policy is added, so a token that passes every check still reports+ // ErrNoMatchingPolicy; that is success here. A failed check reports the+ // check error instead, so expiry still rejects.if err := auth.Authorize(); err == nil || errors.Is(err, biscuit.ErrNoMatchingPolicy) {authorizer = authverified = true@@ -437,7 +457,11 @@ func VerifyAndExtractPeerID(trustedPublicKeys []ed25519.PublicKey, biscuitData [}// VerifyBiscuitRole checks that the biscuit is signed by the control plane's public key-// and contains the specified role fact.+// and contains the specified role fact. It deliberately does NOT enforce expiry: its+// callers either hold a token the control plane minted moments ago, or are deciding+// whether an identity loaded from disk is worth starting with, where a lapsed token+// should trigger a refresh rather than refuse to boot. Do not use it to admit a token+// received from a peer.func VerifyBiscuitRole(biscuitData []byte, controlPlanePubKey ed25519.PublicKey, expectedRole string, timeout time.Duration) error {b, err := biscuit.Unmarshal(biscuitData)if err != nil {diff --git a/internal/identity/biscuit_test.go b/internal/identity/biscuit_test.goindex fc98ea0d..cde10414 100644--- a/internal/identity/biscuit_test.go+++ b/internal/identity/biscuit_test.go@@ -576,6 +576,48 @@ func TestVerifyAndExtractPeerID_MultipleTrustedKeys(t *testing.T) {}}+// TestExtractPeerIDExpiry pins that adding the expiration check still rejects an+// expired token even though neither variant adds a policy, i.e. that a failed+// check outranks ErrNoMatchingPolicy.+func TestExtractPeerIDExpiry(t *testing.T) {+ pub, priv, err := ed25519.GenerateKey(rand.Reader)+ if err != nil {+ t.Fatal(err)+ }+ privNode, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)+ if err != nil {+ t.Fatal(err)+ }+ dummyPeer, err := peer.IDFromPrivateKey(privNode)+ if err != nil {+ t.Fatal(err)+ }+ trustedKeys := []ed25519.PublicKey{pub}++ fresh, err := MintBootstrapBiscuitToken(priv, dummyPeer, api.RoleNode, time.Now().Add(time.Hour), nil, nil)+ if err != nil {+ t.Fatal(err)+ }+ expired, err := MintBootstrapBiscuitToken(priv, dummyPeer, api.RoleNode, time.Now().Add(-time.Hour), nil, nil)+ if err != nil {+ t.Fatal(err)+ }++ if _, err := VerifyAndExtractPeerID(trustedKeys, fresh, 5*time.Second); err != nil {+ t.Errorf("unexpired token rejected: %v", err)+ }+ if _, err := VerifyAndExtractPeerID(trustedKeys, expired, 5*time.Second); err == nil {+ t.Error("expired token accepted by the expiry-enforcing variant")+ }++ // The refresh flow depends on the exempt variant staying permissive.+ if got, err := VerifyExpiredAndExtractPeerID(trustedKeys, expired, 5*time.Second); err != nil {+ t.Errorf("expired token rejected by the refresh variant: %v", err)+ } else if got != dummyPeer {+ t.Errorf("got peer %s, want %s", got, dummyPeer)+ }+}+func TestVerifyBiscuitRole_TimeoutIsHonored(t *testing.T) {pub, priv, err := ed25519.GenerateKey(rand.Reader)if err != nil {diff --git a/internal/node/node.go b/internal/node/node.goindex 67a88bc6..a31e7358 100644--- a/internal/node/node.go+++ b/internal/node/node.go@@ -1197,7 +1197,9 @@ func (n *SamNode) handleBannedEvent(event *api.MeshEvent) {if n.revokedPeers != nil {n.revokedPeers.Add(event.PeerId, event.Timestamp)}+ // Drop any prior admission, otherwise the relay ACL keeps honouring it.if p, err := peer.Decode(event.PeerId); err == nil {+ n.authPeers.Delete(p)if n.Host != nil {_ = n.Host.Network().ClosePeer(p)}@@ -1389,6 +1391,13 @@ func (n *SamNode) HandleAuthHandshake(s network.Stream) {}()remotePeer := s.Conn().RemotePeer()+ if n.revokedPeers != nil {+ if _, revoked := n.revokedPeers.Get(remotePeer.String()); revoked {+ logger.Warnf("[AuthN] Peer %s is revoked", remotePeer)+ return+ }+ }+reader := msgio.NewVarintReaderSize(s, 1024*64)msg, err := reader.ReadMsg()if err != nil {diff --git a/internal/node/relay_acl_test.go b/internal/node/relay_acl_test.goindex c9c31107..7e799443 100644--- a/internal/node/relay_acl_test.go+++ b/internal/node/relay_acl_test.go@@ -18,6 +18,8 @@ import ("testing""time"+ "github.com/google/sam/api"+ lru "github.com/hashicorp/golang-lru/v2""github.com/libp2p/go-libp2p/core/peer""github.com/multiformats/go-multiaddr")@@ -65,3 +67,36 @@ func TestNodeRelayACL_AllowReserve(t *testing.T) {t.Errorf("Expected AllowReserve to return true when peer is authenticated")}}++// TestBannedPeerLosesRelayRights covers a ban arriving after the peer was+// already admitted: the relay ACL reads authPeers, so leaving a stale entry+// there keeps granting reservations to a revoked peer.+func TestBannedPeerLosesRelayRights(t *testing.T) {+ revokedCache, err := lru.New[string, int64](10)+ if err != nil {+ t.Fatal(err)+ }+ node := &SamNode{revokedPeers: revokedCache, BiscuitTimeout: 500 * time.Millisecond}+ acl := &nodeRelayACL{node: node}++ peerID, err := peer.Decode("12D3KooWAFv4iJst5G6MjwXhZ66K5zS1tP7A9vSg4vK8f1T7X8t9")+ if err != nil {+ t.Fatal(err)+ }+ addr, _ := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/1234")++ node.authPeers.Store(peerID, struct{}{})+ if !acl.AllowReserve(peerID, addr) {+ t.Fatal("expected an admitted peer to hold relay rights")+ }++ node.handleBannedEvent(&api.MeshEvent{+ Type: api.MeshEvent_BANNED,+ PeerId: peerID.String(),+ Timestamp: time.Now().UnixMilli(),+ })++ if acl.AllowReserve(peerID, addr) {+ t.Error("banned peer still holds relay rights")+ }+}