p2p: a per-peer guard drops floods and bans peers that send invalid gossip
Per relaying peer and minute: over 1200 messages are dropped unread (no penalty); 20 invalid ones (bad signature, too little work, garbage) ban the peer for an hour, closing the connection and blacklisting it in gossip. Ignored messages (clock trouble, unknown listing) never count. In the simulator the attacker is cut off from every node it reached and honest listings still arrive 50 of 50; the message-flood scenario now uses a second attacker. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
+13
-5
@@ -128,6 +128,8 @@ type Node struct {
|
||||
ps *pubsub.PubSub
|
||||
clock func() time.Time
|
||||
log *slog.Logger
|
||||
// guard rate-limits and bans the peers that relay to us.
|
||||
guard *peerGuard
|
||||
// gossipIn counts listings that arrived by gossip since the last summary.
|
||||
gossipIn tally
|
||||
|
||||
@@ -267,9 +269,15 @@ func New(ctx context.Context, cfg Config) (*Node, error) {
|
||||
// Flood-publish: the origin sends to every subscribed peer instead of only
|
||||
// its mesh. Regional topics are small, and without it a listing published
|
||||
// in the first second after connecting (before the mesh forms) is lost.
|
||||
n.guard, err = newPeerGuard(h.ID(), n.clock, log, func(p peer.ID) { _ = h.Network().ClosePeer(p) })
|
||||
if err != nil {
|
||||
n.Close()
|
||||
return nil, err
|
||||
}
|
||||
n.ps, err = pubsub.NewGossipSub(nctx, h,
|
||||
pubsub.WithMaxMessageSize(listing.MaxSize+4096),
|
||||
pubsub.WithFloodPublish(true))
|
||||
pubsub.WithFloodPublish(true),
|
||||
pubsub.WithBlacklist(n.guard.bl))
|
||||
if err != nil {
|
||||
n.Close()
|
||||
return nil, err
|
||||
@@ -431,7 +439,7 @@ func (n *Node) Close() error {
|
||||
// --- topics ---------------------------------------------------------------
|
||||
|
||||
func (n *Node) startRevocations() error {
|
||||
if err := n.ps.RegisterTopicValidator(RevocationTopic, n.revocationValidator,
|
||||
if err := n.ps.RegisterTopicValidator(RevocationTopic, n.guard.wrap(n.revocationValidator),
|
||||
pubsub.WithValidatorTimeout(5*time.Second)); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -461,7 +469,7 @@ func (n *Node) startRevocations() error {
|
||||
}
|
||||
|
||||
func (n *Node) startReports() error {
|
||||
if err := n.ps.RegisterTopicValidator(ReportTopic, n.reportValidator,
|
||||
if err := n.ps.RegisterTopicValidator(ReportTopic, n.guard.wrap(n.reportValidator),
|
||||
pubsub.WithValidatorTimeout(5*time.Second)); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -651,7 +659,7 @@ func (n *Node) messageTopic(region string) (*pubsub.Topic, error) {
|
||||
return t, nil
|
||||
}
|
||||
name := messageTopicPrefix + region
|
||||
if err := n.ps.RegisterTopicValidator(name, n.messageValidator(region),
|
||||
if err := n.ps.RegisterTopicValidator(name, n.guard.wrap(n.messageValidator(region)),
|
||||
pubsub.WithValidatorTimeout(5*time.Second)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -674,7 +682,7 @@ func (n *Node) regionTopic(region string) (*pubsub.Topic, error) {
|
||||
return t, nil
|
||||
}
|
||||
name := listingTopicPrefix + region
|
||||
if err := n.ps.RegisterTopicValidator(name, n.listingValidator(region),
|
||||
if err := n.ps.RegisterTopicValidator(name, n.guard.wrap(n.listingValidator(region)),
|
||||
pubsub.WithValidatorTimeout(5*time.Second)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user