Skip to content

RPC: Request rate limiting based on size/process time budget (hardening pt.3) - #685

Open
SyntheticBird45 wants to merge 3 commits into
mainfrom
rpc-hardening-rate-limit
Open

RPC: Request rate limiting based on size/process time budget (hardening pt.3)#685
SyntheticBird45 wants to merge 3 commits into
mainfrom
rpc-hardening-rate-limit

Conversation

@SyntheticBird45

Copy link
Copy Markdown
Member

What

This PR follows and is based upon #684

It adds a new Axum middleware for rate limiting requests based on a processing time and response size budget. Each clients (IP address) is allocated a configurable process time budget and response size budget. These budgets are substracted when response is processed. Whenever the budget is below zero the clients is greeted with a 429 Too many requests error. Budgets are refilled following an income variable.

The reasoning behind this system is limiting clients from using resource heavy requests, while permitting the quick use of cheap requests.

Original idea from @Boog900

This commit adds a new struct called RpcServer that is used to accept
new TCP connections and serving them through hyper and axum. This
replaces axum::serve. A new async IO timeout wrapper called StreamTimeout
is added to each new connections. The read_timeout and send_timeout configuration keys are
used to set the duration for which, if elapsed, the client is
disconnected if no bytes has been received or sent respectively.
Add a per-IP connection limit layer to the RPC servers. Every type (public, private or loopback) of IP addresses have a configurable maximum amount of simultaneous connection. The node also have a configurable total maximum amount of simultaneous connections. Whenever one limit is exceeded, the connection is dropped.

Add a short-ban mechanism against IP addresses which fails to be served more than 15 times. This is a protection against malicious clients spamming garbage instead of parseable HTTP requests.
…Axum

Adds a new ratelimit.rs file containing an Axum middleware for rate
limiting requests based on a response size and processing time budget.
Clients that have exceeded either of these two budgets are not served
and are responded with a `429 Too many requests` error response. The
budget is refilled with an income. Both the maximum budgets and incomes
are configurable.

This commit also modifies RpcCacheLimit::remove_connection to return a
boolean indicating whether the last connection for this IP has been
terminated. This is used to start an eviction task that will delete the
IP entry from the rate limiting state after a period of time (or be
cancelled if the IP reconnected in the mean time).

Assisted-by: Boog900 <boog900@tutanota.com>
@github-actions github-actions Bot added A-dependency Area: Related to dependencies, or changes to a Cargo.{toml,lock} file. A-workspace Area: Changes to a root workspace file or general repo file. A-binaries Area: Related to binaries. labels Aug 13, 2026
@Boog900 Boog900 added this to the cuprated v0.1.0 preview 2 milestone Aug 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-binaries Area: Related to binaries. A-dependency Area: Related to dependencies, or changes to a Cargo.{toml,lock} file. A-workspace Area: Changes to a root workspace file or general repo file.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants