feat: rate limit inbound streams per protocol per connection#3446
Draft
paschal533 wants to merge 2 commits intolibp2p:mainfrom
Draft
feat: rate limit inbound streams per protocol per connection#3446paschal533 wants to merge 2 commits intolibp2p:mainfrom
paschal533 wants to merge 2 commits intolibp2p:mainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
As raised in #2093, there's currently no way to rate limit inbound streams per protocol per connection. The existing
maxInboundStreamsonly caps concurrent open streams, a malicious peer can still spam a protocol likeidentify/pushby rapidly opening and closing streams, burning bandwidth and CPU on the receiving end without ever hitting the concurrent limit.Fix
Adds an optional
inboundStreamRateLimitfield toStreamHandlerOptions:This uses a fixed-window counter per protocol per connection. When a peer opens more than
countstreams withinintervalms, the excess streams are aborted withInboundStreamRateLimitErrorbefore they reach the handler.The counter resets automatically after the window expires, so legitimate traffic is unaffected after the window rolls over.
Changes
packages/interface/src/stream-handler.ts- addedinboundStreamRateLimit?: { count, interval }toStreamHandlerOptionspackages/interface/src/errors.ts- addedInboundStreamRateLimitErrorpackages/libp2p/src/connection.ts- added per-protocol fixed-window tracking inonIncomingStream, checked after the existingmaxInboundStreamsguardpackages/libp2p/test/connection/index.spec.ts- 4 new testsTests
inboundStreamRateLimitconfigured are unaffectedNotes
This PR only adds the mechanism to the core. Built-in protocols like
identify/push(the original motivation in #1469/#2093) would need a follow-up inprotocol-identifyto wire up sensible defaults via their init options. Happy to do that as a follow-up once this gets feedback.Closes #2093