From dbd77af2c64335afb7e1b6c55c2ce3921c373d43 Mon Sep 17 00:00:00 2001 From: Inkvi Date: Thu, 22 Aug 2024 11:06:14 -0700 Subject: [PATCH] Add allowed prefixes for RPC methods Introduce a check to validate that RPC method names start with any of the specified allowed prefixes. Update the code to use `HashSet` for efficient prefix storage and add default allowed prefixes when initializing the application state. --- src/main.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 57abc35..e070186 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use actix_web::{error, web, App, Error, HttpResponse, HttpServer}; use anyhow::Context; @@ -83,6 +83,15 @@ async fn rpc_call( } }; + // Check if the method starts with an allowed prefix + if !chain_state.allowed_prefixes.iter().any(|prefix| method.starts_with(prefix)) { + ordered_requests_result.push(Some(JsonRpcResponse::from_error( + Some(id), + DefinedError::MethodNotFound, + ))); + continue; + } + macro_rules! push_uncached_request_and_continue { () => {{ let rpc_request = RpcRequest::new_uncachable(index, id, method, params); @@ -382,6 +391,7 @@ async fn main() -> std::io::Result<()> { rpc_url: rpc_url.clone(), handlers: Default::default(), cache_factory, + allowed_prefixes: vec!["eth_".to_string(), "alchemy_".to_string(), "net_".to_string()], }; for factory in &handler_factories { @@ -472,6 +482,7 @@ struct ChainState { rpc_url: Url, cache_factory: Box, handlers: HashMap, + allowed_prefixes: Vec, } struct HandlerEntry {