Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpc: add option to whitelist ips in rate limiting #3701

Merged
merged 18 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions substrate/client/cli/src/commands/run_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ pub struct RunCmd {
#[arg(long)]
pub rpc_rate_limit: Option<NonZeroU32>,

/// Disable RPC rate limiting for certain hosts.
#[arg(long, num_args = 1..)]
pub rpc_rate_limit_whitelisted_hosts: Vec<String>,

/// Set the maximum RPC request payload size for both HTTP and WS in megabytes.
#[arg(long, default_value_t = RPC_DEFAULT_MAX_REQUEST_SIZE_MB)]
pub rpc_max_request_size: u32,
Expand Down Expand Up @@ -439,6 +443,10 @@ impl CliConfiguration for RunCmd {
Ok(self.rpc_rate_limit)
}

fn rpc_rate_limit_whitelisted_hosts(&self) -> Result<Vec<String>> {
Ok(self.rpc_rate_limit_whitelisted_hosts.clone())
}

fn transaction_pool(&self, is_dev: bool) -> Result<TransactionPoolOptions> {
Ok(self.pool_config.transaction_pool(is_dev))
}
Expand Down
6 changes: 6 additions & 0 deletions substrate/client/cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,11 @@ pub trait CliConfiguration<DCV: DefaultConfigurationValues = ()>: Sized {
Ok(None)
}

/// RPC rate limit configuration.
niklasad1 marked this conversation as resolved.
Show resolved Hide resolved
fn rpc_rate_limit_whitelisted_hosts(&self) -> Result<Vec<String>> {
Ok(vec![])
}

/// Get the prometheus configuration (`None` if disabled)
///
/// By default this is `None`.
Expand Down Expand Up @@ -523,6 +528,7 @@ pub trait CliConfiguration<DCV: DefaultConfigurationValues = ()>: Sized {
rpc_message_buffer_capacity: self.rpc_buffer_capacity_per_connection()?,
rpc_batch_config: self.rpc_batch_config()?,
rpc_rate_limit: self.rpc_rate_limit()?,
rpc_rate_limit_whitelisted_hosts: self.rpc_rate_limit_whitelisted_hosts()?,
prometheus_config: self
.prometheus_config(DCV::prometheus_listen_port(), &chain_spec)?,
telemetry_endpoints,
Expand Down
57 changes: 54 additions & 3 deletions substrate/client/rpc-servers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@
pub mod middleware;

use std::{
convert::Infallible, error::Error as StdError, net::SocketAddr, num::NonZeroU32, time::Duration,
convert::Infallible,
error::Error as StdError,
net::{IpAddr, SocketAddr, ToSocketAddrs},
num::NonZeroU32,
time::Duration,
};

use http::header::HeaderValue;
Expand Down Expand Up @@ -85,6 +89,8 @@ pub struct Config<'a, M: Send + Sync + 'static> {
pub batch_config: BatchRequestConfig,
/// Rate limit calls per minute.
pub rate_limit: Option<NonZeroU32>,
/// Disable rate limit for hosts.
pub rate_limit_whitelisted_hosts: &'a [String],
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -117,11 +123,13 @@ where
tokio_handle,
rpc_api,
rate_limit,
rate_limit_whitelisted_hosts,
} = config;

let std_listener = TcpListener::bind(addrs.as_slice()).await?.into_std()?;
let local_addr = std_listener.local_addr().ok();
let host_filter = hosts_filtering(cors.is_some(), local_addr);
let rate_limit_whitelisted_ip_addrs = hosts_to_ip_addrs(rate_limit_whitelisted_hosts)?;

let http_middleware = tower::ServiceBuilder::new()
.option_layer(host_filter)
Expand Down Expand Up @@ -160,20 +168,31 @@ where
stop_handle: stop_handle.clone(),
};

let make_service = make_service_fn(move |_conn: &AddrStream| {
let make_service = make_service_fn(move |conn: &AddrStream| {
let cfg = cfg.clone();
let conn_ip = conn.remote_addr().ip();
let rate_limit_whitelisted_ip_addrs = rate_limit_whitelisted_ip_addrs.clone();

async move {
let cfg = cfg.clone();
let rate_limit_whitelisted_ip_addrs = rate_limit_whitelisted_ip_addrs.clone();

Ok::<_, Infallible>(service_fn(move |req| {
let ip = read_ip(conn_ip, &req);

let rate_limit_cfg = if rate_limit_whitelisted_ip_addrs.iter().any(|ip2| ip2 == &ip) {
None
} else {
rate_limit
};

let PerConnection { service_builder, metrics, tokio_handle, stop_handle, methods } =
cfg.clone();

let is_websocket = ws::is_upgrade_request(&req);
let transport_label = if is_websocket { "ws" } else { "http" };

let middleware_layer = match (metrics, rate_limit) {
let middleware_layer = match (metrics, rate_limit_cfg) {
(None, None) => None,
(Some(metrics), None) => Some(
MiddlewareLayer::new().with_metrics(Metrics::new(metrics, transport_label)),
Expand Down Expand Up @@ -281,3 +300,35 @@ fn format_cors(maybe_cors: Option<&Vec<String>>) -> String {
format!("{:?}", ["*"])
}
}

/// Helper function that tries to read the ip addr from "X-Real-IP" header
/// which is only set if the connection was made via a reverse-proxy
///
/// If that header is missing then remote addr from the socket is used.
fn read_ip(remote_addr: IpAddr, req: &hyper::Request<hyper::Body>) -> IpAddr {
if let Some(ip) = req.headers().get("X-Real-IP").and_then(|v| v.to_str().ok()).and_then(|s| s.parse().ok()) {
ip
} else {
remote_addr
}
}

fn hosts_to_ip_addrs(hosts: &[String]) -> Result<Vec<IpAddr>, Box<dyn StdError + Send + Sync>> {
let mut ip_list = Vec::new();

for host in hosts {
// The host may contain a port such as `hostname:8080`
// and we don't care about the port to lookup the IP addr.
//
// to_socket_addr without the port will fail though
let host_no_port = if let Some((h, _port)) = host.split_once(":") { h } else { host };

let sockaddrs = (host_no_port, 0).to_socket_addrs()?;

for sockaddr in sockaddrs {
ip_list.push(sockaddr.ip());
}
}

Ok(ip_list)
}
2 changes: 2 additions & 0 deletions substrate/client/service/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ pub struct Configuration {
pub rpc_batch_config: RpcBatchRequestConfig,
/// RPC rate limit per minute.
pub rpc_rate_limit: Option<NonZeroU32>,
/// RPC rate limit whitelisted hosts.
pub rpc_rate_limit_whitelisted_hosts: Vec<String>,
/// Prometheus endpoint configuration. `None` if disabled.
pub prometheus_config: Option<PrometheusConfig>,
/// Telemetry service URL. `None` if disabled.
Expand Down
1 change: 1 addition & 0 deletions substrate/client/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ where
cors: config.rpc_cors.as_ref(),
tokio_handle: config.tokio_handle.clone(),
rate_limit: config.rpc_rate_limit,
rate_limit_whitelisted_hosts: config.rpc_rate_limit_whitelisted_hosts.as_ref(),
};

// TODO: https://github.com/paritytech/substrate/issues/13773
Expand Down
Loading