Skip to content

Commit

Permalink
Add allowed prefixes for RPC methods
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Inkvi committed Aug 22, 2024
1 parent 6e68736 commit dbd77af
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -472,6 +482,7 @@ struct ChainState {
rpc_url: Url,
cache_factory: Box<dyn CacheBackendFactory>,
handlers: HashMap<String, HandlerEntry>,
allowed_prefixes: Vec<String>,
}

struct HandlerEntry {
Expand Down

0 comments on commit dbd77af

Please sign in to comment.